com web/doc-editor: Compute usage statistic for the editor : nb connection, nb commit, etc... per month: install/doc-editor.sql
php/UsageStatistics.php scripts/cron/compute_usage_statistics.php
| From: | Yannick Torres | Date: | Thu, 15 Nov 2012 16:59:58 +0000 |
| Subject: | com web/doc-editor: Compute usage statistic for the editor : nb connection, nb commit, etc... per month: install/doc-editor.sql php/UsageStatistics.php scripts/cron/compute_usage_statistics.php |
||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-10722@lists.php.net to get a copy of this message | ||
Commit: 3fe9e572161235d1b9a4dfebaca8513ae668f9b3
Author: Yannick Torrès <yannick.torres@gmail.com> Thu, 15 Nov 2012 17:59:58 +0100
Parents: a69cf11dc3d1c6a302f67447f9d3255510150130
Branches: master
Link: http://git.php.net/?p=web/doc-editor.git;a=commitdiff;h=3fe9e572161235d1b9a4dfebaca8513ae668f9b3
Log:
Compute usage statistic for the editor : nb connection, nb commit, etc... per month
Changed paths:
M install/doc-editor.sql
A php/UsageStatistics.php
A scripts/cron/compute_usage_statistics.php
diff --git a/install/doc-editor.sql b/install/doc-editor.sql index ffcb3ff..d7b6bed 100644 --- a/install/doc-editor.sql +++ b/install/doc-editor.sql @@ -230,3 +230,17 @@ CREATE TABLE IF NOT EXISTS `patches` ( `date` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + +-- +-- Structure of table `usageStatistics` +-- + +CREATE TABLE IF NOT EXISTS `usageStatistics` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `project` varchar(255) CHARACTER SET utf8 NOT NULL, + `type` varchar(50) CHARACTER SET utf8 NOT NULL, + `subType` varchar(50) CHARACTER SET utf8 NOT NULL, + `value` bigint(20) unsigned DEFAULT NULL, + `yearMonth` date NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; \ No newline at end of file diff --git a/php/UsageStatistics.php b/php/UsageStatistics.php new file mode 100644 index 0000000..b54dd05 --- /dev/null +++ b/php/UsageStatistics.php @@ -0,0 +1,213 @@ +<?php + +require_once dirname(__FILE__) . '/DBConnection.php'; + +class UsageStatistics { + + private static $instance; + + private $conn; + + /** + * @static + * @return UserNotes + */ + public static function getInstance() + { + if (!isset(self::$instance)) { + $c = __CLASS__; + self::$instance = new $c; + } + return self::$instance; + } + + public function __construct() + { + $this->conn = DBConnection::getInstance(); + } + + public function computeAll($startYear) + { + $_date = new DateTime($startYear.'-01-01'); + $now = new DateTime(); + $intervalle = new DateInterval('P1M'); + $now->add($intervalle); // We add one month to all wile statment to compute current month + + while( $_date->format('Y-m') != $now->format('Y-m') ) { + + echo 'Compute usage stats for '.$_date->format('Y-m').'...'; + $this->computeMonth($_date->format('Y-m')); + echo "Done !\n"; + + $_date->add($intervalle); + + } + + + } + + public function computeMonth($yearMonth) + { + $am = AccountManager::getInstance(); + $project = $am->project; + + echo 'Compute usage stats for '.$yearMonth.'...'; + + // We get all info for this Year-Month + // $yearMonth is something like '2012-11' + $tmp = explode('-', $yearMonth); + $year = $tmp[0]; + $month = $tmp[1]; + + $s = 'SELECT + * + FROM + `staticValue` + WHERE + `type` = "info" AND + YEAR(`date`) = "%s" AND + MONTH(`date`) = "%s" AND + `project` = "%s"'; + + $params = array( + $year, + $month, + $project + ); + + $r = $this->conn->query($s, $params); + + $stats = array( + "nbCon"=> array( + "total" => 0, + "authService" => array( + "VCS" => 0, + "google" => 0, + "facebook" => 0 + ) + ), + "nbCreatedFiles"=>0, + "nbDeletedFiles"=>0, + "nbUpdatedFiles"=>0 + ); + + while ($a = $r->fetch_object()) + { + $i = ""; + + // for connexions + if( $a->field == "login" ) + { + $i = json_decode($a->value); + $stats["nbCon"]["authService"][$i->authService] ++; + $stats["nbCon"]["total"] ++; + } + + // for commit + if( $a->field == "commitFiles" ) + { + $i = json_decode($a->value); + $stats['nbCreatedFiles'] += $i->nbFilesCreate; + $stats['nbDeletedFiles'] += $i->nbFilesDelete; + $stats['nbUpdatedFiles'] += $i->nbFilesUpdate; + } + + } + + // Now we have stats for this Year-Month, we add it to DB + + $this->addModStat('nbCon', 'Total', $stats["nbCon"]["total"], $yearMonth); + $this->addModStat('nbCon', 'VCS', $stats["nbCon"]["authService"]["VCS"], $yearMonth); + $this->addModStat('nbCon', 'google', $stats["nbCon"]["authService"]["google"], $yearMonth); + $this->addModStat('nbCon', 'facebook', $stats["nbCon"]["authService"]["facebook"], $yearMonth); + $this->addModStat('nbCreatedFiles', '', $stats["nbCreatedFiles"], $yearMonth); + $this->addModStat('nbDeletedFiles', '', $stats["nbDeletedFiles"], $yearMonth); + $this->addModStat('nbUpdatedFiles', '', $stats["nbUpdatedFiles"], $yearMonth); + + echo "Done !\n"; + } + + private function addModStat($type, $subType, $value, $yearMonth) + { + $am = AccountManager::getInstance(); + $project = $am->project; + + $tmp = explode('-', $yearMonth); + $year = $tmp[0]; + $month = $tmp[1]; + + $s = ' + SELECT + id + FROM + `usageStatistics` + WHERE + `type` = "%s" AND + `subType` = "%s" AND + YEAR(`yearMonth`) = "%s" AND + MONTH(`yearMonth`) = "%s" AND + `project` = "%s" + '; + $params = array( + $type, + $subType, + $year, + $month, + $project + ); + + $r = $this->conn->query($s, $params); + $nb = $r->num_rows; + + if( $nb == 0 ) { + // It's the first time we compute stats for this Year-Month. We add them into DB + $s = 'INSERT INTO + `usageStatistics` + ( + `project`, + `type`, + `subType`, + `value`, + `yearMonth` + ) VALUES ( + "%s", + "%s", + "%s", + "%s", + "%s" + )'; + + $params = array( + $project, + $type, + $subType, + $value, + $year.'-'.$month.'-01' + ); + $r = $this->conn->query($s, $params); + + } else { + $a = $r->fetch_object(); + + // It's not the first time, we update the record + $s = 'UPDATE + `usageStatistics` + SET + `value`="%s" + WHERE + `id`="%s" + '; + + $params = array( + $value, + $a->id + ); + + $r = $this->conn->query($s, $params); + + } + + } +} + +?> diff --git a/scripts/cron/compute_usage_statistics.php b/scripts/cron/compute_usage_statistics.php new file mode 100644 index 0000000..092ef15 --- /dev/null +++ b/scripts/cron/compute_usage_statistics.php @@ -0,0 +1,44 @@ +<?php + +/*** +* This script is intended to be placed in a cronjob. +* It must be run every Thursday, at 01hOO for example. +* On Unix, you can use crontab -e and place this : +* 00 01 * * 4 /path/php/binary /path/to/your/vcs/dir/doc-editor/scripts/cron/compute_usage_statistics.php +****/ + +require_once dirname(__FILE__) . '/../../php/Conf.php'; +require_once dirname(__FILE__) . '/../../php/ProjectManager.php'; +require_once dirname(__FILE__) . '/../../php/UsageStatistics.php'; +require_once dirname(__FILE__) . '/../../php/DBConnection.php'; + +$pm = ProjectManager::getInstance(); +$db = DBConnection::getInstance(); +$us = UsageStatistics::getInstance(); + +$availableProject = $pm->getAvailableProject(); + +while( list($key, $project) = each($availableProject) ) { + + // Define it as a project + $pm->setProject($project['code']); + + // Just for the first run - We check if the usageStatistics table is empty + $s = 'SELECT COUNT(*) as total FROM `usageStatistics`'; + $r = $db->query($s, array()); + $a = $r->fetch_object(); + + if( $a->total == 0 ) { + $us->computeAll('2012'); // Place here the Year we want to start to compute statistics (this Year was included) + exit; + } + + // We start compute stats for yesterday's month + $yesterday = new DateTime(); + $yesterday->sub(new DateInterval('P1D')); + + $us->computeMonth($yesterday->format('Y-m')); + +} + +?> \ No newline at end of file
diff --git a/install/doc-editor.sql b/install/doc-editor.sql index ffcb3ff..d7b6bed 100644 --- a/install/doc-editor.sql +++ b/install/doc-editor.sql @@ -230,3 +230,17 @@ CREATE TABLE IF NOT EXISTS `patches` ( `date` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + +-- +-- Structure of table `usageStatistics` +-- + +CREATE TABLE IF NOT EXISTS `usageStatistics` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `project` varchar(255) CHARACTER SET utf8 NOT NULL, + `type` varchar(50) CHARACTER SET utf8 NOT NULL, + `subType` varchar(50) CHARACTER SET utf8 NOT NULL, + `value` bigint(20) unsigned DEFAULT NULL, + `yearMonth` date NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; \ No newline at end of file diff --git a/php/UsageStatistics.php b/php/UsageStatistics.php new file mode 100644 index 0000000..b54dd05 --- /dev/null +++ b/php/UsageStatistics.php @@ -0,0 +1,213 @@ +<?php + +require_once dirname(__FILE__) . '/DBConnection.php'; + +class UsageStatistics { + + private static $instance; + + private $conn; + + /** + * @static + * @return UserNotes + */ + public static function getInstance() + { + if (!isset(self::$instance)) { + $c = __CLASS__; + self::$instance = new $c; + } + return self::$instance; + } + + public function __construct() + { + $this->conn = DBConnection::getInstance(); + } + + public function computeAll($startYear) + { + $_date = new DateTime($startYear.'-01-01'); + $now = new DateTime(); + $intervalle = new DateInterval('P1M'); + $now->add($intervalle); // We add one month to all wile statment to compute current month + + while( $_date->format('Y-m') != $now->format('Y-m') ) { + + echo 'Compute usage stats for '.$_date->format('Y-m').'...'; + $this->computeMonth($_date->format('Y-m')); + echo "Done !\n"; + + $_date->add($intervalle); + + } + + + } + + public function computeMonth($yearMonth) + { + $am = AccountManager::getInstance(); + $project = $am->project; + + echo 'Compute usage stats for '.$yearMonth.'...'; + + // We get all info for this Year-Month + // $yearMonth is something like '2012-11' + $tmp = explode('-', $yearMonth); + $year = $tmp[0]; + $month = $tmp[1]; + + $s = 'SELECT + * + FROM + `staticValue` + WHERE + `type` = "info" AND + YEAR(`date`) = "%s" AND + MONTH(`date`) = "%s" AND + `project` = "%s"'; + + $params = array( + $year, + $month, + $project + ); + + $r = $this->conn->query($s, $params); + + $stats = array( + "nbCon"=> array( + "total" => 0, + "authService" => array( + "VCS" => 0, + "google" => 0, + "facebook" => 0 + ) + ), + "nbCreatedFiles"=>0, + "nbDeletedFiles"=>0, + "nbUpdatedFiles"=>0 + ); + + while ($a = $r->fetch_object()) + { + $i = ""; + + // for connexions + if( $a->field == "login" ) + { + $i = json_decode($a->value); + $stats["nbCon"]["authService"][$i->authService] ++; + $stats["nbCon"]["total"] ++; + } + + // for commit + if( $a->field == "commitFiles" ) + { + $i = json_decode($a->value); + $stats['nbCreatedFiles'] += $i->nbFilesCreate; + $stats['nbDeletedFiles'] += $i->nbFilesDelete; + $stats['nbUpdatedFiles'] += $i->nbFilesUpdate; + } + + } + + // Now we have stats for this Year-Month, we add it to DB + + $this->addModStat('nbCon', 'Total', $stats["nbCon"]["total"], $yearMonth); + $this->addModStat('nbCon', 'VCS', $stats["nbCon"]["authService"]["VCS"], $yearMonth); + $this->addModStat('nbCon', 'google', $stats["nbCon"]["authService"]["google"], $yearMonth); + $this->addModStat('nbCon', 'facebook', $stats["nbCon"]["authService"]["facebook"], $yearMonth); + $this->addModStat('nbCreatedFiles', '', $stats["nbCreatedFiles"], $yearMonth); + $this->addModStat('nbDeletedFiles', '', $stats["nbDeletedFiles"], $yearMonth); + $this->addModStat('nbUpdatedFiles', '', $stats["nbUpdatedFiles"], $yearMonth); + + echo "Done !\n"; + } + + private function addModStat($type, $subType, $value, $yearMonth) + { + $am = AccountManager::getInstance(); + $project = $am->project; + + $tmp = explode('-', $yearMonth); + $year = $tmp[0]; + $month = $tmp[1]; + + $s = ' + SELECT + id + FROM + `usageStatistics` + WHERE + `type` = "%s" AND + `subType` = "%s" AND + YEAR(`yearMonth`) = "%s" AND + MONTH(`yearMonth`) = "%s" AND + `project` = "%s" + '; + $params = array( + $type, + $subType, + $year, + $month, + $project + ); + + $r = $this->conn->query($s, $params); + $nb = $r->num_rows; + + if( $nb == 0 ) { + // It's the first time we compute stats for this Year-Month. We add them into DB + $s = 'INSERT INTO + `usageStatistics` + ( + `project`, + `type`, + `subType`, + `value`, + `yearMonth` + ) VALUES ( + "%s", + "%s", + "%s", + "%s", + "%s" + )'; + + $params = array( + $project, + $type, + $subType, + $value, + $year.'-'.$month.'-01' + ); + $r = $this->conn->query($s, $params); + + } else { + $a = $r->fetch_object(); + + // It's not the first time, we update the record + $s = 'UPDATE + `usageStatistics` + SET + `value`="%s" + WHERE + `id`="%s" + '; + + $params = array( + $value, + $a->id + ); + + $r = $this->conn->query($s, $params); + + } + + } +} + +?> diff --git a/scripts/cron/compute_usage_statistics.php b/scripts/cron/compute_usage_statistics.php new file mode 100644 index 0000000..092ef15 --- /dev/null +++ b/scripts/cron/compute_usage_statistics.php @@ -0,0 +1,44 @@ +<?php + +/*** +* This script is intended to be placed in a cronjob. +* It must be run every Thursday, at 01hOO for example. +* On Unix, you can use crontab -e and place this : +* 00 01 * * 4 /path/php/binary /path/to/your/vcs/dir/doc-editor/scripts/cron/compute_usage_statistics.php +****/ + +require_once dirname(__FILE__) . '/../../php/Conf.php'; +require_once dirname(__FILE__) . '/../../php/ProjectManager.php'; +require_once dirname(__FILE__) . '/../../php/UsageStatistics.php'; +require_once dirname(__FILE__) . '/../../php/DBConnection.php'; + +$pm = ProjectManager::getInstance(); +$db = DBConnection::getInstance(); +$us = UsageStatistics::getInstance(); + +$availableProject = $pm->getAvailableProject(); + +while( list($key, $project) = each($availableProject) ) { + + // Define it as a project + $pm->setProject($project['code']); + + // Just for the first run - We check if the usageStatistics table is empty + $s = 'SELECT COUNT(*) as total FROM `usageStatistics`'; + $r = $db->query($s, array()); + $a = $r->fetch_object(); + + if( $a->total == 0 ) { + $us->computeAll('2012'); // Place here the Year we want to start to compute statistics (this Year was included) + exit; + } + + // We start compute stats for yesterday's month + $yesterday = new DateTime(); + $yesterday->sub(new DateInterval('P1D')); + + $us->computeMonth($yesterday->format('Y-m')); + +} + +?> \ No newline at end of file