Skip to content

Commit

Permalink
la til umami range parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
eilifjohansen committed Sep 13, 2024
1 parent 882d02f commit 19d54b0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ app.get('/umami/api/*', (req, res) => {
app.get('/umami/v2/api/*', (req, res) => {
let apiUrl = "https://umami.intern.nav.no/api";
req.url = req.url.replace(/\/umami\/v2\/api/, '');

// Check if the URL contains the parameter endsAt=yesterday
if (req.url.includes('endAt=yesterday')) {
// Get the current date
let yesterday = new Date();
// Set the date to yesterday
yesterday.setDate(yesterday.getDate() - 1);
// Set the time to midnight
yesterday.setHours(0, 0, 0, 0);
// Get the timestamp in milliseconds
let yesterdayMidnight = yesterday.getTime();
// Replace 'yesterday' with the timestamp
req.url = req.url.replace('endAt=yesterday', `endAt=${yesterdayMidnight}`);
}

if (req.url.includes('endAt=now')) {
// Get the current date and time
let now = new Date();
// Get the timestamp in milliseconds
let nowTimestamp = now.getTime();
// Replace 'now' with the timestamp
req.url = req.url.replace('endAt=now', `endAt=${nowTimestamp}`);
}

if (req.url.includes('range=day')) {
// Get the current date and time
let now = new Date();
// Get the timestamp in milliseconds
let nowTimestamp = now.getTime();
// Calculate the timestamp for 360 days ago
let startAtTimestamp = new Date(now.setDate(now.getDate() - 196)).getTime();
// Append the parameters to the URL
req.url += `&startAt=${startAtTimestamp}&endAt=${nowTimestamp}&unit=day`;
console.log(req.url);
}

if (req.url.match(/users/)) {
res.end("APIet har blitt blokkert av Team ResearchOps i NAV, ta kontakt med oss for hjelp.");
} else {
Expand Down

0 comments on commit 19d54b0

Please sign in to comment.