mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 08:18:37 +01:00
new selfhosted version
This commit is contained in:
35
dashboard/server/api/raw/export_events.ts
Normal file
35
dashboard/server/api/raw/export_events.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { EventModel } from "~/shared/schema/metrics/EventSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'permission:webAnalytics');
|
||||
const { project_id, domain } = ctx;
|
||||
|
||||
const websiteMatch = domain ? { website: domain } : {};
|
||||
|
||||
const visits = await EventModel.find({ project_id, ...websiteMatch });
|
||||
|
||||
const csvHeader = [
|
||||
"name",
|
||||
"session",
|
||||
"metadata",
|
||||
"website",
|
||||
"created_at",
|
||||
];
|
||||
|
||||
|
||||
const lines: any[] = [];
|
||||
visits.forEach(line => lines.push(line.toJSON()));
|
||||
|
||||
const result = csvHeader.join(',') + '\n' + lines.map(element => {
|
||||
const content: string[] = [];
|
||||
for (const key of csvHeader) {
|
||||
content.push(key === 'metadata' ? JSON.stringify(element[key]) : element[key]);
|
||||
}
|
||||
return content.join(',');
|
||||
}).join('\n');
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user