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

            Line data    Source code
       1            4 : (ns matchscore.domain.player-follow.rankings
       2              :   "Current rankings of imported players (PSA, ESF, FSU), for profiles
       3              :   and the followed players list.
       4              : 
       5              :   A ranking is the player's place in the newest snapshot of one list:
       6              :   {:rank 3 :name \"world_male\" :list \"world_male\" :category-id nil}.
       7              :   `:list` and `:category-id` select that list on the source's
       8              :   rankings page:
       9              :   - psa: :list is world_male / world_female
      10              :   - esf: :list is the circuit (junior, masters), :category-id the category
      11              :   - fsu: :list is the rating category (Absolute, U13...)
      12              : 
      13              :   Rankings come best first, in the order the player search picks its rank: PSA by
      14              :   place, ESF age categories before Overall, FSU Absolute first."
      15              :   (:require
      16              :    [clojure.string :as str]
      17              :    [next.jdbc :as jdbc]
      18              :    [next.jdbc.result-set :as rs]))
      19              : 
      20              : 
      21           20 : (def sources #{"psa" "esf" "fsu"})
      22              : 
      23              : 
      24            4 : (def ^:private rankings-sql
      25              :   "One placeholder: a text[] of player ids."
      26           28 :   {"psa"
      27              :    "WITH latest AS (
      28              :       SELECT list, max(snapshot_date) AS snapshot_date FROM psa_ranking_entries GROUP BY list)
      29              :     SELECT r.psa_player_id::text AS player_id, r.rank, r.list AS name, r.list,
      30              :            NULL::int AS category_id
      31              :       FROM psa_ranking_entries r
      32              :       JOIN latest l ON l.list = r.list AND l.snapshot_date = r.snapshot_date
      33              :      WHERE r.psa_player_id = ANY(?::text[]::int[])
      34              :      ORDER BY r.rank, r.list"
      35              :    "esf"
      36              :    "WITH latest AS (
      37              :       SELECT circuit, max(publication_id) AS publication_id
      38              :         FROM esf_ranking_categories GROUP BY circuit),
      39              :     entries AS (
      40              :       SELECT DISTINCT ON (e.esid, c.category_id)
      41              :              substr(e.esid, 3) AS player_id, e.rank, c.name, c.circuit AS list,
      42              :              c.category_id
      43              :         FROM esf_ranking_entries e
      44              :         JOIN esf_ranking_categories c
      45              :           ON c.publication_id = e.publication_id AND c.category_id = e.category_id
      46              :         JOIN latest l ON l.circuit = c.circuit AND l.publication_id = c.publication_id
      47              :        WHERE e.esid = ANY(?::text[])
      48              :        ORDER BY e.esid, c.category_id, e.rank)
      49              :     SELECT * FROM entries ORDER BY (name ILIKE '%overall%'), rank, name"
      50              :    "fsu"
      51              :    "WITH latest AS (
      52              :       SELECT category, max(snapshot_date) AS snapshot_date FROM fsu_rating_entries GROUP BY category)
      53              :     SELECT e.crm_profile_id::text AS player_id, e.place AS rank, e.category AS name,
      54              :            e.category AS list, NULL::int AS category_id
      55              :       FROM fsu_rating_entries e
      56              :       JOIN latest l ON l.category = e.category AND l.snapshot_date = e.snapshot_date
      57              :      WHERE e.crm_profile_id = ANY(?::text[]::int[])
      58              :      ORDER BY (e.category <> 'Absolute'), e.place, e.category"})
      59              : 
      60              : 
      61            4 : (def ^:private id-pattern
      62              :   "Ids each source's query can cast; anything else is dropped before querying."
      63           28 :   {"psa" #"[0-9]{1,9}"
      64              :    "esf" #"[0-9]{1,12}"
      65              :    "fsu" #"[0-9]{1,9}"})
      66              : 
      67              : 
      68            4 : (defn current-rankings
      69              :  "Map of player id → that player's current rankings on `source`, best first. Player
      70              :   ids are the source's own ids as strings (PSA id, ESF id number, FSU CRM
      71              :   profile id). Players without a ranking are absent."
      72              :  [ds source player-ids]
      73            4 :  (let [pattern (id-pattern source)
      74            2 :        ids     (into
      75            1 :                 []
      76            2 :                 (comp
      77            3 :                  (map
      78           23 :                   #(some->
      79              :                      %
      80            1 :                      str
      81            1 :                      str/trim
      82            1 :                      str/lower-case))
      83           15 :                  (filter #(and pattern % (re-matches pattern %)))
      84            2 :                  (distinct))
      85            1 :                 player-ids)]
      86            4 :    (if (empty? ids)
      87            1 :      {}
      88           14 :      (let [params (if (= "esf" source) (mapv #(str "ES" %) ids) ids)
      89            2 :            rows   (jdbc/execute!
      90            1 :                    ds
      91            8 :                    [(rankings-sql source) (into-array String params)]
      92            3 :                    {:builder-fn rs/as-unqualified-kebab-maps})]
      93            2 :        (reduce
      94            1 :         (fn
      95              :          [acc
      96              :           {:keys [player-id]
      97              :            :as   row}]
      98           12 :          (update acc player-id (fnil conj []) (dissoc row :player-id)))
      99            1 :         {}
     100            1 :         rows)))))
        

Generated by: LCOV version 2.0-1