mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
.
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
<script lang="ts" setup>
|
||||
import { onMounted } from 'vue';
|
||||
|
||||
import DateService, { type Slice } from '@services/DateService';
|
||||
|
||||
const data = ref<number[]>([]);
|
||||
const labels = ref<string[]>([]);
|
||||
const ready = ref<boolean>(false);
|
||||
const key = ref<string>('0');
|
||||
|
||||
const props = defineProps<{ slice: SliceName }>();
|
||||
const props = defineProps<{ slice: Slice }>();
|
||||
|
||||
async function loadData() {
|
||||
const response = await useTimelineData('sessions', props.slice);
|
||||
const response = await useTimeline('sessions', props.slice);
|
||||
if (!response) return;
|
||||
data.value = response.data;
|
||||
labels.value = response.labels;
|
||||
data.value = response.map(e => e.count);
|
||||
labels.value = response.map(e => DateService.getChartLabelFromISO(e._id, navigator.language, props.slice));
|
||||
ready.value = true;
|
||||
key.value = Date.now().toString();
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
<script lang="ts" setup>
|
||||
import { onMounted } from 'vue';
|
||||
import DateService, { type Slice } from '@services/DateService';
|
||||
|
||||
const data = ref<number[]>([]);
|
||||
const labels = ref<string[]>([]);
|
||||
const ready = ref<boolean>(false);
|
||||
|
||||
const props = defineProps<{ slice: SliceName }>();
|
||||
const props = defineProps<{ slice: Slice }>();
|
||||
|
||||
async function loadData() {
|
||||
const response = await useVisitsTimeline(props.slice);
|
||||
const response = await useTimeline('visits', props.slice);
|
||||
if (!response) return;
|
||||
const fixed = fixMetrics(response, props.slice);
|
||||
console.log(fixed);
|
||||
data.value = fixed.data;
|
||||
labels.value = fixed.labels;
|
||||
data.value = response.map(e => e.count);
|
||||
labels.value = response.map(e => DateService.getChartLabelFromISO(e._id, navigator.language, props.slice));
|
||||
ready.value = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ export function useFirstInteractionData() {
|
||||
return metricsInfo;
|
||||
}
|
||||
|
||||
export async function useVisitsTimeline(slice: Slice, fromDate?: string, toDate?: string) {
|
||||
export async function useTimeline(endpoint: 'visits' | 'sessions', slice: Slice, fromDate?: string, toDate?: string) {
|
||||
|
||||
const { from, to } = DateService.prepareDateRange(
|
||||
fromDate || DateService.getDefaultRange(slice).from,
|
||||
@@ -26,13 +26,13 @@ export async function useVisitsTimeline(slice: Slice, fromDate?: string, toDate?
|
||||
|
||||
const activeProject = useActiveProject();
|
||||
const response = await $fetch(
|
||||
`/api/metrics/${activeProject.value?._id}/timeline/visits`, {
|
||||
`/api/metrics/${activeProject.value?._id}/timeline/${endpoint}`, {
|
||||
method: 'POST',
|
||||
...signHeaders({ 'Content-Type': 'application/json' }),
|
||||
body: JSON.stringify({ slice, from, to })
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
return response as { _id: string, count: number }[];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,8 @@ watch(pending, () => {
|
||||
|
||||
const selectLabels = [
|
||||
{ label: 'Hour', value: 'hour' },
|
||||
{ label: 'Day', value: 'day' }
|
||||
{ label: 'Day', value: 'day' },
|
||||
// { label: 'Month', value: 'month' },
|
||||
];
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { getTimeline } from "./generic";
|
||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
|
||||
import DateService from "@services/DateService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getRequestProjectId(event);
|
||||
@@ -12,11 +12,37 @@ export default defineEventHandler(async event => {
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const { slice, duration } = await readBody(event);
|
||||
|
||||
return await Redis.useCache({ key: `timeline:sessions:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
|
||||
const timelineSessions = await getTimeline(SessionModel, project_id, slice, duration);
|
||||
return timelineSessions;
|
||||
const { slice, from, to } = await readBody(event);
|
||||
|
||||
if (!from) return setResponseStatus(event, 400, 'from is required');
|
||||
if (!from) return setResponseStatus(event, 400, 'to is required');
|
||||
if (!from) return setResponseStatus(event, 400, 'slice is required');
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `timeline:sessions:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
||||
exp: TIMELINE_EXPIRE_TIME
|
||||
}, async () => {
|
||||
|
||||
const { group, sort, fromParts } = DateService.getQueryDateRange(slice);
|
||||
|
||||
const aggregation = [
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||
}
|
||||
},
|
||||
{ $group: { _id: group, count: { $sum: 1 } } },
|
||||
{ $sort: sort },
|
||||
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
||||
]
|
||||
|
||||
const timelineVisits: { _id: string, count: number }[] = await SessionModel.aggregate(aggregation);
|
||||
const filledDates = DateService.fillDates(timelineVisits.map(e => e._id), slice);
|
||||
const merged = DateService.mergeFilledDates(filledDates, timelineVisits, '_id', slice, { count: 0 });
|
||||
return merged;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getTimeline } from "./generic";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import DateService from "@services/DateService";
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -12,11 +12,37 @@ export default defineEventHandler(async event => {
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const { slice, duration } = await readBody(event);
|
||||
|
||||
return await Redis.useCache({ key: `timeline:visits:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
|
||||
const timelineVisits = await getTimeline(VisitModel, project_id, slice, duration);
|
||||
return timelineVisits;
|
||||
const { slice, from, to } = await readBody(event);
|
||||
|
||||
if (!from) return setResponseStatus(event, 400, 'from is required');
|
||||
if (!from) return setResponseStatus(event, 400, 'to is required');
|
||||
if (!from) return setResponseStatus(event, 400, 'slice is required');
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `timeline:visits:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
||||
exp: TIMELINE_EXPIRE_TIME
|
||||
}, async () => {
|
||||
|
||||
const { group, sort, fromParts } = DateService.getQueryDateRange(slice);
|
||||
|
||||
const aggregation = [
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||
}
|
||||
},
|
||||
{ $group: { _id: group, count: { $sum: 1 } } },
|
||||
{ $sort: sort },
|
||||
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
||||
]
|
||||
|
||||
const timelineVisits: { _id: string, count: number }[] = await VisitModel.aggregate(aggregation);
|
||||
const filledDates = DateService.fillDates(timelineVisits.map(e => e._id), slice);
|
||||
const merged = DateService.mergeFilledDates(filledDates, timelineVisits, '_id', slice, { count: 0 });
|
||||
return merged;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ export class Redis {
|
||||
url: runtimeConfig.REDIS_URL,
|
||||
username: runtimeConfig.REDIS_USERNAME,
|
||||
password: runtimeConfig.REDIS_PASSWORD,
|
||||
database: process.dev ? 1 : 0
|
||||
});
|
||||
|
||||
static async init() {
|
||||
|
||||
Reference in New Issue
Block a user