Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type AnalyticsData = Record<string, Analytics>;

export interface Analytics {
active_installations: number;
countries: Record<string, number>;
addons: Addons;
avg_addons: number;
avg_automations: number;
Expand Down
1 change: 1 addition & 0 deletions worker/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface SanitizedPayload {
version: string;
country?: string;
installation_type: string;
integrations?: string[];
addons?: { slug: string }[];
Expand Down
8 changes: 4 additions & 4 deletions worker/src/handlers/get.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Serve JSON to https://analytics.home-assistant.io

export async function handleGet(request: Request): Promise<Response> {
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": "*",
},
})
});
}
9 changes: 7 additions & 2 deletions worker/src/handlers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export async function handlePost(request: Request): Promise<Response> {
}

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 });
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -91,5 +95,6 @@ const sanitizePayload = (payload: any): SanitizedPayload => {
}
}

payload.country = country;
return payload;
};
10 changes: 10 additions & 0 deletions worker/src/handlers/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const generateCurrentDataset = (
const installation_types = { os: 0, container: 0, core: 0, supervised: 0 };
const integrations: Record<string, number> = {};
const addons: Record<string, number> = {};
const countries: Record<string, number> = {};
const versions: Record<string, number> = {};
const count_addons: number[] = [];
const count_automations: number[] = [];
Expand All @@ -74,6 +75,14 @@ const generateCurrentDataset = (
versions[huuid.version]++;
}

if (huuid.country) {
if (!countries[huuid.country]) {
countries[huuid.country] = 1;
} else {
countries[huuid.country]++;
}
}

if (huuid.addon_count) {
count_addons.push(huuid.addon_count);
}
Expand Down Expand Up @@ -119,6 +128,7 @@ const generateCurrentDataset = (

return {
last_updated,
countries,
installation_types,
active_installations:
installation_types.container +
Expand Down