diff --git a/backend/alembic/versions/2023_01_14_1509-619255ae9076_add_rank_to_message_table.py b/backend/alembic/versions/2023_01_14_1509-619255ae9076_add_rank_to_message_table.py new file mode 100644 index 00000000..ac8765f3 --- /dev/null +++ b/backend/alembic/versions/2023_01_14_1509-619255ae9076_add_rank_to_message_table.py @@ -0,0 +1,90 @@ +"""add rank to message table + +Revision ID: 619255ae9076 +Revises: bcc2fe18d214 +Create Date: 2023-01-14 15:09:03.462482 + +""" +import sqlalchemy as sa +import sqlmodel +from alembic import op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "619255ae9076" +down_revision = "bcc2fe18d214" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("message", sa.Column("rank", sa.Integer(), nullable=True)) + op.add_column("message_toxicity", sa.Column("score", sa.Float(), nullable=True)) + op.add_column("message_toxicity", sa.Column("label", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=False)) + op.drop_column("message_toxicity", "toxicity") + op.add_column("user_stats", sa.Column("time_frame", sqlmodel.sql.sqltypes.AutoString(), nullable=False)) + op.add_column("user_stats", sa.Column("prompts", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("replies_assistant", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("replies_prompter", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("labels_simple", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("labels_full", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("rankings_total", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("rankings_good", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("accepted_prompts", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("accepted_replies_assistant", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("accepted_replies_prompter", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_assistant_ranked_1", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_assistant_ranked_2", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_assistant_ranked_3", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_prompter_ranked_1", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_prompter_ranked_2", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("reply_prompter_ranked_3", sa.Integer(), nullable=False)) + op.add_column("user_stats", sa.Column("streak_last_day_date", sa.DateTime(), nullable=True)) + op.add_column("user_stats", sa.Column("streak_days", sa.Integer(), nullable=True)) + op.drop_column("user_stats", "messages") + op.drop_column("user_stats", "upvotes") + op.drop_column("user_stats", "task_reward") + op.drop_column("user_stats", "compare_wins") + op.drop_column("user_stats", "compare_losses") + op.drop_column("user_stats", "downvotes") + op.drop_column("user_stats", "reactions") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("user_stats", sa.Column("reactions", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("downvotes", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("compare_losses", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("compare_wins", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("task_reward", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("upvotes", sa.INTEGER(), autoincrement=False, nullable=False)) + op.add_column("user_stats", sa.Column("messages", sa.INTEGER(), autoincrement=False, nullable=False)) + op.drop_column("user_stats", "streak_days") + op.drop_column("user_stats", "streak_last_day_date") + op.drop_column("user_stats", "reply_prompter_ranked_3") + op.drop_column("user_stats", "reply_prompter_ranked_2") + op.drop_column("user_stats", "reply_prompter_ranked_1") + op.drop_column("user_stats", "reply_assistant_ranked_3") + op.drop_column("user_stats", "reply_assistant_ranked_2") + op.drop_column("user_stats", "reply_assistant_ranked_1") + op.drop_column("user_stats", "accepted_replies_prompter") + op.drop_column("user_stats", "accepted_replies_assistant") + op.drop_column("user_stats", "accepted_prompts") + op.drop_column("user_stats", "rankings_good") + op.drop_column("user_stats", "rankings_total") + op.drop_column("user_stats", "labels_full") + op.drop_column("user_stats", "labels_simple") + op.drop_column("user_stats", "replies_prompter") + op.drop_column("user_stats", "replies_assistant") + op.drop_column("user_stats", "prompts") + op.drop_column("user_stats", "time_frame") + op.add_column( + "message_toxicity", + sa.Column("toxicity", postgresql.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True), + ) + op.drop_column("message_toxicity", "label") + op.drop_column("message_toxicity", "score") + op.drop_column("message", "rank") + # ### end Alembic commands ### diff --git a/backend/oasst_backend/models/message.py b/backend/oasst_backend/models/message.py index 488656a5..9583510f 100644 --- a/backend/oasst_backend/models/message.py +++ b/backend/oasst_backend/models/message.py @@ -45,6 +45,8 @@ class Message(SQLModel, table=True): review_result: bool = Field(sa_column=sa.Column(sa.Boolean, default=False, server_default=false(), nullable=False)) ranking_count: int = Field(sa_column=sa.Column(sa.Integer, default=0, server_default=sa.text("0"), nullable=False)) + rank: Optional[int] = Field(nullable=True) + def ensure_is_message(self) -> None: if not self.payload or not isinstance(self.payload.payload, MessagePayload): raise OasstError("Invalid message", OasstErrorCode.INVALID_MESSAGE, HTTPStatus.INTERNAL_SERVER_ERROR) diff --git a/backend/oasst_backend/models/user_stats.py b/backend/oasst_backend/models/user_stats.py index b7b3231a..0bdaa3f4 100644 --- a/backend/oasst_backend/models/user_stats.py +++ b/backend/oasst_backend/models/user_stats.py @@ -1,4 +1,5 @@ from datetime import datetime +from enum import Enum from typing import Optional from uuid import UUID @@ -7,21 +8,46 @@ import sqlalchemy.dialects.postgresql as pg from sqlmodel import Field, SQLModel +class UserStatsTimeFrame(str, Enum): + day = "day" + week = "week" + month = "month" + total = "total" + + class UserStats(SQLModel, table=True): __tablename__ = "user_stats" user_id: Optional[UUID] = Field( sa_column=sa.Column(pg.UUID(as_uuid=True), sa.ForeignKey("user.id"), primary_key=True) ) + time_frame: Optional[str] = Field(nullable=False, primary_key=True) + leader_score: int = 0 modified_date: Optional[datetime] = Field( sa_column=sa.Column(sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()) ) - reactions: int = 0 # reactions sent by user - messages: int = 0 # messages sent by user - upvotes: int = 0 # received upvotes (form other users) - downvotes: int = 0 # received downvotes (from other users) - task_reward: int = 0 # reward for task completions - compare_wins: int = 0 # num times user's message won compare tasks - compare_losses: int = 0 # num times users's message lost compare tasks + prompts: int = 0 + replies_assistant: int = 0 + replies_prompter: int = 0 + labels_simple: int = 0 + labels_full: int = 0 + rankings_total: int = 0 + rankings_good: int = 0 + + accepted_prompts: int = 0 + accepted_replies_assistant: int = 0 + accepted_replies_prompter: int = 0 + + reply_assistant_ranked_1: int = 0 + reply_assistant_ranked_2: int = 0 + reply_assistant_ranked_3: int = 0 + + reply_prompter_ranked_1: int = 0 + reply_prompter_ranked_2: int = 0 + reply_prompter_ranked_3: int = 0 + + # only used for time span "total" + streak_last_day_date: Optional[datetime] = Field(nullable=True) + streak_days: Optional[int] = Field(nullable=True)