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