From 6b97b4f49dd3549728a4597323486f4f11c7637d Mon Sep 17 00:00:00 2001 From: Emily Date: Tue, 2 Jul 2024 17:28:29 +0200 Subject: [PATCH] fix dashboard cards --- dashboard/components/dashboard/CountCard.vue | 2 +- dashboard/components/dashboard/TopCards.vue | 8 +++++--- dashboard/server/api/metrics/[project_id]/counts.ts | 13 ++++++------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/dashboard/components/dashboard/CountCard.vue b/dashboard/components/dashboard/CountCard.vue index ddefeb1..eebd985 100644 --- a/dashboard/components/dashboard/CountCard.vue +++ b/dashboard/components/dashboard/CountCard.vue @@ -23,7 +23,7 @@ const props = defineProps<{
-
{{ value }}
+
{{ value }}
{{ avg }}
{{ text }}
diff --git a/dashboard/components/dashboard/TopCards.vue b/dashboard/components/dashboard/TopCards.vue index ec52abb..7810e6b 100644 --- a/dashboard/components/dashboard/TopCards.vue +++ b/dashboard/components/dashboard/TopCards.vue @@ -72,9 +72,11 @@ async function loadData(timelineEndpointName: string, target: Data) { const pool = [...response.map(e => e.count)]; pool.pop(); const avg = pool.reduce((a, e) => a + e, 0) / pool.length; - const diffPercent = (100 / avg * (response.at(-1)?.count || 0)) - 100; - target.trend = diffPercent; - + + const diffPercent: number = (100 / avg * (response.at(-1)?.count || 0)) - 100; + + target.trend = Math.max(Math.min(diffPercent, 99), -99); + target.ready = true; } diff --git a/dashboard/server/api/metrics/[project_id]/counts.ts b/dashboard/server/api/metrics/[project_id]/counts.ts index b1d1311..c151be4 100644 --- a/dashboard/server/api/metrics/[project_id]/counts.ts +++ b/dashboard/server/api/metrics/[project_id]/counts.ts @@ -2,6 +2,8 @@ import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA"; import { ProjectCountModel } from "@schema/ProjectsCounts"; import { SessionModel } from "@schema/metrics/SessionSchema"; import { COUNTS_EXPIRE_TIME, COUNTS_SESSIONS_EXPIRE_TIME, Redis } from "~/server/services/CacheService"; +import { EventModel } from "@schema/metrics/EventSchema"; +import { VisitModel } from "@schema/metrics/VisitSchema"; export type MetricsCounts = { eventsCount: number, @@ -54,19 +56,16 @@ export default defineEventHandler(async event => { const totalSessionsTime = sessionsVisitsCount.reduce((a, e) => a + e.time, 0); const avgSessionDuration = totalSessionsTime / totalSessions; - const year = new Date().getFullYear(); - const month = new Date().getMonth(); - - const firstEventDate = new Date(year, month, 0, 0, 0, 0, 0).getTime(); - const firstViewDate = new Date(year, month, 0, 0, 0, 0, 0).getTime(); + const firstEvent = await EventModel.findOne({ project_id: project._id }, { created_at: 1 }); + const firstView = await VisitModel.findOne({ project_id: project._id }, { created_at: 1 }); return { eventsCount: count[0].events, visitsCount: count[0].visits, sessionsVisitsCount: totalSessions || 0, avgSessionDuration, - firstEventDate, - firstViewDate, + firstEventDate: firstEvent?.created_at.getTime() || Date.now(), + firstViewDate: firstView?.created_at.getTime() || Date.now(), } as MetricsCounts; });