From 95047f5d570b20fbc3359b1247327099e399cd8b Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Mon, 21 Feb 2011 08:35:33 +0100 Subject: [PATCH] add points archiver, manages the upper limit of time in placing to produce exactly midnight hour statistics --- TODO.txt | 3 + sql/STORAGE.txt | 3 + web/briskin5/statadm.php | 115 +++++++++++++++++++++++++++++++++++---- 3 files changed, 110 insertions(+), 11 deletions(-) diff --git a/TODO.txt b/TODO.txt index 3922384..dcddeb2 100644 --- a/TODO.txt +++ b/TODO.txt @@ -14,6 +14,9 @@ DONE - produrle DONE - mostrarle + STEP 1.1 + - points archiver + STEP 1.5 - sistema di inserimento nuovi utenti (riversatore da form di inserimento) diff --git a/sql/STORAGE.txt b/sql/STORAGE.txt index 749e71f..df9bc21 100644 --- a/sql/STORAGE.txt +++ b/sql/STORAGE.txt @@ -1,3 +1,6 @@ +== how to move match == +select m.code, m.ttok, min(g.tstamp) from bsk_bin5_matches as m, bsk_bin5_games as g where g.mcode = m.code GROUP BY m.code, m.ttok; + == rende la view dei punteggi == select m.ttok, m.tidx, g.code, g.tstamp, p.pts, u.login from bsk_bin5_points as p, bsk_bin5_games as g, bsk_bin5_matches as m, bsk_users as u where p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND g.tstamp > '2010-10-01 00:00:00' ORDER BY g.tstamp; diff --git a/web/briskin5/statadm.php b/web/briskin5/statadm.php index 3c3e954..3c3e3e1 100644 --- a/web/briskin5/statadm.php +++ b/web/briskin5/statadm.php @@ -25,11 +25,6 @@ /* line example: 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1| - -TODO: - - update the STAT file with differential - - recalculate points - */ @@ -45,6 +40,7 @@ require_once("Obj/placing.phh"); function main_file($curtime) { + GLOBAL $G_alarm_passwd; $tri = array(); $mon = array(); $wee = array(); @@ -225,14 +221,109 @@ function main_pgsql($curtime) log_crit("statadm: begin failed"); break; } + + $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp + FROM %sbin5_matches as m, %sbin5_games as g + WHERE g.mcode = m.code GROUP BY m.code, m.ttok", + $G_dbpfx, $G_dbpfx, $G_dbpfx); + // error_log($mtc_sql, 0); + if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) { + log_crit("statadm: temporary matches table creation [$mtc_sql] failed"); + break; + } + + $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE tstamp < to_timestamp(%d)", + $G_dbpfx, $curtime - TRI_LIMIT); + // error_log($tmt_sql, 0); + + // if deletable old matches exists then ... + if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) { + // + // store matches before clean them + // + $fname = sprintf("%s/pts_archive%s.log", LEGAL_PATH, date("Ymd", $curtime)); + if (($fp = @fopen($fname, 'a')) == FALSE) { + log_crit("statadm: log file [$fname] open failed"); + break; + } + + $tmt_n = pg_numrows($tmt_pg); + // get matches + for ($m = 0 ; $m < $tmt_n ; $m++) { + $tmt_obj = pg_fetch_object($tmt_pg, $m); + + $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d", + $G_dbpfx, $tmt_obj->code); + + error_log($mtc_sql, 0); + if (($mtc_pg = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) { + log_crit("statadm: matches row select failed"); + break; + } + $mtc_obj = pg_fetch_object($mtc_pg, 0); + + if (fwrite($fp, sprintf("M|%d|%s|%d|\n", $mtc_obj->code, xcapelt($mtc_obj->ttok), $mtc_obj->tidx)) == FALSE) { + log_crit("statadm: log file [$fname] write match failed"); + break; + } + + // get games associated to each match + $gam_sql = sprintf("SELECT code, mcode, EXTRACT(epoch FROM tstamp) AS tstamp FROM %sbin5_games + WHERE mcode = %d ORDER BY tstamp", + $G_dbpfx, $mtc_obj->code); + if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE) { + log_crit("statadm: games row select failed"); + break; + } + + $gam_n = pg_numrows($gam_pg); + for ($g = 0 ; $g < $gam_n ; $g++) { + $gam_obj = pg_fetch_object($gam_pg, $g); + + if (fwrite($fp, sprintf("G|%d|%d|%d|\n", $gam_obj->mcode, $gam_obj->code, $gam_obj->tstamp)) == FALSE) { + log_crit("statadm: log file [$fname] write game failed"); + break; + } + $pts_sql = sprintf("SELECT * FROM %sbin5_points WHERE gcode = %d", $G_dbpfx, $gam_obj->code); + if (($pts_pg = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) { + log_crit("statadm: points row select [$pts_sql] failed"); + break; + } + $pts_n = pg_numrows($pts_pg); + for ($p = 0 ; $p < $pts_n ; $p++) { + $pts_obj = pg_fetch_object($pts_pg, $p); + + if (fwrite($fp, sprintf("P|%d|%d|%d|\n", $pts_obj->gcode, $pts_obj->ucode, $pts_obj->pts)) == FALSE) { + log_crit("statadm: log file [$fname] write pts failed"); + break; + } + } + if ($p < $pts_n) + break; + } + if ($g < $gam_n) + break; + + // delete match and all it's childs (games and points) + $del_sql = sprintf("DELETE FROM %sbin5_matches WHERE code = %d", + $G_dbpfx, $tmt_obj->code); + if (($del_pg = pg_query($bdb->dbconn->db(),$del_sql)) == FALSE || pg_affected_rows($del_pg) != 1) { + log_crit("statadm: matches row deletion failed"); + break; + } + + } + if ($m < $tmt_n) + break; + } // if (($tmt_pg = pg_query($bdb->dbco... - // Truncate table (postgresql extension, in other SQL you must user unqualified DELETE + // GEN: Truncate table (postgresql extension, in other SQL you must user unqualified DELETE $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx); if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) { log_crit("statadm: truncate failed"); break; } - + for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) { $old_score = array( 1000000000, 1000000000); $old_gam = array( -1, -1); @@ -241,15 +332,15 @@ function main_pgsql($curtime) $pla_sql = sprintf("SELECT (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) as score, sum(p.pts) as points, count(p.pts) as games, u.code as ucode, u.login as login FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u WHERE p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND - g.tstamp > to_timestamp(%d) + g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d) GROUP BY u.code, u.login ORDER BY (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) DESC, count(p.pts) DESC", - $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $ming[$dtime]); + $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $curtime); // log_crit("statadm: INFO: [$pla_sql]"); - if (($pla_pg = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) { + if (($pla_pg = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE) { // no point found, abort log_crit("statadm: main placement select failed [$pla_sql]"); break; @@ -324,7 +415,9 @@ function main() $fun_name = "main_${G_dbasetype}"; - $curtime = time(); + $ctime = time(); + + $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600); if ($ret = $fun_name($curtime)) echo "Success.
\n"; else -- 2.17.1