LCOV - code coverage report
Current view: top level - matchscore/domain/psa/repository - postgres.clj Coverage Total Hit
Test: lcov.info Lines: 100.0 % 150 150
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.psa.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.country-exclusion :as country-exclusion]
       5              :    [matchscore.domain.source-sync.query :as source-sync]
       6              :    [next.jdbc :as jdbc]
       7              :    [next.jdbc.result-set :as rs]))
       8              : 
       9              : 
      10            4 : (defprotocol
      11              :   PsaPostgresRepository
      12              :   (find-seasons
      13              :    [this])
      14              :   (find-tournaments
      15              :    [this season])
      16              :   (find-tournament
      17              :    [this psa-id])
      18              :   (find-tournament-videos
      19              :    [this psa-id])
      20              :   (find-latest-ranking-snapshot-date
      21              :    [this list-name])
      22              :   (find-ranking-entries
      23              :    [this list-name snapshot-date])
      24              :   (find-followed-players
      25              :    [this user-id]))
      26              : 
      27              : 
      28           16 : (def ^:private query-options {:builder-fn rs/as-unqualified-kebab-maps})
      29              : 
      30              : 
      31            4 : (def ^:private tournament-allowed
      32              :   "PSA keeps a tournament's country only at the end of its location: \"Moscow, RUS\"."
      33            8 :   [:not-in
      34           36 :    [:coalesce [:upper [:substring :pt.location ",\\s*([A-Za-z]{3})\\s*$"]] ""]
      35            4 :    country-exclusion/codes])
      36              : 
      37              : 
      38           16 : (def ^:private player-allowed (country-exclusion/allowed-honey :pp.country_code))
      39              : 
      40              : 
      41            4 : (defn- value-for
      42              :   [m k]
      43           14 :   (let [snake-name (.replace (name k) "-" "_")]
      44           53 :     (or (get m k) (get m (name k)) (get m (keyword snake-name)) (get m snake-name))))
      45              : 
      46              : 
      47            4 : (defn- as-int
      48              :   [value]
      49           33 :   (when (some? value) (if (number? value) (int value) (parse-long (str value)))))
      50              : 
      51              : 
      52            4 : (defn- published-draws
      53              :   [competition]
      54           28 :   (filter #(true? (value-for % :published)) (or (value-for competition :draws) [])))
      55              : 
      56              : 
      57            4 : (defn- competition-row
      58              :   [competition]
      59           12 :   (let [draws (vec (published-draws competition))]
      60           18 :     {:name           (value-for competition :name)
      61           12 :      :prize-total    (as-int (value-for competition :prize-total))
      62           40 :      :draw-size      (some->>
      63            2 :                        draws
      64           14 :                        (keep #(as-int (value-for % :size)))
      65            2 :                        seq
      66            2 :                        (apply max))
      67            8 :      :draw-published (boolean (seq draws))}))
      68              : 
      69              : 
      70            4 : (defn- tournament-row
      71              :   [row]
      72           33 :   (let [competitions (mapv competition-row (or (:competitions row) []))
      73           12 :         prizes       (keep :prize-total competitions)]
      74            3 :     (->
      75            3 :       row
      76           18 :       (assoc :competitions competitions
      77           17 :              :prize-total  (when (seq prizes) (reduce + prizes))
      78           15 :              :has-draw     (some? (:event-id row)))
      79           12 :       (update :psa-id as-int)
      80           33 :       (update :video-count #(or (as-int %) 0))
      81           24 :       (as-> tournament (assoc tournament :has-videos (pos? (:video-count tournament)))))))
      82              : 
      83              : 
      84            4 : (defn- ranking-row
      85              :   [row]
      86           12 :   (->
      87            4 :     (reduce
      88            2 :      (fn [entry k]
      89           21 :       (update entry k #(when (some? %) (double %))))
      90            2 :      row
      91            8 :      [:total-points :included-points :average-points])
      92           24 :     (assoc :player {:psa-id       (:player-psa-id row)
      93            6 :                     :full-name    (:player-full-name row)
      94            6 :                     :country-code (:player-country-code row)
      95            6 :                     :photo-url    (:player-photo-url row)
      96            6 :                     :gender       (:player-gender row)})
      97            2 :     (dissoc
      98              :      :player-psa-id
      99              :      :player-full-name
     100              :      :player-country-code
     101              :      :player-photo-url
     102              :      :player-gender)))
     103              : 
     104              : 
     105            4 : (defn- video-row
     106              :   [row]
     107           10 :   (cond-> (update row :match-id str)
     108            8 :     (:published-at row) (update :published-at #(str (.toInstant ^java.sql.Timestamp %)))))
     109              : 
     110              : 
     111            4 : (def ^:private tournament-select
     112           16 :   [[:pt.psa_id :psa-id]
     113           12 :    [:pt.name :name]
     114           12 :    [:pt.slug :slug]
     115           12 :    [:pt.url :url]
     116           24 :    [[:cast :pt.date_start :text] :date-start]
     117           24 :    [[:cast :pt.date_end :text] :date-end]
     118           12 :    [:pt.season :season]
     119           12 :    [:pt.location :location]
     120           12 :    [:pt.website :website]
     121           12 :    [:pt.competitions :competitions]
     122              :    ;; Junior events have no PSA draw; their draw comes from ESF.
     123           36 :    [[:cast [:coalesce :pt.event_id :esf.event_id] :text] :event-id]
     124           12 :    [:e.public_id :event-public-id]
     125           20 :    [(source-sync/iso-utc :e.source_synced_at) :synced-at]
     126           12 :    [:esf.url :esf-url]
     127              :    ;; Linked YouTube highlight videos, for the calendar and event page counts.
     128           52 :    [{:select [[[:count [:distinct :pmv.video_id]]]]
     129           16 :      :from   [[:psa_match_videos :pmv]]
     130           16 :      :where  [:= :pmv.psa_tournament_id :pt.psa_id]}
     131              :     :video-count]])
     132              : 
     133              : 
     134            4 : (def ^:private tournament-joins
     135           16 :   [[:esf_tournaments :esf]
     136           16 :    [:= :esf.uuid :pt.esf_tournament_id]
     137           12 :    [:events :e]
     138           28 :    [:= :e.uuid [:coalesce :pt.event_id :esf.event_id]]])
     139              : 
     140              : 
     141            4 : (def ^:private player-select
     142           16 :   [[:pp.psa_id :psa-id]
     143           12 :    [:pp.full_name :full-name]
     144           12 :    [:pp.country_code :country-code]
     145           12 :    [:pp.photo_url :photo-url]
     146           12 :    [:pp.gender :gender]])
     147              : 
     148              : 
     149            4 : (defrecord
     150              :   PostgresPsaRepository
     151              :   [ds]
     152              :   PsaPostgresRepository
     153              :   (find-seasons
     154              :    [_]
     155            8 :    (let [query {:select-distinct [:pt.season]
     156            4 :                 :from            [[:psa_tournaments :pt]]
     157            7 :                 :where           [:and [:is-not :pt.season nil] tournament-allowed]
     158            4 :                 :order-by        [[:pt.season :desc]]}]
     159           10 :      (mapv :season (jdbc/execute! ds (sql/format query) query-options))))
     160              :   (find-tournaments
     161              :    [_ season]
     162            3 :    (let [query
     163           21 :          {:select    tournament-select
     164           12 :           :from      [[:psa_tournaments :pt]]
     165            3 :           :left-join tournament-joins
     166           33 :           :where     [:and [:= :pt.season season] tournament-allowed [:= :pt.esf_tournament_id nil]]
     167           21 :           :order-by  [[:pt.date_start :asc] [:pt.name :asc]]}]
     168           30 :      (mapv tournament-row (jdbc/execute! ds (sql/format query) query-options))))
     169              :   (find-tournament
     170              :    [_ psa-id]
     171           14 :    (let [query {:select    tournament-select
     172            8 :                 :from      [[:psa_tournaments :pt]]
     173            2 :                 :left-join tournament-joins
     174           14 :                 :where     [:and [:= :pt.psa_id psa-id] tournament-allowed]}
     175           14 :          row   (jdbc/execute-one! ds (sql/format query) query-options)]
     176           12 :      (when row (tournament-row row))))
     177              :   (find-tournament-videos
     178              :    [_ psa-id]
     179           14 :    (let [query {:select   [[[:cast :pmv.match_id :text] :match-id]
     180            3 :                            [:yv.video_id :video-id]
     181            3 :                            [:pmv.kind :kind]
     182            3 :                            [:yv.title :title]
     183            3 :                            [:yv.published_at :published-at]
     184            3 :                            [:yv.duration_s :duration-s]
     185            3 :                            [:pmv.round_number :round-number]
     186            3 :                            [:yv.playlist_position :playlist-position]]
     187            4 :                 :from     [[:psa_match_videos :pmv]]
     188            4 :                 :join     [[:youtube_videos :yv]
     189            4 :                            [:= :yv.video_id :pmv.video_id]
     190            3 :                            [:psa_tournaments :pt]
     191            4 :                            [:= :pt.psa_id :pmv.psa_tournament_id]]
     192            7 :                 :where    [:and [:= :pmv.psa_tournament_id psa-id] tournament-allowed]
     193            4 :                 :order-by [[:pmv.round_number :asc-nulls-last]
     194            3 :                            [:yv.published_at :asc-nulls-last]
     195            3 :                            [:yv.playlist_position :desc-nulls-last]]}]
     196           10 :      (mapv video-row (jdbc/execute! ds (sql/format query) query-options))))
     197              :   (find-latest-ranking-snapshot-date
     198              :    [_ list-name]
     199           28 :    (let [query {:select [[[:cast [:max :snapshot_date] :text] :snapshot-date]]
     200            4 :                 :from   [:psa_ranking_entries]
     201            8 :                 :where  [:= :list list-name]}]
     202           18 :      (:snapshot-date (jdbc/execute-one! ds (sql/format query) query-options))))
     203              :   (find-ranking-entries
     204              :    [_ list-name snapshot-date]
     205           22 :    (let [query {:select   [[:pre.rank :rank]
     206            6 :                            [:pre.rank_change :rank-change]
     207            6 :                            [:pre.best_rank :best-rank]
     208            6 :                            [:pre.total_points :total-points]
     209            6 :                            [:pre.included_points :included-points]
     210            6 :                            [:pre.average_points :average-points]
     211            6 :                            [:pre.tournaments :tournaments]
     212            6 :                            [:pp.psa_id :player-psa-id]
     213            6 :                            [:pp.full_name :player-full-name]
     214            6 :                            [:pp.country_code :player-country-code]
     215            6 :                            [:pp.photo_url :player-photo-url]
     216            6 :                            [:pp.gender :player-gender]]
     217            8 :                 :from     [[:psa_ranking_entries :pre]]
     218           16 :                 :join     [[:psa_players :pp] [:= :pp.psa_id :pre.psa_player_id]]
     219            4 :                 :where    [:and
     220            8 :                            [:= :pre.list list-name]
     221           14 :                            [:= :pre.snapshot_date [:cast snapshot-date :date]]
     222            2 :                            player-allowed]
     223           14 :                 :order-by [[:pre.rank :asc] [:pp.full_name :asc]]}]
     224           20 :      (mapv ranking-row (jdbc/execute! ds (sql/format query) query-options))))
     225              :   (find-followed-players
     226              :    [_ user-id]
     227            8 :    (let [query {:select   player-select
     228            4 :                 :from     [[:psa_player_follows :f]]
     229            8 :                 :join     [[:psa_players :pp] [:= :pp.psa_id :f.psa_player_id]]
     230           10 :                 :where    [:and [:= :f.user_id [:cast user-id :uuid]] player-allowed]
     231            7 :                 :order-by [[:f.created_datetime :desc] [:pp.full_name :asc]]}]
     232           15 :      (mapv #(assoc % :followed true) (jdbc/execute! ds (sql/format query) query-options)))))
     233              : 
     234              : 
     235            4 : (defn make-repository
     236              :  "Creates a PostgresPsaRepository from a datasource."
     237              :  [ds]
     238           12 :  (->PostgresPsaRepository ds))
        

Generated by: LCOV version 2.0-1