|
| 1 | +""" |
| 2 | +Basic analysis of Pretalx Submissions data |
| 3 | +""" |
| 4 | + |
| 5 | +from datetime import datetime |
| 6 | +from typing import ClassVar, Iterable |
| 7 | + |
| 8 | +import plotly.express as px |
| 9 | +import polars as pl |
| 10 | +from core.models import PretalxData |
| 11 | +from pydantic import BaseModel, model_validator |
| 12 | + |
| 13 | + |
| 14 | +class LocalisedFieldsMixin: |
| 15 | + # Marking as ClassVar here is important. It doens't work without it :) |
| 16 | + _localised_fields: ClassVar[Iterable[str]] = () |
| 17 | + |
| 18 | + @model_validator(mode="before") |
| 19 | + @classmethod |
| 20 | + def extract(cls, values): |
| 21 | + for field in cls._localised_fields: |
| 22 | + if isinstance(values[field], dict) and "en" in values[field]: |
| 23 | + values[field] = values[field]["en"] |
| 24 | + continue |
| 25 | + |
| 26 | + return values |
| 27 | + |
| 28 | + |
| 29 | +class Submission(LocalisedFieldsMixin, BaseModel): |
| 30 | + code: str |
| 31 | + title: str |
| 32 | + submission_type: str | dict |
| 33 | + track: str | None |
| 34 | + state: str |
| 35 | + abstract: str |
| 36 | + duration: int |
| 37 | + created: datetime |
| 38 | + level: str = "" |
| 39 | + outline: str | None = None |
| 40 | + event: str = "ep2025" |
| 41 | + |
| 42 | + _localised_fields = ["submission_type", "track"] |
| 43 | + |
| 44 | + class Questions: |
| 45 | + level = "Expected audience expertise" |
| 46 | + outline = "Outline" |
| 47 | + |
| 48 | + @model_validator(mode="before") |
| 49 | + def extract_answers(cls, values): |
| 50 | + # Some things are available as answers to questions and we can extract |
| 51 | + # them here |
| 52 | + # But using .get since this should be optional for creating objects |
| 53 | + # manually |
| 54 | + for answer in values.get("answers", ""): |
| 55 | + if answer["submission"] is not None and cls.question_is( |
| 56 | + answer, cls.Questions.level |
| 57 | + ): |
| 58 | + values["level"] = answer["answer"] |
| 59 | + |
| 60 | + if answer["submission"] is not None and cls.question_is( |
| 61 | + answer, cls.Questions.outline |
| 62 | + ): |
| 63 | + values["outline"] = answer["answer"] |
| 64 | + |
| 65 | + return values |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def question_is(answer: dict, question: str) -> bool: |
| 69 | + return answer.get("question", {}).get("question", {}).get("en") == question |
| 70 | + |
| 71 | + |
| 72 | +def get_latest_submissions_data() -> PretalxData: |
| 73 | + qs = PretalxData.objects.filter(resource=PretalxData.PretalxResources.submissions) |
| 74 | + return qs.latest("created_at") |
| 75 | + |
| 76 | + |
| 77 | +def parse_latest_submissions_to_objects(pretalx_data: PretalxData) -> list[Submission]: |
| 78 | + data = pretalx_data.content |
| 79 | + # NOTE: add event as context here |
| 80 | + submissions = [Submission.model_validate(entry) for entry in data] |
| 81 | + return submissions |
| 82 | + |
| 83 | + |
| 84 | +def flat_submissions_data(submissions: list[Submission]) -> pl.DataFrame: |
| 85 | + """ |
| 86 | + Returns a polars data frame with flat description of Submissions |
| 87 | +
|
| 88 | + This functions mostly exists to make the API consistent between different |
| 89 | + types of data |
| 90 | + """ |
| 91 | + return pl.DataFrame(submissions) |
| 92 | + |
| 93 | + |
| 94 | +def latest_flat_submissions_data() -> pl.DataFrame: |
| 95 | + """ |
| 96 | + Thin wrapper on getting latest information from the database, and |
| 97 | + converting into a polars data frame |
| 98 | + """ |
| 99 | + pretalx_data = get_latest_submissions_data() |
| 100 | + submissions = parse_latest_submissions_to_objects(pretalx_data) |
| 101 | + return flat_submissions_data(submissions) |
| 102 | + |
| 103 | + |
| 104 | +def group_submissions_by_state(submissions: pl.DataFrame) -> pl.DataFrame: |
| 105 | + by_state = submissions.group_by("state").len().sort("len", descending=True) |
| 106 | + |
| 107 | + return by_state |
| 108 | + |
| 109 | + |
| 110 | +def piechart_submissions_by_state(submissions_by_state: pl.DataFrame): |
| 111 | + fig = px.pie( |
| 112 | + submissions_by_state, |
| 113 | + values="len", |
| 114 | + names="state", |
| 115 | + title="state of the submission", |
| 116 | + color_discrete_sequence=px.colors.qualitative.Pastel, |
| 117 | + ) |
| 118 | + |
| 119 | + return fig |
0 commit comments