feat: Add history caching

This commit is contained in:
overflowerror 2024-08-03 18:17:14 +02:00
parent 005dc8026b
commit daa84d14c7
4 changed files with 68 additions and 14 deletions

6
bin/cron/updateCache.php Normal file
View file

@ -0,0 +1,6 @@
<?php
require_once __DIR__ . "/../../core.php";
require_once __DIR__ . "/../../lib/updateCache.php";
updateCache();

View file

@ -1,7 +1,7 @@
<?php <?php
require_once __DIR__ . "/../core.php"; require_once __DIR__ . "/../../core.php";
require_once __DIR__ . "/../lib/updateData.php"; require_once __DIR__ . "/../../lib/updateData.php";
echo "Loading mob list...\n"; echo "Loading mob list...\n";
$mobs = getMobs(); $mobs = getMobs();

24
lib/updateCache.php Normal file
View file

@ -0,0 +1,24 @@
<?php
function updateCache(): void {
global $pdo;
$query = $pdo->query(<<<EOF
INSERT INTO mm_history_cache
SELECT
ratings,
last_update
FROM mm_rating_history
WHERE last_update > (
SELECT max(last_update)
FROM (
SELECT last_update
FROM mm_history_cache
UNION ALL
SELECT 0
) AS with_default
)
EOF
);
$query->execute();
}

View file

@ -23,6 +23,12 @@ create table mm_matches
session varchar(255) session varchar(255)
); );
create table public.mm_history_cache
(
ratings jsonb not null,
last_update bigint not null
);
CREATE VIEW mm_matches_of_mob(id, mob, opponent, won, created) AS CREATE VIEW mm_matches_of_mob(id, mob, opponent, won, created) AS
SELECT mm_matches.id, SELECT mm_matches.id,
mm_matches.mob1fk AS mob, mm_matches.mob1fk AS mob,
@ -40,18 +46,33 @@ SELECT mm_matches.id,
mm_matches.session mm_matches.session
FROM mm_matches; FROM mm_matches;
CREATE VIEW mm_current_rating(mob, rating) AS CREATE VIEW mm_rating_history(ratings, last_update) AS
WITH RECURSIVE ratings_history (ratings, last_update) AS ( WITH RECURSIVE ratings_history (ratings, last_update) AS (
WITH ratings_seed (ratings, last_update) AS (
SELECT
jsonb_object_agg(id, start_value) AS ratings,
0::bigint AS last_update
FROM mm_mobs
CROSS JOIN
(
SELECT
1500 AS start_value
) AS start_value
WHERE enabled
UNION ALL
SELECT
ratings,
last_update
FROM mm_history_cache
)
SELECT SELECT
jsonb_object_agg(id, start_value) AS ratings, ratings,
0::bigint AS last_update last_update
FROM mm_mobs FROM ratings_seed
CROSS JOIN WHERE
( last_update = (
SELECT SELECT max(last_update) FROM ratings_seed
1500 AS start_value )
) AS start_value
WHERE enabled
UNION ALL UNION ALL
SELECT SELECT
jsonb_set( jsonb_set(
@ -133,6 +154,9 @@ WITH RECURSIVE ratings_history (ratings, last_update) AS (
) AS expectation ) AS expectation
) AS new_ratings ) AS new_ratings
) )
SELECT * from ratings_history;
CREATE VIEW mm_current_rating(mob, rating) AS
SELECT SELECT
cast(key as numeric) as mob, cast(key as numeric) as mob,
cast(value as numeric) as rating cast(value as numeric) as rating
@ -140,10 +164,10 @@ FROM jsonb_each(
( (
SELECT SELECT
ratings ratings
FROM ratings_history FROM mm_rating_history
WHERE last_update = ( WHERE last_update = (
SELECT max(last_update) SELECT max(last_update)
FROM ratings_history FROM mm_rating_history
) )
) )
); );