forge
Explore
Log in

Don't you dare go Hollow

hezarfen_zeka

Clone
★ 0

0 watching0 forksPython1.9 MiBe01a4faAhmet Kaan Gümüş2 months ago25 commits

reader.py118 lines, 4.5 KiBHistoryRawDownload
1"""Rapor katmanının **okuma** yüzeyi.
2
3Köprü okumaları ve zeka veritabanı BURADA YOKTUR: ZEKA'nın kendi veritabanı
4yok (değişmez kural: bir AI servisi uygulama veritabanına asla doğrudan
5erişmez), o yüzden rapor katmanı yalnız bir `Reader` protokolü tanımlar ve
6bellek içi bir uygulama verir:
7
8* `MemoryReader` — sözlükten okuyan uygulama; fikstür yolu ve testler bunu
9 kullanır. Sütun izdüşümünü (`columns`) **gerçekten uygular**; böylece
10 dikkat listesi kapısı testte de veri düzeyinde sınanır.
11
12Canlı bir dağıtımın satırlarını okumak backend'in `/insights` uçlarının
13işidir; buradan geçen bir okuma yolu yoktur ve olmayacaktır.
14"""
15
16from __future__ import annotations
17
18from typing import Any, Protocol
19
20#: `question_segment` satırından rapora **hiç** çıkmayan alanlar.
21#: `CIKTI-SOZLESMESI.md` §5: `rationale` modelin gerekçesi (hatalıdır ve
22#: sorunun çözüm yapısını anlatır), `trap_choice` doğrudan cevap ipucudur.
23#: Ham çıktı teknik ekibe gider ve oradan bir öğrenci ekranına bağlanabilir;
24#: bu yüzden ihracın kendisinden çıkarılırlar.
25QUESTION_SEGMENT_HIDDEN = ("rationale", "trap_choice")
26
27
28class Reader(Protocol):
29 """Rapor kurucularının gördüğü tek okuma yüzeyi."""
30
31 async def summaries(
32 self, school: str, *, columns: tuple[str, ...], student: str | None = None
33 ) -> list[dict[str, Any]]: ...
34
35 async def recommendations(
36 self,
37 school: str,
38 *,
39 audience: str | None = None,
40 audience_role: str | None = None,
41 ) -> list[dict[str, Any]]: ...
42
43 async def segment_profiles(
44 self, school: str, *, student: str | None = None
45 ) -> list[dict[str, Any]]: ...
46
47 async def runs(self, school: str, *, limit: int = 5) -> list[dict[str, Any]]: ...
48
49 async def question_segments(self, school: str) -> list[dict[str, Any]]: ...
50
51
52class MemoryReader:
53 """Sözlükten okuyan `Reader` — testler ve fikstür yolu.
54
55 `tables` anahtarları beş tablonun adıdır; değerler satır listesidir.
56 Eksik tablo boş sayılır: **boş veri bir hata değildir**, boş rapor üretir.
57 """
58
59 def __init__(self, tables: dict[str, list[dict[str, Any]]] | None = None) -> None:
60 self.tables = {name: list(rows) for name, rows in (tables or {}).items()}
61
62 def _table(self, name: str) -> list[dict[str, Any]]:
63 return [r for r in self.tables.get(name, []) if isinstance(r, dict)]
64
65 async def summaries(
66 self, school: str, *, columns: tuple[str, ...], student: str | None = None
67 ) -> list[dict[str, Any]]:
68 out = []
69 for row in self._table("student_summary"):
70 if row.get("school") != school:
71 continue
72 if student is not None and row.get("student") != student:
73 continue
74 # İzdüşüm gerçekten uygulanır: listede olmayan alan dönmez.
75 out.append({k: row[k] for k in columns if k in row})
76 return out
77
78 async def recommendations(
79 self,
80 school: str,
81 *,
82 audience: str | None = None,
83 audience_role: str | None = None,
84 ) -> list[dict[str, Any]]:
85 out = []
86 for row in self._table("recommendation"):
87 if row.get("school") != school:
88 continue
89 if audience is not None and row.get("audience") != audience:
90 continue
91 if audience_role is not None and row.get("audience_role") != audience_role:
92 continue
93 out.append(dict(row))
94 return out
95
96 async def segment_profiles(
97 self, school: str, *, student: str | None = None
98 ) -> list[dict[str, Any]]:
99 out = []
100 for row in self._table("student_segment_profile"):
101 if row.get("school") != school:
102 continue
103 if student is not None and row.get("student") != student:
104 continue
105 out.append(dict(row))
106 return out
107
108 async def runs(self, school: str, *, limit: int = 5) -> list[dict[str, Any]]:
109 rows = [r for r in self._table("insight_run") if r.get("school") == school]
110 rows.sort(key=lambda r: int(r.get("started_at") or 0), reverse=True)
111 return [dict(r) for r in rows[:limit]]
112
113 async def question_segments(self, school: str) -> list[dict[str, Any]]:
114 return [
115 {k: v for k, v in row.items() if k not in QUESTION_SEGMENT_HIDDEN}
116 for row in self._table("question_segment")
117 if row.get("school") == school
118 ]