Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: annual statistics for reporting #261

Merged
merged 1 commit into from
Oct 30, 2024
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
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
Loading