Skip to content

Commit

Permalink
feat: annual statistics for reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdurdin committed Oct 30, 2024
1 parent 54f7e4f commit 8bd7f9e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
28 changes: 28 additions & 0 deletions script/statistics/annual-statistics.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Basic annual statistics for SIL reports
*/

namespace Keyman\Site\com\keyman\api;

function filter_columns_by_name($key) {
return !is_numeric($key);
}

class AnnualStatistics {

function execute($mssql, $startDate, $endDate) {

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

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

$stmt->execute();
$data = $stmt->fetchAll()[0];
$data = array_filter($data, "Keyman\\Site\\com\\keyman\\api\\filter_columns_by_name", ARRAY_FILTER_USE_KEY );
return $data;
}
}
32 changes: 32 additions & 0 deletions script/statistics/annual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Basic annual 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/annual.php
*/

$stats = new \Keyman\Site\com\keyman\api\AnnualStatistics();
$data = $stats->execute($mssql, $startDate, $endDate);
json_print($data);
40 changes: 40 additions & 0 deletions tools/db/build/annual-statistics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Basic annual statistics for SIL reports -- SQL Query
*/

/*
Some rough notes:
* We have, in our cloud database <AllKeyboards> keyboards
* Of these, <CurrentKeyboards> are listed as "not obsolete". Obsolete
keyboards are keyboards which have been renamed and for which there is a new
version, or keyboards which are non-Unicode.
* We updated <ModifiedKeyboards> keyboards from startDate-endDate, according
to each keyboard's last update date. This can be anything from a metadata
change to significant keyboard rewrite.
* We list <LanguageCount> languages today, for <LanguageKeyboardPairs>
language:keyboard pairs. Many keyboards support more than one language.
* We list <LexicalModelCount> lexical models.
* <RawKeyboardDownloadCount> lists the total number of downloads of keyboards
through keyman.com over the last year. This does not match the number of
users or keyboards in use, but gives a rough volume.
*/

DROP PROCEDURE IF EXISTS sp_annual_statistics;
GO

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

SELECT
(select count(*) from k0.t_keyboard) AS AllKeyboards,
(select count(*) from k0.t_keyboard where obsolete = 0) AS CurrentKeyboards,
(select count(*) from k0.t_keyboard where last_modified >= @prmStartDate and last_modified < @prmEndDate) AS ModifiedKeyboards,
(select count(distinct tag) from k0.t_keyboard_langtag) AS LanguageCount,
(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
1 change: 1 addition & 0 deletions tools/db/build/build.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function BuildDatabase($DBDataSources, $schema, $do_force) {
$this->sqlrun($script);
}

$this->sqlrun(dirname(__FILE__)."/annual-statistics.sql");
$this->sqlrun(dirname(__FILE__)."/model-queries.sql");
$this->sqlrun(dirname(__FILE__)."/legacy-queries.sql");
$this->sqlrun(dirname(__FILE__)."/legacy-statistics.sql");
Expand Down

0 comments on commit 8bd7f9e

Please sign in to comment.