|
8 | 8 |
|
9 | 9 | from arango.typings import Json |
10 | 10 | from attrs import evolve |
| 11 | +from math import floor |
11 | 12 |
|
12 | 13 | from fixcore.constants import less_greater_then_operations as lgt_ops, arangodb_matches_null_ops |
13 | 14 | from fixcore.db import EstimatedSearchCost, EstimatedQueryCostRating as Rating |
@@ -137,18 +138,23 @@ def history_query(db: Any, query_model: QueryModel) -> Tuple[str, Json]: |
137 | 138 | return f"""{query_str} FOR result in {cursor}{last_limit} RETURN UNSET(result, {unset_props})""", ctx.bind_vars |
138 | 139 |
|
139 | 140 |
|
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]: |
141 | 144 | ctx = ArangoQueryContext() |
142 | 145 | query = rewrite_query(query_model) |
143 | 146 | in_cursor, query_str = query_string( |
144 | 147 | db, query, query_model, f"`{db.name}_node_history`", False, ctx, id_column="id", use_fulltext_index=False |
145 | 148 | ) |
146 | 149 | 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})" |
148 | 155 | query_str += ( |
149 | 156 | 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 " |
152 | 158 | 'RETURN {"at": at, "group": {"change": change}, "v": v}' |
153 | 159 | ) |
154 | 160 | return query_str, ctx.bind_vars |
|
0 commit comments