Merge pull request #1294 from umami-software/brian/um-22-query-folder-structure

um-22 query folder structure
pull/1330/head
Mike Cao 2022-07-13 08:21:25 -07:00 committed by GitHub
commit cef365bb55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
51 changed files with 595 additions and 485 deletions

View File

@ -1,6 +1,6 @@
import { parseSecureToken, parseToken } from './crypto'; import { parseSecureToken, parseToken } from './crypto';
import { SHARE_TOKEN_HEADER } from './constants'; import { SHARE_TOKEN_HEADER } from './constants';
import { getWebsiteById } from './queries'; import { getWebsiteById } from 'queries';
export async function getAuthToken(req) { export async function getAuthToken(req) {
try { try {

View File

@ -1,13 +1,6 @@
import moment from 'moment-timezone'; import { MYSQL, MYSQL_DATE_FORMATS, POSTGRESQL, POSTGRESQL_DATE_FORMATS } from 'lib/constants';
import prisma from 'lib/db'; import prisma from 'lib/db';
import { subMinutes } from 'date-fns'; import moment from 'moment-timezone';
import {
MYSQL,
POSTGRESQL,
MYSQL_DATE_FORMATS,
POSTGRESQL_DATE_FORMATS,
URL_LENGTH,
} from 'lib/constants';
export function getDatabase() { export function getDatabase() {
const type = const type =
@ -160,459 +153,3 @@ export async function rawQuery(query, params = []) {
return runQuery(prisma.$queryRawUnsafe.apply(prisma, [sql, ...params])); return runQuery(prisma.$queryRawUnsafe.apply(prisma, [sql, ...params]));
} }
export async function getWebsiteById(website_id) {
return runQuery(
prisma.website.findUnique({
where: {
website_id,
},
}),
);
}
export async function getWebsiteByUuid(website_uuid) {
return runQuery(
prisma.website.findUnique({
where: {
website_uuid,
},
}),
);
}
export async function getWebsiteByShareId(share_id) {
return runQuery(
prisma.website.findUnique({
where: {
share_id,
},
}),
);
}
export async function getUserWebsites(user_id) {
return runQuery(
prisma.website.findMany({
where: {
user_id,
},
orderBy: {
name: 'asc',
},
}),
);
}
export async function getAllWebsites() {
let data = await runQuery(
prisma.website.findMany({
orderBy: [
{
user_id: 'asc',
},
{
name: 'asc',
},
],
include: {
account: {
select: {
username: true,
},
},
},
}),
);
return data.map(i => ({ ...i, account: i.account.username }));
}
export async function createWebsite(user_id, data) {
return runQuery(
prisma.website.create({
data: {
account: {
connect: {
user_id,
},
},
...data,
},
}),
);
}
export async function updateWebsite(website_id, data) {
return runQuery(
prisma.website.update({
where: {
website_id,
},
data,
}),
);
}
export async function resetWebsite(website_id) {
return runQuery(prisma.$queryRaw`delete from session where website_id=${website_id}`);
}
export async function deleteWebsite(website_id) {
return runQuery(
prisma.website.delete({
where: {
website_id,
},
}),
);
}
export async function createSession(website_id, data) {
return runQuery(
prisma.session.create({
data: {
website_id,
...data,
},
select: {
session_id: true,
},
}),
);
}
export async function getSessionByUuid(session_uuid) {
return runQuery(
prisma.session.findUnique({
where: {
session_uuid,
},
}),
);
}
export async function savePageView(website_id, session_id, url, referrer) {
return runQuery(
prisma.pageview.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
referrer: referrer?.substr(0, URL_LENGTH),
},
}),
);
}
export async function saveEvent(website_id, session_id, url, event_type, event_value) {
return runQuery(
prisma.event.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
event_type: event_type?.substr(0, 50),
event_value: event_value?.substr(0, 50),
},
}),
);
}
export async function getAccounts() {
return runQuery(
prisma.account.findMany({
orderBy: [
{ is_admin: 'desc' },
{
username: 'asc',
},
],
select: {
user_id: true,
username: true,
is_admin: true,
created_at: true,
updated_at: true,
},
}),
);
}
export async function getAccountById(user_id) {
return runQuery(
prisma.account.findUnique({
where: {
user_id,
},
}),
);
}
export async function getAccountByUsername(username) {
return runQuery(
prisma.account.findUnique({
where: {
username,
},
}),
);
}
export async function updateAccount(user_id, data) {
return runQuery(
prisma.account.update({
where: {
user_id,
},
data,
}),
);
}
export async function deleteAccount(user_id) {
return runQuery(
prisma.account.delete({
where: {
user_id,
},
}),
);
}
export async function createAccount(data) {
return runQuery(
prisma.account.create({
data,
}),
);
}
export async function getSessions(websites, start_at) {
return runQuery(
prisma.session.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}
export async function getPageviews(websites, start_at) {
return runQuery(
prisma.pageview.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}
export async function getEvents(websites, start_at) {
return runQuery(
prisma.event.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}
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', false, 'date')},
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,
);
}
export function getPageviewStats(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
count = '*',
filters = {},
) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
return rawQuery(
`
select
${getDateStringQuery('g.t', unit)} as t,
g.y as y
from
(select ${getDateQuery('pageview.created_at', unit, timezone)} t,
count(${count}) y
from pageview
${joinSession}
where pageview.website_id=$1
and pageview.created_at between $2 and $3
${pageviewQuery}
${sessionQuery}
group by 1) g
order by 1
`,
params,
);
}
export function getSessionMetrics(website_id, start_at, end_at, field, filters = {}) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
return rawQuery(
`
select ${field} x, count(*) y
from session as x
where x.session_id in (
select pageview.session_id
from pageview
${joinSession}
where pageview.website_id=$1
and pageview.created_at between $2 and $3
${pageviewQuery}
${sessionQuery}
)
group by 1
order by 2 desc
`,
params,
);
}
export function getPageviewMetrics(website_id, start_at, end_at, field, table, filters = {}) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table,
filters,
params,
);
return rawQuery(
`
select ${field} x, count(*) y
from ${table}
${joinSession}
where ${table}.website_id=$1
and ${table}.created_at between $2 and $3
${pageviewQuery}
${joinSession && sessionQuery}
${eventQuery}
group by 1
order by 2 desc
`,
params,
);
}
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,
);
}
export function getEventMetrics(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
filters = {},
) {
const params = [website_id, start_at, end_at];
return rawQuery(
`
select
event_value x,
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} t,
count(*) y
from event
where website_id=$1
and created_at between $2 and $3
${getFilterQuery('event', filters, params)}
group by 1, 2
order by 2
`,
params,
);
}
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(),
};
}

