Initial import
This commit is contained in:
44
app/pages/stats.py
Normal file
44
app/pages/stats.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
import streamlit as st
|
||||
import json
|
||||
import sql
|
||||
import pandas as pd
|
||||
|
||||
from enums import CounterType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if "counter_id" in st.query_params.keys():
|
||||
counter_id = int(st.query_params["counter_id"])
|
||||
df = sql.get_counter(counter_id)
|
||||
|
||||
st.header('Counter: ' + df['name'])
|
||||
|
||||
color ='#' + df['color']
|
||||
match df['type']:
|
||||
case CounterType.DAILY.value | CounterType.SIMPLE.value:
|
||||
st.bar_chart(sql.get_daily_analytics(int(df['id'])), x="date", y="count", color=color)
|
||||
case CounterType.WEEKLY.value:
|
||||
st.bar_chart(sql.get_weekly_analytics(int(df['id'])), x="week", y="count", color=color)
|
||||
case CounterType.MONTHLY.value:
|
||||
st.bar_chart(sql.get_monthly_analytics(int(df['id'])), x="month", y="count", color=color)
|
||||
case CounterType.YEARLY.value:
|
||||
st.bar_chart(sql.get_yearly_analytics(int(df['id'])), x="year", y="count", color=color)
|
||||
|
||||
else:
|
||||
|
||||
st.header("Statistics")
|
||||
|
||||
entries = sql.get_analytics()
|
||||
entries_norm = pd.json_normalize(entries.counters.apply(json.loads)).fillna(0)
|
||||
entries_full = pd.concat([entries, entries_norm], axis=1).drop(['counters'], axis=1)
|
||||
|
||||
selected_counters = [c for c in entries_full.columns if c != "date"]
|
||||
all_counters = sql.get_counters()
|
||||
colors = all_counters.loc[all_counters['name'].isin(selected_counters), ["name", "color"]]
|
||||
colors.name = colors.name.astype("category")
|
||||
colors.name = colors.name.cat.set_categories(selected_counters)
|
||||
colors = colors.sort_values(["name"])
|
||||
colors = colors.color.apply(lambda c: "#" + c).tolist()
|
||||
|
||||
st.bar_chart(entries_full, x="date", x_label="Date", y_label="Count", color=colors)
|
||||
Reference in New Issue
Block a user