def compare(): available_countries = [ country["code"] for country in database.get_collection( "countries").find({"code": { "$ne": None }}) ] selected_countries = request.args.getlist("country") etag = database.get_stats("etags", location="johns_hopkins") stats = database.get_stats(COLLECTION["country"], country="World") if not stats: return render_template("compare.html", archive=[], stats_last_updated="N/A") return render_template( "compare.html", top_stats=[{ "name": key, "value": value, "icon": ICONS.get(key, "user"), "change": stats.get(CHANGES.get(key)), } for key, value in parse_top_stats(stats).items()], archive=parse_countries_for_comparison(selected_countries), data_countries=",".join(available_countries), search_default=",".join(selected_countries), stats_last_updated=etag["last_updated"].strftime("%d.%m.%Y %H:%M") if etag else None, )
def test_get_stats_with_id_and_slug(self, get_collection): get_collection.return_value.find_one.return_value = { "_id": 1, "slug": 1, "foo": "bar", } assert database.get_stats(slug=1) == {"foo": "bar"}
def local_counties(): stats = database.get_stats(slug=SLUG["romania"]) if not stats or not stats.get("Judete"): return "Nu sunt date despre județe pentru ziua de azi" deserialized = DLZArchiveSerializer.deserialize(stats) stats = deserialized["Judete"] counties = list(reversed(sorted(stats, key=stats.get))) max_key_len = len(counties[0]) max_val_len = len(str(stats[counties[0]])) top_three = counties[:3] remaining = list(sorted(counties[3:])) return formatters.parse_global( title=f"🇷🇴Top cazuri confirmate", stats=[ f"🦠 `{name:<{max_key_len}}: {stats[name]:<{max_val_len}}`" for name in top_three ], items=["\nRestul județelor"] + [ " ".join( [ f"`{name:<{max_key_len}}: {stats[name]:<{max_val_len}}`" for name in judete ] ) for judete in chunks(remaining, 15) ], emoji="🦠", footer=f"\n`Actualizat la: {deserialized['Data']}`", )
def local_latest_article(): stats = database.get_stats(slug=SLUG["stiri-oficiale"]) if not stats: return "Nu sunt stiri salvate pentru ziua de azi" items = {stats.pop("descriere"): [stats.pop("url")]} return formatters.parse_global( title=f"🔵 {stats.pop('titlu')}", stats=stats, items=items, emoji="❗" )
def route_default(): today = database.get_stats(slug=SLUG["romania"]) if not today: return render_template("home.html", archive=[], stats_last_updated="N/A") incidence = today.get("Incidență", {}) infections = today.get("Judete", {}) today_date = datetime.strptime(today["Data"], DATE_FORMAT) today_keys = ["Confirmați", "Decedați", "Vindecați"] if not os.environ["DATELAZI_DATA_URL"].endswith("smallData.json"): today_keys.extend( ["Procent barbati", "Procent femei", "Procent copii"]) today = {key: today[key] for key in today_keys} archive_today = deepcopy(today) archive_today["Data"] = today_date archive = list(database.get_many(COLLECTION["archive"], "Data", how=1)) yesterday = today_date - timedelta(days=1) yesterday = [ x for x in archive if x["Data"] == yesterday.strftime(DATE_FORMAT) ] archive = [ DLZArchiveSerializer.deserialize( x, fields=["Confirmați", "Vindecați", "Decedați", "Data"]) for x in archive ] + [archive_today] return render_template( "home.html", stats_last_updated=today_date.strftime("%d.%m.%Y"), archive=archive, top_stats=[{ "name": key, "value": value, "icon": ICONS.get(key, "user"), "change": (today[key] - yesterday[0][key]) if yesterday else None, } for key, value in today.items()], incidence=[{ "county": c, "incidence": incidence[c] } for c in incidence.keys()], infections=[{ "county": c, "infections": infections[c] } for c in infections.keys()], )
def sync(self): data = self._fetch() db_stats = database.get_stats(slug=SLUG["stiri-oficiale"]) if db_stats and data.items() <= db_stats.items(): logger.info("Stiri: No updates") return database.set_stats( stats=data, collection=COLLECTION["romania"], slug=SLUG["stiri-oficiale"], ) logger.info("Stiri: Completed") return data
def local_age(): stats = database.get_stats(slug=SLUG["romania"]) if not stats: return "Nu sunt statistici de vârstă pentru ziua de azi" deserialized = DLZArchiveSerializer.deserialize(stats) stats = deserialized["Categorii de vârstă"] categories = list(reversed(sorted(stats))) max_key_len = len(categories[0]) max_val_len = len(str(stats[categories[0]])) return formatters.parse_global( title=f"🇷🇴Categorii de vârstă", stats=[ f"🦠 `{k:<{max_key_len}}: {stats[k]:<{max_val_len}}`" for k in stats ], items=[], emoji="🦠", footer=f"\n`Actualizat la: {deserialized['Data']}`", )
def local_global_stats(): top_stats = database.get_stats( collection=COLLECTION["global"], slug=SLUG["global"] ) if not top_stats: return "Nu sunt statistici globale pentru ziua de azi" countries = list( database.get_many(COLLECTION["country"], order_by="total_cases")[:3] ) for item in countries: del item["_id"] last_updated = top_stats.pop("last_updated") return formatters.parse_global( title="🌎 Global Stats", stats=top_stats, items=prepare_items("country", countries), emoji="🦠", footer=f"\n`{last_updated}`\n[Source: worldometers.info](https://worldometers.info/)", )
def global_map(): countries = list(database.get_many(COLLECTION["country"], "total_cases")) if not countries: return render_template("global.html", countries=[], stats_last_updated="N/A") stats = [c for c in countries if c["country"].strip() == "World"][0] return render_template( "global.html", stats_last_updated=database.get_stats( COLLECTION["global"], slug=SLUG["global"])["last_updated"].replace("Last updated: ", ""), top_stats=[{ "name": key, "value": value, "icon": ICONS.get(key, "user"), "change": stats.get(CHANGES.get(key)), } for key, value in parse_top_stats(stats).items()], countries=parse_countries(countries), )
def sync(self): soup = self._fetch() top_stats = { string_to_field(x.h1.text.strip()): x.div.span.text.strip() for x in soup.find_all(id="maincounter-wrap") } top_stats["last_updated"] = soup.find( string=re.compile("Last updated: ")) db_stats = database.get_stats(COLLECTION["global"], slug=SLUG["global"]) if db_stats and top_stats.items() <= db_stats.items(): logger.info("Global: No updates") return database.set_stats(top_stats, collection=COLLECTION["global"], slug=SLUG["global"]) logger.info("Global: Completed") return top_stats
def test_get_stats_with_no_stats(self, get_collection, collection, slug): get_collection.return_value.find_one.return_value = {} assert database.get_stats(collection, slug=slug) == {} get_collection.assert_called_once_with(collection) get_collection.return_value.find_one.assert_called_once_with( {"slug": slug})
def test_get_stats_with_no_kwargs(self): with pytest.raises(ValueError) as e: database.get_stats() assert e.value.args[0] == "filter kwargs required"
def _fetch_game(self): return database.get_stats( self.COLLECTION, chat_id=self.chat_id, name=self.name )
def _fetch_local(self): self._local_data = database.get_stats(slug=self.slug) return self._local_data
def local_quick_stats(): stats = database.get_stats(slug=SLUG["romania"]) if not stats: return "Nu sunt statistici salvate pentru ziua de azi" stats = DLZSerializer.deserialize(stats) return formatters.parse_global(title="🔴 Cazuri noi", stats=stats, items={})