hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! ZEKA's own tables, and the operations that write and read them. |
| 2 | //! |
| 3 | //! ZEKA (`hezarfen_zeka`) computes student and class insights, but it does |
| 4 | //! **not** touch a school database: every statement against the `zeka_*` |
| 5 | //! tables runs here, on the backend, under the school the caller named. That |
| 6 | //! is a standing rule of this deployment, not a preference — an AI service |
| 7 | //! process holding a database credential of its own would be a second, silent |
| 8 | //! door into every school's rows, one no request-scoped check can close. So |
| 9 | //! the service's storage surface is nine named operations ([`crate::ai::insight`] |
| 10 | //! carries the wire contract and the dispatch), and this module is what they |
| 11 | //! execute. |
| 12 | //! |
| 13 | //! The row types here are therefore **both** the SQL's input and the bridge's |
| 14 | //! payload: a nightly batch arrives as JSON, is parsed into these structs |
| 15 | //! (`invalid_payload` on anything that does not fit), and is written verbatim — |
| 16 | //! the backend is a trust boundary for the *shape*, never an author of the |
| 17 | //! content. Fields are spelled exactly as the tables' columns are, because a |
| 18 | //! consumer that has to guess a mapping between two spellings will eventually |
| 19 | //! guess wrong. |
| 20 | //! |
| 21 | //! Three invariants are worth stating up front, since they are what the SQL |
| 22 | //! below is arranged around: |
| 23 | //! |
| 24 | //! * **A batch is one transaction.** Every bulk write is all-or-nothing: a |
| 25 | //! refusal means nothing landed, so a service that retries a batch cannot |
| 26 | //! half-apply it. Row-at-a-time statements inside one transaction keep that |
| 27 | //! honesty without a multi-row statement whose bind count would be a second |
| 28 | //! bound to police; the batch ceiling [`MAX_INSIGHT_BATCH_ROWS`] keeps the |
| 29 | //! statement count bounded. |
| 30 | //! * **Child rows are replaced, never merged.** `attention`, the segment |
| 31 | //! `dimension` rows, and a run's `pending`/`failed_module` rows are deleted |
| 32 | //! for their owners and re-inserted from the payload. Merging would leave a |
| 33 | //! fact the service stopped producing on the screen forever. |
| 34 | //! * **The school is the database.** Every function here takes the school's |
| 35 | //! own pool and nothing else: there is no `school` column to filter on |
| 36 | //! because there is no row from another school in reach. Foreign keys carry |
| 37 | //! the rest — an id from another school's database is not a row this |
| 38 | //! database will accept, and that refusal is mapped to a validation error |
| 39 | //! rather than a `500` so the caller learns which id was wrong. |
| 40 | |
| 41 | use BTreeMap; |
| 42 | |
| 43 | use ; |
| 44 | use Value as JsonValue; |
| 45 | use AssertSqlSafe; |
| 46 | use Json as SqlJson; |
| 47 | use ToSchema; |
| 48 | use Uuid; |
| 49 | |
| 50 | use crate |
| 51 | |
| 52 | ; |
| 53 | use crate; |
| 54 | use cratenext_uuid; |
| 55 | use crate; |
| 56 | use crateSchoolId; |
| 57 | |
| 58 | // ---- the wire rows --------------------------------------------------------- |
| 59 | |
| 60 | /// One student's computed summary, and the attention items that travel with |
| 61 | /// it. The two tables are replaced together — see the module docs. |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | /// One attention item: a fact about a student that a teacher should see. |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | /// One recommendation card. |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// The four rubric labels of one question, in the service's nested shape. |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | /// The model's confidence in each of the four labels, `0..=1`. |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | /// One question's segment labels, and the dimension split that travels with |
| 165 | /// them (replaced together). |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | /// One student's accuracy inside one rubric segment, against the same |
| 199 | /// student's overall rate. `contrast` is the only field rules may fire on. |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | /// One compute run's ledger row, with the run's own children. |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | // ---- the request/receipt envelopes ---------------------------------------- |
| 248 | |
| 249 | /// `insight.summary.upsert` / `POST /insights/summaries`. |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | /// `insight.recommendation.upsert` / `POST /insights/recommendations`. |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | /// `insight.segment.upsert` / `POST /insights/segments`. |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | /// `insight.profile.upsert` / `POST /insights/profiles`. |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | /// `insight.run.upsert` / `POST /insights/runs`. |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | /// `insight.departed.purge` / `POST /insights/purge`. The **active** roster: |
| 280 | /// every student not named loses their derived rows. |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | /// What a bulk write did: rows written, and (for recommendations only) rows |
| 287 | /// the service's own evidence rule kept out. |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | /// The freshest run's pending students, in the service's order. |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | /// The per-table verdict of a sweep or a purge, keyed by table name — the |
| 306 | /// shape the service's CLI prints and exits non-zero on. |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | /// The deployment's active schools, as hyphenated uuids. |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | /// One school's own identity, as a rendered report prints it. The bridge |
| 319 | /// frame already names the school by uuid, and a document that reaches a |
| 320 | /// manager's screen also carries the display name. The control row is the |
| 321 | /// only authority on it. |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | /// One class (şube) as a rendered document labels it: the id the summaries' |
| 330 | /// own evidence carries, and the display name a reader should see in its |
| 331 | /// place. A document that prints the id instead is a machine identifier on |
| 332 | /// the most human-facing artifact the product ships. |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | // ---- shared validation ----------------------------------------------------- |
| 340 | |
| 341 | /// Refuse a batch past [`MAX_INSIGHT_BATCH_ROWS`] rather than taking it |
| 342 | /// piecemeal: a batch is the unit the service retries, so accepting part of |
| 343 | /// one would turn its retry into a duplicate. |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | /// A foreign-key violation is a payload error here, not a server fault: the |
| 355 | /// statement named a row this school's database does not have (most often an |
| 356 | /// id belonging to another school), and the caller can fix it. |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | /// One hyphenated uuid from the wire. Ids cross the bridge as strings — the |
| 370 | /// `uuid` crate's serde support is not enabled here, and a string that is not |
| 371 | /// a uuid should read as a field error, not as a decode failure with no field. |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | /// `YYYY-MM-DD`, the ledger's key. Checked here so a malformed day is the |
| 382 | /// caller's `invalid_payload` and not a CHECK violation dressed as a `500`. |
| 383 | /// The report doors share it: a run day shapes a blob key there, and a |
| 384 | /// caller-supplied path segment that shapes a path is validated before it is |
| 385 | /// joined. |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | // ---- writes ---------------------------------------------------------------- |
| 396 | |
| 397 | /// Store a batch of student summaries, replacing each student's attention |
| 398 | /// items. One transaction: a batch lands whole or not at all. |
| 399 | pub async |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | /// The transaction body of [`write_summaries`], a named fn rather than an async |
| 412 | /// closure: an `AsyncFnMut` closure whose future does the work trips rustc's |
| 413 | /// "implementation of `Send` is not general enough" (see `tx_with_retry`). |
| 414 | async |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | /// The service's own evidence rule, applied where the write happens: a card |
| 472 | /// whose evidence carries nothing but its limitation is not a finding. Such a |
| 473 | /// row is counted `rejected` in the receipt and left unwritten — silently |
| 474 | /// dropping it would be the one thing this rule exists to prevent. |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | /// Store a batch of recommendation cards. The backend mints each row's id: |
| 484 | /// an id belongs to whoever writes the row, and that is now this process. |
| 485 | pub async |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | /// The service's own evidence rule, applied where the write happens: a card |
| 498 | /// whose evidence carries nothing but its limitation is not a finding. Such a |
| 499 | /// row is counted `rejected` and left unwritten — silently dropping it would |
| 500 | /// be the one thing this rule exists to prevent. Returns `(written, rejected)`. |
| 501 | async |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | /// The stored evidence object: the payload's own object with `limitation` |
| 561 | /// written into it, exactly as the service kept it. A payload whose `evidence` |
| 562 | /// is not an object cannot be merged and is refused. |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | /// Store a batch of question segments, replacing each question's dimension |
| 584 | /// split. |
| 585 | pub async |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | /// The transaction body of [`write_segments`], a named fn rather than an async |
| 598 | /// closure: an `AsyncFnMut` closure whose future does the work trips rustc's |
| 599 | /// "implementation of `Send` is not general enough" (see `tx_with_retry`). |
| 600 | async |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | /// Store a batch of student segment profiles. |
| 686 | pub async |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | /// The transaction body of [`write_profiles`], a named fn rather than an async |
| 699 | /// closure: an `AsyncFnMut` closure whose future does the work trips rustc's |
| 700 | /// "implementation of `Send` is not general enough" (see `tx_with_retry`). |
| 701 | async |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | /// Store one run's ledger row, replacing the run's pending/failed children. |
| 738 | pub async |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | /// The transaction body of [`write_run`], a named fn rather than an async |
| 760 | /// closure: an `AsyncFnMut` closure whose future does the work trips rustc's |
| 761 | /// "implementation of `Send` is not general enough" (see `tx_with_retry`). |
| 762 | async |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | // ---- reads ----------------------------------------------------------------- |
| 828 | |
| 829 | /// The freshest run's pending students, in the order the run recorded them. |
| 830 | /// |
| 831 | /// Refused rather than clipped past [`MAX_INSIGHT_PENDING_STUDENTS`]: the |
| 832 | /// service resumes its next run from this list, so a shortened one would drop |
| 833 | /// students silently. The read asks for one row past the ceiling so the |
| 834 | /// refusal never has to materialize the whole list. |
| 835 | pub async |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | /// The deployment's active schools, as hyphenated uuids — the one read that |
| 859 | /// is not scoped to a school, because it is how a shared AI fleet learns |
| 860 | /// which schools exist. |
| 861 | pub async |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | /// One school's identity row, by id. `None` only for an id the control |
| 873 | /// database does not hold — a resolved tenant always has one, so a caller |
| 874 | /// reads that as an internal fault, not as a refusal. |
| 875 | pub async |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | /// Every class (şube) of the school, id and display name, in name order. |
| 889 | /// |
| 890 | /// The service's summaries carry class ids inside their own evidence objects, |
| 891 | /// so a document that groups by class has only those ids to print — and an id |
| 892 | /// is not a label. This is the lookup that turns them into names. It is sent |
| 893 | /// whole rather than narrowed to the ids the current rows mention: a school's |
| 894 | /// class list is tens of rows, and a partial list would make the renderer's |
| 895 | /// fallback depend on how complete this read's filters were. |
| 896 | pub async |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | // ---- the school-wide reads the report payload carries ----------------------- |
| 910 | // |
| 911 | // Every read above is per-student or per-audience, because that is every |
| 912 | // reader this nest had. A *document* about the school is the first caller that |
| 913 | // needs the whole row set at once, so these four project each table straight |
| 914 | // into the wire rows the bridge payload carries ([`SummaryRow`], |
| 915 | // [`RecommendationRow`], [`ProfileRow`], [`RunRow`]) — one mapping, in one |
| 916 | // place, shared by SQL and the wire. No filters: the document's own reader |
| 917 | // (the service's report package) decides what it shows, and a backend that |
| 918 | // pre-filtered would be a second, silent author of the report. |
| 919 | |
| 920 | /// Every stored summary with its attention items, in student order. The |
| 921 | /// attention list is one query for the whole set, grouped by student — not |
| 922 | /// one query per summary. |
| 923 | pub async |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | /// Every stored card, whatever its audience, in a stable order. The write |
| 970 | /// merged each row's `limitation` into its stored evidence object, so the |
| 971 | /// read leaves the [write-side](RecommendationRow::limitation) field empty — |
| 972 | /// a second copy here could only disagree with the one inside `evidence`. |
| 973 | /// |
| 974 | /// The wire row carries no `dismissed_at`, so the stored dismissal trail |
| 975 | /// (`dismissed_at`/`dismiss_by`/`dismiss_reason` on the table) does not |
| 976 | /// travel: a consumer that gates on dismissal cannot fire on this read. That |
| 977 | /// is a known gap of the report payload's contract — the reader that filters |
| 978 | /// dismissals is the per-audience read ([`crate::web::insights`]), and |
| 979 | /// widening the row here is a contract change this lane does not make. |
| 980 | pub async |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | /// Every stored segment profile, in student order. Noise rows (`confidence = |
| 1013 | /// 'none'`) are included: this read feeds a document, and the display floor is |
| 1014 | /// the reader's rule, applied where the reader applies it. |
| 1015 | pub async |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | /// How many ledger rows a report carries: the named day plus recent context. |
| 1044 | /// Deliberately small — the ledger is context for the document, which is |
| 1045 | /// about one day, not a ledger dump. |
| 1046 | const REPORT_RUNS: i64 = 5; |
| 1047 | |
| 1048 | /// The run ledger a report carries, with each run's children. The named day |
| 1049 | /// leads when it exists (a report about it must be able to show it even if it |
| 1050 | /// has scrolled out of the newest rows), then the newest days follow. |
| 1051 | pub async |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | // ---- retention ------------------------------------------------------------- |
| 1113 | |
| 1114 | /// The nine tables a sweep walks, children before their parents: the foreign |
| 1115 | /// keys are `ON DELETE NO ACTION`, so a parent cannot go while a child row |
| 1116 | /// still names it. |
| 1117 | const SWEEP_CHILDREN: & = & |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | ; |
| 1123 | |
| 1124 | /// The tables that carry their own `retain_until`. |
| 1125 | const SWEEP_PARENTS: & = & |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | ; |
| 1132 | |
| 1133 | /// Delete every row whose retention window has closed. `now_ms` is the |
| 1134 | /// backend's own clock — one clock decides expiry, never the caller's. |
| 1135 | /// |
| 1136 | /// One table at a time, each in its own statement, and a table whose delete |
| 1137 | /// fails is reported `false` rather than aborting the rest: a sweep is |
| 1138 | /// housekeeping, and losing the whole night's cleanup because one table is |
| 1139 | /// locked would leave every other table's expired rows in place. The caller |
| 1140 | /// reads the verdict per table, exactly as the service's own sweep did. |
| 1141 | pub async |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | /// The table names come from [`SWEEP_CHILDREN`]/[`SWEEP_PARENTS`], never from |
| 1162 | /// a caller — the only dynamic SQL in this module, and the reason |
| 1163 | /// [`AssertSqlSafe`] is licensed here. |
| 1164 | async |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | /// Delete the derived rows of everyone who is no longer on the roster. One |
| 1173 | /// transaction: a purge is a decision about a cohort, not a per-row repair. |
| 1174 | /// |
| 1175 | /// The empty list is **refused**, never obeyed: an empty roster is almost |
| 1176 | /// always a fetch that failed, and reading it as "nobody is enrolled any |
| 1177 | /// more" would delete every student's derived data in one call. |
| 1178 | pub async |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | /// The transaction body of [`purge_departed`], a named fn rather than an async |
| 1215 | /// closure: an `AsyncFnMut` closure whose future does the work trips rustc's |
| 1216 | /// "implementation of `Send` is not general enough" (see `tx_with_retry`). |
| 1217 | async |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 |