From ff914aaf7aff575b342179d3fbd1f2c0d86fadbd Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Thu, 4 Dec 2025 17:10:22 +0100 Subject: [PATCH 1/2] feat: add extra download stats Test-bot: skip --- script/statistics/annual-statistics.inc.php | 29 ++++++++++++++++++--- script/statistics/annual.php | 4 ++- tools/db/build/annual-statistics.sql | 20 ++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/script/statistics/annual-statistics.inc.php b/script/statistics/annual-statistics.inc.php index f194de1..af4a8cd 100644 --- a/script/statistics/annual-statistics.inc.php +++ b/script/statistics/annual-statistics.inc.php @@ -7,8 +7,19 @@ namespace Keyman\Site\com\keyman\api; - function filter_columns_by_name($key) { - return !is_numeric($key); + // strip out repeated columns with numeric keys + function filter_columns_by_name($data) { + $result = []; + foreach($data as $row) { + $r = []; + foreach($row as $id => $val) { + if(!is_numeric($id)) { + $r[$id] = intval($val); + } + } + array_push($result, $r); + } + return $result; } class AnnualStatistics { @@ -22,7 +33,17 @@ function execute($mssql, $startDate, $endDate) { $stmt->execute(); $data = $stmt->fetchAll(); - //$data = array_filter($data, "Keyman\\Site\\com\\keyman\\api\\filter_columns_by_name", ARRAY_FILTER_USE_KEY ); - return $data; + return filter_columns_by_name($data); + } + + function executeDownloadsByMonth($mssql, $startDate, $endDate) { + $stmt = $mssql->prepare('EXEC sp_keyboard_downloads_by_month_statistics :prmStartDate, :prmEndDate'); + + $stmt->bindParam(":prmStartDate", $startDate); + $stmt->bindParam(":prmEndDate", $endDate); + + $stmt->execute(); + $data = $stmt->fetchAll(); + return filter_columns_by_name($data); } } diff --git a/script/statistics/annual.php b/script/statistics/annual.php index fe617bd..c9b2df0 100644 --- a/script/statistics/annual.php +++ b/script/statistics/annual.php @@ -28,5 +28,7 @@ */ $stats = new \Keyman\Site\com\keyman\api\AnnualStatistics(); - $data = $stats->execute($mssql, $startDate, $endDate); + $summary = $stats->execute($mssql, $startDate, $endDate); + $downloads = $stats->executeDownloadsByMonth($mssql, $startDate, $endDate); + $data = ["summary" => $summary, "keyboardDownloadsByMonth" => $downloads]; json_print($data); diff --git a/tools/db/build/annual-statistics.sql b/tools/db/build/annual-statistics.sql index 5d04e97..6cbf826 100644 --- a/tools/db/build/annual-statistics.sql +++ b/tools/db/build/annual-statistics.sql @@ -38,3 +38,23 @@ SELECT (select count(*) from k0.t_keyboard_langtag) AS LanguageKeyboardPairs, (select count(*) from k0.t_model) AS LexicalModelCount, (select sum(count) from kstats.t_keyboard_downloads WHERE statdate >= @prmStartDate AND statdate < @prmEndDate) RawKeyboardDownloadCount +GO + +DROP PROCEDURE IF EXISTS sp_keyboard_downloads_by_month_statistics; +GO + +CREATE PROCEDURE sp_keyboard_downloads_by_month_statistics ( + @prmStartDate DATE, + @prmEndDate DATE +) AS + + select + month(statdate) Month, + year(statdate) Year, + sum(count) RawKeyboardDownloadCount, + sum(count)/day(eomonth(datefromparts(year(statdate),month(statdate),1))) DownloadsPerDay + from kstats.t_keyboard_downloads + WHERE statdate >= @prmStartDate AND statdate < @prmEndDate + group by month(statdate), year(statdate) + order by 2, 1 +GO \ No newline at end of file From d583f3a4a6e2ca2549b0e82f825c66bddd2a530f Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Fri, 5 Dec 2025 11:14:35 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Eberhard Beilharz --- script/statistics/annual-statistics.inc.php | 3 ++- tools/db/build/annual-statistics.sql | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/script/statistics/annual-statistics.inc.php b/script/statistics/annual-statistics.inc.php index af4a8cd..96f50d8 100644 --- a/script/statistics/annual-statistics.inc.php +++ b/script/statistics/annual-statistics.inc.php @@ -7,7 +7,8 @@ namespace Keyman\Site\com\keyman\api; - // strip out repeated columns with numeric keys + // strip out repeated columns with numeric keys (by default the results returned + // give each column twice, once with a column name, and once with a column index) function filter_columns_by_name($data) { $result = []; foreach($data as $row) { diff --git a/tools/db/build/annual-statistics.sql b/tools/db/build/annual-statistics.sql index 6cbf826..8307055 100644 --- a/tools/db/build/annual-statistics.sql +++ b/tools/db/build/annual-statistics.sql @@ -52,7 +52,7 @@ CREATE PROCEDURE sp_keyboard_downloads_by_month_statistics ( month(statdate) Month, year(statdate) Year, sum(count) RawKeyboardDownloadCount, - sum(count)/day(eomonth(datefromparts(year(statdate),month(statdate),1))) DownloadsPerDay + sum(count)/day(eomonth(datefromparts(year(statdate),month(statdate),1))) DownloadsPerDay from kstats.t_keyboard_downloads WHERE statdate >= @prmStartDate AND statdate < @prmEndDate group by month(statdate), year(statdate)