data endpoint for entity 90008 (aka. a website)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: send metric for when pet is bounced [skip ci]

dusk 3acba11c 0aaff9d4

+53 -2
bun.lockb

This is a binary file and will not be displayed.

+2
package.json
··· 47 47 "@types/node-schedule": "^2.1.7", 48 48 "nanoid": "^5.1.0", 49 49 "node-schedule": "^2.1.1", 50 + "prometheus-remote-write": "^0.5.1", 50 51 "robots-parser": "^3.0.1", 51 52 "steamgriddb": "^2.2.0" 52 53 }, 53 54 "trustedDependencies": [ 54 55 "@sveltejs/kit", 55 56 "esbuild", 57 + "protobufjs", 56 58 "sharp", 57 59 "svelte-preprocess" 58 60 ]
+10 -1
src/components/pet.svelte
··· 69 69 let groundFriction = 0.9; // Ground friction 70 70 let bounciness = 0.8; // How much energy is preserved on bounce 71 71 72 + const sendBounceMetrics = () => { 73 + fetch('/pet/bounce'); 74 + console.log('bouncy'); 75 + }; 76 + 72 77 const move = () => { 73 78 if (dragged) { 74 79 return; ··· 95 100 if (position.x < 0) { 96 101 position.x = 0; 97 102 velocityX = -velocityX * bounciness; 103 + sendBounceMetrics(); 98 104 } else if (position.x > viewportWidth) { 99 105 position.x = viewportWidth; 100 106 velocityX = -velocityX * bounciness; 107 + sendBounceMetrics(); 101 108 } 102 109 103 110 // Bounce off bottom (floor) ··· 105 112 position.y = 0; 106 113 velocityY = -velocityY * bounciness; 107 114 // Only bounce if velocity is significant 108 - if (Math.abs(velocityY) < 5) { 115 + if (Math.abs(velocityY) < 80) { 109 116 velocityY = 0; 110 117 position.y = 0; 118 + } else { 119 + sendBounceMetrics(); 111 120 } 112 121 } 113 122
+28
src/lib/metrics.ts
··· 1 + import { env } from '$env/dynamic/private'; 2 + import { existsSync, readFileSync, writeFileSync } from 'fs'; 3 + import { pushMetrics } from 'prometheus-remote-write'; 4 + import { get, writable } from 'svelte/store'; 5 + 6 + export const pushMetric = async (metrics: Record<string, number>) => { 7 + await pushMetrics(metrics, { 8 + url: env.PROMETHEUS_URL, 9 + labels: { 10 + service: 'website' 11 + } 12 + }); 13 + }; 14 + 15 + const bounceCountFile = `${env.WEBSITE_DATA_DIR}/bouncecount`; 16 + const bounceCount = writable( 17 + parseInt(existsSync(bounceCountFile) ? readFileSync(bounceCountFile).toString() : '0') 18 + ); 19 + 20 + export const incrementBounceCount = () => { 21 + let currentBounceCount = get(bounceCount); 22 + // increment current and write to the store 23 + currentBounceCount += 1; 24 + bounceCount.set(currentBounceCount); 25 + // write the bounce count to a file so we can load it later again 26 + writeFileSync(bounceCountFile, currentBounceCount.toString()); 27 + return currentBounceCount; 28 + };
+1 -1
src/lib/visits.ts
··· 86 86 return visitors; 87 87 }; 88 88 89 - const isBot = (request: Request) => { 89 + export const isBot = (request: Request) => { 90 90 const ua = request.headers.get('user-agent'); 91 91 return ua 92 92 ? ua.toLowerCase().match(/(bot|crawl|spider|walk|fetch|scrap|proxy|image)/) !== null
+12
src/routes/pet/bounce/+server.ts
··· 1 + import { incrementBounceCount, pushMetric } from '$lib/metrics'; 2 + import { isBot } from '$lib/visits'; 3 + 4 + export const GET = async ({ request }) => { 5 + if (isBot(request)) return new Response(); 6 + try { 7 + await pushMetric({ gazesys_pet_bounce_total: incrementBounceCount() }); 8 + } catch (error) { 9 + console.log(`error while pushing bounce metric: ${error}`); 10 + } 11 + return new Response(); 12 + };