Skip to content

Commit

Permalink
fix: pubilc pages and team websites
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru committed Jan 28, 2024
1 parent 630442a commit 95d636d
Show file tree
Hide file tree
Showing 4 changed files with 646 additions and 610 deletions.
37 changes: 18 additions & 19 deletions apps/api/src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { client } from "./clickhouse";
import { db } from "./drizzle";

export const hitsQuery = (startDate: string, endDate: string, websiteId: string) =>
`select id, sessionId, visitorId, JSONExtract(properties, 'city', 'String') as city, JSONExtract(properties, 'country', 'String') as country,JSONExtract(properties, 'browser', 'String') as browser,JSONExtract(properties, 'language', 'String') as locale,JSONExtract(properties, 'referrerPath', 'String') as referrerPath, JSONExtract(properties, 'currentPath', 'String') as currentPath, JSONExtract(properties, 'referrerDomain', 'String') as referrerDomain, JSONExtract(properties, 'queryParams', 'String') as queryParams, JSONExtract(properties, 'device', 'String') as device, JSONExtract(properties, 'duration', 'Float32') as duration, JSONExtract(properties, 'os', 'String') as os, event, timestamp from loglib.event WHERE ${
startDate && `timestamp >= '${startDate}' AND`
`select id, sessionId, visitorId, JSONExtract(properties, 'city', 'String') as city, JSONExtract(properties, 'country', 'String') as country,JSONExtract(properties, 'browser', 'String') as browser,JSONExtract(properties, 'language', 'String') as locale,JSONExtract(properties, 'referrerPath', 'String') as referrerPath, JSONExtract(properties, 'currentPath', 'String') as currentPath, JSONExtract(properties, 'referrerDomain', 'String') as referrerDomain, JSONExtract(properties, 'queryParams', 'String') as queryParams, JSONExtract(properties, 'device', 'String') as device, JSONExtract(properties, 'duration', 'Float32') as duration, JSONExtract(properties, 'os', 'String') as os, event, timestamp from loglib.event WHERE ${startDate && `timestamp >= '${startDate}' AND`
} timestamp <= '${endDate}' AND websiteId = '${websiteId}' AND event = 'hits'`;

export const customEventsQuery = (startDate: string, endDate: string, websiteId: string) =>
Expand Down Expand Up @@ -177,11 +176,10 @@ async function getHitsData(startDateObj: Date, endDateObj: Date, websiteId: stri
.select()
.from(event)
.where(
sql`${event.websiteId} = ${websiteId} and event = 'hits' and ${
event.timestamp
} >= ${new Date(startDateObj.getTime())} and ${event.timestamp} <= ${new Date(
endDateObj,
).getTime()}`,
sql`${event.websiteId} = ${websiteId} and event = 'hits' and ${event.timestamp
} >= ${new Date(startDateObj.getTime())} and ${event.timestamp} <= ${new Date(
endDateObj,
).getTime()}`,
)
.then((res) =>
res.map((event) => {
Expand All @@ -195,7 +193,7 @@ async function getHitsData(startDateObj: Date, endDateObj: Date, websiteId: stri
);
},
clickhouse: async () => {
return await client
const res = await client
.query({
query: hitsQuery(
convertToUTC(startDateObj),
Expand All @@ -205,6 +203,8 @@ async function getHitsData(startDateObj: Date, endDateObj: Date, websiteId: stri
format: "JSONEachRow",
})
.then(async (res) => (await res.json()) as LoglibEvent[]);
console.log({ res: res.length, startDateObj, endDateObj })
return res
},
};
}
Expand All @@ -217,11 +217,10 @@ function getSiteVitals(websiteId: string, startDate: Date, endDate: Date) {
.select()
.from(event)
.where(
sql`${event.websiteId} = ${websiteId} and event = 'vitals' and ${
event.timestamp
} >= ${new Date(startDate.getTime())} and ${event.timestamp} <= ${new Date(
endDate,
).getTime()}`,
sql`${event.websiteId} = ${websiteId} and event = 'vitals' and ${event.timestamp
} >= ${new Date(startDate.getTime())} and ${event.timestamp} <= ${new Date(
endDate,
).getTime()}`,
)
.then((res) =>
res.map((r) => ({
Expand Down Expand Up @@ -250,11 +249,10 @@ async function getCustomEventData(startDateObj: Date, endDateObj: Date, websiteI
.select()
.from(event)
.where(
sql`${event.websiteId} = ${websiteId} and event != 'hits' and ${
event.timestamp
} >= ${new Date(startDateObj.getTime())} and ${event.timestamp} <= ${new Date(
endDateObj,
).getTime()}`,
sql`${event.websiteId} = ${websiteId} and event != 'hits' and ${event.timestamp
} >= ${new Date(startDateObj.getTime())} and ${event.timestamp} <= ${new Date(
endDateObj,
).getTime()}`,
)
.then((res) =>
res.map((event) => {
Expand Down Expand Up @@ -321,7 +319,8 @@ export function loglibDb(db: "sqlite" | "clickhouse") {
websiteId: string,
) {
const query1 = await getHitsData(startDateObj, endDateObj, websiteId);
const query2 = await getHitsData(endDateObj, pastEndDateObj, websiteId);
const query2 = await getHitsData(pastEndDateObj, startDateObj, websiteId);

return await Promise.all([query1[db](), query2[db]()]);
},
async getCustomEvents(startDateObj: Date, endDateObj: Date, websiteId: string) {
Expand Down
12 changes: 7 additions & 5 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ app.get("/", async (c) => {
3,
4
);

const tack = performance.now();
console.log(tack - tick, "ms taken to query");
const filters = JSON.parse(queries.data.filter) as Filter<LoglibEvent>[];
Expand All @@ -168,6 +169,7 @@ app.get("/", async (c) => {
.where(f.key, f.operator, f.value)
.execute();
});

const insightData = getInsight(
events as LoglibEvent[],
lastEvents as LoglibEvent[]
Expand Down Expand Up @@ -362,11 +364,11 @@ app.get("/v1/insight", async (c) => {
const today = new Date();
const startDateObj = new Date(
startDate ??
new Date(
today.getFullYear() - 1,
today.getMonth(),
today.getDate()
).toISOString()
new Date(
today.getFullYear() - 1,
today.getMonth(),
today.getDate()
).toISOString()
);
const endDateObj = new Date(endDate ?? today);
const duration = endDateObj.getTime() - startDateObj.getTime();
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/routes/insight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const transformData = (events: LoglibEvent[], pastEvents: LoglibEvent[]) => {
const uniqueValues = new Set();
let bounces = 0;
let duration = 0;
for (let i = 0; i < events.length; i++) {
const event = events[i];
for (const event of events) {
uniqueValues.add(event.visitorId);
event.duration < 10 && bounces++;
duration += event.duration;
Expand All @@ -31,8 +30,7 @@ const transformData = (events: LoglibEvent[], pastEvents: LoglibEvent[]) => {
const pastUniqueValues = new Set();
let pastBounces = 0;
let pastDuration = 0;
for (let i = 0; i < pastEvents.length; i++) {
const event = pastEvents[i];
for (const event of pastEvents) {
pastUniqueValues.add(event.visitorId);
event.duration < 10 && pastBounces++;
pastDuration += event.duration;
Expand Down
Loading

0 comments on commit 95d636d

Please sign in to comment.