All files / src/util domainGeo.ts

45.45% Statements 5/11
0% Branches 0/8
33.33% Functions 1/3
45.45% Lines 5/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112              1x                                                                                                                 1x                                                               6x   6x                 6x        
import { WebSearchDomainStat } from "@/services/api/routers/StatsRouter";
 
/**
 * Country-code TLD -> region name, for the common ccTLDs likely to show up
 * in web search results. Not exhaustive — anything unmapped falls back to
 * "Other".
 */
const CCTLD_REGION: Record<string, string> = {
  uk: "United Kingdom",
  de: "Germany",
  fr: "France",
  jp: "Japan",
  cn: "China",
  in: "India",
  au: "Australia",
  ca: "Canada",
  us: "United States",
  br: "Brazil",
  ru: "Russia",
  kr: "South Korea",
  it: "Italy",
  es: "Spain",
  nl: "Netherlands",
  se: "Sweden",
  ch: "Switzerland",
  sg: "Singapore",
  ie: "Ireland",
  nz: "New Zealand",
  za: "South Africa",
  mx: "Mexico",
  no: "Norway",
  dk: "Denmark",
  fi: "Finland",
  pl: "Poland",
  pt: "Portugal",
  be: "Belgium",
  at: "Austria",
  il: "Israel",
  ae: "United Arab Emirates",
  hk: "Hong Kong",
  tw: "Taiwan",
  id: "Indonesia",
  th: "Thailand",
  vn: "Vietnam",
  ph: "Philippines",
  my: "Malaysia",
  tr: "Turkey",
  gr: "Greece",
  cz: "Czechia",
  ro: "Romania",
  hu: "Hungary",
  ua: "Ukraine",
  ar: "Argentina",
  cl: "Chile",
  co: "Colombia",
  eg: "Egypt",
  ng: "Nigeria",
  ke: "Kenya",
  sa: "Saudi Arabia",
  pk: "Pakistan",
  bd: "Bangladesh",
};
 
/** Generic/international TLDs that carry no reliable country signal. */
const GENERIC_TLDS = new Set([
  "com",
  "org",
  "net",
  "io",
  "edu",
  "gov",
  "info",
  "biz",
  "dev",
  "app",
  "ai",
  "co",
]);
 
export interface RegionStat {
  region: string;
  visitCount: number;
}
 
/**
 * Buckets web-search domains by an inferred country/region, using the
 * domain's TLD as a heuristic (e.g. `.co.uk` -> United Kingdom).
 *
 * This is NOT verified hosting or operator geolocation — a `.com` domain
 * can be operated and hosted anywhere, so those (and other generic TLDs)
 * are grouped as "International / Generic" rather than guessed at. Treat
 * this as a rough signal of source diversity, not a precise map.
 */
export function aggregateDomainsByRegion(
  domains: WebSearchDomainStat[],
): RegionStat[] {
  const byRegion = new Map<string, number>();
 
  for (const d of domains) {
    const parts = d.domain.toLowerCase().split(".");
    const tld = parts[parts.length - 1] ?? "";
    const region = GENERIC_TLDS.has(tld)
      ? "International / Generic"
      : (CCTLD_REGION[tld] ?? "Other");
    byRegion.set(region, (byRegion.get(region) ?? 0) + d.visit_count);
  }
 
  return Array.from(byRegion.entries())
    .map(([region, visitCount]) => ({ region, visitCount }))
    .sort((a, b) => b.visitCount - a.visitCount);
}