diff --git a/src/server/routes/query/routes/visualization.ts b/src/server/routes/query/routes/visualization.ts index 1887ae935..814264202 100644 --- a/src/server/routes/query/routes/visualization.ts +++ b/src/server/routes/query/routes/visualization.ts @@ -14,9 +14,11 @@ * limitations under the License. */ +import { Duration } from "chronoshift"; import { Response } from "express"; import { Expression } from "plywood"; import makeGridQuery from "../../../../client/visualizations/grid/make-query"; +import { Logger } from "../../../../common/logger/logger"; import { Essence } from "../../../../common/models/essence/essence"; import { Timekeeper } from "../../../../common/models/timekeeper/timekeeper"; import makeQuery from "../../../../common/utils/query/visualization-query"; @@ -27,8 +29,42 @@ function getQuery(essence: Essence, timekeeper: Timekeeper): Expression { return essence.visualization.name === "grid" ? makeGridQuery(essence, timekeeper) : makeQuery(essence, timekeeper); } +function timeVariables(essence: Essence, timekeeper: Timekeeper): Record { + const timeDimension = essence.getTimeDimension(); + const timeFilter = essence.currentTimeFilter(timekeeper); + const timeRange = timeFilter.values.first(); + const timeSplit = essence.splits.findSplitForDimension(timeDimension); + + const startTime = timeRange.start.toUTCString(); + const interval = timeRange.end.getTime() - timeRange.start.getTime(); + + const variables: Record = { startTime, interval }; + if (timeSplit && timeSplit.bucket instanceof Duration) { + variables.granularity = timeSplit.bucket.getDescription(); + } + if (essence.hasComparison()) { + const previousTimeFilter = essence.previousTimeFilter(timekeeper); + variables.shiftedTimeStart = previousTimeFilter.values.first().start.toUTCString(); + } + + return variables; +} + +function log(essence: Essence, logger: Logger, timekeeper: Timekeeper) { + const nonTimeFilters = essence.filter.removeClause(essence.getTimeDimension().name); + + logger.log("visualization query", { + ...timeVariables(essence, timekeeper), + visualization: essence.visualization.name, + filters: nonTimeFilters.clauses.map(clause => clause.reference).toArray(), + splits: essence.splits.splits.map(split => split.reference).toArray(), + series: essence.series.series.map(series => series.reference).toArray() + }); +} + export default async function visualizationRoute({ context }: QueryRouterRequest, res: Response) { - const { dataCube, essence, decorator, timekeeper } = context; + const { dataCube, essence, decorator, timekeeper, logger } = context; + log(essence, logger, timekeeper); const query = getQuery(essence, timekeeper); const result = await executeQuery(dataCube, query, essence.timezone, decorator); res.json({ result });