Skip to content

Commit 89cf817

Browse files
authored
[core][fix] History timeseries slotter (#2169)
1 parent 4fffcfa commit 89cf817

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

fixcore/fixcore/db/arango_query.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from arango.typings import Json
1010
from attrs import evolve
11-
from math import floor
12-
1311
from fixcore.constants import less_greater_then_operations as lgt_ops, arangodb_matches_null_ops
1412
from fixcore.db import EstimatedSearchCost, EstimatedQueryCostRating as Rating
1513
from fixcore.db.arango_query_rewrite import rewrite_query
@@ -148,14 +146,13 @@ def history_query_timeline(
148146
)
149147
crs = ctx.next_crs()
150148
gran = granularity.total_seconds()
151-
at = after.timestamp()
152-
offset = ctx.add_bind_var((at - int(floor(at / gran) * gran)) * 1000)
153-
slot = ctx.add_bind_var(gran * 1000)
154-
slot_fn = f"DATE_ISO8601((FLOOR(DATE_TIMESTAMP({crs}.changed_at) / @{slot}) * @{slot}) + @{offset})"
149+
atms = after.timestamp() * 1000
150+
slotter = ctx.add_bind_var(gran * 1000)
151+
slot_fn = f"(FLOOR((DATE_TIMESTAMP({crs}.changed_at)-{atms}) / @{slotter}))"
155152
query_str += (
156153
f" FOR {crs} IN {in_cursor} "
157-
f"COLLECT change={crs}.change, at={slot_fn} WITH COUNT INTO v SORT at ASC "
158-
'RETURN {"at": at, "group": {"change": change}, "v": v}'
154+
f"COLLECT change={crs}.change, slot={slot_fn} WITH COUNT INTO v SORT slot ASC "
155+
f'RETURN {{"at": DATE_ISO8601((slot*@{slotter}) + {atms}), "group": {{"change": change}}, "v": v}}'
159156
)
160157
return query_str, ctx.bind_vars
161158

0 commit comments

Comments
 (0)