Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions script/statistics/annual-statistics.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@

namespace Keyman\Site\com\keyman\api;

// 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) {
$r = [];
foreach($row as $id => $val) {
if(!is_numeric($id)) {
$r[$id] = intval($val);
}
}
array_push($result, $r);
}
return $result;
}

class AnnualStatistics {

function execute($mssql, $startDate, $endDate) {
return $this->_execute('sp_annual_statistics', $mssql, $startDate, $endDate);
}

$stmt = $mssql->prepare('EXEC sp_annual_statistics :prmStartDate, :prmEndDate');

$stmt->bindParam(":prmStartDate", $startDate);
$stmt->bindParam(":prmEndDate", $endDate);
function executeDownloadsByMonth($mssql, $startDate, $endDate) {
return $this->_execute('sp_keyboard_downloads_by_month_statistics', $mssql, $startDate, $endDate);
}

$stmt->execute();
$data = $stmt->fetchAll();
return filter_columns_by_name($data);
function executeKeyboards($mssql, $startDate, $endDate) {
return $this->_execute('sp_statistics_keyboard_downloads_by_id', $mssql, $startDate, $endDate);
}

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

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

$stmt->execute();
$data = $stmt->fetchAll();
return filter_columns_by_name($data);
return $this->filter_columns_by_name($data);
}

// 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)
private function filter_columns_by_name($data) {
$result = [];
foreach($data as $row) {
$r = [];
foreach($row as $id => $val) {
if(!is_numeric($id)) {
$r[$id] = is_numeric($val) ? intval($val) : $val;
}
}
array_push($result, $r);
}
return $result;
}
}
49 changes: 49 additions & 0 deletions script/statistics/keyboards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Basic keyboard download statistics for SIL reports
*/

require_once(__DIR__ . '/../../tools/util.php');

allow_cors();
json_response();

require_once(__DIR__ . '/../../tools/db/db.php');
require_once(__DIR__ . '/annual-statistics.inc.php');
require_once __DIR__ . '/../../tools/autoload.php';
use Keyman\Site\Common\KeymanHosts;
$mssql = Keyman\Site\com\keyman\api\Tools\DB\DBConnect::Connect();

if(!isset($_REQUEST['startDate']) || !isset($_REQUEST['endDate'])) {
fail('startDate, endDate parameters must be set');
}

$startDate = $_REQUEST['startDate'];
$endDate = $_REQUEST['endDate'];

/**
* https://api.keyman.com/script/statistics/keyboards.php
*/

$stats = new \Keyman\Site\com\keyman\api\AnnualStatistics();
$keyboards = $stats->executeKeyboards($mssql, $startDate, $endDate);

if(isset($_REQUEST['csv'])) {
$out = fopen('php://output', 'w');

if(sizeof($keyboards) > 0) {
$data = array_keys($keyboards[0]);
fputcsv($out, $data, ',', '"', '');
foreach($keyboards as $row) {
$data = array_values($row);
fputcsv($out, $data, ',', '"', '');
}
}

fclose($out);
} else {
$data = ["keyboards" => $keyboards];
json_print($data);
}
35 changes: 34 additions & 1 deletion tools/db/build/annual-statistics.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
users or keyboards in use, but gives a rough volume.
*/

/* TODO(lowpri): rename these procs to sp_statistics_xxxx */

DROP PROCEDURE IF EXISTS sp_annual_statistics;
GO

Expand All @@ -40,6 +42,8 @@ SELECT
(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

Expand All @@ -57,4 +61,33 @@ CREATE PROCEDURE sp_keyboard_downloads_by_month_statistics (
WHERE statdate >= @prmStartDate AND statdate < @prmEndDate
group by month(statdate), year(statdate)
order by 2, 1
GO
GO

/* ======================================================================== */

DROP PROCEDURE IF EXISTS sp_statistics_keyboard_downloads_by_id;
GO

CREATE PROCEDURE sp_statistics_keyboard_downloads_by_id (
@prmStartDate DATE,
@prmEndDate DATE
) AS

DECLARE @DayCount INT = DATEDIFF(day,@prmStartDate,@prmEndDate) + 1

SELECT
k.keyboard_id,
k.name,
SUM(count) RawKeyboardDownloadCount,
SUM(count)/@DayCount DownloadsPerDay
FROM
k0.t_keyboard k LEFT JOIN
kstats.t_keyboard_downloads d ON d.keyboard_id = k.keyboard_id
WHERE
(d.statdate >= @prmStartDate AND d.statdate < @prmEndDate) OR (d.keyboard_id IS NULL)
GROUP BY
k.keyboard_id,
k.name
ORDER BY
k.keyboard_id
GO