mirror of
https://github.com/umami-software/umami.git
synced 2026-02-13 00:55:37 +01:00
move queries
This commit is contained in:
parent
910f165103
commit
8aec6d7406
53 changed files with 920 additions and 485 deletions
17
queries/analytics/stats/getActiveVisitors.js
Normal file
17
queries/analytics/stats/getActiveVisitors.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { rawQuery } from 'queries';
|
||||
import { subMinutes } from 'date-fns';
|
||||
|
||||
export function getActiveVisitors(website_id) {
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params = [website_id, date];
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select count(distinct session_id) x
|
||||
from pageview
|
||||
where website_id=$1
|
||||
and created_at >= $2
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
30
queries/analytics/stats/getRealtimeData.js
Normal file
30
queries/analytics/stats/getRealtimeData.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { getPageviews } from '../pageview/getPageviews';
|
||||
import { getSessions } from '../session/getSessions';
|
||||
import { getEvents } from '../event/getEvents';
|
||||
|
||||
export async function getRealtimeData(websites, time) {
|
||||
const [pageviews, sessions, events] = await Promise.all([
|
||||
getPageviews(websites, time),
|
||||
getSessions(websites, time),
|
||||
getEvents(websites, time),
|
||||
]);
|
||||
|
||||
return {
|
||||
pageviews: pageviews.map(({ view_id, ...props }) => ({
|
||||
__id: `p${view_id}`,
|
||||
view_id,
|
||||
...props,
|
||||
})),
|
||||
sessions: sessions.map(({ session_id, ...props }) => ({
|
||||
__id: `s${session_id}`,
|
||||
session_id,
|
||||
...props,
|
||||
})),
|
||||
events: events.map(({ event_id, ...props }) => ({
|
||||
__id: `e${event_id}`,
|
||||
event_id,
|
||||
...props,
|
||||
})),
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
}
|
||||
29
queries/analytics/stats/getWebsiteStats.js
Normal file
29
queries/analytics/stats/getWebsiteStats.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { parseFilters, rawQuery, getDateQuery, getTimestampInterval } from 'queries';
|
||||
|
||||
export function getWebsiteStats(website_id, start_at, end_at, filters = {}) {
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(t.time) as "totaltime"
|
||||
from (
|
||||
select pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
count(*) c,
|
||||
${getTimestampInterval('pageview.created_at')} as "time"
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1, 2
|
||||
) t
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue