studysystem 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- studysystem/__init__.py +1 -0
- studysystem/assets/note-callouts.lua +100 -0
- studysystem/assets/note-no-html.lua +28 -0
- studysystem/assets/note.css +146 -0
- studysystem/cli.py +344 -0
- studysystem/dashboard.html +781 -0
- studysystem/dashboard.py +377 -0
- studysystem/db/__init__.py +1 -0
- studysystem/db/engine.py +158 -0
- studysystem/db/migrate.py +122 -0
- studysystem/db/migrations/env.py +59 -0
- studysystem/db/migrations/script.py.mako +29 -0
- studysystem/db/migrations/versions/0000_baseline.py +19 -0
- studysystem/db/migrations/versions/0001_the_whole_schema.py +1743 -0
- studysystem/db/migrations/versions/0002_semester_ignores_case.py +38 -0
- studysystem/db/migrations/versions/0003_course_vault_folder.py +26 -0
- studysystem/db/migrations/versions/0004_note_pdf_path.py +26 -0
- studysystem/db/migrations/versions/0005_tagging_kind.py +39 -0
- studysystem/db/migrations/versions/0006_practice_mode.py +36 -0
- studysystem/db/migrations/versions/0007_exam_prep_from.py +25 -0
- studysystem/db/migrations/versions/0008_more_papers_kind.py +37 -0
- studysystem/db/migrations/versions/0009_past_exam_month_date.py +28 -0
- studysystem/db/migrations/versions/0010_task_parts.py +70 -0
- studysystem/db/migrations/versions/0011_observation_kind.py +39 -0
- studysystem/db/migrations/versions/0012_profile_claim.py +52 -0
- studysystem/db/migrations/versions/0013_assumed_instructor_papers.py +40 -0
- studysystem/db/migrations/versions/0014_scan_confirmation.py +44 -0
- studysystem/db/migrations/versions/0015_practice_item_count.py +37 -0
- studysystem/db/migrations/versions/0016_timetable_patterns.py +102 -0
- studysystem/db/migrations/versions/0017_course_dropped.py +36 -0
- studysystem/db/migrations/versions/0018_syllabus_topics.py +82 -0
- studysystem/db/migrations/versions/0019_course_lab_group.py +36 -0
- studysystem/db/migrations/versions/0020_chapter_scope.py +61 -0
- studysystem/db/migrations/versions/0021_note_retired_at.py +28 -0
- studysystem/db/migrations/versions/0022_rescope.py +48 -0
- studysystem/db/migrations/versions/0023_backlog_reason.py +42 -0
- studysystem/db/migrations/versions/0024_guess_sensitivity.py +35 -0
- studysystem/db/migrations/versions/0025_shortfall_inputs.py +83 -0
- studysystem/db/migrations/versions/0026_trials_switch.py +36 -0
- studysystem/db/tables.py +1096 -0
- studysystem/demo.py +722 -0
- studysystem/errors.py +39 -0
- studysystem/server/__init__.py +83 -0
- studysystem/server/__main__.py +5 -0
- studysystem/services/__init__.py +1 -0
- studysystem/services/adherence.py +147 -0
- studysystem/services/assessments.py +171 -0
- studysystem/services/capacity.py +164 -0
- studysystem/services/claims.py +405 -0
- studysystem/services/course_folder.py +82 -0
- studysystem/services/courses.py +176 -0
- studysystem/services/coverage.py +121 -0
- studysystem/services/dashboard_key.py +102 -0
- studysystem/services/feedback.py +412 -0
- studysystem/services/forward_eval.py +65 -0
- studysystem/services/gate.py +77 -0
- studysystem/services/generation.py +2864 -0
- studysystem/services/ids.py +15 -0
- studysystem/services/intake.py +278 -0
- studysystem/services/launcher.py +455 -0
- studysystem/services/lookups.py +91 -0
- studysystem/services/materials.py +97 -0
- studysystem/services/note_markdown.py +241 -0
- studysystem/services/notes.py +396 -0
- studysystem/services/parts.py +65 -0
- studysystem/services/past_exams.py +115 -0
- studysystem/services/pdf_render.py +148 -0
- studysystem/services/pdf_worker.py +184 -0
- studysystem/services/plan.py +1300 -0
- studysystem/services/private_view.py +38 -0
- studysystem/services/profiles.py +544 -0
- studysystem/services/programs.py +16 -0
- studysystem/services/replacements.py +107 -0
- studysystem/services/rescope.py +277 -0
- studysystem/services/settings.py +50 -0
- studysystem/services/setup.py +235 -0
- studysystem/services/shortfall.py +443 -0
- studysystem/services/state.py +846 -0
- studysystem/services/statements.py +222 -0
- studysystem/services/study_loop.py +998 -0
- studysystem/services/summaries.py +369 -0
- studysystem/services/syllabus.py +138 -0
- studysystem/services/system_work.py +113 -0
- studysystem/services/taxonomy.py +459 -0
- studysystem/services/topics.py +477 -0
- studysystem/services/trials.py +201 -0
- studysystem/services/unconfirmed.py +95 -0
- studysystem/services/units.py +34 -0
- studysystem/services/users.py +53 -0
- studysystem/services/values.py +168 -0
- studysystem/startup.py +147 -0
- studysystem/tools/__init__.py +4 -0
- studysystem/tools/course_setup.py +185 -0
- studysystem/tools/dashboard.py +27 -0
- studysystem/tools/feedback.py +66 -0
- studysystem/tools/gate.py +24 -0
- studysystem/tools/generation.py +156 -0
- studysystem/tools/materials.py +32 -0
- studysystem/tools/notes.py +29 -0
- studysystem/tools/past_exams.py +51 -0
- studysystem/tools/ping.py +9 -0
- studysystem/tools/plan.py +40 -0
- studysystem/tools/profiles.py +94 -0
- studysystem/tools/results.py +77 -0
- studysystem/tools/study_loop.py +145 -0
- studysystem/tools/summaries.py +52 -0
- studysystem/tools/topics.py +143 -0
- studysystem-1.0.0.dist-info/METADATA +254 -0
- studysystem-1.0.0.dist-info/RECORD +112 -0
- studysystem-1.0.0.dist-info/WHEEL +4 -0
- studysystem-1.0.0.dist-info/entry_points.txt +3 -0
- studysystem-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
"""The shortfall detector and the three levers (4.9; D-15, D-292 - D-300).
|
|
2
|
+
|
|
3
|
+
Hours-to-all-targets against declared capacity, read on demand - nothing is stored (no offer
|
|
4
|
+
table). SQL gathers the rows (`_gather`); pure functions do the arithmetic and build the alert,
|
|
5
|
+
so the math is tested without a database, as in plan.py. Only `respond_to_offer` writes.
|
|
6
|
+
|
|
7
|
+
Who reads it: `GET /state` (its `shortfall` section) and the `study_get_plan` reply, so the
|
|
8
|
+
dashboard and the host both see the alert. He answers it through a tool - nothing here changes
|
|
9
|
+
his capacity, targets or strategy (D-15, D-299).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import datetime
|
|
13
|
+
from collections.abc import Sequence
|
|
14
|
+
|
|
15
|
+
from sqlalchemy import Connection, Engine, and_, case, func, select, update
|
|
16
|
+
|
|
17
|
+
from studysystem.db.tables import assessment, assessment_slot, capacity, course, hours_entry
|
|
18
|
+
from studysystem.errors import StudyError
|
|
19
|
+
from studysystem.services.ids import now
|
|
20
|
+
from studysystem.services.lookups import find_course
|
|
21
|
+
from studysystem.services.units import write_unit
|
|
22
|
+
from studysystem.services.values import invalid
|
|
23
|
+
|
|
24
|
+
H_DEFAULT = 6.0 # hours per credit for a 75% target, inferred: ~2 h a week per 4-credit (D-294)
|
|
25
|
+
PASS_MARK = 50.0 # a conceded course's need is read at the pass mark (D-299)
|
|
26
|
+
PACE_DAYS = 14 # the pace is read over the last 14 days (D-298)
|
|
27
|
+
LOWER_BY = 10 # the target lever's hours are shown for a target this many points lower (D-300)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# --- the gather (SQL) --------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _gather(conn: Connection, user_id: str, today: datetime.date) -> dict:
|
|
34
|
+
"""Everything the detector reads, as plain values, from one moment. Writes nothing.
|
|
35
|
+
|
|
36
|
+
Returns {
|
|
37
|
+
"courses": [one dict per course in play - planned and not dropped (D-216, D-217) - with
|
|
38
|
+
course_id, code, credits, target_grade, conceded (bool), strategy,
|
|
39
|
+
hours_per_credit, hours_per_credit_tier, elective (bool),
|
|
40
|
+
minutes (all logged, not voided), recent_minutes (logged in the last
|
|
41
|
+
PACE_DAYS days, not voided)],
|
|
42
|
+
"capacity": [(from_date, hours_per_week), ...] every capacity row, oldest first, ties by
|
|
43
|
+
id - the later id wins a tie (the capacity rule),
|
|
44
|
+
"last_exam": the latest upcoming exam date (a date) among the courses in play, or None,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
"The last 14 days" compares `occurred_at` against the UTC midnight 13 days before `today`,
|
|
48
|
+
so today and the 13 days before it count.
|
|
49
|
+
"""
|
|
50
|
+
since = (today - datetime.timedelta(days=PACE_DAYS - 1)).isoformat()
|
|
51
|
+
logged = (
|
|
52
|
+
select(
|
|
53
|
+
hours_entry.c.course_id,
|
|
54
|
+
func.sum(hours_entry.c.minutes).label("minutes"),
|
|
55
|
+
func.sum(
|
|
56
|
+
case((hours_entry.c.occurred_at >= since, hours_entry.c.minutes), else_=0)
|
|
57
|
+
).label("recent_minutes"),
|
|
58
|
+
)
|
|
59
|
+
.where(hours_entry.c.user_id == user_id, hours_entry.c.voided_at.is_(None))
|
|
60
|
+
.group_by(hours_entry.c.course_id)
|
|
61
|
+
.subquery()
|
|
62
|
+
)
|
|
63
|
+
rows = conn.execute(
|
|
64
|
+
select(
|
|
65
|
+
course.c.id.label("course_id"),
|
|
66
|
+
course.c.code,
|
|
67
|
+
course.c.credits,
|
|
68
|
+
course.c.target_grade,
|
|
69
|
+
course.c.conceded,
|
|
70
|
+
course.c.strategy,
|
|
71
|
+
course.c.hours_per_credit,
|
|
72
|
+
course.c.hours_per_credit_tier,
|
|
73
|
+
course.c.elective,
|
|
74
|
+
func.coalesce(logged.c.minutes, 0).label("minutes"),
|
|
75
|
+
func.coalesce(logged.c.recent_minutes, 0).label("recent_minutes"),
|
|
76
|
+
)
|
|
77
|
+
.join_from(course, logged, logged.c.course_id == course.c.id, isouter=True)
|
|
78
|
+
.where(
|
|
79
|
+
course.c.user_id == user_id,
|
|
80
|
+
course.c.planned == 1,
|
|
81
|
+
course.c.dropped_at.is_(None),
|
|
82
|
+
)
|
|
83
|
+
.order_by(course.c.code)
|
|
84
|
+
).mappings()
|
|
85
|
+
courses = [
|
|
86
|
+
dict(row, conceded=bool(row["conceded"]), elective=bool(row["elective"])) for row in rows
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
capacity_rows = conn.execute(
|
|
90
|
+
select(capacity.c.from_date, capacity.c.hours_per_week)
|
|
91
|
+
.where(capacity.c.user_id == user_id)
|
|
92
|
+
.order_by(capacity.c.from_date, capacity.c.id)
|
|
93
|
+
).all()
|
|
94
|
+
|
|
95
|
+
last = conn.execute(
|
|
96
|
+
select(func.max(assessment.c.date))
|
|
97
|
+
.join_from(assessment, assessment_slot, assessment_slot.c.id == assessment.c.slot_id)
|
|
98
|
+
.join(course, course.c.id == assessment_slot.c.course_id)
|
|
99
|
+
.where(
|
|
100
|
+
and_(
|
|
101
|
+
assessment.c.user_id == user_id,
|
|
102
|
+
assessment_slot.c.kind == "exam",
|
|
103
|
+
assessment.c.date >= today.isoformat(),
|
|
104
|
+
course.c.planned == 1,
|
|
105
|
+
course.c.dropped_at.is_(None),
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
).scalar()
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
"courses": courses,
|
|
112
|
+
"capacity": [(row.from_date, row.hours_per_week) for row in capacity_rows],
|
|
113
|
+
"last_exam": datetime.date.fromisoformat(last) if last else None,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# --- the core (pure) --------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def course_need(
|
|
121
|
+
credits: float | None,
|
|
122
|
+
hours_per_credit: float,
|
|
123
|
+
target_grade: float | None,
|
|
124
|
+
conceded: bool,
|
|
125
|
+
minutes_logged: int,
|
|
126
|
+
) -> float | None:
|
|
127
|
+
"""One course's hours still needed to reach its target, from today (D-292). Pure.
|
|
128
|
+
|
|
129
|
+
need = credits x hours_per_credit x target / 100 - minutes_logged / 60, never below 0.
|
|
130
|
+
A conceded course reads target PASS_MARK, whatever its declared target (D-299).
|
|
131
|
+
None when credits or target_grade is unknown: the course is left out and named, never
|
|
132
|
+
counted at a guess (unknown stays unknown).
|
|
133
|
+
|
|
134
|
+
Examples:
|
|
135
|
+
- I3302: 4 credits, H 6, target 75, 70 min logged -> 18 - 1.17 = 16.83
|
|
136
|
+
- I3351: 5 credits, H 6, target 75, 0 logged -> 22.5
|
|
137
|
+
- I3351 with no target grade -> None
|
|
138
|
+
- I3304 conceded: 4, H 6, target 75 (read at 50), 235 -> 12 - 3.92 = 8.08
|
|
139
|
+
- DHR300 at its own H 2.7: 3, 2.7, 75, 0 -> 6.08
|
|
140
|
+
- a course logged past its need: 3, 6, 75, 1000 min -> 0.0, not -3.17
|
|
141
|
+
- conceded with a declared target under the pass mark: 3, 6, 40 -> read at 40, never raised
|
|
142
|
+
"""
|
|
143
|
+
if conceded:
|
|
144
|
+
# concession lowers a target to the pass mark - it never raises one already below it
|
|
145
|
+
target = PASS_MARK if target_grade is None else min(target_grade, PASS_MARK)
|
|
146
|
+
else:
|
|
147
|
+
target = target_grade
|
|
148
|
+
if credits is None or target is None:
|
|
149
|
+
return None
|
|
150
|
+
return max(credits * hours_per_credit * target / 100 - minutes_logged / 60, 0.0)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def capacity_until(
|
|
154
|
+
rows: Sequence[tuple[str, float]], today: datetime.date, last_exam: datetime.date
|
|
155
|
+
) -> float:
|
|
156
|
+
"""The hours his declared capacity gives from `today` up to `last_exam` (D-293). Pure.
|
|
157
|
+
|
|
158
|
+
Each day from today (counted) up to the exam day (not counted) gets the hours_per_week / 7
|
|
159
|
+
of the row in force that day: the latest from_date on or before it. Rows may come in any
|
|
160
|
+
order; on a tie the later row in the list wins (`_gather` orders ties by id, the capacity
|
|
161
|
+
rule). A day before every row gives 0.
|
|
162
|
+
|
|
163
|
+
Examples - his rows: ("2026-09-27", 10), ("2026-11-02", 14.17), ("2026-12-24", 21),
|
|
164
|
+
("2027-01-04", 10):
|
|
165
|
+
- today 2026-11-15, last exam 2027-01-18 -> 39 x 14.17/7 + 11 x 21/7 + 14 x 10/7 = 131.95
|
|
166
|
+
- today 2026-11-15, last exam 2026-12-07 -> 22 x 14.17/7 = 44.53
|
|
167
|
+
- a row ("2026-11-15", 8) added on the copy -> 39 x 8/7 + 33 + 20 = 97.57
|
|
168
|
+
- today on the exam day -> 0.0
|
|
169
|
+
"""
|
|
170
|
+
total = 0.0
|
|
171
|
+
day = today
|
|
172
|
+
while day < last_exam:
|
|
173
|
+
stamp = day.isoformat()
|
|
174
|
+
# the row with the latest start on or before this day; `>=` lets a later tie win
|
|
175
|
+
in_force = None
|
|
176
|
+
for from_date, hours in rows:
|
|
177
|
+
if from_date <= stamp and (in_force is None or from_date >= in_force[0]):
|
|
178
|
+
in_force = (from_date, hours)
|
|
179
|
+
if in_force is not None:
|
|
180
|
+
total += in_force[1] / 7
|
|
181
|
+
day += datetime.timedelta(days=1)
|
|
182
|
+
return total
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def behind_by(need: float, days_left: int, recent_minutes: int) -> float:
|
|
186
|
+
"""How far a course's last PACE_DAYS days fall under the pace of its need (D-295, D-298).
|
|
187
|
+
Pure. Positive = behind by that many hours; 0 or below = on pace or ahead.
|
|
188
|
+
|
|
189
|
+
pace per day = need / days_left; behind = pace x PACE_DAYS - recent_minutes / 60.
|
|
190
|
+
`days_left` is from today to the last exam, at least 1.
|
|
191
|
+
|
|
192
|
+
Examples, 64 days to 2027-01-18:
|
|
193
|
+
- I3351: need 22.5, 0 recent minutes -> 22.5 / 64 x 14 = 4.92
|
|
194
|
+
- I3304: need 14.08, 100 recent minutes -> 3.08 - 1.67 = 1.41
|
|
195
|
+
- I3303: need 15.23, 166 recent minutes -> 3.33 - 2.77 = 0.56
|
|
196
|
+
- need 0 (target reached) -> 0 - recent / 60, never behind
|
|
197
|
+
"""
|
|
198
|
+
pace_per_day = need / max(days_left, 1)
|
|
199
|
+
return pace_per_day * PACE_DAYS - recent_minutes / 60
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def shortfall_of(gathered: dict, today: datetime.date) -> dict:
|
|
203
|
+
"""The alert: need against capacity, who is behind, and the ladder of offers (D-15). Pure:
|
|
204
|
+
`gathered` is `_gather`'s dict.
|
|
205
|
+
|
|
206
|
+
Returns {
|
|
207
|
+
"need": total hours of the counted courses, "need_tier": "inferred",
|
|
208
|
+
"capacity": capacity_until(...), "capacity_tier": "declared",
|
|
209
|
+
"gap": capacity - need (below 0 = short),
|
|
210
|
+
"short": gap < 0,
|
|
211
|
+
"not_counted": [{"code", "reason"}] - no target grade, or no credits,
|
|
212
|
+
"courses": [per counted course: code, credits, need, h (its H) + h_tier ("declared",
|
|
213
|
+
or "inferred" for the default), behind (hours, from behind_by), strategy,
|
|
214
|
+
conceded, elective, frees_conceding (hours conceding frees: 0 when already
|
|
215
|
+
conceded), frees_lowering (hours a target LOWER_BY points lower frees: 0 when
|
|
216
|
+
conceded - its need already reads at the pass mark)],
|
|
217
|
+
listed by credits descending, then code, electives last (D-300),
|
|
218
|
+
"offers": the ladder, in order, each {"kind", "tool", "hours"} (D-15, D-295, D-296):
|
|
219
|
+
1. {"kind": "catch-up", "tool": "study_respond_to_offer", "code", "hours": how far
|
|
220
|
+
behind} for the course furthest behind (most hours) that is behind (> 0) and not
|
|
221
|
+
on catch-up already; none when nobody is behind. Offered whether or not the total
|
|
222
|
+
is short: one course behind is the first rung.
|
|
223
|
+
2-4. only when short, "hours" = the gap as a positive number:
|
|
224
|
+
{"kind": "lower-target", "tool": "study_set_course_input"},
|
|
225
|
+
{"kind": "add-capacity", "tool": "study_set_capacity"},
|
|
226
|
+
{"kind": "concede", "tool": "study_respond_to_offer"}.
|
|
227
|
+
}
|
|
228
|
+
With no upcoming exam (`last_exam` None) there is no window: need and the list still read,
|
|
229
|
+
capacity, gap and short are None with tier unknown, and no offer is made.
|
|
230
|
+
|
|
231
|
+
Examples, H 6 (DHR300 at its own 2.7), I3351 not yet given a target:
|
|
232
|
+
- need 93.22 (I3351 not counted: no target grade), capacity 131.95, gap +38.73, short false;
|
|
233
|
+
offers: catch-up for the furthest behind (I3301, 3.8 - 0.67 = 3.13), nothing more.
|
|
234
|
+
- the Done-when copy: I3351 at 5 credits / 75, a capacity row ("2026-11-15", 8) -> need
|
|
235
|
+
115.72, capacity 97.57, gap -18.15, short true; offers: catch-up I3351 (4.92), then the
|
|
236
|
+
three levers; DHR300 (elective, H 2.7) ends the course list.
|
|
237
|
+
"""
|
|
238
|
+
last_exam = gathered["last_exam"]
|
|
239
|
+
days_left = (last_exam - today).days if last_exam is not None else None
|
|
240
|
+
|
|
241
|
+
courses, not_counted = [], []
|
|
242
|
+
for row in gathered["courses"]:
|
|
243
|
+
entry = _course_entry(row, days_left)
|
|
244
|
+
if entry is None:
|
|
245
|
+
# every input it lacks; a conceded course reads at 50, so it never lacks a target
|
|
246
|
+
missing = []
|
|
247
|
+
if row["credits"] is None:
|
|
248
|
+
missing.append("no credits")
|
|
249
|
+
if row["target_grade"] is None and not row["conceded"]:
|
|
250
|
+
missing.append("no target grade")
|
|
251
|
+
not_counted.append({"code": row["code"], "reason": " and ".join(missing)})
|
|
252
|
+
else:
|
|
253
|
+
courses.append(entry)
|
|
254
|
+
courses.sort(key=lambda c: (c["elective"], -c["credits"], c["code"])) # electives last
|
|
255
|
+
need = sum(c["need"] for c in courses)
|
|
256
|
+
|
|
257
|
+
answer = {
|
|
258
|
+
"need": need,
|
|
259
|
+
"need_tier": "inferred",
|
|
260
|
+
"not_counted": not_counted,
|
|
261
|
+
"courses": courses,
|
|
262
|
+
}
|
|
263
|
+
if last_exam is None: # no window to compare against
|
|
264
|
+
return answer | {
|
|
265
|
+
"capacity": None,
|
|
266
|
+
"capacity_tier": "unknown",
|
|
267
|
+
"gap": None,
|
|
268
|
+
"short": None,
|
|
269
|
+
"offers": [],
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
capacity_hours = capacity_until(gathered["capacity"], today, last_exam)
|
|
273
|
+
gap = capacity_hours - need
|
|
274
|
+
return answer | {
|
|
275
|
+
"capacity": capacity_hours,
|
|
276
|
+
"capacity_tier": "declared",
|
|
277
|
+
"gap": gap,
|
|
278
|
+
"short": gap < 0,
|
|
279
|
+
"offers": _offers(courses, gap),
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _course_entry(row: dict, days_left: int | None) -> dict | None:
|
|
284
|
+
"""One gathered course as the alert lists it, or None when it is not counted (no credits or
|
|
285
|
+
no target). Pure. `days_left` None (no exam ahead) leaves `behind` None."""
|
|
286
|
+
declared = row["hours_per_credit"] is not None
|
|
287
|
+
h = row["hours_per_credit"] if declared else H_DEFAULT
|
|
288
|
+
need = course_need(row["credits"], h, row["target_grade"], row["conceded"], row["minutes"])
|
|
289
|
+
if need is None:
|
|
290
|
+
return None
|
|
291
|
+
if row["conceded"]: # already at the pass mark: neither lever frees more
|
|
292
|
+
frees_conceding = frees_lowering = 0
|
|
293
|
+
else:
|
|
294
|
+
frees_conceding = max(row["credits"] * h * (row["target_grade"] - PASS_MARK) / 100, 0)
|
|
295
|
+
frees_lowering = row["credits"] * h * LOWER_BY / 100
|
|
296
|
+
return {
|
|
297
|
+
"course_id": row["course_id"], # /state keys courses by id, with the code beside (D-163)
|
|
298
|
+
"code": row["code"],
|
|
299
|
+
"credits": row["credits"],
|
|
300
|
+
"need": need,
|
|
301
|
+
"h": h,
|
|
302
|
+
"h_tier": "declared" if declared else "inferred",
|
|
303
|
+
"behind": None if days_left is None else behind_by(need, days_left, row["recent_minutes"]),
|
|
304
|
+
"strategy": row["strategy"],
|
|
305
|
+
"conceded": row["conceded"],
|
|
306
|
+
"elective": row["elective"],
|
|
307
|
+
"frees_conceding": frees_conceding,
|
|
308
|
+
"frees_lowering": frees_lowering,
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
LEVERS = (
|
|
313
|
+
("lower-target", "study_set_course_input"),
|
|
314
|
+
("add-capacity", "study_set_capacity"),
|
|
315
|
+
("concede", "study_respond_to_offer"),
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def _offers(courses: list[dict], gap: float) -> list[dict]:
|
|
320
|
+
"""The ladder (D-15): catch-up for the course furthest behind that is not on it yet, then
|
|
321
|
+
- only when the total is short - the three levers, each naming the gap. Pure."""
|
|
322
|
+
offers = []
|
|
323
|
+
behind = [c for c in courses if c["behind"] > 0 and c["strategy"] != "catch-up"]
|
|
324
|
+
if behind:
|
|
325
|
+
furthest = max(behind, key=lambda c: c["behind"]) # a tie keeps the list's first
|
|
326
|
+
offers.append(
|
|
327
|
+
{
|
|
328
|
+
"kind": "catch-up",
|
|
329
|
+
"tool": "study_respond_to_offer",
|
|
330
|
+
"code": furthest["code"],
|
|
331
|
+
"hours": furthest["behind"],
|
|
332
|
+
}
|
|
333
|
+
)
|
|
334
|
+
if gap < 0:
|
|
335
|
+
offers += [{"kind": kind, "tool": tool, "hours": -gap} for kind, tool in LEVERS]
|
|
336
|
+
return offers
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def shortfall(conn: Connection, user_id: str, today: datetime.date) -> dict:
|
|
340
|
+
"""Read the alert for `today`: `_gather`, then `shortfall_of`, every hour figure rounded to
|
|
341
|
+
2 places for the reader (the pure pieces keep full precision). Writes nothing."""
|
|
342
|
+
return _rounded(shortfall_of(_gather(conn, user_id, today), today))
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
HOUR_FIELDS = ("need", "capacity", "gap", "h", "behind", "frees_conceding", "frees_lowering")
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _rounded(alert: dict) -> dict:
|
|
349
|
+
"""`alert` with each hour figure - top level, per course and per offer - to 2 places."""
|
|
350
|
+
|
|
351
|
+
def two(record: dict, fields: Sequence[str]) -> dict:
|
|
352
|
+
return {
|
|
353
|
+
k: round(v, 2) if k in fields and isinstance(v, float) else v for k, v in record.items()
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return two(alert, HOUR_FIELDS) | {
|
|
357
|
+
"courses": [two(c, HOUR_FIELDS) for c in alert["courses"]],
|
|
358
|
+
"offers": [two(o, ("hours",)) for o in alert["offers"]],
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
# --- the answer (one write unit) ---------------------------------------------------------------
|
|
363
|
+
|
|
364
|
+
CHOICES = ("catch-up", "default", "concede", "unconcede")
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def respond_to_offer(
|
|
368
|
+
engine: Engine, user_id: str, code: str, choice: str, semester_name: str | None = None
|
|
369
|
+
) -> dict:
|
|
370
|
+
"""His answer to an offer, on one course, as one write unit (Write units, D-296).
|
|
371
|
+
|
|
372
|
+
- "catch-up" / "default": `strategy` and `strategy_changed_at` = now.
|
|
373
|
+
- "concede": `conceded` = 1, `conceded_at` = now. "unconcede": `conceded` = 0,
|
|
374
|
+
`conceded_at` = NULL - the history stays in each plan's `strategy_snapshot`.
|
|
375
|
+
Refused, writing nothing: an unknown choice (`invalid_value`, the four choices in the fix),
|
|
376
|
+
a course not found (`find_course`'s error), a dropped or unplanned course, or a choice that
|
|
377
|
+
is already the course's state (`already_set`, e.g. catch-up on a course on catch-up).
|
|
378
|
+
No ranking changes (D-299).
|
|
379
|
+
|
|
380
|
+
Returns {"course_id", "code", "strategy", "strategy_changed_at", "conceded",
|
|
381
|
+
"conceded_at"} after the write.
|
|
382
|
+
|
|
383
|
+
Examples: I3351 + "catch-up" -> strategy catch-up, strategy_changed_at set · I3351 +
|
|
384
|
+
"catch-up" again -> already_set · DHR300 + "concede" -> conceded 1 with a date ·
|
|
385
|
+
DHR300 + "unconcede" -> conceded 0, conceded_at NULL.
|
|
386
|
+
`semester_name` narrows the code when it exists in two semesters, as in every course tool.
|
|
387
|
+
"""
|
|
388
|
+
if choice not in CHOICES: # before the unit: a bad choice opens no transaction
|
|
389
|
+
raise invalid(
|
|
390
|
+
"choice",
|
|
391
|
+
f"{choice!r} is not an answer to an offer",
|
|
392
|
+
f"send one of: {', '.join(CHOICES)}",
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
with write_unit(engine) as conn:
|
|
396
|
+
course_id = find_course(conn, user_id, code, semester_name)
|
|
397
|
+
row = conn.execute(select(course).where(course.c.id == course_id)).one()
|
|
398
|
+
if not row.planned or row.dropped_at is not None:
|
|
399
|
+
raise StudyError(
|
|
400
|
+
code="not_in_plan",
|
|
401
|
+
message=f"{row.code} is not planned this semester, so it gets no offer",
|
|
402
|
+
fix="answer offers only for the courses the shortfall alert lists",
|
|
403
|
+
field_errors=[{"field": "code", "problem": "dropped or never planned"}],
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
stamp = now()
|
|
407
|
+
if choice in ("catch-up", "default"):
|
|
408
|
+
current, changes = row.strategy, {"strategy": choice, "strategy_changed_at": stamp}
|
|
409
|
+
else:
|
|
410
|
+
current = "concede" if row.conceded else "unconcede"
|
|
411
|
+
changes = (
|
|
412
|
+
{"conceded": 1, "conceded_at": stamp}
|
|
413
|
+
if choice == "concede"
|
|
414
|
+
else {"conceded": 0, "conceded_at": None}
|
|
415
|
+
)
|
|
416
|
+
if current == choice:
|
|
417
|
+
raise StudyError(
|
|
418
|
+
code="already_set",
|
|
419
|
+
message=f"{row.code} is already {_state_word(choice)}",
|
|
420
|
+
fix="nothing to do - read the course's state in the shortfall alert",
|
|
421
|
+
field_errors=[{"field": "choice", "problem": f"already {_state_word(choice)}"}],
|
|
422
|
+
)
|
|
423
|
+
conn.execute(update(course).where(course.c.id == course_id).values(changes))
|
|
424
|
+
after = conn.execute(select(course).where(course.c.id == course_id)).one()
|
|
425
|
+
|
|
426
|
+
return {
|
|
427
|
+
"course_id": course_id,
|
|
428
|
+
"code": after.code,
|
|
429
|
+
"strategy": after.strategy,
|
|
430
|
+
"strategy_changed_at": after.strategy_changed_at,
|
|
431
|
+
"conceded": bool(after.conceded),
|
|
432
|
+
"conceded_at": after.conceded_at,
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def _state_word(choice: str) -> str:
|
|
437
|
+
"""How a choice reads as a state, for the already_set message."""
|
|
438
|
+
return {
|
|
439
|
+
"catch-up": "on catch-up",
|
|
440
|
+
"default": "on the default strategy",
|
|
441
|
+
"concede": "conceded",
|
|
442
|
+
"unconcede": "not conceded",
|
|
443
|
+
}[choice]
|