def confirmed_by_testdate(context): source_name = jobs["confirmed_by_testdate"]["source"]["name"] source_url = jobs["confirmed_by_testdate"]["source"]["url"] data = c19api.timeseries("confirmed") curr_data = list(filter(lambda x: x["source"] == "fhi:git", data))[-1] curr_total = curr_data.get("total") last_data = file_open_json("confirmed_by_testdate") last_total = last_data.get("total") if curr_total - last_total > 0: ret_str = "đŠ <b>Antall meldte smittetilfeller</b>" ret_str += "\nAntall meldte COVID-19 tilfeller etter prĂžvetakingsdato." ret_str += "\nDet er 1-2 dagers forsinkelse i tiden fra diagnose til registrering i MSIS." ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" file_write_json("confirmed_by_testdate", curr_data) print(ret_str, "\n") context.bot.send_photo( bot["autopost"]["chatid"], graphs.confirmed(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def tested_lab(context): source_name = jobs["tested_lab"]["source"]["name"] source_url = jobs["tested_lab"]["source"]["url"] curr_data = c19api.timeseries("tested_lab")[-1] curr_total = curr_data.get("total") last_data = file_open_json("tested_lab") last_total = last_data.get("total") if curr_total - last_total > 0: ret_str = "đŹ <b>Antall testet (Laboratoriedata)</b>" ret_str += "\nAntall personer testet og andelen positive blant disse i Norge siden epidemiens start." ret_str += "\nEn ny test pĂ„ en person defineres som en test utfĂžrt minst 7 dager etter forrige test av samme person." ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" file_write_json("tested_lab", curr_data) print(ret_str, "\n") context.bot.send_photo( bot["autopost"]["chatid"], graphs.tested(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def vaccine(context): population = 5391369 source_name = jobs["vaccine"]["source"]["name"] source_url = jobs["vaccine"]["source"]["url"] data = c19api.timeseries("vaccine_doses") curr_data = list(filter(lambda x: x["granularity_geo"] == "nation", data))[-1] curr_total_doses = curr_data.get("total_doses") last_data = file_open_json("vaccine_doses") last_total_doses = last_data.get("total_doses") diff_total_doses = curr_total_doses - last_total_doses if diff_total_doses > 0: curr_total_dose_1 = curr_data.get("total_dose_1") curr_total_dose_2 = curr_data.get("total_dose_2") curr_total_dose_1_pct = curr_total_dose_1 / population curr_total_dose_2_pct = curr_total_dose_2 / population last_total_dose_1 = last_data.get("total_dose_1") last_total_dose_2 = last_data.get("total_dose_2") diff_total_dose_1 = curr_total_dose_1 - last_total_dose_1 diff_total_dose_2 = curr_total_dose_2 - last_total_dose_2 ret_str = "đ <b>Antall vaksinerte</b>" if diff_total_dose_1 != 0: ret_str += ( f"\n<b>{diff_total_dose_1:,}</b> nye personer vaksinert med 1. dose" ) if diff_total_dose_2 != 0: ret_str += f"\n<b>{diff_total_dose_2:,}</b> nye personer fullvaksinert" ret_str += f"\n\nTotalt <b>{curr_total_dose_1:,}</b> personer (<b>{curr_total_dose_1_pct:,.02%}</b> av befolkningen) har fĂ„tt minst Ă©n vaksinedose" ret_str += f"\nTotalt <b>{curr_total_dose_2:,}</b> personer (<b>{curr_total_dose_2_pct:,.02%}</b> av befolkningen) er fullvaksinert" ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" file_write_json("vaccine_doses", curr_data) ret_str = ret_str.replace(",", " ") print(ret_str, "\n") context.bot.send_photo( bot["autopost"]["chatid"], graphs.vaccine_doses(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def smittestopp(context): source_name = jobs["smittestopp"]["source"]["name"] source_url = jobs["smittestopp"]["source"]["url"] curr_data = c19api.timeseries("smittestopp")[-1] curr_total_downloads = int(curr_data.get("total_downloads")) curr_total_reported = int(curr_data.get("total_reported")) last_data = file_open_json("smittestopp") last_total_downloads = int(last_data.get("total_downloads")) last_total_reported = int(last_data.get("total_reported")) if (curr_total_downloads != last_total_downloads or curr_total_reported != last_total_reported): new_downloads = int(curr_data.get("new_downloads")) new_reported = int(curr_data.get("new_reported")) if new_downloads == 1: new_downloads_text = "ny nedlasting av appen Smittestopp" else: new_downloads_text = "nye nedlastinger av appen Smittestopp" if new_reported == 1: new_reported_text = "ny person meldt smittet gjennom appen Smittestopp" else: new_reported_text = "nye personer meldt smittet gjennom appen Smittestopp" ret_str = "đ± <b>Smittestopp</b>" if new_downloads != 0: ret_str += f"\n<b>{new_downloads:,}</b> {new_downloads_text}" if new_reported != 0: ret_str += f"\n<b>{new_reported:,}</b> {new_reported_text}" ret_str += f"\n\nTotalt antall nedlastinger: <b>{curr_total_downloads:,}</b>" ret_str += f"\nTotalt <b>{curr_total_reported:,}</b> personer er meldt smittet gjennom appen" ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" file_write_json("smittestopp", curr_data) ret_str = ret_str.replace(",", " ") print(ret_str, "\n") context.bot.send_photo( bot["autopost"]["chatid"], graphs.smittestopp(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def hospitalized(context): source_name = jobs["hospitalized"]["source"]["name"] source_url = jobs["hospitalized"]["source"]["url"] curr_data = c19api.timeseries("hospitalized")[-1] curr_respiratory = int(curr_data.get("respiratory")) curr_admissions = int(curr_data.get("admissions")) last_data = file_open_json("hospitalized") last_respiratory = int(last_data.get("respiratory")) last_admissions = int(last_data.get("admissions")) if curr_admissions != last_admissions or curr_respiratory != last_respiratory: diff_admissions = curr_admissions - last_admissions diff_respiratory = curr_respiratory - last_respiratory respiratory_pct = curr_respiratory / curr_admissions ret_str = "đ„ <b>Innlagte pasienter pĂ„ sykehus</b>" if diff_admissions != 0: ret_str += f"\nEndring i antall innlagte: <b>{diff_admissions:+,}</b>" if diff_respiratory != 0: ret_str += f"\nEndring i antall pĂ„ respirator: <b>{diff_respiratory:+,}</b>" ret_str += f"\n\n<b>{curr_admissions:,}</b> personer er innlagt pĂ„ sykehus" ret_str += f"\n<b>{curr_respiratory:,}</b> personer er pĂ„ respirator ({respiratory_pct:.01%})" ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" ret_str = ret_str.replace(",", " ") print(ret_str, "\n") file_write_json("hospitalized", curr_data) context.bot.send_photo( bot["autopost"]["chatid"], graphs.hospitalized(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def dead(context): source_name = jobs["dead"]["source"]["name"] source_url = jobs["dead"]["source"]["url"] curr_data = c19api.timeseries("dead")[-1] curr_total = curr_data.get("total") last_data = file_open_json("dead") last_total = last_data.get("total") diff_dead = curr_total - last_total if diff_dead > 0: messagetext = get_messagetext("dead", diff_dead) curr_new_today = curr_data.get("new") ret_str = "â <b>COVID-19 assosierte dĂždsfall</b>" ret_str += f"\n<b>{diff_dead}</b> {messagetext}" ret_str += ( f"\nTotalt: <b>{curr_total:,}</b> (Nye i dag: <b>{curr_new_today:,}</b>)" ) ret_str += f"\n\nKilde: <a href='{source_url}'>{source_name}</a>" file_write_json("dead", curr_data) ret_str = ret_str.replace(",", " ") print(ret_str, "\n") context.bot.send_photo( bot["autopost"]["chatid"], graphs.dead(), parse_mode=ParseMode.HTML, caption=ret_str, ) else: return None
def stats(update, context): today = date.today().strftime("%d.%m.%Y") # metadata population = 5391369 tested = c19api.metadata("tested") confirmed = c19api.metadata("confirmed") dead = c19api.metadata("dead") admissions = c19api.metadata("admissions") respiratory = c19api.metadata("respiratory") vaccine_doses = c19api.timeseries("vaccine_doses") # totals tested_total = tested.get("total") confirmed_total = confirmed.get("total") dead_total = dead.get("total") admissions_total = admissions.get("total") respiratory_total = respiratory.get("total") # newToday tested_newToday = tested.get("newToday", 0) confirmed_newToday = confirmed.get("newToday", 0) dead_newToday = dead.get("newToday", 0) # newSince confirmed_newSince_d7 = confirmed.get("newSince_d7", 0) confirmed_newSince_d14 = confirmed.get("newSince_d14", 0) # percentages dead_pct = round(dead_total / confirmed_total * 100, 1) respiratory_pct = round(respiratory_total / admissions_total * 100, 1) # vaccine data vaccine_data = list( filter(lambda x: x["granularity_geo"] == "nation", vaccine_doses) )[-1] vacc_total_dose_1 = vaccine_data.get("total_dose_1") vacc_total_dose_2 = vaccine_data.get("total_dose_2") vacc_total_dose_1_pct = vacc_total_dose_1 / population vacc_total_dose_2_pct = vacc_total_dose_2 / population ret_str = f"đą <b>NĂžkkeltall - {today}</b>" ret_str += f"\n\nđŠ Smittetilfeller i dag: <b>{confirmed_newToday:,}</b>" ret_str += f"\nSiste 7d: <b>{confirmed_newSince_d7:,}</b>" ret_str += f"\nSiste 14d: <b>{confirmed_newSince_d14:,}</b>" ret_str += f"\nTotalt: <b>{confirmed_total:,}</b>" ret_str += f"\n\nâ DĂždsfall i dag: <b>{dead_newToday:,}</b>" ret_str += f"\nTotalt: <b>{dead_total:,}</b> ({dead_pct}% av smittede)" ret_str += f"\n\nđŹ Testede i dag: <b>{tested_newToday:,}</b>" ret_str += f"\nTotalt: <b>{tested_total:,}</b>" ret_str += f"\n\nđ„ Innlagt pĂ„ sykehus: <b>{admissions_total:,}</b>" ret_str += f"\nđ· Tilkoblet respirator: <b>{respiratory_total:,}</b> ({respiratory_pct}% av innlagte)" ret_str += "\n\nđ Andel av befolkningen vaksinert" ret_str += f"\n<b>{vacc_total_dose_1_pct:,.02%}</b> har fĂ„tt minst Ă©n dose (<b>{vacc_total_dose_1:,}</b> personer)" ret_str += f"\n<b>{vacc_total_dose_2_pct:,.02%}</b> er fullvaksinert (<b>{vacc_total_dose_2:,}</b> personer)" ret_str = ret_str.replace(",", " ") context.bot.send_message( chat_id=update.message.chat_id, text=ret_str, parse_mode=ParseMode.HTML )
def tested(): filename = "./graphs/no_tested.png" if os.path.exists(filename): os.remove(filename) data = c19api.timeseries("tested_lab") df = pd.DataFrame(data) mapping = { "new_neg": "Nye (Negative)", "new_pos": "Nye (Positive)", "new_total": "Nye", "pr100_pos": "Andel Positive", "total": "Akkumulert", } df = df.rename(columns=mapping) df["date"] = pd.to_datetime(df["date"]) df["Andel Negative"] = 100 - df["Andel Positive"] df = df.melt(id_vars=["date", "Andel Positive"], var_name="category", value_name="value") base = alt.Chart( df, title= "Antall personer testet for covid-19 per dag og andel positive blant disse (Kilde: FHI)", ).encode( alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40))) andel = base.mark_line(color="red", opacity=0.8).encode(y=alt.Y( "Andel Positive:Q", title="% Positive", axis=alt.Axis(grid=True))) bar = (base.transform_filter( (alt.datum.category == "Nye (Negative)") | (alt.datum.category == "Nye (Positive)")).mark_bar().encode( y=alt.Y("value:Q", title="Antall personer testet for covid-19 per dag"), color=alt.Color( "category:N", scale=alt.Scale( domain=["Nye (Positive)", "Nye (Negative)", "% Positive"], range=["#FF9622", "#6DA9FF", "red"], ), legend=alt.Legend(title=None), ), )) chart = (alt.layer(bar, andel).resolve_scale(y="independent").properties( width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=480, legendY=660, )) save(chart, filename) return open(filename, "rb")
def confirmed(): data = c19api.timeseries("confirmed") filename = "./graphs/no_confirmed.png" if os.path.exists(filename): os.remove(filename) df = pd.DataFrame(data) df["date"] = pd.to_datetime(df["date"]) df = df.loc[df["source"] == "fhi:git"] df["new_sma7"] = df.new.rolling(window=7).mean().shift() df = df.melt( id_vars=["date"], value_vars=["new", "new_sma7", "total"], var_name="category", value_name="value", ).dropna() rename = {"new": "Nye", "new_sma7": "Snitt 7 d.", "total": "Akkumulert"} df["category"] = df["category"].replace(rename) base = alt.Chart( df, title= "Antall meldte COVID-19 tilfeller etter prĂžvetakingsdato (Kilde: FHI/MSIS)", ).encode( alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40))) bar = (base.transform_filter(alt.datum.category == "Nye").mark_bar( color="#FFD1D1").encode( y=alt.Y("value:Q", axis=alt.Axis(title="Nye per dag", grid=True)))) line = (base.transform_filter( alt.datum.category == "Akkumulert").mark_line( color="#2E507B", strokeWidth=3).encode( y=alt.Y("value:Q", axis=alt.Axis(title="Akkumulert")), color=alt.Color( "category:N", scale=alt.Scale( domain=["Nye", "Snitt 7 d.", "Akkumulert"], range=["#FFD1D1", "red", "#2E507B"], ), legend=alt.Legend(title=None), ), )) ma7 = (base.transform_filter(alt.datum.category == "Snitt 7 d.").mark_line( opacity=0.8).encode(y=alt.Y("value:Q"), color=alt.Color("category:N"))) chart = (alt.layer(bar + ma7, line).resolve_scale(y="independent").properties( width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=480, legendY=660, )) save(chart, filename) return open(filename, "rb")
def vaccine_doses(): data = c19api.timeseries("vaccine_doses") filename = "./graphs/no_vaccine_doses.png" if os.path.exists(filename): os.remove(filename) df = pd.DataFrame(data) df["date"] = pd.to_datetime(df["date"]) df = df[df["granularity_geo"] == "nation"] df["new_sma7"] = df.new_doses.rolling(window=7).mean().shift() df = df.melt( id_vars=["date"], value_vars=["total_dose_1", "total_dose_2"], var_name="category", value_name="value", ).dropna() rename = { "total_dose_1": "Har fÄtt minst én dose", "total_dose_2": "Fullvaksinert", } df["category"] = df["category"].replace(rename) chart = (alt.Chart( df, title= "Antall personer vaksinert med 1. og 2. dose av vaksine mot COVID-19 i Norge (Kilde: FHI)", ).mark_area(line={}, opacity=0.3).encode( x=alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40)), y=alt.Y( "value:Q", stack=None, title="Antall", ), color=alt.Color( "category:N", scale=alt.Scale( domain=["Har fÄtt minst én dose", "Fullvaksinert"], range=["#5dade2", "#2ecc71"], ), legend=alt.Legend(title=None), ), ).properties(width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=480, legendY=660, )) save(chart, filename) return open(filename, "rb")
def smittestopp(): data = c19api.timeseries("smittestopp") filename = "./graphs/no_smittestopp.png" if os.path.exists(filename): os.remove(filename) df = pd.DataFrame(data) df["date"] = pd.to_datetime(df["date"]) df = df.melt( id_vars=["date"], value_vars=["new_reported", "total_downloads"], var_name="category", value_name="value", ).dropna() rename = { "new_reported": "Antall meldt smittet i Smittestopp", "total_downloads": "Antall nedlastinger av Smittestopp", } df["category"] = df["category"].replace(rename) base = alt.Chart( df, title= "Antall nedlastinger av Smittestopp og antall som har meldt gjennom appen at de er smittet (Kilde: FHI)", ).encode( alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40))) downloads = (base.transform_filter( alt.datum.category == "Antall nedlastinger av Smittestopp").mark_area( line={}, color="#5BC1FF", opacity=0.2).encode(y=alt.Y( "value:Q", axis=alt.Axis(title="Antall nedlastinger av Smittestopp", grid=True), ))) reported = (base.transform_filter( alt.datum.category == "Antall meldt smittet i Smittestopp").mark_bar( color="#FFA57E").encode( y=alt.Y( "value:Q", axis=alt.Axis(title="Antall meldt smittet i Smittestopp")), color=alt.Color( "category:N", scale=alt.Scale( domain=[ "Antall nedlastinger av Smittestopp", "Antall meldt smittet i Smittestopp", ], range=["#5BC1FF", "#FFA57E"], ), legend=alt.Legend(title=None), ), )) chart = (alt.layer(reported, downloads).resolve_scale(y="independent").properties( width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, labelLimit=200, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=390, legendY=660, )) save(chart, filename) return open(filename, "rb")
def hospitalized(): data = c19api.timeseries("hospitalized") today = date.today() filename = "./graphs/no_hospitalized.png" if os.path.exists(filename): os.remove(filename) df = pd.DataFrame(data) idx = pd.date_range("2020-03-08", today) df.index = pd.DatetimeIndex(df["date"]) df = df.reindex(idx) df["date"] = df.index df = df.reset_index(drop=True) df["admissions"] = df["admissions"].fillna(method="ffill").astype(int) df["respiratory"] = df["respiratory"].fillna(method="ffill").astype(int) df_melt = pd.melt( df, id_vars=["date"], value_vars=["admissions", "respiratory"], value_name="value", ).replace({ "admissions": "Innlagt", "respiratory": "PÄ respirator" }) chart = (alt.Chart( df_melt, title="Innlagt pÄ sykehus (Kilde: Helsedirektoratet)").mark_area( line={}, opacity=0.3).encode( x=alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40)), y=alt.Y( "value:Q", stack=None, title="Antall innlagte med pÄvist COVID-19 i Norge", ), color=alt.Color( "variable:N", scale=alt.Scale(domain=["Innlagt", "PÄ respirator"], range=["#5A9DFF", "#FF8B1B"]), legend=alt.Legend(title=None), ), ).properties(width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=480, legendY=660, )) save(chart, filename) return open(filename, "rb")
def dead(): data = c19api.timeseries("dead") today = date.today() filename = "./graphs/no_dead.png" if os.path.exists(filename): os.remove(filename) df = pd.DataFrame(data) idx = pd.date_range("2020-03-07", df["date"].max()) df.index = pd.DatetimeIndex(df["date"]) df = df.reindex(idx) df["date"] = df.index df = df.reset_index(drop=True) df = df[df.date <= str(today)] df["new"] = df["new"].fillna(0).astype(int) df["total"] = df["total"].fillna(method="bfill").astype(int) df["new_sma7"] = df.new.rolling(window=7).mean() df = df.melt( id_vars=["date"], value_vars=["new", "new_sma7", "total"], var_name="category", value_name="value", ).dropna() rename = {"new": "Nye", "new_sma7": "Snitt 7 d.", "total": "Akkumulert"} df["category"] = df["category"].replace(rename) base = alt.Chart(df, title="COVID-19 dĂždsfall (Kilde: FHI)").encode( alt.X("yearmonthdate(date):O", axis=alt.Axis(title=None, labelAngle=-40))) bar = (base.transform_filter(alt.datum.category == "Nye").mark_bar( color="#FFD1D1").encode( y=alt.Y("value:Q", axis=alt.Axis(title="Nye per dag", grid=True)))) line = (base.transform_filter( alt.datum.category == "Akkumulert").mark_line( color="#2E507B", strokeWidth=3).encode( y=alt.Y("value:Q", axis=alt.Axis(title="Akkumulert")), color=alt.Color( "category:N", scale=alt.Scale( domain=["Nye", "Snitt 7 d.", "Akkumulert"], range=["#FFD1D1", "red", "#2E507B"], ), legend=alt.Legend(title=None), ), )) ma7 = (base.transform_filter(alt.datum.category == "Snitt 7 d.").mark_line( opacity=0.8).encode(y=alt.Y("value:Q"), color=alt.Color("category:N"))) chart = (alt.layer(bar + ma7, line).resolve_scale(y="independent").properties( width=1200, height=600).configure_legend( strokeColor="gray", fillColor="#FFFFFF", labelFontSize=12, symbolStrokeWidth=2, symbolSize=160, padding=6, cornerRadius=5, direction="horizontal", orient="none", legendX=480, legendY=660, )) save(chart, filename) return open(filename, "rb")