Skip to main content

Utility for building templated metric extraction queries that can be traversed through time.

Project description

Metric Builder

Utility for building templated metric extraction queries that can be traversed through time.

Prerequisites

You will need the following to run this code:

  • Python 3

Installation

To be determined...

Usage

In order to extract a given metric, a Metric object needs to be instantiated:

metric = Metric(
    query="""
        SELECT count(*) AS total
        FROM `project.dataset.table`
        WHERE DATETIME_TRUNC(created_datetime, DAY) = '{{ reference_time | format_date('%Y-%m-%d') }}'
    """,
    reader = BigQueryReader(json_credentials_path='/path/to/creds.json')
)

The query parameter is a templated query where you can format the reference_time datetime object to the required format using template filters.

The reader parameter is the object that is actually going to connect to the desired database and perform the queries.

The metric object can now be used to fetch metrics for a given point in time as follows:

result = metric.fetch(reference_time=datetime.date(2019, 10, 21))

The result is returned as a list of dictionaries.

Template filters

Jinja2 is used as the templating engine. All built in Jinja filters are thus available. Relevant custom template filters have been added though for convenience:

format_date

Specify format of datetime:

'{{ reference_time | format_date('%Y-%m-%d') }}'

day_delta

Change a given datetime object by a specified number of days:

'{{ reference_time | day_delta(-7) | format_date('%Y-%m-%d') }}'

Readers

Any reader will implement the following method that is used to execute queries:

def execute(self, query) -> List[Dict[str, Any]]:
    ...

BigQueryReader

The underlying client is required to be authenticated with the necessary priviledges to read from the requested BigQuery tables.

If you authenticate with:

gcloud auth login

or

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/keyfile.json"

then you can just instantiate your Reader like this:

reader = BigQueryReader()

The other option is to explicitly authenticate with a service account key file:

reader = BigQueryReader(json_credentials_path='/path/to/creds.json')

HiveReader

Coming soon...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metric_builder-2.1.4.4.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

metric_builder-2.1.4.4-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file metric_builder-2.1.4.4.tar.gz.

File metadata

  • Download URL: metric_builder-2.1.4.4.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.15

File hashes

Hashes for metric_builder-2.1.4.4.tar.gz
Algorithm Hash digest
SHA256 ef6a180ea7fe38659af11c1dc03869634ce7911deafa948424667303354260d2
MD5 ff91f24a6c40e568b3f1b64fd1c33b72
BLAKE2b-256 55c6b3fe0308b8582967d7edded18c3050d333ebd9585a23f7a4b6a715092be6

See more details on using hashes here.

File details

Details for the file metric_builder-2.1.4.4-py3-none-any.whl.

File metadata

File hashes

Hashes for metric_builder-2.1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e53493fd476e3eaa06df1397c183990e8d5811dbca53ae1685e306aad77a3cd8
MD5 be4e02de742d1ec04502439e0ffe9830
BLAKE2b-256 f2e7c0a56171ddcaef46a331fd1c3478bdd96ebfbe743b36b354ccaad89992ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page