LCOV - code coverage report
Current view: top level - matchscore/jobs - advancements.clj Coverage Total Hit
Test: lcov.info Lines: 92.2 % 129 119
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.jobs.advancements
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.application.advancement.bracket-reconcile :as bracket-reconcile]
       6              :    [matchscore.application.advancement.bye :as bye]
       7              :    [matchscore.application.advancement.reconcile :as advancement-reconcile]
       8              :    [matchscore.db.postgres.telegram-match-notifications :as notification-db]
       9              :    [matchscore.domain.class.repository.postgres :as class-repo]
      10              :    [matchscore.domain.draw.repository.postgres :as draw-repo]
      11              :    [matchscore.domain.stage.repository.postgres :as stage-repo]
      12              :    [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
      13              :    [matchscore.realtime.core :as realtime]
      14              :    [matchscore.realtime.subscriptions :as subscriptions]
      15              :    [taoensso.telemere :as t]))
      16              : 
      17              : 
      18            4 : (defn- summarize-advancement
      19              :   [adv]
      20           14 :   {:uuid                (str (:uuid adv))
      21            5 :    :rule-id             (str (:rule-id adv))
      22            5 :    :participant-id      (str (:participant-id adv))
      23            7 :    :to-draw-id          (some->
      24            3 :                           (:to-draw-id adv)
      25            0 :                           str)
      26            7 :    :to-group-id         (some->
      27            3 :                           (:to-group-id adv)
      28            0 :                           str)
      29            7 :    :to-bracket-match-id (some->
      30            3 :                           (:to-bracket-match-id adv)
      31            0 :                           str)
      32            3 :    :assigned-seed       (:assigned-seed adv)
      33            3 :    :assigned-slot       (:assigned-slot adv)})
      34              : 
      35              : 
      36            4 : (defn- extract-diff
      37              :   "Unwraps an Either<Error, diff> into a diff, logging Left and returning
      38              :    an empty diff in that case so the job can continue with partial results."
      39              :   [label match-uuid result]
      40            4 :   (if (either/right? result)
      41            3 :     (c/extract result)
      42            1 :     (do
      43           92 :       (t/log!
      44              :        :warn
      45            8 :        {:message    (str label " reconcile returned Left")
      46            3 :         :error      (c/extract result)
      47            3 :         :match-uuid (str match-uuid)})
      48            4 :       {:promoted []
      49            1 :        :revoked  []})))
      50              : 
      51              : 
      52            4 : (defn- resolve-byes-safely!
      53              :   "bracket-reconcile/execute! (triggered here for ANY bracket match result,
      54              :    not just group-sourced promotions) can newly populate a slot that makes
      55              :    a downstream match walkover-eligible — e.g. a quarterfinal loser landing
      56              :    in a consolation bracket's semifinal slot opposite a bye. Re-checking
      57              :    byes/walkovers for the affected draw here (in addition to the hook
      58              :    inside advancement-reconcile/execute! for group-sourced promotions)
      59              :    covers that path too. Only runs once draw-ready-for-byes? confirms no
      60              :    rule targeting draw-id still has a pending candidate (same guard
      61              :    advancement-reconcile/resolve-byes-for-target-draws! applies on the
      62              :    group-source path), so a still-pending sibling rule doesn't get its
      63              :    target seeds finalized as byes prematurely. Swallows exceptions so a
      64              :    bye-resolution failure doesn't sink the whole job; the next match
      65              :    result will retry."
      66              :   [system-components match-uuid draw-id]
      67            3 :   (when draw-id
      68            1 :     (try
      69            8 :       (when (advancement-reconcile/draw-ready-for-byes? (:repos system-components) draw-id)
      70            4 :         (bye/resolve-byes-and-walkovers! system-components draw-id))
      71              :       (catch Exception e
      72            0 :         (t/log!
      73              :          :error
      74            0 :          {:message    "bye resolution failed"
      75            0 :           :match-uuid (str match-uuid)
      76            0 :           :draw-id    (str draw-id)
      77            0 :           :exception  e})))))
      78              : 
      79              : 
      80            4 : (defn- resolve-target-context
      81              :   "Reconcile advancements share one target draw→stage→class→event. Group
      82              :    and bracket matches carry a null event-id, so the event is resolved
      83              :    from the class. stage-id/class-id are also returned for the client's
      84              :    cache invalidation."
      85              :   [repos draw-id]
      86           12 :   (let [draw     (when draw-id (draw-repo/find-by-id (:draw-postgres-repo repos) (str draw-id)))
      87           18 :         stage-id (some->
      88            1 :                    draw
      89              :                    :stage-id
      90            1 :                    str)
      91            9 :         stage    (when stage-id (stage-repo/find-by-id (:stage-postgres-repo repos) stage-id))
      92           18 :         class-id (some->
      93            1 :                    stage
      94              :                    :class-id
      95            1 :                    str)
      96            9 :         class    (when class-id (class-repo/find-by-id (:class-postgres-repo repos) class-id))]
      97            5 :     {:stage-id   stage-id
      98            1 :      :class-id   class-id
      99           18 :      :event-uuid (some->
     100            1 :                    class
     101              :                    :event-id
     102            1 :                    str)}))
     103              : 
     104              : 
     105            4 : (defn- notify-subscribers!
     106              :   "Resolves the target event via the draw→stage→class chain (group and
     107              :    bracket matches don't carry event-id directly) and sends the
     108              :    reconciled payload to that event's subscribers across all backend
     109              :    replicas.
     110              : 
     111              :    `bracket-promoted` / `bracket-revoked` carry the slot-shaped maps
     112              :    produced by `bracket-reconcile/execute!` and are forwarded as-is."
     113              :   [{:keys [sente redis repos]} match-uuid draw-id promoted revoked bracket-promoted bracket-revoked]
     114            2 :   (when
     115           14 :     (and
     116            1 :      sente
     117            1 :      redis
     118            1 :      repos
     119            3 :      (or (seq promoted) (seq revoked) (seq bracket-promoted) (seq bracket-revoked)))
     120            5 :     (let [{:keys [stage-id class-id event-uuid]} (resolve-target-context repos draw-id)]
     121            4 :       (if-not event-uuid
     122           92 :         (t/log!
     123              :          :warn
     124            4 :          {:message    "advancement reconcile: could not resolve event-id; skipping notify"
     125            3 :           :match-uuid (str match-uuid)})
     126            5 :         (let [subscribers (subscriptions/get-subscribers redis event-uuid)
     127           10 :               payload     {:event-uuid       event-uuid
     128            3 :                            :match-uuid       (str match-uuid)
     129            1 :                            :stage-id         stage-id
     130            1 :                            :class-id         class-id
     131            4 :                            :promoted         (mapv summarize-advancement promoted)
     132            4 :                            :revoked          (mapv summarize-advancement revoked)
     133            3 :                            :bracket-promoted (vec bracket-promoted)
     134            3 :                            :bracket-revoked  (vec bracket-revoked)}]
     135           92 :           (t/log!
     136              :            :info
     137            5 :            (str
     138              :             "Notifying "
     139            2 :             (count subscribers)
     140              :             " subscribers of event "
     141            1 :             event-uuid
     142              :             " about advancement reconcile"))
     143            8 :           (realtime/relay-to-event! sente redis event-uuid [:advancement/reconciled payload]))))))
     144              : 
     145              : 
     146            4 : (defn reconcile-for-match-job
     147              :  "Background job: reconciles advancements affected by the given match.
     148              :    Runs three independent reconciles in sequence:
     149              :      1. Group-source advancements (cross-stage promotion to target
     150              :         groups / bracket round-1 seed slots, via advancement_rules).
     151              :      2. Bracket-to-bracket winner_of / loser_of propagation within the
     152              :         match's draw.
     153              :      3. Bracket-match-source advancement rules (qualification_monrad
     154              :         winner/loser cross-stage promotion via bracket_match rules).
     155              :    All three may return Left; we log and continue with the others.
     156              :    Args:
     157              :      - system-components: standard Goose injected components (:repos, :pg, ...)
     158              :      - {:keys [match-uuid]} : the match whose result changed."
     159              :  [system-components {:keys [match-uuid]}]
     160           96 :  (t/log! :info (str "Starting Goose job: reconciling advancements for match " match-uuid))
     161            5 :  (let [group-result (advancement-reconcile/execute! system-components match-uuid)
     162            4 :        bracket-result (bracket-reconcile/execute! system-components match-uuid)
     163              :        bm-rules-result
     164            4 :        (advancement-reconcile/execute-for-bracket-match-rules! system-components match-uuid)
     165            5 :        group-diff (extract-diff "group-source" match-uuid group-result)
     166            5 :        bracket-diff (extract-diff "bracket-to-bracket" match-uuid bracket-result)
     167            5 :        bm-rules-diff (extract-diff "bracket-match-rules" match-uuid bm-rules-result)
     168            8 :        promoted (into (:promoted group-diff) (:promoted bm-rules-diff))
     169            8 :        revoked (into (:revoked group-diff) (:revoked bm-rules-diff))
     170              :        ;; Bracket slot changes come from direct propagation (2) plus the
     171              :        ;; cascades started when (1)/(3) revoke a bracket seed.
     172            2 :        bracket-promoted (vec
     173            2 :                          (concat
     174            3 :                           (:promoted bracket-diff)
     175            3 :                           (:bracket-promoted group-diff)
     176            3 :                           (:bracket-promoted bm-rules-diff)))
     177            2 :        bracket-revoked (vec
     178            2 :                         (concat
     179            3 :                          (:revoked bracket-diff)
     180            3 :                          (:bracket-revoked group-diff)
     181            3 :                          (:bracket-revoked bm-rules-diff)))
     182           17 :        draw-id (or (:draw-uuid group-diff) (:draw-uuid bracket-diff) (:draw-uuid bm-rules-diff))
     183           23 :        any-applied? (or (seq promoted) (seq revoked) (seq bracket-promoted) (seq bracket-revoked))]
     184            5 :    (resolve-byes-safely! system-components match-uuid draw-id)
     185           92 :    (t/log!
     186              :     :info
     187            6 :     (str
     188            2 :      "Completed Goose job: advancement reconcile — promoted " (count promoted)
     189            2 :      ", revoked "          (count revoked)
     190            2 :      "; bracket promoted " (count bracket-promoted)
     191            2 :      ", revoked "          (count bracket-revoked)))
     192            2 :    (notify-subscribers!
     193            1 :     system-components
     194            1 :     match-uuid
     195            1 :     draw-id
     196            1 :     promoted
     197            1 :     revoked
     198            1 :     bracket-promoted
     199            1 :     bracket-revoked)
     200           17 :    (when-let [event-id (or
     201            8 :                         (:event-uuid (resolve-target-context (:repos system-components) draw-id))
     202              :                         (some->
     203            6 :                           (notification-db/match-context (:pg system-components) match-uuid)
     204              :                           :event-id))]
     205            6 :      (match-notification-jobs/schedule-event-sync! (:goose system-components) event-id))
     206            2 :    (if any-applied?
     207            7 :      {:status           :success
     208            2 :       :promoted         (count promoted)
     209            2 :       :revoked          (count revoked)
     210            2 :       :bracket-promoted (count bracket-promoted)
     211            2 :       :bracket-revoked  (count bracket-revoked)}
     212            4 :      {:status :no-op
     213            4 :       :reason (or
     214           10 :                (when (either/left? group-result) (:code (c/extract group-result)))
     215            0 :                (when (either/left? bracket-result) (:code (c/extract bracket-result)))
     216            0 :                (when (either/left? bm-rules-result) (:code (c/extract bm-rules-result))))})))
        

Generated by: LCOV version 2.0-1