Skip to content

Commit

Permalink
Merge pull request #54 from axiomhq/islam/dx-289-deprecate-structured…
Browse files Browse the repository at this point in the history
…-query-support-from
  • Loading branch information
bahlo authored Nov 14, 2022
2 parents 08d867c + a64773b commit b7316c9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ access_token = os.getenv("AXIOM_TOKEN")
org_id = os.getenv("AXIOM_ORG_ID")


client.datasets.apl_query(r"['my-dataset'] | where foo == 'bar' | limit 100")
client.datasets.query(r"['my-dataset'] | where foo == 'bar' | limit 100")
```

## Contributing
Expand Down
18 changes: 12 additions & 6 deletions axiom/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from datetime import datetime, timedelta, timezone

from .util import Util
from .query import Query, QueryOptions, QueryKind, AplQueryResult
from .query.result import QueryResult
from .query import QueryLegacy, QueryOptions, QueryKind, QueryResult
from .query.result import QueryLegacyResult


@dataclass
Expand Down Expand Up @@ -244,7 +244,9 @@ def delete(self, id: str):
path = "datasets/%s" % id
self.session.delete(path)

def query(self, id: str, query: Query, opts: QueryOptions) -> QueryResult:
def query_legacy(
self, id: str, query: QueryLegacy, opts: QueryOptions
) -> QueryLegacyResult:
"""Executes the given query on the dataset identified by its id."""
if not opts.saveAsKind or (opts.saveAsKind == QueryKind.APL):
raise WrongQueryKindException(
Expand All @@ -257,14 +259,18 @@ def query(self, id: str, query: Query, opts: QueryOptions) -> QueryResult:
self.logger.debug("sending query %s" % payload)
params = self._prepare_query_options(opts)
res = self.session.post(path, data=payload, params=params)
result = Util.from_dict(QueryResult, res.json())
result = Util.from_dict(QueryLegacyResult, res.json())
self.logger.debug(f"query result: {result}")
query_id = res.headers.get("X-Axiom-History-Query-Id")
self.logger.info(f"received query result with query_id: {query_id}")
result.savedQueryID = query_id
return result

def apl_query(self, apl: str, opts: AplOptions) -> AplQueryResult:
def apl_query(self, apl: str, opts: AplOptions) -> QueryResult:
"""Executes the given apl query on the dataset identified by its id."""
return self.query(apl, opts)

def query(self, apl: str, opts: AplOptions) -> QueryResult:
"""Executes the given apl query on the dataset identified by its id."""

path = "datasets/_apl"
Expand All @@ -275,7 +281,7 @@ def apl_query(self, apl: str, opts: AplOptions) -> AplQueryResult:
self.logger.debug("sending query %s" % payload)
params = self._prepare_apl_options(opts)
res = self.session.post(path, data=payload, params=params)
result = Util.from_dict(AplQueryResult, res.json())
result = Util.from_dict(QueryResult, res.json())
self.logger.debug(f"apl query result: {result}")
query_id = res.headers.get("X-Axiom-History-Query-Id")
self.logger.info(f"received query result with query_id: {query_id}")
Expand Down
2 changes: 1 addition & 1 deletion axiom/query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Projection:


@dataclass
class Query:
class QueryLegacy:
"""represents a query that gets executed on a dataset."""

# start time of the query. Required.
Expand Down
8 changes: 4 additions & 4 deletions axiom/query/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Dict, Any, Optional
from enum import Enum

from .query import Query
from .query import QueryLegacy


class MessageCode(Enum):
Expand Down Expand Up @@ -136,7 +136,7 @@ class Timeseries:


@dataclass
class QueryResult:
class QueryLegacyResult:
"""Result is the result of a query."""

# Status of the query result.
Expand All @@ -152,10 +152,10 @@ class QueryResult:


@dataclass
class AplQueryResult:
class QueryResult:
"""Result is the result of apl query."""

request: Query
request: QueryLegacy
# Status of the apl query result.
status: QueryStatus
# Matches are the events that matched the apl query.
Expand Down
20 changes: 10 additions & 10 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
WrongQueryKindException,
)
from axiom.query import (
AplQueryResult,
Query,
QueryResult,
QueryLegacy,
QueryOptions,
QueryKind,
Filter,
Expand All @@ -29,7 +29,7 @@
FilterOperation,
)
from axiom.query.result import (
QueryResult,
QueryLegacyResult,
QueryStatus,
Entry,
EntryGroup,
Expand Down Expand Up @@ -196,13 +196,13 @@ def test_step007_query(self):
startTime = datetime.utcnow() - timedelta(minutes=2)
endTime = datetime.utcnow()

q = Query(startTime=startTime, endTime=endTime)
q = QueryLegacy(startTime=startTime, endTime=endTime)
opts = QueryOptions(
streamingDuration=timedelta(seconds=60),
nocache=True,
saveAsKind=QueryKind.ANALYTICS,
)
qr = self.client.datasets.query(self.dataset_name, q, opts)
qr = self.client.datasets.query_legacy(self.dataset_name, q, opts)

self.assertIsNotNone(qr.savedQueryID)
self.assertEqual(len(qr.matches), len(self.events))
Expand All @@ -221,7 +221,7 @@ def test_step007_apl_query(self):
save=False,
format=AplResultFormat.Legacy,
)
qr = self.client.datasets.apl_query(apl, opts)
qr = self.client.datasets.query(apl, opts)

self.assertEqual(len(qr.matches), len(self.events))

Expand All @@ -234,10 +234,10 @@ def test_step007_wrong_query_kind(self):
nocache=True,
saveAsKind=QueryKind.APL,
)
q = Query(startTime, endTime)
q = QueryLegacy(startTime, endTime)

try:
self.client.datasets.query(self.dataset_name, q, opts)
self.client.datasets.query_legacy(self.dataset_name, q, opts)
except WrongQueryKindException as err:
self.logger.info("passing kind apl to query raised exception as expected")
return
Expand All @@ -251,7 +251,7 @@ def test_step007_complex_query(self):
aggregations = [
Aggregation(alias="event_count", op=AggregationOperation.COUNT, field="*")
]
q = Query(startTime, endTime, aggregations=aggregations)
q = QueryLegacy(startTime, endTime, aggregations=aggregations)
q.groupBy = ["success", "remote_ip"]
q.filter = Filter(FilterOperation.EQUAL, "response", 304)
q.order = [
Expand All @@ -261,7 +261,7 @@ def test_step007_complex_query(self):
q.virtualFields = [VirtualField("success", "response < 400")]
q.project = [Projection("remote_ip", "ip")]

res = self.client.datasets.query(self.dataset_name, q, QueryOptions())
res = self.client.datasets.query_legacy(self.dataset_name, q, QueryOptions())

# self.assertEqual(len(self.events), res.status.rowsExamined)
self.assertEqual(len(self.events), res.status.rowsMatched)
Expand Down

0 comments on commit b7316c9

Please sign in to comment.