Skip to main content

Snuba SDK for generating SnQL queries.

Project description

snuba-sdk - SDK for generating SnQL queries for Snuba

PyPi page link -- version Discord Tests

Examples

Snuba SDK is a tool that allows SnQL queries (the language that Snuba uses) to be built programatically. A SnQL query is represented by a Query object, and has a number of attributes corresponding to different parts of the query.

Queries can be created directly:

query = Query(
    dataset="discover",
    match=Entity("events"),
    select=[
        Column("title"),
        Function("uniq", [Column("event_id")], "uniq_events"),
    ],
    groupby=[Column("title")],
    where=[
        Condition(Column("timestamp"), Op.GT, datetime.datetime.(2021, 1, 1)),
        Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),
    ],
    limit=Limit(10),
    offset=Offset(0),
    granularity=Granularity(3600),
)

Queries can also be built incrementally:

query = Query("discover", Entity("events"))
    .set_select(
        [Column("title"), Function("uniq", [Column("event_id")], "uniq_events")]
    )
    .set_groupby([Column("title")])
    .set_where(
        [
            Condition(Column("timestamp"), Op.GT, datetime.datetime.(2021, 1, 1)),
            Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),
        ]
    )
    .set_limit(10)
    .set_offset(0)
    .set_granularity(3600)

Once the query is built, it can be translated into a SnQL query that can be sent to Snuba or a human readable query.

# Outputs a formatted SnQL query
print(query.print())

This outputs:

MATCH (events)
SELECT title, uniq(event_id) AS uniq_events
BY title
WHERE timestamp > toDateTime('2021-01-01T00:00:00.000000')
AND project_id IN tuple(1, 2, 3)
LIMIT 10
OFFSET 0
GRANULARITY 3600

If an expression in the query is invalid (e.g. Column(1)) then an InvalidExpression exception will be thrown. If there is a problem with a query, it will throw an InvalidQuery exception when .validate() or .translate() is called.

TODO List

  • Sample support in Entity
  • Subscriptable support (measurements[fcp.first])
  • Curried function calls
  • unary conditions
  • Complex boolean conditions (AND/OR)
  • Join support
  • Have the Entity object take a set of columns
  • Syntactic sugar

Contributing to the SDK

Please refer to CONTRIBUTING.md.

License

Licensed under MIT, see LICENSE

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

snuba_sdk-0.0.4-py2.py3-none-any.whl (14.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file snuba_sdk-0.0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: snuba_sdk-0.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for snuba_sdk-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2e9de8cf052186b458d58ad8ae7a1bc71665d85a5d0c04a8f87f3410f025a43a
MD5 23f8df1fc57fa5dc6508a0609e5fee0c
BLAKE2b-256 b535db9462dbf0ac15f5b234d198b15faa61697866eadecdaadb91917112ebb0

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