Skip to content
2 changes: 1 addition & 1 deletion backend/app/routes/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StatsResultCategoryTotals(BaseModel):

class StatsResult(BaseModel):
totals: dict[str, int]
countries: dict[str, int]
countries: dict[str, stats.CountryData]
downloads_per_day: dict[str, int]
updates_per_day: dict[str, int]
delta_downloads_per_day: dict[str, int]
Expand Down
30 changes: 25 additions & 5 deletions backend/app/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
StatsType = dict[str, dict[str, list[int]]]


class StatsFromServer(TypedDict):
class DateStatsFromServer(TypedDict):
countries: dict[str, int]
date: datetime.date
delta_downloads: int
Expand All @@ -26,14 +26,19 @@ class StatsFromServer(TypedDict):
refs: dict[str, dict[str, list[int]]]


class CountryData(TypedDict):
population: int
downloads: int


POPULAR_DAYS_NUM = 7

FIRST_STATS_DATE = datetime.date(2018, 4, 29)


def _get_stats_for_date(
date: datetime.date, session: httpx.Client
) -> StatsFromServer | None:
) -> DateStatsFromServer | None:
stats_json_url = urlparse(
config.settings.stats_baseurl + date.strftime("/%Y/%m/%d.json")
)
Expand Down Expand Up @@ -138,14 +143,23 @@ def _get_app_stats_per_day() -> dict[str, dict[str, int]]:
return app_stats_per_day


def _get_population_country(country: str) -> int:
country_population: dict[str, int] = {}
# Lazy import the first time the data is needed
if len(country_population) == 0:
with open("../data/country_population.json") as f:
country_population = json.load(f)
return country_population[country] if country in country_population else 0


def _get_stats(app_count: int):
edate = datetime.date.today()
sdate = FIRST_STATS_DATE

downloads_per_day: dict[str, int] = {}
delta_downloads_per_day: dict[str, int] = {}
updates_per_day: dict[str, int] = {}
totals_country: dict[str, int] = {}
totals_country: dict[str, CountryData] = {}
with httpx.Client() as session:
for i in range((edate - sdate).days + 1):
date = sdate + datetime.timedelta(days=i)
Expand Down Expand Up @@ -179,8 +193,14 @@ def _get_stats(app_count: int):
):
for country, downloads in stats["countries"].items():
if country not in totals_country:
totals_country[country] = 0
totals_country[country] = totals_country[country] + downloads
totals_country[country] = CountryData(population=0, downloads=0)
totals_country[country]["downloads"] = (
totals_country[country]["downloads"] + downloads
)
for country, _ in totals_country:
totals_country[country]["population"] = _get_population_country(
country
)

category_totals = get_category_totals()

