Skip to content

Commit 7dce728

Browse files
authored
Merge pull request #317 from keymanapp/fix/stats
feat: add extra download stats
2 parents cb8aa35 + d583f3a commit 7dce728

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

script/statistics/annual-statistics.inc.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@
77

88
namespace Keyman\Site\com\keyman\api;
99

10-
function filter_columns_by_name($key) {
11-
return !is_numeric($key);
10+
// strip out repeated columns with numeric keys (by default the results returned
11+
// give each column twice, once with a column name, and once with a column index)
12+
function filter_columns_by_name($data) {
13+
$result = [];
14+
foreach($data as $row) {
15+
$r = [];
16+
foreach($row as $id => $val) {
17+
if(!is_numeric($id)) {
18+
$r[$id] = intval($val);
19+
}
20+
}
21+
array_push($result, $r);
22+
}
23+
return $result;
1224
}
1325

1426
class AnnualStatistics {
@@ -22,7 +34,17 @@ function execute($mssql, $startDate, $endDate) {
2234

2335
$stmt->execute();
2436
$data = $stmt->fetchAll();
25-
//$data = array_filter($data, "Keyman\\Site\\com\\keyman\\api\\filter_columns_by_name", ARRAY_FILTER_USE_KEY );
26-
return $data;
37+
return filter_columns_by_name($data);
38+
}
39+
40+
function executeDownloadsByMonth($mssql, $startDate, $endDate) {
41+
$stmt = $mssql->prepare('EXEC sp_keyboard_downloads_by_month_statistics :prmStartDate, :prmEndDate');
42+
43+
$stmt->bindParam(":prmStartDate", $startDate);
44+
$stmt->bindParam(":prmEndDate", $endDate);
45+
46+
$stmt->execute();
47+
$data = $stmt->fetchAll();
48+
return filter_columns_by_name($data);
2749
}
2850
}

script/statistics/annual.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@
2828
*/
2929

3030
$stats = new \Keyman\Site\com\keyman\api\AnnualStatistics();
31-
$data = $stats->execute($mssql, $startDate, $endDate);
31+
$summary = $stats->execute($mssql, $startDate, $endDate);
32+
$downloads = $stats->executeDownloadsByMonth($mssql, $startDate, $endDate);
33+
$data = ["summary" => $summary, "keyboardDownloadsByMonth" => $downloads];
3234
json_print($data);

tools/db/build/annual-statistics.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,23 @@ SELECT
3838
(select count(*) from k0.t_keyboard_langtag) AS LanguageKeyboardPairs,
3939
(select count(*) from k0.t_model) AS LexicalModelCount,
4040
(select sum(count) from kstats.t_keyboard_downloads WHERE statdate >= @prmStartDate AND statdate < @prmEndDate) RawKeyboardDownloadCount
41+
GO
42+
43+
DROP PROCEDURE IF EXISTS sp_keyboard_downloads_by_month_statistics;
44+
GO
45+
46+
CREATE PROCEDURE sp_keyboard_downloads_by_month_statistics (
47+
@prmStartDate DATE,
48+
@prmEndDate DATE
49+
) AS
50+
51+
select
52+
month(statdate) Month,
53+
year(statdate) Year,
54+
sum(count) RawKeyboardDownloadCount,
55+
sum(count)/day(eomonth(datefromparts(year(statdate),month(statdate),1))) DownloadsPerDay
56+
from kstats.t_keyboard_downloads
57+
WHERE statdate >= @prmStartDate AND statdate < @prmEndDate
58+
group by month(statdate), year(statdate)
59+
order by 2, 1
60+
GO

0 commit comments

Comments
 (0)