improved SQL queries
This commit is contained in:
@@ -87,8 +87,7 @@ class Kwaylon(Client):
|
||||
)
|
||||
return
|
||||
|
||||
now = datetime.today().astimezone()
|
||||
df = self.data.read_emoji(emoji_name, con=con, after=(now - timedelta(days=days)))
|
||||
df = self.data.read_emoji(emoji_name, con=con, days=days)
|
||||
con.close()
|
||||
|
||||
if df.shape[0] > 0:
|
||||
|
||||
@@ -43,12 +43,13 @@ class ReactionData:
|
||||
# else:
|
||||
# LOGGER.info(f'Wrote {len(data)} rows to {self.path.name}')
|
||||
|
||||
def read_emoji(self, emoji: str, after: datetime = None,
|
||||
def read_emoji(self, emoji: str, days: int = None,
|
||||
con: sqlite3.Connection = None) -> pd.DataFrame:
|
||||
q = f"SELECT * FROM reactions WHERE emoji LIKE '{emoji}'"
|
||||
if after is not None:
|
||||
q += f" AND datetime >= '{after}'"
|
||||
return self.read_sql(query=q, con=con).sort_values('count', ascending=False)
|
||||
if days is not None:
|
||||
q += f" AND datetime >= datetime('now', '-{days} days')"
|
||||
q += ' ORDER BY count DESC'
|
||||
return self.read_sql(query=q, con=con)
|
||||
|
||||
def read_all(self, con: sqlite3.Connection = None) -> pd.DataFrame:
|
||||
return self.read_sql(query='SELECT * FROM reactions', con=con)
|
||||
@@ -64,11 +65,9 @@ class ReactionData:
|
||||
con.close()
|
||||
|
||||
res['datetime'] = pd.to_datetime(res['datetime'], utc=True)
|
||||
res['datetime'] = res['datetime'].dt.tz_convert(
|
||||
datetime.now().astimezone().tzinfo
|
||||
)
|
||||
res['datetime'] = res['datetime'].dt.tz_convert(datetime.now().astimezone().tzinfo)
|
||||
|
||||
return res.sort_values('datetime').reset_index(drop=True)
|
||||
return res.reset_index(drop=True)
|
||||
|
||||
def row_count(self, con: sqlite3.Connection = None) -> int:
|
||||
with con or self.connect() as con:
|
||||
|
||||
Reference in New Issue
Block a user