Events table.

This commit is contained in:
Mike Cao 2020-08-24 23:49:14 -07:00
parent a19248d713
commit 5e9299be1e
13 changed files with 131 additions and 31 deletions

View file

@ -35,5 +35,6 @@ export default prisma;
export async function runQuery(query) {
return query.catch(e => {
console.error(e);
throw e;
});
}

View file

@ -124,5 +124,5 @@ export const countryFilter = data =>
export const percentFilter = data => {
const total = data.reduce((n, { y }) => n + y, 0);
return data.map(({ x, y }) => ({ x, y, z: total ? (y / total) * 100 : 0 }));
return data.map(({ x, y, ...props }) => ({ x, y, z: total ? (y / total) * 100 : 0, ...props }));
};

View file

@ -400,3 +400,41 @@ export function getActiveVisitors(website_id) {
return Promise.resolve([]);
}
export function getEvents(website_id, start_at, end_at) {
const db = getDatabase();
if (db === POSTGRESQL) {
return prisma.$queryRaw(
`
select distinct event_type w, event_value x, count(*) y
from event
where website_id=$1
and created_at between $2 and $3
group by 1, 2
order by 3 desc
`,
website_id,
start_at,
end_at,
);
}
if (db === MYSQL) {
return prisma.$queryRaw(
`
select distinct event_type w, event_value x, count(*) y
from event
where website_id=?
and created_at between ? and ?
group by 1, 2
order by 3 desc
`,
website_id,
start_at,
end_at,
);
}
return Promise.resolve([]);
}