Skip to content

Commit

Permalink
log information for viz query
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Feb 13, 2023
1 parent 6254e1a commit d897614
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/server/routes/query/routes/visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<string, unknown> {
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<string, unknown> = { 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 });
Expand Down

0 comments on commit d897614

Please sign in to comment.