Files
Open-Assistant/backend/oasst_backend/models/user.py
T
James Melvin Ebenezer 063157355c 906: update user streaks (#1016)
* fix: update user streaks
* Moved  streak_last_day_date & streak_days from UserStats to User
* Updated Alembic version
* update last_activity after tm.handle_interaction()
* periodically executed function in main.py to update the user_streak every 4hrs

* fix:removed log messages

* fix: pre commit issues

* refactor: incorporated review comments

* fix: removed the managed -scope.

* refactor: managed_tx to Outer REST Db Ops in /interaction API

* fix: pre-commit changes

* fix: added streak and last_activity_date to protocol.FrontEndUser

* fix: pre-commit fixes after merge

* fix: added streak info to user_stat_repository, leaderboard and review comments

* fix: proper 4h delay and simpler startup_time initialisation

---------

Co-authored-by: James Melvin <melvin@gameface.ai>
2023-01-31 12:27:45 +01:00

59 lines
2.5 KiB
Python

from datetime import datetime
from typing import Optional
from uuid import UUID, uuid4
import sqlalchemy as sa
import sqlalchemy.dialects.postgresql as pg
from oasst_shared.schemas import protocol
from sqlmodel import AutoString, Field, Index, SQLModel
class User(SQLModel, table=True):
__tablename__ = "user"
__table_args__ = (
Index("ix_user_username", "api_client_id", "username", "auth_method", unique=True),
Index("ix_user_display_name_id", "display_name", "id", unique=True),
)
id: Optional[UUID] = Field(
sa_column=sa.Column(
pg.UUID(as_uuid=True), primary_key=True, default=uuid4, server_default=sa.text("gen_random_uuid()")
),
)
username: str = Field(nullable=False, max_length=128)
auth_method: str = Field(nullable=False, max_length=128, default="local")
display_name: str = Field(nullable=False, max_length=256)
created_date: Optional[datetime] = Field(
sa_column=sa.Column(sa.DateTime(timezone=True), nullable=False, server_default=sa.func.current_timestamp())
)
api_client_id: UUID = Field(foreign_key="api_client.id")
enabled: bool = Field(sa_column=sa.Column(sa.Boolean, nullable=False, server_default=sa.true()))
notes: str = Field(sa_column=sa.Column(AutoString(length=1024), nullable=False, server_default=""))
deleted: bool = Field(sa_column=sa.Column(sa.Boolean, nullable=False, server_default=sa.false()))
show_on_leaderboard: bool = Field(sa_column=sa.Column(sa.Boolean, nullable=False, server_default=sa.true()))
# only used for time span "total"
streak_last_day_date: Optional[datetime] = Field(
sa_column=sa.Column(sa.DateTime(timezone=True), nullable=True, server_default=sa.func.current_timestamp())
)
streak_days: Optional[int] = Field(nullable=True)
last_activity_date: Optional[datetime] = Field(
sa_column=sa.Column(sa.DateTime(timezone=True), nullable=True, server_default=sa.func.current_timestamp())
)
def to_protocol_frontend_user(self):
return protocol.FrontEndUser(
user_id=self.id,
id=self.username,
display_name=self.display_name,
auth_method=self.auth_method,
enabled=self.enabled,
deleted=self.deleted,
notes=self.notes,
created_date=self.created_date,
show_on_leaderboard=self.show_on_leaderboard,
streak_days=self.streak_days,
streak_last_day_date=self.streak_last_day_date,
last_activity_date=self.last_activity_date,
)