View File

@ -1,4 +1,4 @@
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries'; import { getWebsiteByUuid, getSessionByUuid, createSession } from 'queries';
import { getJsonBody, getClientInfo } from 'lib/request'; import { getJsonBody, getClientInfo } from 'lib/request';
import { uuid, isValidUuid, parseToken } from 'lib/crypto'; import { uuid, isValidUuid, parseToken } from 'lib/crypto';

View File

@ -1,4 +1,4 @@
import { getAccountById, deleteAccount } from 'lib/queries'; import { getAccountById, deleteAccount } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { methodNotAllowed, ok, unauthorized } from 'lib/response'; import { methodNotAllowed, ok, unauthorized } from 'lib/response';

View File

@ -1,4 +1,4 @@
import { getAccountById, getAccountByUsername, updateAccount, createAccount } from 'lib/queries'; import { getAccountById, getAccountByUsername, updateAccount, createAccount } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { hashPassword } from 'lib/crypto'; import { hashPassword } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed, badRequest } from 'lib/response'; import { ok, unauthorized, methodNotAllowed, badRequest } from 'lib/response';

View File

@ -1,4 +1,4 @@
import { getAccountById, updateAccount } from 'lib/queries'; import { getAccountById, updateAccount } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'lib/response'; import { badRequest, methodNotAllowed, ok, unauthorized } from 'lib/response';
import { checkPassword, hashPassword } from 'lib/crypto'; import { checkPassword, hashPassword } from 'lib/crypto';

View File

@ -1,4 +1,4 @@
import { getAccounts } from 'lib/queries'; import { getAccounts } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { ok, unauthorized, methodNotAllowed } from 'lib/response'; import { ok, unauthorized, methodNotAllowed } from 'lib/response';

View File

