From a3277f2c31293bd397e6197a6b1f8e3907af987c Mon Sep 17 00:00:00 2001 From: Pelotfr Date: Wed, 22 Nov 2023 19:05:41 +0100 Subject: [PATCH] Default agg to sum and simplified checks --- src/config.ts | 2 +- src/utils.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index dd792df..eff5347 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,7 +15,7 @@ export const DEFAULT_MAX_LIMIT = 500; export const DEFAULT_VERBOSE = false; export const APP_NAME = pkg.name; export const DEFAULT_SORT_BY = "DESC"; -export const DEFAULT_AGGREGATE_FUNCTION = "count"; +export const DEFAULT_AGGREGATE_FUNCTION = "sum"; // parse command line options const opts = program diff --git a/src/utils.ts b/src/utils.ts index e85dcb9..6f1d182 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import { z } from 'zod'; -import { DEFAULT_SORT_BY, config } from "./config.js"; +import { DEFAULT_SORT_BY, DEFAULT_AGGREGATE_FUNCTION, config } from "./config.js"; import { store } from "./clickhouse/stores.js"; import { toText } from './fetch/cors.js'; import { NormalizedHistoryData } from './queries.js'; @@ -80,7 +80,9 @@ export function parseTimestamp(timestamp?: string|null|number) { } export function parseAggregateFunction(aggregate_function?: string|null) { - if (aggregate_function == undefined || aggregate_function == null || aggregate_function == '') return "sum"; + // if not defined by user, use default + if (!aggregate_function) return DEFAULT_AGGREGATE_FUNCTION; + // if defined but not valid, return undefined else if (!z.enum(["min", "max", "avg", "sum", "count", "median"]).safeParse(aggregate_function).success) { return undefined; } @@ -88,11 +90,10 @@ export function parseAggregateFunction(aggregate_function?: string|null) { } export function parseHistoryRange(range?: string|null) { - if (range == undefined || range == '') return "24h"; + if (!range) return "24h"; if (!z.enum(["24h", "7d", "30d", "90d", "1y", "all"]).safeParse(range).success) { return undefined; } - //let interval = range.includes("h") ? 3600 : 86400; return range; }