Expand Down
217 changes: 217 additions & 0 deletions backend/data/country_population.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"AW" : 107359,
"AF" : 41454761,
"AO" : 36749906,
"AL" : 2745972,
"AD" : 80856,
"AE" : 10483751,
"AR" : 45538401,
"AM" : 2990900,
"AS" : 47521,
"AG" : 93316,
"AU" : 26658948,
"AT" : 9131761,
"AZ" : 10153958,
"BI" : 13689450,
"BE" : 11787423,
"BJ" : 14111034,
"BF" : 23025776,
"BD" : 171466990,
"BG" : 6446596,
"BH" : 1577059,
"BS" : 399440,
"BA" : 3185073,
"BY" : 9178298,
"BZ" : 411106,
"BM" : 64698,
"BO" : 12244159,
"BR" : 211140729,
"BB" : 282336,
"BN" : 458949,
"BT" : 786385,
"BW" : 2480244,
"CF" : 5152421,
"CA" : 40097761,
"CH" : 8888093,
"CL" : 19658835,
"CN" : 1410710000,
"CI" : 31165654,
"CM" : 28372687,
"CD" : 105789731,
"CG" : 6182885,
"CO" : 52321152,
"KM" : 850387,
"CV" : 522331,
"CR" : 5105525,
"CU" : 11019931,
"CW" : 147862,
"KY" : 73038,
"CY" : 1344976,
"CZ" : 10864042,
"DE" : 83280000,
"DJ" : 1152944,
"DM" : 66510,
"DK" : 5946952,
"DO" : 11331265,
"DZ" : 46164219,
"EC" : 17980083,
"EG" : 114535772,
"ER" : 3470390,
"ES" : 48347910,
"EE" : 1370286,
"ET" : 128691692,
"FI" : 5583911,
"FJ" : 924145,
"FR" : 68287487,
"FO" : 54482,
"FM" : 112630,
"GA" : 2484789,
"GB" : 68350000,
"GE" : 3715483,
"GH" : 33787914,
"GI" : 38471,
"GN" : 14405468,
"GM" : 2697845,
"GW" : 2153339,
"GQ" : 1847549,
"GR" : 10405588,
"GD" : 117081,
"GL" : 56865,
"GT" : 18124838,
"GU" : 166506,
"GY" : 826353,
"HK" : 7536100,
"HN" : 10644851,
"HR" : 3859686,
"HT" : 11637398,
"HU" : 9592186,
"ID" : 281190067,
"IM" : 84165,
"IN" : 1438069596,
"IE" : 5307600,
"IR" : 90608707,
"IQ" : 45074049,
"IS" : 393349,
"IL" : 9756600,
"IT" : 58993475,
"JM" : 2839786,
"JO" : 11439213,
"JP" : 124516650,
"KZ" : 20330104,
"KE" : 55339003,
"KG" : 7099750,
"KH" : 17423880,
"KI" : 132530,
"KN" : 46758,
"KR" : 51712619,
"KW" : 4853420,
"LA" : 7664993,
"LB" : 5773493,
"LR" : 5493031,
"LY" : 7305659,
"LC" : 179285,
"LI" : 39850,
"LK" : 22037000,
"LS" : 2311472,
"LT" : 2871585,
"LU" : 666430,
"LV" : 1877445,
"MO" : 678800,
"MF" : 27515,
"MA" : 37712505,
"MC" : 38956,
"MD" : 2457783,
"MG" : 31195932,
"MV" : 525994,
"MX" : 129739759,
"MH" : 38827,
"MK" : 1827816,
"ML" : 23769127,
"MT" : 552747,
"MM" : 54133798,
"ME" : 616177,
"MN" : 3481145,
"MP" : 45143,
"MZ" : 33635160,
"MR" : 5022441,
"MU" : 1261041,
"MW" : 21104482,
"MY" : 35126298,
"NA" : 2963095,
"NC" : 289870,
"NE" : 26159867,
"NG" : 227882945,
"NI" : 6823613,
"NL" : 17877117,
"NO" : 5519594,
"NP" : 29694614,
"NR" : 11875,
"NZ" : 5223100,
"OM" : 5049269,
"PK" : 247504495,
"PA" : 4458759,
"PE" : 33845617,
"PH" : 114891199,
"PW" : 17727,
"PG" : 10389635,
"PL" : 36687353,
"PR" : 3205691,
"KP" : 26418204,
"PT" : 10578174,
"PY" : 6844146,
"PS" : 5165775,
"PF" : 281118,
"QA" : 2656032,
"RO" : 19059479,
"RU" : 143826130,
"RW" : 13954471,
"SA" : 33264292,
"SD" : 50042791,
"SN" : 18077573,
"SG" : 5917648,
"SB" : 800005,
"SL" : 8460512,
"SV" : 6309624,
"SM" : 33860,
"SO" : 18358615,
"RS" : 6623183,
"SS" : 11483374,
"ST" : 230871,
"SR" : 628886,
"SK" : 5426740,
"SI" : 2120461,
"SE" : 10536632,
"SZ" : 1230506,
"SX" : 42749,
"SC" : 119773,
"SY" : 23594623,
"TC" : 46198,
"TD" : 19319064,
"TG" : 9304337,
"TH" : 71702435,
"TJ" : 10389799,
"TM" : 7364438,
"TL" : 1384286,
"TO" : 104597,
"TT" : 1367510,
"TN" : 12200431,
"TR" : 85325965,
"TV" : 9816,
"TZ" : 66617606,
"UG" : 48656601,
"UA" : 37732836,
"UY" : 3388081,
"US" : 334914895,
"UZ" : 35652307,
"VC" : 101323,
"VE" : 28300854,
"VG" : 38985,
"VI" : 104917,
"VN" : 100352192,
"VU" : 320409,
"WS" : 216663,
"YE" : 39390799,
"ZA" : 63212384,
"ZM" : 20723965,
"ZW" : 16340822
}
Loading
Loading