Skip to content

Commit 59e53cc

Browse files
authored
[core][fix] Move timeline to request time after (#2165)
1 parent 3c831f8 commit 59e53cc

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

fixcore/fixcore/db/arango_query.py

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

99
from arango.typings import Json
1010
from attrs import evolve
11+
from math import floor
1112

1213
from fixcore.constants import less_greater_then_operations as lgt_ops, arangodb_matches_null_ops
1314
from fixcore.db import EstimatedSearchCost, EstimatedQueryCostRating as Rating
@@ -137,18 +138,23 @@ def history_query(db: Any, query_model: QueryModel) -> Tuple[str, Json]:
137138
return f"""{query_str} FOR result in {cursor}{last_limit} RETURN UNSET(result, {unset_props})""", ctx.bind_vars
138139

139140

140-
def history_query_histogram(db: Any, query_model: QueryModel, granularity: timedelta) -> Tuple[str, Json]:
141+
def history_query_timeline(
142+
db: Any, query_model: QueryModel, after: datetime, granularity: timedelta
143+
) -> Tuple[str, Json]:
141144
ctx = ArangoQueryContext()
142145
query = rewrite_query(query_model)
143146
in_cursor, query_str = query_string(
144147
db, query, query_model, f"`{db.name}_node_history`", False, ctx, id_column="id", use_fulltext_index=False
145148
)
146149
crs = ctx.next_crs()
147-
slot = ctx.add_bind_var(granularity.total_seconds() * 1000)
150+
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})"
148155
query_str += (
149156
f" FOR {crs} IN {in_cursor} "
150-
f"COLLECT change={crs}.change, at=DATE_ISO8601(FLOOR(DATE_TIMESTAMP({crs}.changed_at) / @{slot}) * @{slot}) "
151-
f"WITH COUNT INTO v SORT at ASC "
157+
f"COLLECT change={crs}.change, at={slot_fn} WITH COUNT INTO v SORT at ASC "
152158
'RETURN {"at": at, "group": {"change": change}, "v": v}'
153159
)
154160
return query_str, ctx.bind_vars

fixcore/fixcore/db/graphdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ async def history_timeline(
815815
# in case no granularity is provided we will compute one: 1/25 of the time range but at least one hour
816816
gran = max(granularity or abs(before - after) / 25, timedelta(hours=1))
817817
query = self._history_query_model(query, changes, before, after)
818-
q_string, bind = arango_query.history_query_histogram(self, query, gran)
818+
q_string, bind = arango_query.history_query_timeline(self, query, after, gran)
819819
ttl = cast(Number, int(timeout.total_seconds())) if timeout else None
820820
return await self.db.aql_cursor(query=q_string, bind_vars=bind, batch_size=10000, ttl=ttl)
821821

0 commit comments

Comments
 (0)