🏦 Database Change: convert created utc to datetimez for volunteer_janitor (#679)

This commit is contained in:
Viet Than 2023-08-25 01:00:09 -05:00 committed by GitHub
parent 3187a372fe
commit 4f8ba42738
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 11 deletions

View file

@ -72,6 +72,8 @@ class CreatedDateTimeBase(Base):
Declare default column for classes/tables inheriting `CreatedDateTimeBase`.
Retrieving `created_datetimez` will return a `datetime` object with `tzinfo` for UTC.
New rows have a default created_datetimez of the database server's `now()`
"""
return Column(DateTime(timezone=True), nullable=False, server_default=now())

View file

@ -3,6 +3,7 @@ import enum
from files.classes.base import Base
from sqlalchemy import *
from sqlalchemy.orm import relationship
from sqlalchemy.sql.functions import now
class VolunteerJanitorResult(enum.Enum):
Pending = 0
@ -19,7 +20,8 @@ class VolunteerJanitorRecord(Base):
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
comment_id = Column(Integer, ForeignKey("comments.id"), nullable=False)
recorded_utc = Column(DateTime, default=0, nullable=False)
recorded_datetimez = Column(DateTime(timezone=True), default=0, nullable=False, server_default=now())
"""new rows have a default recorded_datetimez of the database server's `now()`"""
result = Column(Enum(VolunteerJanitorResult), default=VolunteerJanitorResult.Pending, nullable=False)
Index('volunteer_comment_index', user_id, comment_id)

View file

@ -10,16 +10,16 @@ def volunteer_janitor_histogram_cmd():
import pandas as pd
import matplotlib.pyplot as plt
result_set = db_session().query(VolunteerJanitorRecord.recorded_utc).all()
result_set = db_session().query(VolunteerJanitorRecord.recorded_datetimez).all()
# convert the result into a pandas DataFrame
df = pd.DataFrame(result_set, columns=['recorded_utc'])
df = pd.DataFrame(result_set, columns=['recorded_datetimez'])
# convert the date column to datetime
df['recorded_utc'] = pd.to_datetime(df['recorded_utc'])
df['recorded_datetimez'] = pd.to_datetime(df['recorded_datetimez'])
# set 'recorded_utc' as the index of the DataFrame
df.set_index('recorded_utc', inplace=True)
# set 'recorded_datetimez' as the index of the DataFrame
df.set_index('recorded_datetimez', inplace=True)
# resample the data to daily frequency
df_resampled = df.resample('D').size()

View file

@ -24,7 +24,7 @@ CONFIG_default_user_accuracy = 0.2
CONFIG_user_correctness_lerp = 0.2
def _compile_records(db):
vrecords = db.query(VolunteerJanitorRecord).order_by(VolunteerJanitorRecord.recorded_utc).all()
vrecords = db.query(VolunteerJanitorRecord).order_by(VolunteerJanitorRecord.recorded_datetimez).all()
# get the info we need for all mentioned posts
reported_comment_ids = {record.comment_id for record in vrecords}

View file

@ -51,7 +51,7 @@ def update_comment_badness(db, cid, diagnostics: bool = False):
# This probably does more SQL queries than it should
records = db.query(VolunteerJanitorRecord) \
.where(VolunteerJanitorRecord.comment_id == cid) \
.order_by(VolunteerJanitorRecord.recorded_utc)
.order_by(VolunteerJanitorRecord.recorded_datetimez)
user_has_pending = {}
earliest_submission = {}
@ -61,7 +61,7 @@ def update_comment_badness(db, cid, diagnostics: bool = False):
user_has_pending[rec.user_id] = True
else:
if rec.user_id in user_has_pending:
if rec.user_id not in earliest_submission or earliest_submission[rec.user_id].recorded_utc > rec.recorded_utc:
if rec.user_id not in earliest_submission or earliest_submission[rec.user_id].recorded_datetimez > rec.recorded_datetimez:
earliest_submission[rec.user_id] = rec
badness = 0

View file

@ -24,7 +24,6 @@ class VolunteerDutyJanitor(VolunteerDuty):
record = VolunteerJanitorRecord()
record.user_id = v.id
record.comment_id = item
record.recorded_utc = sqlalchemy.func.now()
record.result = VolunteerJanitorResult.Pending
g.db.add(record)
@ -96,7 +95,6 @@ def submitted(v: User, key: str, val: str) -> None:
record = VolunteerJanitorRecord()
record.user_id = v.id
record.comment_id = key
record.recorded_utc = sqlalchemy.func.now()
record.result = VolunteerJanitorResult(int(val))
g.db.add(record)