kopia lustrzana https://github.com/mkdryden/telegram-stats-bot
stats.py: Allow sorting title history by duration
rodzic
f1ac2f661d
commit
2ef820bfb6
|
@ -474,12 +474,13 @@ class StatsRunner(object):
|
||||||
|
|
||||||
return None, bio
|
return None, bio
|
||||||
|
|
||||||
def get_title_history(self, start: str = None, end: str = None) \
|
def get_title_history(self, start: str = None, end: str = None, duration: bool = False) \
|
||||||
-> Tuple[None, BytesIO]:
|
-> Tuple[None, BytesIO]:
|
||||||
"""
|
"""
|
||||||
Make a plot of group titles history over time
|
Make a plot of group titles history over time
|
||||||
:param start: Start timestamp (e.g. 2019, 2019-01, 2019-01-01, "2019-01-01 14:21")
|
:param start: Start timestamp (e.g. 2019, 2019-01, 2019-01-01, "2019-01-01 14:21")
|
||||||
:param end: End timestamp (e.g. 2019, 2019-01, 2019-01-01, "2019-01-01 14:21")
|
:param end: End timestamp (e.g. 2019, 2019-01, 2019-01-01, "2019-01-01 14:21")
|
||||||
|
:param duration: If true, order by duration instead of time.
|
||||||
"""
|
"""
|
||||||
query_conditions = []
|
query_conditions = []
|
||||||
sql_dict = {}
|
sql_dict = {}
|
||||||
|
@ -507,8 +508,8 @@ class StatsRunner(object):
|
||||||
df = pd.read_sql_query(query, con, params=sql_dict)
|
df = pd.read_sql_query(query, con, params=sql_dict)
|
||||||
|
|
||||||
df['idx'] = np.arange(len(df))
|
df['idx'] = np.arange(len(df))
|
||||||
df['diff'] = df['date'].diff(-1)
|
df['diff'] = -df['date'].diff(-1)
|
||||||
df['end'] = df['date'] - df['diff']
|
df['end'] = df['date'] + df['diff']
|
||||||
|
|
||||||
if end:
|
if end:
|
||||||
last = pd.Timestamp(sql_dict['end_dt'], tz=self.tz).tz_convert('utc')
|
last = pd.Timestamp(sql_dict['end_dt'], tz=self.tz).tz_convert('utc')
|
||||||
|
@ -518,10 +519,27 @@ class StatsRunner(object):
|
||||||
df_end = df['end']
|
df_end = df['end']
|
||||||
df_end.iloc[-1] = last
|
df_end.iloc[-1] = last
|
||||||
df.loc[:, 'end'] = df_end
|
df.loc[:, 'end'] = df_end
|
||||||
|
df.loc[:, 'diff'].iloc[-1] = df.iloc[-1]['end'] - df.iloc[-1]['date']
|
||||||
|
|
||||||
fig = Figure(constrained_layout=True, figsize=(12, 0.15 * len(df)))
|
fig = Figure(constrained_layout=True, figsize=(12, 0.15 * len(df)))
|
||||||
ax = fig.subplots()
|
ax = fig.subplots()
|
||||||
|
|
||||||
|
if duration:
|
||||||
|
df = df.sort_values('diff')
|
||||||
|
df = df.reset_index(drop=True)
|
||||||
|
df['idx'] = df.index
|
||||||
|
|
||||||
|
ax.barh(df.idx, df['diff'].dt.days, tick_label=df.new_chat_title)
|
||||||
|
|
||||||
|
ax.margins(0.2)
|
||||||
|
ax.set_ylabel("")
|
||||||
|
ax.set_xlabel("Duration (days)")
|
||||||
|
ax.set_ylim(-1, (df.idx.max() + 1))
|
||||||
|
ax.set_title("Chat Title History")
|
||||||
|
ax.grid(False, which='both', axis='y')
|
||||||
|
sns.despine(fig=fig, left=True)
|
||||||
|
|
||||||
|
else:
|
||||||
x = df.iloc[:-1].end
|
x = df.iloc[:-1].end
|
||||||
y = df.iloc[:-1].idx + .5
|
y = df.iloc[:-1].idx + .5
|
||||||
|
|
||||||
|
@ -536,10 +554,11 @@ class StatsRunner(object):
|
||||||
xytext=(10, 0), textcoords='offset points',
|
xytext=(10, 0), textcoords='offset points',
|
||||||
horizontalalignment='left', verticalalignment='bottom')
|
horizontalalignment='left', verticalalignment='bottom')
|
||||||
|
|
||||||
ax.margins(0.2)
|
|
||||||
ax.set_ylabel("")
|
|
||||||
ax.set_ylim(-1, (df.idx.max() + 1))
|
ax.set_ylim(-1, (df.idx.max() + 1))
|
||||||
ax.set_xlim(titles[0][0] - 1, None)
|
ax.set_xlim(titles[0][0] - 1, None)
|
||||||
|
|
||||||
|
ax.margins(0.2)
|
||||||
|
ax.set_ylabel("")
|
||||||
ax.set_xlabel("")
|
ax.set_xlabel("")
|
||||||
ax.set_title("Chat Title History")
|
ax.set_title("Chat Title History")
|
||||||
ax.grid(False, which='both', axis='y')
|
ax.grid(False, which='both', axis='y')
|
||||||
|
|
Ładowanie…
Reference in New Issue