From 707cdc139acbce2164a8a579fede7903a4cae513 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 Signed-off-by: Chris Cormack --- C4/UsageStats.pm | 95 ++++++++++++++++++++++ .../prog/en/modules/admin/preferences/admin.pref | 25 ++++++ 2 files changed, 120 insertions(+) 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/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index c16138c..bbdcc73 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 @@ -105,3 +105,28 @@ Administration: yes: Allow no: "Don't Allow" - Mozilla persona for login + Search Engine: + - + - pref: SearchEngine + default: Zebra + choices: + 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.9.1