data endpoint for entity 90008 (aka. a website)
at svelte 1.6 kB view raw
1import { env } from '$env/dynamic/private'; 2import { pushMetrics } from 'prometheus-remote-write'; 3import { createFileCounter } from './counter'; 4 5const endpoint = env.PROMETHEUS_URL; 6 7export const pushMetric = async ( 8 metrics: Record<string, number>, 9 labels: Record<string, string> = {} 10) => { 11 if (endpoint === undefined) return; 12 try { 13 const result = await pushMetrics(metrics, { 14 url: endpoint, 15 labels: { 16 service: 'website', 17 ...labels 18 } 19 }); 20 if (result.status != 204) { 21 throw new Error(`failed to push metrics: ${result.status} ${result.errorMessage}`); 22 } 23 } catch (err) { 24 console.log(`failed to push metrics: ${err}`); 25 } 26}; 27 28export const sendAllMetrics = async () => { 29 try { 30 await pushMetric({ 31 gazesys_pet_bounce_total: bounceCount.get(), 32 gazesys_visit_fake_total: fakeVisitCount.get(), 33 gazesys_visit_real_total: legitVisitCount.get(), 34 gazesys_pet_distance_total: distanceTravelled.get() 35 }); 36 } catch (error) { 37 console.log(`failed to push metrics: ${error}`); 38 } 39}; 40 41export const bounceCount = await createFileCounter(`${env.WEBSITE_DATA_DIR}/bouncecount`); 42export const incrementBounceCount = bounceCount.increment; 43 44export const legitVisitCount = await createFileCounter(`${env.WEBSITE_DATA_DIR}/legitvisitcount`); 45export const incrementLegitVisitCount = legitVisitCount.increment; 46 47export const fakeVisitCount = await createFileCounter(`${env.WEBSITE_DATA_DIR}/fakevisitcount`); 48export const incrementFakeVisitCount = fakeVisitCount.increment; 49 50export const distanceTravelled = await createFileCounter( 51 `${env.WEBSITE_DATA_DIR}/distancetravelled` 52);