Skip to content

Commit 40c6e09

Browse files
authored
Merge pull request #320 from keymanapp/feat/keyboard-download-stats-by-period-and-id
feat: add keyboard download stats by period
2 parents 7dce728 + 514b59a commit 40c6e09

3 files changed

Lines changed: 109 additions & 27 deletions

File tree

script/statistics/annual-statistics.inc.php

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

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

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;
24-
}
25-
2610
class AnnualStatistics {
2711

2812
function execute($mssql, $startDate, $endDate) {
13+
return $this->_execute('sp_annual_statistics', $mssql, $startDate, $endDate);
14+
}
2915

30-
$stmt = $mssql->prepare('EXEC sp_annual_statistics :prmStartDate, :prmEndDate');
31-
32-
$stmt->bindParam(":prmStartDate", $startDate);
33-
$stmt->bindParam(":prmEndDate", $endDate);
16+
function executeDownloadsByMonth($mssql, $startDate, $endDate) {
17+
return $this->_execute('sp_keyboard_downloads_by_month_statistics', $mssql, $startDate, $endDate);
18+
}
3419

35-
$stmt->execute();
36-
$data = $stmt->fetchAll();
37-
return filter_columns_by_name($data);
20+
function executeKeyboards($mssql, $startDate, $endDate) {
21+
return $this->_execute('sp_statistics_keyboard_downloads_by_id', $mssql, $startDate, $endDate);
3822
}
3923

40-
function executeDownloadsByMonth($mssql, $startDate, $endDate) {
41-
$stmt = $mssql->prepare('EXEC sp_keyboard_downloads_by_month_statistics :prmStartDate, :prmEndDate');
24+
private function _execute($proc, $mssql, $startDate, $endDate) {
25+
$stmt = $mssql->prepare("EXEC $proc :prmStartDate, :prmEndDate");
4226

4327
$stmt->bindParam(":prmStartDate", $startDate);
4428
$stmt->bindParam(":prmEndDate", $endDate);
4529

4630
$stmt->execute();
4731
$data = $stmt->fetchAll();
48-
return filter_columns_by_name($data);
32+
return $this->filter_columns_by_name($data);
33+
}
34+
35+
// strip out repeated columns with numeric keys (by default the results returned
36+
// give each column twice, once with a column name, and once with a column index)
37+
private function filter_columns_by_name($data) {
38+
$result = [];
39+
foreach($data as $row) {
40+
$r = [];
41+
foreach($row as $id => $val) {
42+
if(!is_numeric($id)) {
43+
$r[$id] = is_numeric($val) ? intval($val) : $val;
44+
}
45+
}
46+
array_push($result, $r);
47+
}
48+
return $result;
4949
}
5050
}

script/statistics/keyboards.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* Keyman is copyright (C) SIL Global. MIT License.
4+
*
5+
* Basic keyboard download statistics for SIL reports
6+
*/
7+
8+
require_once(__DIR__ . '/../../tools/util.php');
9+
10+
allow_cors();
11+
json_response();
12+
13+
require_once(__DIR__ . '/../../tools/db/db.php');
14+
require_once(__DIR__ . '/annual-statistics.inc.php');
15+
require_once __DIR__ . '/../../tools/autoload.php';
16+
use Keyman\Site\Common\KeymanHosts;
17+
$mssql = Keyman\Site\com\keyman\api\Tools\DB\DBConnect::Connect();
18+
19+
if(!isset($_REQUEST['startDate']) || !isset($_REQUEST['endDate'])) {
20+
fail('startDate, endDate parameters must be set');
21+
}
22+
23+
$startDate = $_REQUEST['startDate'];
24+
$endDate = $_REQUEST['endDate'];
25+
26+
/**
27+
* https://api.keyman.com/script/statistics/keyboards.php
28+
*/
29+
30+
$stats = new \Keyman\Site\com\keyman\api\AnnualStatistics();
31+
$keyboards = $stats->executeKeyboards($mssql, $startDate, $endDate);
32+
33+
if(isset($_REQUEST['csv'])) {
34+
$out = fopen('php://output', 'w');
35+
36+
if(sizeof($keyboards) > 0) {
37+
$data = array_keys($keyboards[0]);
38+
fputcsv($out, $data, ',', '"', '');
39+
foreach($keyboards as $row) {
40+
$data = array_values($row);
41+
fputcsv($out, $data, ',', '"', '');
42+
}
43+
}
44+
45+
fclose($out);
46+
} else {
47+
$data = ["keyboards" => $keyboards];
48+
json_print($data);
49+
}

tools/db/build/annual-statistics.sql

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
users or keyboards in use, but gives a rough volume.
2323
*/
2424

25+
/* TODO(lowpri): rename these procs to sp_statistics_xxxx */
26+
2527
DROP PROCEDURE IF EXISTS sp_annual_statistics;
2628
GO
2729

@@ -40,6 +42,8 @@ SELECT
4042
(select sum(count) from kstats.t_keyboard_downloads WHERE statdate >= @prmStartDate AND statdate < @prmEndDate) RawKeyboardDownloadCount
4143
GO
4244

45+
/* ======================================================================== */
46+
4347
DROP PROCEDURE IF EXISTS sp_keyboard_downloads_by_month_statistics;
4448
GO
4549

@@ -57,4 +61,33 @@ CREATE PROCEDURE sp_keyboard_downloads_by_month_statistics (
5761
WHERE statdate >= @prmStartDate AND statdate < @prmEndDate
5862
group by month(statdate), year(statdate)
5963
order by 2, 1
60-
GO
64+
GO
65+
66+
/* ======================================================================== */
67+
68+
DROP PROCEDURE IF EXISTS sp_statistics_keyboard_downloads_by_id;
69+
GO
70+
71+
CREATE PROCEDURE sp_statistics_keyboard_downloads_by_id (
72+
@prmStartDate DATE,
73+
@prmEndDate DATE
74+
) AS
75+
76+
DECLARE @DayCount INT = DATEDIFF(day,@prmStartDate,@prmEndDate) + 1
77+
78+
SELECT
79+
k.keyboard_id,
80+
k.name,
81+
SUM(count) RawKeyboardDownloadCount,
82+
SUM(count)/@DayCount DownloadsPerDay
83+
FROM
84+
k0.t_keyboard k LEFT JOIN
85+
kstats.t_keyboard_downloads d ON d.keyboard_id = k.keyboard_id
86+
WHERE
87+
(d.statdate >= @prmStartDate AND d.statdate < @prmEndDate) OR (d.keyboard_id IS NULL)
88+
GROUP BY
89+
k.keyboard_id,
90+
k.name
91+
ORDER BY
92+
k.keyboard_id
93+
GO

0 commit comments

Comments
 (0)