@ -1,5 +1,5 @@
import { checkPassword, createSecureToken } from 'lib/crypto'; import { checkPassword, createSecureToken } from 'lib/crypto';
import { getAccountByUsername } from 'lib/queries'; import { getAccountByUsername } from 'queries/admin/account/getAccountByUsername';
import { ok, unauthorized, badRequest } from 'lib/response'; import { ok, unauthorized, badRequest } from 'lib/response';
export default async (req, res) => { export default async (req, res) => {

View File

@ -1,7 +1,7 @@
const { Resolver } = require('dns').promises; const { Resolver } = require('dns').promises;
import isbot from 'isbot'; import isbot from 'isbot';
import ipaddr from 'ipaddr.js'; import ipaddr from 'ipaddr.js';
import { savePageView, saveEvent } from 'lib/queries'; import { savePageView, saveEvent } from 'queries';
import { useCors, useSession } from 'lib/middleware'; import { useCors, useSession } from 'lib/middleware';
import { getJsonBody, getIpAddress } from 'lib/request'; import { getJsonBody, getIpAddress } from 'lib/request';
import { ok, send, badRequest, forbidden } from 'lib/response'; import { ok, send, badRequest, forbidden } from 'lib/response';

View File

@ -1,7 +1,7 @@
import { subMinutes } from 'date-fns'; import { subMinutes } from 'date-fns';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed } from 'lib/response'; import { ok, methodNotAllowed } from 'lib/response';
import { getUserWebsites, getRealtimeData } from 'lib/queries'; import { getUserWebsites, getRealtimeData } from 'queries';
import { createToken } from 'lib/crypto'; import { createToken } from 'lib/crypto';
export default async (req, res) => { export default async (req, res) => {

View File

@ -1,6 +1,6 @@
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed, badRequest } from 'lib/response'; import { ok, methodNotAllowed, badRequest } from 'lib/response';
import { getRealtimeData } from 'lib/queries'; import { getRealtimeData } from 'queries';
import { parseToken } from 'lib/crypto'; import { parseToken } from 'lib/crypto';
import { SHARE_TOKEN_HEADER } from 'lib/constants'; import { SHARE_TOKEN_HEADER } from 'lib/constants';

View File

@ -1,4 +1,4 @@
import { getWebsiteByShareId } from 'lib/queries'; import { getWebsiteByShareId } from 'queries';
import { ok, notFound, methodNotAllowed } from 'lib/response'; import { ok, notFound, methodNotAllowed } from 'lib/response';
import { createToken } from 'lib/crypto'; import { createToken } from 'lib/crypto';

View File

@ -1,7 +1,7 @@
import { getActiveVisitors } from 'lib/queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response'; import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';
import { getActiveVisitors } from 'queries';
export default async (req, res) => { export default async (req, res) => {
if (req.method === 'GET') { if (req.method === 'GET') {

View File

@ -1,5 +1,5 @@
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { getEventMetrics } from 'lib/queries'; import { getEventMetrics } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response'; import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';

View File

@ -1,4 +1,4 @@
import { deleteWebsite, getWebsiteById } from 'lib/queries'; import { deleteWebsite, getWebsiteById } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response'; import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';

View File

@ -1,4 +1,4 @@
import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'lib/queries'; import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'queries';
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response'; import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';

View File

@ -1,5 +1,5 @@
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { getPageviewStats } from 'lib/queries'; import { getPageviewStats } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response'; import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';

View File

@ -1,4 +1,4 @@
import { resetWebsite } from 'lib/queries'; import { resetWebsite } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response'; import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';

View File

@ -1,4 +1,4 @@
import { getWebsiteStats } from 'lib/queries'; import { getWebsiteStats } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response'; import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';

View File

@ -1,4 +1,4 @@
import { updateWebsite, createWebsite, getWebsiteById } from 'lib/queries'; import { updateWebsite, createWebsite, getWebsiteById } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { uuid, getRandomChars } from 'lib/crypto'; import { uuid, getRandomChars } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed } from 'lib/response'; import { ok, unauthorized, methodNotAllowed } from 'lib/response';

View File

@ -1,4 +1,4 @@
import { getAllWebsites, getUserWebsites } from 'lib/queries'; import { getAllWebsites, getUserWebsites } from 'queries';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed, unauthorized } from 'lib/response'; import { ok, methodNotAllowed, unauthorized } from 'lib/response';

View File

@ -0,0 +1,10 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function createAccount(data) {
return runQuery(
prisma.account.create({
data,
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function deleteAccount(user_id) {
return runQuery(
prisma.account.delete({
where: {
user_id,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getAccountById(user_id) {
return runQuery(
prisma.account.findUnique({
where: {
user_id,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getAccountByUsername(username) {
return runQuery(
prisma.account.findUnique({
where: {
username,
},
}),
);
}

View File

@ -0,0 +1,22 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getAccounts() {
return runQuery(
prisma.account.findMany({
orderBy: [
{ is_admin: 'desc' },
{
username: 'asc',
},
],
select: {
user_id: true,
username: true,
is_admin: true,
created_at: true,
updated_at: true,
},
}),
);
}

View File

@ -0,0 +1,13 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function updateAccount(user_id, data) {
return runQuery(
prisma.account.update({
where: {
user_id,
},
data,
}),
);
}

View File

@ -0,0 +1,17 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function createWebsite(user_id, data) {
return runQuery(
prisma.website.create({
data: {
account: {
connect: {
user_id,
},
},
...data,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function deleteWebsite(website_id) {
return runQuery(
prisma.website.delete({
where: {
website_id,
},
}),
);
}

View File

@ -0,0 +1,25 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getAllWebsites() {
let data = await runQuery(
prisma.website.findMany({
orderBy: [
{
user_id: 'asc',
},
{
name: 'asc',
},
],
include: {
account: {
select: {
username: true,
},
},
},
}),
);
return data.map(i => ({ ...i, account: i.account.username }));
}

View File

@ -0,0 +1,15 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getUserWebsites(user_id) {
return runQuery(
prisma.website.findMany({
where: {
user_id,
},
orderBy: {
name: 'asc',
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getWebsiteById(website_id) {
return runQuery(
prisma.website.findUnique({
where: {
website_id,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getWebsiteByShareId(share_id) {
return runQuery(
prisma.website.findUnique({
where: {
share_id,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getWebsiteByUuid(website_uuid) {
return runQuery(
prisma.website.findUnique({
where: {
website_uuid,
},
}),
);
}

View File

@ -0,0 +1,6 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function resetWebsite(website_id) {
return runQuery(prisma.$queryRaw`delete from session where website_id=${website_id}`);
}

View File

@ -0,0 +1,13 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function updateWebsite(website_id, data) {
return runQuery(
prisma.website.update({
where: {
website_id,
},
data,
}),
);
}

View File

@ -0,0 +1,28 @@
import { getDateQuery, getDateStringQuery, getFilterQuery, rawQuery } from 'lib/queries';
export function getEventMetrics(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
filters = {},
) {
const params = [website_id, start_at, end_at];
return rawQuery(
`
select
event_value x,
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} t,
count(*) y
from event
where website_id=$1
and created_at between $2 and $3
${getFilterQuery('event', filters, params)}
group by 1, 2
order by 2
`,
params,
);
}

View File

@ -0,0 +1,19 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getEvents(websites, start_at) {
return runQuery(
prisma.event.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}

View File

@ -0,0 +1,17 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
import { URL_LENGTH } from 'lib/constants';
export async function saveEvent(website_id, session_id, url, event_type, event_value) {
return runQuery(
prisma.event.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
event_type: event_type?.substr(0, 50),
event_value: event_value?.substr(0, 50),
},
}),
);
}

View File

@ -0,0 +1,26 @@
import { parseFilters, rawQuery } from 'lib/queries';
export function getPageviewMetrics(website_id, start_at, end_at, field, table, filters = {}) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table,
filters,
params,
);
return rawQuery(
`
select ${field} x, count(*) y
from ${table}
${joinSession}
where ${table}.website_id=$1
and ${table}.created_at between $2 and $3
${pageviewQuery}
${joinSession && sessionQuery}
${eventQuery}
group by 1
order by 2 desc
`,
params,
);
}

View File

@ -0,0 +1,34 @@
import { parseFilters, rawQuery, getDateQuery, getDateStringQuery } from 'lib/queries';
export function getPageviewStats(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
count = '*',
filters = {},
) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
return rawQuery(
`
select
${getDateStringQuery('g.t', unit)} as t,
g.y as y
from
(select ${getDateQuery('pageview.created_at', unit, timezone)} t,
count(${count}) y
from pageview
${joinSession}
where pageview.website_id=$1
and pageview.created_at between $2 and $3
${pageviewQuery}
${sessionQuery}
group by 1) g
order by 1
`,
params,
);
}

View File

@ -0,0 +1,19 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getPageviews(websites, start_at) {
return runQuery(
prisma.pageview.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}

View File

@ -0,0 +1,16 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
import { URL_LENGTH } from 'lib/constants';
export async function savePageView(website_id, session_id, url, referrer) {
return runQuery(
prisma.pageview.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
referrer: referrer?.substr(0, URL_LENGTH),
},
}),
);
}

View File

@ -0,0 +1,16 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function createSession(website_id, data) {
return runQuery(
prisma.session.create({
data: {
website_id,
...data,
},
select: {
session_id: true,
},
}),
);
}

View File

@ -0,0 +1,12 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getSessionByUuid(session_uuid) {
return runQuery(
prisma.session.findUnique({
where: {
session_uuid,
},
}),
);
}

View File

@ -0,0 +1,25 @@
import { parseFilters, rawQuery } from 'lib/queries';
export function getSessionMetrics(website_id, start_at, end_at, field, filters = {}) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
return rawQuery(
`
select ${field} x, count(*) y
from session as x
where x.session_id in (
select pageview.session_id
from pageview
${joinSession}
where pageview.website_id=$1
and pageview.created_at between $2 and $3
${pageviewQuery}
${sessionQuery}
)
group by 1
order by 2 desc
`,
params,
);
}

View File

@ -0,0 +1,19 @@
import { runQuery } from 'lib/queries';
import prisma from 'lib/db';
export async function getSessions(websites, start_at) {
return runQuery(
prisma.session.findMany({
where: {
website: {
website_id: {
in: websites,
},
},
created_at: {
gte: start_at,
},
},
}),
);
}

View File

@ -0,0 +1,17 @@
import { rawQuery } from 'lib/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,
);
}

View 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(),
};
}

View File

@ -0,0 +1,29 @@
import { parseFilters, rawQuery, getDateQuery, getTimestampInterval } from 'lib/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,
);
}

61
queries/index.js Normal file
View File

@ -0,0 +1,61 @@
import { createAccount } from './admin/account/createAccount';
import { deleteAccount } from './admin/account/deleteAccount';
import { getAccountById } from './admin/account/getAccountById';
import { getAccountByUsername } from './admin/account/getAccountByUsername';
import { getAccounts } from './admin/account/getAccounts';
import { updateAccount } from './admin/account/updateAccount';
import { createWebsite } from './admin/website/createWebsite';
import { deleteWebsite } from './admin/website/deleteWebsite';
import { getAllWebsites } from './admin/website/getAllWebsites';
import { getUserWebsites } from './admin/website/getUserWebsites';
import { getWebsiteById } from './admin/website/getWebsiteById';
import { getWebsiteByShareId } from './admin/website/getWebsiteByShareId';
import { getWebsiteByUuid } from './admin/website/getWebsiteByUuid';
import { resetWebsite } from './admin/website/resetWebsite';
import { updateWebsite } from './admin/website/updateWebsite';
import { getEventMetrics } from './analytics/event/getEventMetrics';
import { getEvents } from './analytics/event/getEvents';
import { saveEvent } from './analytics/event/saveEvent';
import { getPageviewMetrics } from './analytics/pageview/getPageviewMetrics';
import { getPageviews } from './analytics/pageview/getPageviews';
import { getPageviewStats } from './analytics/pageview/getPageviewStats';
import { savePageView } from './analytics/pageview/savePageView';
import { createSession } from './analytics/session/createSession';
import { getSessionByUuid } from './analytics/session/getSessionByUuid';
import { getSessionMetrics } from './analytics/session/getSessionMetrics';
import { getSessions } from './analytics/session/getSessions';
import { getActiveVisitors } from './analytics/stats/getActiveVisitors';
import { getRealtimeData } from './analytics/stats/getRealtimeData';
import { getWebsiteStats } from './analytics/stats/getWebsiteStats';
export {
createWebsite,
deleteWebsite,
getAllWebsites,
getUserWebsites,
getWebsiteById,
getWebsiteByShareId,
getWebsiteByUuid,
resetWebsite,
updateWebsite,
createAccount,
deleteAccount,
getAccountById,
getAccountByUsername,
getAccounts,
updateAccount,
getEventMetrics,
getEvents,
saveEvent,
getPageviewMetrics,
getPageviews,
getPageviewStats,
savePageView,
createSession,
getSessionByUuid,
getSessionMetrics,
getSessions,
getActiveVisitors,
getRealtimeData,
getWebsiteStats,
};