From 35b5f35144cd05af8c711cde71d996871db28c7e Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 12 Mar 2014 09:07:06 +0100 Subject: [PATCH] Bug 11926: Render community koha statistic usages Goals: - Collecting Koha usage statistics - Rendering stats on a community website - Having a big bicture of how koha is used 3 parts in the project: - this patch in koha - hea-ws which collects data - hea-app which renders data Installation: 1/ Fill systempreferences: UsageStatsLastUpdateTime UsageStatsID UsageStatsShare UsageStatsLibraryName 2/ Setup a cron in your crontab (ex: at 3:00 every first of the month): 0 3 1 * * export KOHA_CONF=/home/koha/etc/koha-conf.xml; export PERL5LIB=/home/koha/src; perl /home/koha/src/C4/UsageStats.pm --- C4/UsageStats.pm | 95 ++++++++++++++++++++ installer/data/mysql/sysprefs.sql | 4 + installer/data/mysql/updatedatabase.pl | 10 ++ .../prog/en/modules/admin/preferences/admin.pref | 17 ++++ 4 files changed, 126 insertions(+), 0 deletions(-) create mode 100644 C4/UsageStats.pm diff --git a/C4/UsageStats.pm b/C4/UsageStats.pm new file mode 100644 index 0000000..5338df9 --- /dev/null +++ b/C4/UsageStats.pm @@ -0,0 +1,95 @@ +package UsageStats; + +# Copyright 2000-2003 Katipo Communications +# Copyright 2010 BibLibre +# Parts Copyright 2010 Catalyst IT +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use C4::Context; +use POSIX qw(strftime); +use LWP::UserAgent; +use JSON; + +sub NeedUpdate { + my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0; + my $now = strftime("%s", localtime); + + # Need to launch cron. + return 1 if $now - $lastupdated >= 2592000; + + # Cron no need to be launched. + return 0; +} + +sub LaunchCron { + if (!C4::Context->preference('UsageStatsShare')) { + die ("UsageStats is not configured"); + } + if (NeedUpdate) { + C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime)); + my $data = BuildReport(); + ReportToComunity($data); + } +} + +sub BuildReport { + my $report = { + 'library' => { + 'name' => C4::Context->preference('UsageStatsLibraryName'), + 'id' => C4::Context->preference('UsageStatsID') || 0, + }, + }; + + # Get database volumetry. + foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) { + $report->{volumetry}{$_} = _count($_); + } + + # Get systempreferences. + foreach (qw/IntranetBiblioDefaultView marcflavour/) { + $report->{systempreferences}{$_} = C4::Context->preference($_); + } + return $report; +} + +sub ReportToComunity { + my $data = shift; + my $json = to_json($data); + + my $url = C4::Context->config('mebaseurl'); + + my $ua = LWP::UserAgent->new; + my $req = HTTP::Request->new(POST => "$url/upload.pl"); + $req->content_type('application/x-www-form-urlencoded'); + $req->content("data=$json"); + my $res = $ua->request($req); + my $content = from_json($res->decoded_content); + C4::Context->set_preference('UsageStatsID', $content->{library}{library_id}); +} + +sub _count { + my $table = shift; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT count(*) from $table"); + $sth->execute; + return $sth->fetchrow_array; +} + +&LaunchCron; +1; diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 661910b..cb2987a 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -403,6 +403,10 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('UpdateTotalIssuesOnCirc','0',NULL,'Whether to update the totalissues field in the biblio on each circ.','YesNo'), ('uppercasesurnames','0',NULL,'If ON, surnames are converted to upper case in patron entry form','YesNo'), ('URLLinkText','',NULL,'Text to display as the link anchor in the OPAC','free'), +('UsageStatsLastUpdateTime', '', '', '', 'Free'), +('UsageStatsID', '', '', '', 'Free'), +('UsageStatsShare', 'no', '0|1|2', 'Tell if you want to share you data volumetry with koha community. You can be anonymous', 'Choice'), +('UsageStatsLibraryName', '', '', 'Your library name shown on koha community site. Can be disabled with UsageStatsShare system preference.', 'Free'), ('UseAuthoritiesForTracings','1','0','Use authority record numbers for subject tracings instead of heading strings.','YesNo'), ('UseBranchTransferLimits','0','','If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.','YesNo'), ('UseControlNumber','0','','If ON, record control number (w subfields) and control number (001) are used for linking of bibliographic records.','YesNo'), diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 125a668..42c3ec6 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8056,6 +8056,16 @@ if ( CheckVersion($DBversion) ) { SetVersion ($DBversion); } +$DBversion = "3.15.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsLastUpdateTime', '', '', '', 'Free');"); + $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsID', '', '', '', 'Free');"); + $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsShare', 'no', '0|1|2', 'Tell if you want to share you data volumetry with koha community. You can be anonymous', 'Choice');"); + $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsLibraryName', '', '', 'Your library name shown on koha community site. Can be disabled with UsageStatsShare system preference.', 'Free');"); + print "Upgrade to $DBversion done (Bug 11926: Setup basic config for community usage koha statistics)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 1215ac1..7a4b296 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -113,3 +113,20 @@ Administration: Solr: Solr Zebra: Zebra - is the search engine used. + Usage Stats: + - + - pref: UsageStatsLastUpdateTime + default: 0 + - stores the last time when cron were launch + - + - pref: UsageStatsID + default: + - + - + - pref: UsageStatsShare + default: + - + - + - pref: UsageStatsLibraryName + default: + - -- 1.7.2.5