Skip to main content

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...

Metadata

Release files for metric-builder 2.1.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metric-builder 2.1.4.4
File Size Uploaded Provenance Actions
metric_builder-2.1.4.4.tar.gz 7.2 kB Without provenance Details for metric_builder-2.1.4.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for metric-builder 2.1.4.4
File Interpreter ABI Platform Actions
metric_builder-2.1.4.4-py3-none-any.whl Python 3 none any Details for metric_builder-2.1.4.4-py3-none-any.whl

Total release size: 15.6 kB

Release files / metric_builder-2.1.4.4.tar.gz

Download URL metric_builder-2.1.4.4.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ef6a180ea7fe38659af11c1dc03869634ce7911deafa948424667303354260d2
BLAKE2b-256 checksum
How to use checksums
55c6b3fe0308b8582967d7edded18c3050d333ebd9585a23f7a4b6a715092be6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.15

Release files / metric_builder-2.1.4.4-py3-none-any.whl

Download URL metric_builder-2.1.4.4-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e53493fd476e3eaa06df1397c183990e8d5811dbca53ae1685e306aad77a3cd8
BLAKE2b-256 checksum
How to use checksums
f2e7c0a56171ddcaef46a331fd1c3478bdd96ebfbe743b36b354ccaad89992ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.15

Security for metric-builder 2.1.4.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page