diff --git a/dashboard/components/AdvancedLineChart.vue b/dashboard/components/AdvancedLineChart.vue index 00c9c8d..6f50a19 100644 --- a/dashboard/components/AdvancedLineChart.vue +++ b/dashboard/components/AdvancedLineChart.vue @@ -80,6 +80,9 @@ const { lineChartProps, lineChartRef } = useLineChart({ chartData: chartData, op onMounted(async () => { + + console.log('MOUNTED') + const c = document.createElement('canvas'); const ctx = c.getContext("2d"); let gradient: any = `${props.color}22`; @@ -106,5 +109,5 @@ onMounted(async () => { diff --git a/dashboard/components/dashboard/SessionsLineChart.vue b/dashboard/components/dashboard/SessionsLineChart.vue index 3ce23f3..59dacaf 100644 --- a/dashboard/components/dashboard/SessionsLineChart.vue +++ b/dashboard/components/dashboard/SessionsLineChart.vue @@ -1,46 +1,43 @@ \ No newline at end of file diff --git a/dashboard/components/dashboard/TopCards.vue b/dashboard/components/dashboard/TopCards.vue index ea2bbe9..417cdb9 100644 --- a/dashboard/components/dashboard/TopCards.vue +++ b/dashboard/components/dashboard/TopCards.vue @@ -1,6 +1,7 @@ \ No newline at end of file diff --git a/dashboard/composables/useCustomFetch.ts b/dashboard/composables/useCustomFetch.ts index 8fdd53f..63bf471 100644 --- a/dashboard/composables/useCustomFetch.ts +++ b/dashboard/composables/useCustomFetch.ts @@ -6,7 +6,9 @@ type NitroFetchRequest = Exclude Record } type OnResponseCallback = (data: Ref) => any @@ -27,7 +29,13 @@ export function useCustomFetch(url: NitroFetchRequest, getHeaders: () => Reco pending.value = true; error.value = undefined; try { - data.value = await $fetch(url, { headers: getHeaders() }); + + data.value = await $fetch(url, { + headers: getHeaders(), + method: (options?.method || 'GET') as any, + body: options?.getBody ? JSON.stringify(options.getBody()) : undefined + }); + onResponseCallback(data); } catch (err) { error.value = err as Error; diff --git a/dashboard/composables/useDataService.ts b/dashboard/composables/useDataService.ts index e7678ac..8c6e1a2 100644 --- a/dashboard/composables/useDataService.ts +++ b/dashboard/composables/useDataService.ts @@ -19,6 +19,26 @@ export function useMetricsData() { return metricsInfo; } + + + +const { safeSnapshotDates, snapshot } = useSnapshot() +const activeProject = useActiveProject(); + +const createFromToHeaders = (headers: Record = {}) => ({ + 'x-from': safeSnapshotDates.value.from, + 'x-to': safeSnapshotDates.value.to, + ...headers +}); + +const createFromToBody = (body: Record = {}) => ({ + from: safeSnapshotDates.value.from, + to: safeSnapshotDates.value.to, + ...body +}); + + + export function useFirstInteractionData() { const activeProject = useActiveProject(); const metricsInfo = useFetch(`/api/metrics/${activeProject.value?._id}/first_interaction`, signHeaders()); @@ -26,33 +46,25 @@ export function useFirstInteractionData() { } -export async function useTimelineAdvanced(endpoint: string, slice: Slice, fromDate?: string, toDate?: string, customBody: Object = {}) { - - const { from, to } = DateService.prepareDateRange( - fromDate || DateService.getDefaultRange(slice).from, - toDate || DateService.getDefaultRange(slice).to, - slice - ); - - const activeProject = useActiveProject(); - const response = await $fetch( - `/api/metrics/${activeProject.value?._id}/timeline/${endpoint}`, { +export function useTimelineAdvanced(endpoint: string, slice: Ref, customBody: Object = {}) { + const response = useCustomFetch<{ _id: string, count: number }[]>( + `/api/metrics/${activeProject.value?._id}/timeline/${endpoint}`, + () => signHeaders({ 'Content-Type': 'application/json' }).headers, { method: 'POST', - ...signHeaders({ 'Content-Type': 'application/json' }), - body: JSON.stringify({ slice, from, to, ...customBody }) + getBody: () => createFromToBody({ slice: slice.value, ...customBody }), + lazy: true, + watchProps: [snapshot, slice] }); - - return response as { _id: string, count: number }[]; - + return response; } -export async function useTimeline(endpoint: 'visits' | 'sessions' | 'referrers', slice: Slice, fromDate?: string, toDate?: string) { - return await useTimelineAdvanced(endpoint, slice, fromDate, toDate, {}); +export function useTimeline(endpoint: 'visits' | 'sessions' | 'referrers', slice: Ref) { + return useTimelineAdvanced(endpoint, slice); } -export async function useReferrersTimeline(referrer: string, slice: Slice, fromDate?: string, toDate?: string) { - return await useTimelineAdvanced('referrers', slice, fromDate, toDate, { referrer }); +export async function useReferrersTimeline(referrer: string, slice: Ref) { + return await useTimelineAdvanced('referrers', slice, { referrer }); } @@ -93,21 +105,12 @@ export function usePagesData(website: string, limit: number = 10) { } -const { safeSnapshotDates, snapshot } = useSnapshot() -const activeProject = useActiveProject(); - -const getFromToHeaders = (headers: Record = {}) => ({ - 'x-from': safeSnapshotDates.value.from, - 'x-to': safeSnapshotDates.value.to, - ...headers -}); - export function useWebsitesData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/websites`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -115,7 +118,7 @@ export function useWebsitesData(limit: number = 10) { export function useEventsData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/events`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -123,7 +126,7 @@ export function useEventsData(limit: number = 10) { export function useReferrersData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/referrers`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -131,7 +134,7 @@ export function useReferrersData(limit: number = 10) { export function useBrowsersData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/browsers`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -139,7 +142,7 @@ export function useBrowsersData(limit: number = 10) { export function useOssData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/oss`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -147,7 +150,7 @@ export function useOssData(limit: number = 10) { export function useGeolocationData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/countries`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; @@ -155,7 +158,7 @@ export function useGeolocationData(limit: number = 10) { export function useDevicesData(limit: number = 10) { const res = useCustomFetch(`/api/metrics/${activeProject.value?._id}/data/devices`, - () => signHeaders(getFromToHeaders({ 'x-query-limit': limit.toString() })).headers, + () => signHeaders(createFromToHeaders({ 'x-query-limit': limit.toString() })).headers, { lazy: false, watchProps: [snapshot] } ); return res; diff --git a/dashboard/pages/index.vue b/dashboard/pages/index.vue index 1a5fb8d..71073c2 100644 --- a/dashboard/pages/index.vue +++ b/dashboard/pages/index.vue @@ -119,7 +119,6 @@ const selectLabels = [ -