-
Notifications
You must be signed in to change notification settings - Fork 0
parse Submissions data from pretalx #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
""" | ||
Basic analysis of Pretalx Submissions data | ||
""" | ||
|
||
from datetime import datetime | ||
from typing import ClassVar, Iterable | ||
|
||
import plotly.express as px | ||
import polars as pl | ||
from core.models import PretalxData | ||
from pydantic import BaseModel, model_validator | ||
|
||
|
||
class LocalisedFieldsMixin: | ||
# Marking as ClassVar here is important. It doens't work without it :) | ||
_localised_fields: ClassVar[Iterable[str]] = () | ||
|
||
@model_validator(mode="before") | ||
@classmethod | ||
def extract(cls, values): | ||
for field in cls._localised_fields: | ||
if isinstance(values[field], dict) and "en" in values[field]: | ||
values[field] = values[field]["en"] | ||
continue | ||
|
||
return values | ||
|
||
|
||
class Submission(LocalisedFieldsMixin, BaseModel): | ||
code: str | ||
title: str | ||
submission_type: str | dict | ||
track: str | None | ||
state: str | ||
abstract: str | ||
duration: int | ||
created: datetime | ||
level: str = "" | ||
outline: str | None = None | ||
event: str = "ep2025" | ||
|
||
_localised_fields = ["submission_type", "track"] | ||
|
||
class Questions: | ||
level = "Expected audience expertise" | ||
outline = "Outline" | ||
|
||
@model_validator(mode="before") | ||
def extract_answers(cls, values): | ||
# Some things are available as answers to questions and we can extract | ||
# them here | ||
# But using .get since this should be optional for creating objects | ||
# manually | ||
for answer in values.get("answers", ""): | ||
if answer["submission"] is not None and cls.question_is( | ||
answer, cls.Questions.level | ||
): | ||
values["level"] = answer["answer"] | ||
|
||
if answer["submission"] is not None and cls.question_is( | ||
answer, cls.Questions.outline | ||
): | ||
values["outline"] = answer["answer"] | ||
|
||
return values | ||
|
||
@staticmethod | ||
def question_is(answer: dict, question: str) -> bool: | ||
return answer.get("question", {}).get("question", {}).get("en") == question | ||
clytaemnestra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_latest_submissions_data() -> PretalxData: | ||
qs = PretalxData.objects.filter(resource=PretalxData.PretalxResources.submissions) | ||
return qs.latest("created_at") | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def parse_latest_submissions_to_objects(pretalx_data: PretalxData) -> list[Submission]: | ||
data = pretalx_data.content | ||
# NOTE: add event as context here | ||
submissions = [Submission.model_validate(entry) for entry in data] | ||
return submissions | ||
|
||
|
||
def flat_submissions_data(submissions: list[Submission]) -> pl.DataFrame: | ||
""" | ||
Returns a polars data frame with flat description of Submissions | ||
|
||
This functions mostly exists to make the API consistent between different | ||
types of data | ||
""" | ||
return pl.DataFrame(submissions) | ||
|
||
|
||
def latest_flat_submissions_data() -> pl.DataFrame: | ||
""" | ||
Thin wrapper on getting latest information from the database, and | ||
converting into a polars data frame | ||
""" | ||
pretalx_data = get_latest_submissions_data() | ||
submissions = parse_latest_submissions_to_objects(pretalx_data) | ||
return flat_submissions_data(submissions) | ||
|
||
|
||
def group_submissions_by_state(submissions: pl.DataFrame) -> pl.DataFrame: | ||
by_state = submissions.group_by("state").len().sort("len", descending=True) | ||
|
||
return by_state | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def piechart_submissions_by_state(submissions_by_state: pl.DataFrame): | ||
fig = px.pie( | ||
submissions_by_state, | ||
values="len", | ||
names="state", | ||
title="state of the submission", | ||
color_discrete_sequence=px.colors.qualitative.Pastel, | ||
) | ||
|
||
return fig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
<html> | ||
<body> | ||
<style type="text/css"> | ||
body { background-color: #ffe; color: #151f38; font-family: sans-serif;} | ||
h1 { font-size: 3em; text-align: center; margin-top: 3em; } | ||
thead { background: #aaa; } | ||
table { font-family: monospace; width: 100%} | ||
table td { padding: .5em 1em; } | ||
table tr:nth-child(2n) td { background: #eee; } | ||
table tr:hover td { background: #fee; } | ||
</style> | ||
<div style="margin: 3em"> | ||
|
||
<h1>Submissions</h1> | ||
|
||
<div id="piechart"> | ||
{{ piechart }} | ||
</div> | ||
<p style="text-align: center">This is not particularly useful now, but it's here as a proof of concept :)</p> | ||
|
||
<table> | ||
<thead> | ||
{% for column in columns %} | ||
<td>{{ column }}</td> | ||
{% endfor %} | ||
</thead> | ||
|
||
{% for row in rows %} | ||
<tr> | ||
{% for cell in row %} | ||
<td>{{ cell }}</td> | ||
{% endfor %} | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
|
||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.