From 2640c209c312882a0e8e07f3bbb018a8c188b15d Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Tue, 13 Apr 2021 13:42:59 +0000 Subject: [PATCH 1/4] Add state to US --- site/src/analytics-map.ts | 17 ++++++++++++++--- worker/src/handlers/post.ts | 5 ++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/site/src/analytics-map.ts b/site/src/analytics-map.ts index 291bacac..2e1da1b3 100644 --- a/site/src/analytics-map.ts +++ b/site/src/analytics-map.ts @@ -27,9 +27,20 @@ export class AnalyticsMap extends UpdatingElement { if (this.showMap) { const countries: Record> = {}; for (const country of Object.keys(this.lastDataEntry?.countries || {})) { - countries[country] = { - installations: this.lastDataEntry?.countries[country] || 0, - }; + if (country.startsWith("US_")) { + const value = this.lastDataEntry?.countries[country] || 0; + if (!countries["US"]) { + countries["US"] = { installations: value }; + } else { + countries["US"].installations = + countries["US"].installations + value; + } + console.log(country); + } else { + countries[country] = { + installations: this.lastDataEntry?.countries[country] || 0, + }; + } } if (this._svgMap) { this._svgMap.applyData({ diff --git a/worker/src/handlers/post.ts b/worker/src/handlers/post.ts index ee6afbae..9a3c91e3 100644 --- a/worker/src/handlers/post.ts +++ b/worker/src/handlers/post.ts @@ -18,7 +18,10 @@ export async function handlePost(request: Request): Promise { } const storageKey = `${KV_PREFIX_UUID}:${payload.uuid}`; - const country = request.headers.get("cf-ipcountry"); + let country = request.cf.country; + if (country === "US" && request.cf.regionCode) { + country = `${country}_${request.cf.regionCode}`; + } let sanitizedPayload: SanitizedPayload; From f0dda61d0aee9135aa8dbd0f7e37cb3bd0f5fb66 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Tue, 13 Apr 2021 14:51:39 +0000 Subject: [PATCH 2/4] remove log --- site/src/analytics-map.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/site/src/analytics-map.ts b/site/src/analytics-map.ts index 2e1da1b3..59f84df1 100644 --- a/site/src/analytics-map.ts +++ b/site/src/analytics-map.ts @@ -35,7 +35,6 @@ export class AnalyticsMap extends UpdatingElement { countries["US"].installations = countries["US"].installations + value; } - console.log(country); } else { countries[country] = { installations: this.lastDataEntry?.countries[country] || 0, From 26331479faa432b66e057bf5ce289faa3bcde9b3 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Thu, 15 Apr 2021 00:23:51 +0000 Subject: [PATCH 3/4] Split country and region --- site/src/analytics-map.ts | 16 +++------------- worker/src/handlers/post.ts | 4 ++++ worker/src/utils/validate.ts | 3 ++- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/site/src/analytics-map.ts b/site/src/analytics-map.ts index 59f84df1..291bacac 100644 --- a/site/src/analytics-map.ts +++ b/site/src/analytics-map.ts @@ -27,19 +27,9 @@ export class AnalyticsMap extends UpdatingElement { if (this.showMap) { const countries: Record> = {}; for (const country of Object.keys(this.lastDataEntry?.countries || {})) { - if (country.startsWith("US_")) { - const value = this.lastDataEntry?.countries[country] || 0; - if (!countries["US"]) { - countries["US"] = { installations: value }; - } else { - countries["US"].installations = - countries["US"].installations + value; - } - } else { - countries[country] = { - installations: this.lastDataEntry?.countries[country] || 0, - }; - } + countries[country] = { + installations: this.lastDataEntry?.countries[country] || 0, + }; } if (this._svgMap) { this._svgMap.applyData({ diff --git a/worker/src/handlers/post.ts b/worker/src/handlers/post.ts index 35b12e65..d2d5db68 100644 --- a/worker/src/handlers/post.ts +++ b/worker/src/handlers/post.ts @@ -13,6 +13,7 @@ import { assertIncomingPayload } from "../utils/validate"; const updateThreshold = daysToMs(30); const expirationTtl = daysToMs(60); +const withRegion = new Set(["US"]); export async function handlePostWrapper( request: Request, @@ -30,6 +31,9 @@ async function handlePost(request: Request, sentry: Toucan): Promise { sentry.addBreadcrumb({ message: "Prosess started" }); const incomingPayload = await request.json(); incomingPayload.country = request.cf.country; + if (incomingPayload.country in withRegion) { + incomingPayload.region = request.cf.regionCode; + } sentry.setUser({ id: incomingPayload.uuid }); sentry.setExtras(incomingPayload); diff --git a/worker/src/utils/validate.ts b/worker/src/utils/validate.ts index e71b948d..93418df1 100644 --- a/worker/src/utils/validate.ts +++ b/worker/src/utils/validate.ts @@ -36,7 +36,8 @@ export const IncomingPayload = object({ ) ), automation_count: optional(number()), - country: size(string(), 2, 5), + country: size(string(), 2, 2), + region: optional(size(string(), 2, 2)), custom_integrations: optional( array(object({ domain: string(), version: optional(string()) })) ), From d477d76d6d7bb9448ffd09fd28b01d7824866b03 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Thu, 15 Apr 2021 00:28:14 +0000 Subject: [PATCH 4/4] Update metadata --- worker/src/data.ts | 4 ++++ worker/src/utils/validate.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/worker/src/data.ts b/worker/src/data.ts index d4bf58e9..5af3b148 100644 --- a/worker/src/data.ts +++ b/worker/src/data.ts @@ -10,6 +10,7 @@ export enum UuidMetadataKey { EXTRA = "e", INSTALLATION_TYPE = "i", UPDATED = "u", + REGION = "r", VERSION = "v", } @@ -32,6 +33,7 @@ export interface UuidMetadata { [UuidMetadataKey.VERSION]: string; [UuidMetadataKey.INSTALLATION_TYPE]: ShortInstallationType; [UuidMetadataKey.COUNTRY]?: string; + [UuidMetadataKey.REGION]?: string; [UuidMetadataKey.EXTRA]: MetadataExtra[]; } @@ -65,6 +67,7 @@ export interface IncomingPayload { addons?: { slug: string }[]; automation_count?: number; country?: string; + region?: string; custom_integrations?: { domain: string; version: string | null }[]; installation_type: string; integration_count?: number; @@ -140,6 +143,7 @@ export const generateUuidMetadata = ( [UuidMetadataKey.INSTALLATION_TYPE]: InstallationTypes[payload.installation_type], [UuidMetadataKey.COUNTRY]: payload.country, + [UuidMetadataKey.REGION]: payload.region, [UuidMetadataKey.VERSION]: payload.version, [UuidMetadataKey.EXTRA]: extra, }; diff --git a/worker/src/utils/validate.ts b/worker/src/utils/validate.ts index 93418df1..918563e2 100644 --- a/worker/src/utils/validate.ts +++ b/worker/src/utils/validate.ts @@ -36,7 +36,7 @@ export const IncomingPayload = object({ ) ), automation_count: optional(number()), - country: size(string(), 2, 2), + country: optional(size(string(), 2, 2)), region: optional(size(string(), 2, 2)), custom_integrations: optional( array(object({ domain: string(), version: optional(string()) }))