From ea591b375fb945df62e7893ee93c5cf1c4153f33 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Fri, 2 Apr 2021 12:24:04 +0000 Subject: [PATCH 1/3] Store the origin country-code --- worker/src/data.ts | 1 + worker/src/handlers/get.ts | 8 ++++---- worker/src/handlers/post.ts | 9 +++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/worker/src/data.ts b/worker/src/data.ts index 58bc6b8e..67b3741c 100644 --- a/worker/src/data.ts +++ b/worker/src/data.ts @@ -1,5 +1,6 @@ export interface SanitizedPayload { version: string; + country?: string; installation_type: string; integrations?: string[]; addons?: { slug: string }[]; diff --git a/worker/src/handlers/get.ts b/worker/src/handlers/get.ts index 8ea55737..49dd80d5 100644 --- a/worker/src/handlers/get.ts +++ b/worker/src/handlers/get.ts @@ -1,13 +1,13 @@ // Serve JSON to https://analytics.home-assistant.io export async function handleGet(request: Request): Promise { - const core_analytics = await KV.get('core_analytics') + const core_analytics = await KV.get("core_analytics"); return new Response(core_analytics, { status: 200, headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', + "Content-Type": "application/json", + "Access-Control-Allow-Origin": "*", }, - }) + }); } diff --git a/worker/src/handlers/post.ts b/worker/src/handlers/post.ts index 29750812..d5c6a307 100644 --- a/worker/src/handlers/post.ts +++ b/worker/src/handlers/post.ts @@ -17,11 +17,12 @@ export async function handlePost(request: Request): Promise { } const storageKey = `huuid:${payload.huuid}`; + const country = request.headers.get("cf-ipcountry"); let sanitizedPayload: SanitizedPayload; try { - sanitizedPayload = sanitizePayload(payload); + sanitizedPayload = sanitizePayload(payload, country); } catch (err) { return new Response(err.message, { status: 400 }); } @@ -63,7 +64,10 @@ async function storePayload( ); } -const sanitizePayload = (payload: any): SanitizedPayload => { +const sanitizePayload = ( + payload: any, + country: string | null +): SanitizedPayload => { if (!payload.installation_type || !payload.version) { throw new Error("Missing required keys in the payload"); } @@ -91,5 +95,6 @@ const sanitizePayload = (payload: any): SanitizedPayload => { } } + payload.country = country; return payload; }; From e563a5cf7d8e5a469d6358aa1121e87d39110fc8 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Sat, 3 Apr 2021 11:08:05 +0000 Subject: [PATCH 2/3] count countries --- site/src/data.ts | 1 + worker/src/handlers/schedule.ts | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/site/src/data.ts b/site/src/data.ts index e2156cea..6a17ea47 100644 --- a/site/src/data.ts +++ b/site/src/data.ts @@ -12,6 +12,7 @@ export type AnalyticsData = Record; export interface Analytics { active_installations: number; + country: Record; addons: Addons; avg_addons: number; avg_automations: number; diff --git a/worker/src/handlers/schedule.ts b/worker/src/handlers/schedule.ts index 2ea9dfd4..5b25fb56 100644 --- a/worker/src/handlers/schedule.ts +++ b/worker/src/handlers/schedule.ts @@ -60,6 +60,7 @@ const generateCurrentDataset = ( const installation_types = { os: 0, container: 0, core: 0, supervised: 0 }; const integrations: Record = {}; const addons: Record = {}; + const country: Record = {}; const versions: Record = {}; const count_addons: number[] = []; const count_automations: number[] = []; @@ -74,6 +75,14 @@ const generateCurrentDataset = ( versions[huuid.version]++; } + if (huuid.country) { + if (!country[huuid.country]) { + country[huuid.country] = 1; + } else { + country[huuid.country]++; + } + } + if (huuid.addon_count) { count_addons.push(huuid.addon_count); } @@ -119,6 +128,7 @@ const generateCurrentDataset = ( return { last_updated, + country, installation_types, active_installations: installation_types.container + From 29a1b106642600ecb25dd21192760cde70e8b2ce Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Sat, 3 Apr 2021 15:43:12 +0000 Subject: [PATCH 3/3] rename --- site/src/data.ts | 2 +- worker/src/handlers/schedule.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/site/src/data.ts b/site/src/data.ts index 6a17ea47..d66449b3 100644 --- a/site/src/data.ts +++ b/site/src/data.ts @@ -12,7 +12,7 @@ export type AnalyticsData = Record; export interface Analytics { active_installations: number; - country: Record; + countries: Record; addons: Addons; avg_addons: number; avg_automations: number; diff --git a/worker/src/handlers/schedule.ts b/worker/src/handlers/schedule.ts index 5b25fb56..0df58b2f 100644 --- a/worker/src/handlers/schedule.ts +++ b/worker/src/handlers/schedule.ts @@ -60,7 +60,7 @@ const generateCurrentDataset = ( const installation_types = { os: 0, container: 0, core: 0, supervised: 0 }; const integrations: Record = {}; const addons: Record = {}; - const country: Record = {}; + const countries: Record = {}; const versions: Record = {}; const count_addons: number[] = []; const count_automations: number[] = []; @@ -76,10 +76,10 @@ const generateCurrentDataset = ( } if (huuid.country) { - if (!country[huuid.country]) { - country[huuid.country] = 1; + if (!countries[huuid.country]) { + countries[huuid.country] = 1; } else { - country[huuid.country]++; + countries[huuid.country]++; } } @@ -128,7 +128,7 @@ const generateCurrentDataset = ( return { last_updated, - country, + countries, installation_types, active_installations: installation_types.container +