aboutsummaryrefslogtreecommitdiff
path: root/generalresearch/models/thl/report_task.py
blob: e4a8f48f75a756d6d0d5df86f201283d6497655c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations

import random
from collections import defaultdict
from collections.abc import Collection
from typing import TYPE_CHECKING

from pydantic import BaseModel, ConfigDict, Field

from generalresearch.models.thl.definitions import ReportValue

if TYPE_CHECKING:
    from generalresearch.models.thl.user import BPUIDStr

# If a report is made with multiple values, we'll take the one with the
# highest priority
REPORT_PRIORITY = defaultdict(
    lambda: 2,
    {
        ReportValue.REASON_UNKNOWN: 0,  # lowest priority
        ReportValue.TECHNICAL_ERROR: 1,  # next highest
        ReportValue.DIDNT_LIKE: 1,
    },
)


def prioritize_report_values(
    report_values: Collection[ReportValue],
) -> ReportValue | None:
    if not report_values:
        return None
    report_values = list(set(report_values))
    random.shuffle(report_values)
    return max(report_values, key=lambda x: REPORT_PRIORITY[x])


class ReportTask(BaseModel):
    model_config = ConfigDict(extra="forbid")

    bpuid: BPUIDStr = Field(
        title="product_user_id",
        description="The unique identifier for the user, which is set by the "
        "Supplier.",
        examples=["app-user-9329ebd"],
    )

    reasons: list[ReportValue] = Field(
        description=ReportValue.as_openapi_with_value_descriptions(),
        examples=[[3, 4]],
        default_factory=list,
    )

    notes: str = Field(
        default="", examples=["The survey wanted to watch me eat Haejang-guk"]
    )