From 240a9cd6331a4efa83a506d7b0161d872a7d0a25 Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 21 Jun 2024 17:50:51 +0200 Subject: [PATCH] . --- .../components/dashboard/VisitsLineChart.vue | 2 +- dashboard/composables/useDataService.ts | 15 ++++++++++++ shared/services/DateService.ts | 24 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/dashboard/components/dashboard/VisitsLineChart.vue b/dashboard/components/dashboard/VisitsLineChart.vue index 57330ef..4504db2 100644 --- a/dashboard/components/dashboard/VisitsLineChart.vue +++ b/dashboard/components/dashboard/VisitsLineChart.vue @@ -8,7 +8,7 @@ const ready = ref(false); const props = defineProps<{ slice: SliceName }>(); async function loadData() { - const response = await useTimelineDataRaw('visits', props.slice); + const response = await useVisitsTimeline(props.slice); if (!response) return; const fixed = fixMetrics(response, props.slice); console.log(fixed); diff --git a/dashboard/composables/useDataService.ts b/dashboard/composables/useDataService.ts index 6498b47..66f2ffc 100644 --- a/dashboard/composables/useDataService.ts +++ b/dashboard/composables/useDataService.ts @@ -1,3 +1,5 @@ +import type { Slice } from "@services/DateService"; +import DateService from "@services/DateService"; import type { MetricsCounts } from "~/server/api/metrics/[project_id]/counts"; import type { VisitsWebsiteAggregated } from "~/server/api/metrics/[project_id]/data/websites"; import type { MetricsTimeline } from "~/server/api/metrics/[project_id]/timeline/generic"; @@ -14,6 +16,19 @@ export function useFirstInteractionData() { return metricsInfo; } +export async function useVisitsTimeline(fromDate: string, toDate: string, slice: Slice) { + const { from, to } = DateService.prepareDateRange(fromDate, toDate, slice); + const activeProject = useActiveProject(); + const response = await $fetch( + `/api/metrics/${activeProject.value?._id}/timeline/visits`, { + method: 'POST', + ...signHeaders({ 'Content-Type': 'application/json' }), + body: JSON.stringify({ slice, from, to }) + }); + return response; + +} + export async function useTimelineDataRaw(timelineEndpointName: string, slice: SliceName) { const activeProject = useActiveProject(); diff --git a/shared/services/DateService.ts b/shared/services/DateService.ts index 7f5afa4..2793d5b 100644 --- a/shared/services/DateService.ts +++ b/shared/services/DateService.ts @@ -1,10 +1,32 @@ import dayjs from 'dayjs'; -export type Slice = 'day' | 'hour' | 'month' | 'year'; +export type Slice = keyof typeof slicesData; + +const slicesData = { + hour: { + fromOffset: 1000 * 60 * 60 * 24 + }, + day: { + fromOffset: 1000 * 60 * 60 * 24 * 7 + }, + month: { + fromOffset: 1000 * 60 * 60 * 24 * 30 * 12 + }, + year: { + fromOffset: 1000 * 60 * 60 * 24 * 30 * 12 * 10 + } +} + class DateService { + public slicesData = slicesData; + + getDefaultRange(slice: Slice, from?: string, to?: string) { + + } + getQueryDateRange(slice: Slice) { const group: Record = {}