Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-party committed Jun 7, 2024
1 parent bba4887 commit fb2c52b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "umami-views",
"version": "1.0.4",
"version": "1.1.0",
"author": "Astrid <git@astrid.email>",
"license": "MIT",
"type": "module",
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ app.get('/svg', async (req, res) => {

const views = await getViews(websiteAPI, websiteId, token);
if (!views) return res.status(500).send('Failed to get views');
const { pageviews } = views;

const dom = new JSDOM(`<!DOCTYPE html><body></body>`);
const body = dom.window.document.querySelector('body')!;
Expand Down Expand Up @@ -71,10 +70,10 @@ app.get('/svg', async (req, res) => {
{
label: website.domain,
logo: '',
data: pageviews
.map((pageview) => ({
x: new Date(pageview.x),
y: pageview.y,
data: views
.map((view) => ({
x: new Date(view.x),
y: view.y,
}))
.sort((a, b) => b.x.getTime() - a.x.getTime()),
},
Expand Down
29 changes: 17 additions & 12 deletions src/lib/umami/getViews.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import ky from 'ky';

import getNearestMidnight from '../../utils/getNearestMidnight';

export interface XY {
x: string;
y: number;
Expand All @@ -18,15 +16,14 @@ export default async function getViews(
websiteAPI: string,
websiteId: string,
token: string,
): Promise<PageViews | null> {
const today = getNearestMidnight();

const monthAgo = today - 30 * oneDay;
): Promise<Array<{ x: Date; y: number }> | null> {
const now = 1717714800 * 1_000 || Date.now();
const monthAgo = now - 30 * oneDay;

const response = await ky.get(
`${websiteAPI}api/websites/${websiteId}/pageviews?${new URLSearchParams({
startAt: String(monthAgo),
endAt: String(today - oneDay),
endAt: String(now),
unit: 'day',
timezone: 'Europe/Amsterdam',
})}`,
Expand All @@ -41,11 +38,19 @@ export default async function getViews(
const data = (await response.json()) as PageViews;

if (
typeof data === 'object' &&
typeof data.pageviews === 'object' &&
typeof data.sessions === 'object'
typeof data !== 'object' ||
typeof data.pageviews !== 'object' ||
typeof data.sessions !== 'object'
)
return data;
return null;

const pageviews = data.pageviews
.map((view) => ({
x: new Date(view.x),
y: view.y,
}))
.sort((a, b) => b.x.getTime() - a.x.getTime());

return null;
// ignore the last day
return pageviews.slice(1);
}
20 changes: 0 additions & 20 deletions src/utils/getNearestMidnight.ts

This file was deleted.

0 comments on commit fb2c52b

Please sign in to comment.