From bc9d855997db3a2e86c4922087c50f6527b66734 Mon Sep 17 00:00:00 2001 From: kalarina Date: Fri, 6 Nov 2026 16:11:58 +0200 Subject: [PATCH] Name other sites in Company dominance The share outside the known companies is now split by site names taken from the domains of recent queries (cdn.example.co.uk shows as Example). Domains are read only from the in-memory log; the saved statistics still keep numbers only. --- .../skysparkle/sparkledns/ActivityScreen.kt | 41 +++++++++++++------ .../cc/skysparkle/sparkledns/Companies.kt | 19 +++++++++ app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-lt/strings.xml | 2 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 8 files changed, 53 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/cc/skysparkle/sparkledns/ActivityScreen.kt b/app/src/main/java/cc/skysparkle/sparkledns/ActivityScreen.kt index 063227a..7164d0d 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/ActivityScreen.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/ActivityScreen.kt @@ -238,7 +238,7 @@ fun ActivityScreen(bottomPadding: Dp, modifier: Modifier = Modifier) { ) { ActivityHeader(period) { period = it } ActivityStats(summary, Modifier.padding(top = 20.dp)) - CompanyCard(summary, Modifier.padding(top = 20.dp)) + CompanyCard(summary, entries, Modifier.padding(top = 20.dp)) } LazyColumn( modifier = Modifier @@ -255,7 +255,7 @@ fun ActivityScreen(bottomPadding: Dp, modifier: Modifier = Modifier) { ) { item(key = "header") { ActivityHeader(period) { period = it } } item(key = "stats") { ActivityStats(summary, Modifier.padding(top = 20.dp)) } - item(key = "companies") { CompanyCard(summary, Modifier.padding(top = 20.dp)) } + item(key = "companies") { CompanyCard(summary, entries, Modifier.padding(top = 20.dp)) } log() } } @@ -366,9 +366,9 @@ private fun LogSearch( private const val MIN_COMPANY_REQUESTS = 50L @Composable -private fun CompanyCard(summary: StatsSummary, modifier: Modifier = Modifier) { +private fun CompanyCard(summary: StatsSummary, recent: List, modifier: Modifier = Modifier) { val other = stringResource(R.string.companies_other) - val slices = remember(summary, other) { companySlices(summary, other) } + val slices = remember(summary, recent, other) { companySlices(summary, recent, other) } Column( modifier = modifier .fillMaxWidth() @@ -430,17 +430,32 @@ private fun percentLabel(share: Float): String { return if (percent < 1f) "<1%" else "${Math.round(percent)}%" } -// The four companies with most requests, then everything else; empty slices are left out -private fun companySlices(summary: StatsSummary, otherLabel: String): List { +// Shares of the known companies over the period. The statistics keep no domains, so the share +// left over is split by the names of the recent queries to other domains, from the log in memory. +// Then the four largest slices and everything else; empty slices are left out. +private fun companySlices(summary: StatsSummary, recent: List, otherLabel: String): List { if (summary.total == 0L) return emptyList() - val top = Company.entries - .map { it to summary.companies[it.ordinal] } - .filter { it.second > 0 } - .sortedByDescending { it.second } - .take(SliceColors.size) - val slices = top.mapIndexed { index, (company, count) -> - Slice(company.label, count.toFloat() / summary.total, SliceColors[index]) + val shares = HashMap() + for (company in Company.entries) { + val count = summary.companies[company.ordinal] + if (count > 0) shares[company.label] = count.toFloat() / summary.total } + val unknownShare = (1f - shares.values.sum()).coerceAtLeast(0f) + val names = HashMap() + var unknownCount = 0 + for (entry in recent) { + if (Companies.of(entry.domain) != null) continue + unknownCount++ + Companies.nameOf(entry.domain)?.let { names.merge(it, 1, Int::plus) } + } + // Queries without a name (local names, reverse lookups) stay in the rest + if (unknownCount > 0) { + for ((name, count) in names) shares.merge(name, unknownShare * count / unknownCount, Float::plus) + } + val slices = shares.entries + .sortedByDescending { it.value } + .take(SliceColors.size) + .mapIndexed { index, (label, share) -> Slice(label, share, SliceColors[index]) } val rest = (1f - slices.sumOf { it.share.toDouble() }.toFloat()).coerceAtLeast(0f) return if (rest >= 0.005f) slices + Slice(otherLabel, rest, OtherColor) else slices } diff --git a/app/src/main/java/cc/skysparkle/sparkledns/Companies.kt b/app/src/main/java/cc/skysparkle/sparkledns/Companies.kt index 716ac59..0914d31 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/Companies.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/Companies.kt @@ -1,5 +1,6 @@ package cc.skysparkle.sparkledns +import java.util.Locale import java.util.TreeMap // The position of each company is saved in StatsStore: add new ones at the end only @@ -21,6 +22,12 @@ enum class Company(val label: String) { object Companies { private const val GOOGLE_PREFIX = "google." + // Second-level labels that belong to a country's suffix: example.co.uk, example.com.br + private val COUNTRY_SECOND_LEVELS = setOf("co", "com", "net", "org", "gov", "edu", "ac", "or", "ne", "go") + + // Names that are not public sites: reverse lookups and local networks + private val PRIVATE_TLDS = setOf("arpa", "local", "lan", "home", "internal", "localhost") + // Hashes of the domains, sorted, with the company of each at the same index: this runs for // every query, and parent domains are looked up by hash without copying the name private val hashes: LongArray @@ -93,4 +100,16 @@ object Companies { } return null } + + // A readable name for a domain of no known company, taken from its main label: + // "cdn.example.co.uk" gives "Example". Null for IP addresses, local names and bare labels. + fun nameOf(name: String): String? { + val labels = name.lowercase(Locale.ROOT).split('.').filter { it.isNotEmpty() } + if (labels.size < 2) return null + val tld = labels.last() + if (tld in PRIVATE_TLDS || tld.all { it.isDigit() }) return null + var index = labels.size - 2 + if (tld.length == 2 && labels.size >= 3 && labels[index] in COUNTRY_SECOND_LEVELS) index-- + return labels[index].replaceFirstChar { it.titlecase(Locale.ROOT) } + } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 3bdcd99..4831f9f 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -100,7 +100,7 @@ Aktivität Einstellungen Anteil der Konzerne - Welcher Anteil deiner Anfragen an die größten Konzerne geht. + Welcher Anteil deiner Anfragen an die größten Konzerne und andere Websites geht. Andere Letzte Anfragen Filter diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index d32c850..6c80281 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -100,7 +100,7 @@ アクティビティ 設定 企業の占有率 - リクエストのうち大手企業に送られる割合です。 + リクエストのうち大手企業やその他のサイトに送られる割合です。 その他 最近のアクティビティ フィルター diff --git a/app/src/main/res/values-lt/strings.xml b/app/src/main/res/values-lt/strings.xml index f62f963..f5c3021 100644 --- a/app/src/main/res/values-lt/strings.xml +++ b/app/src/main/res/values-lt/strings.xml @@ -100,7 +100,7 @@ Veikla Nustatymai Įmonių dalis - Kokia jūsų užklausų dalis tenka didžiausioms įmonėms. + Kokia jūsų užklausų dalis tenka didžiausioms įmonėms ir kitoms svetainėms. Kitos Naujausios užklausos Filtras diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index a2976f5..84f7ac3 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -100,7 +100,7 @@ Активность Настройки Доля компаний - Какая часть ваших запросов уходит крупнейшим компаниям. + Какая часть ваших запросов уходит крупнейшим компаниям и другим сайтам. Другие Последние запросы Фильтр diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index e8d9b58..056d0f7 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -100,7 +100,7 @@ 活动 设置 公司占比 - 你的请求中发往大公司的比例。 + 你的请求中发往大公司和其他网站的比例。 其他 最近活动 筛选 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 20e1686..ad91536 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,7 +101,7 @@ Activity Settings Company dominance - Share of your requests that go to the biggest companies. + Share of your requests that go to the biggest companies and other sites. Other Recent activity Filter