web: histogram

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-01-07 21:56:30 +01:00
parent 31a4e1a277
commit 39827226d8
29 changed files with 523 additions and 321 deletions

View File

@@ -8,6 +8,7 @@ export const operators = {
export const chartTypes = {
linear: 'Linear',
bar: 'Bar',
histogram: 'Histogram',
pie: 'Pie',
metric: 'Metric',
area: 'Area',
@@ -33,19 +34,19 @@ export const alphabetIds = [
'J',
] as const;
export const timeRanges = [
{ range: 0.3, title: '30m' },
{ range: 0.6, title: '1h' },
{ range: 0, title: 'Today' },
{ range: 1, title: '24h' },
{ range: 7, title: '7d' },
{ range: 14, title: '14d' },
{ range: 30, title: '30d' },
{ range: 90, title: '3mo' },
{ range: 180, title: '6mo' },
{ range: 365, title: '1y' },
] as const;
export const timeRanges = {
'30min': '30min',
'1h': '1h',
today: 'today',
'24h': '24h',
'7d': '7d',
'14d': '14d',
'1m': '1m',
'3m': '3m',
'6m': '6m',
'1y': '1y',
} as const;
export function isMinuteIntervalEnabledByRange(range: number) {
return range === 0.3 || range === 0.6;
export function isMinuteIntervalEnabledByRange(range: keyof typeof timeRanges) {
return range === '30min' || range === '1h';
}

View File

@@ -1,7 +0,0 @@
import type { IChartRange } from '@/types';
import { timeRanges } from './constants';
export function getRangeLabel(range: IChartRange) {
return timeRanges.find((item) => item.range === range)?.title ?? null;
}

View File

@@ -1,6 +1,6 @@
import { z } from 'zod';
import { chartTypes, intervals, operators } from './constants';
import { chartTypes, intervals, operators, timeRanges } from './constants';
function objectToZodEnums<K extends string>(obj: Record<K, any>): [K, ...K[]] {
const [firstKey, ...otherKeys] = Object.keys(obj) as K[];
@@ -11,7 +11,7 @@ export const zChartEvent = z.object({
id: z.string(),
name: z.string(),
displayName: z.string().optional(),
segment: z.enum(['event', 'user', 'user_average']),
segment: z.enum(['event', 'user', 'user_average', 'one_event_per_user']),
filters: z.array(
z.object({
id: z.string(),
@@ -39,17 +39,7 @@ export const zChartInput = z.object({
interval: zTimeInterval,
events: zChartEvents,
breakdowns: zChartBreakdowns,
range: z
.literal(0)
.or(z.literal(0.3))
.or(z.literal(0.6))
.or(z.literal(1))
.or(z.literal(7))
.or(z.literal(14))
.or(z.literal(30))
.or(z.literal(90))
.or(z.literal(180))
.or(z.literal(365)),
range: z.enum(objectToZodEnums(timeRanges)),
});
export const zChartInputWithDates = zChartInput.extend({