From c761cef8b62dfdd7e6e813a7f393c1ab7d0fd925 Mon Sep 17 00:00:00 2001
From: "Eduardo L. Buratti" <elb09@c3sl.ufpr.br>
Date: Thu, 31 Oct 2013 22:02:53 -0200
Subject: [PATCH] web: Add report generation

Signed-off-by: Eduardo L. Buratti <elb09@c3sl.ufpr.br>
---
 web/assets/js/attendance.js | 10 +++++-
 web/reports/build-report.sh | 10 +++++-
 web/routes/charts.js        | 64 +++++++++++++++++++++++++++++++++++++
 web/server.js               |  2 ++
 4 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/web/assets/js/attendance.js b/web/assets/js/attendance.js
index fec5b52..808c042 100644
--- a/web/assets/js/attendance.js
+++ b/web/assets/js/attendance.js
@@ -197,7 +197,7 @@ angular.module('datasid.attendance', []).
                 config.plotOptions.series.events = {
                     click: function(event) {
                         $scope.$apply(function () {
-                            $location.path($location.path() + '/' + event.point.category[0]);
+                            $scope.barChart.click(event);
                         });
                     }
                 };
@@ -206,6 +206,14 @@ angular.module('datasid.attendance', []).
                 $scope.barChart.load();
             },
 
+            click: function (event) {
+                if (typeof $state.params.state !== 'undefined') {
+                    window.open('/api/reports/'+$state.current.project+'/avail_report/'+event.point.category[0], '_blank');
+                }
+                else
+                    $location.path($location.path() + '/' + event.point.category[0]);
+            },
+
             load: function () {
                 var options = {
                     project: $state.current.project,
diff --git a/web/reports/build-report.sh b/web/reports/build-report.sh
index 7eb0365..53b9a2e 100755
--- a/web/reports/build-report.sh
+++ b/web/reports/build-report.sh
@@ -1,3 +1,11 @@
 #!/bin/bash
 
-java -cp .:lib/* ReportBuilder $*
+cd $(dirname $(readlink -f $0))
+
+tmp=$(mktemp)
+
+java -cp .:lib/* ReportBuilder $* > $tmp
+
+echo -n $tmp
+
+cd - >/dev/null 2>&1
diff --git a/web/routes/charts.js b/web/routes/charts.js
index da98c45..d873419 100644
--- a/web/routes/charts.js
+++ b/web/routes/charts.js
@@ -1,3 +1,6 @@
+var exec = require('child_process').exec,
+    fs = require('fs');
+
 exports.get_data = function(req, res) {
     if (typeof req.params.project === 'undefined')
         return res.json(400, {error: 'missing_project'});
@@ -132,3 +135,64 @@ exports.get_data = function(req, res) {
 
     res.json(400, {error: 'invalid_params'});
 }
+
+exports.get_report = function(req, res) {
+    if (typeof req.params.project === 'undefined')
+        return res.json(400, {error: 'missing_project'});
+
+    if (typeof req.params.type === 'undefined')
+        return res.json(400, {error: 'missing_report_type'});
+
+    /*
+     * routes = { 'project': [{ type: 'type', file: 'file', params: 'params}, ...]}
+     */
+    var routes = {
+        'tlbr': [
+            /* Availability */
+            {
+                type: 'avail_report', file: 'telecentroAvail.jrxml'
+                , params: [ req.params.id_city ]
+            }
+        ],
+
+        'gesac': [
+            /* Availability */
+            {
+                type: 'avail_report', file: 'gesacAvail.jrxml'
+                , params: [ req.params.id_city ]
+            }
+        ]
+    };
+
+    var project = req.params.project;
+    if (routes[project] === 'undefined')
+        return res.json(400, {error: 'invalid_project'});
+
+    var queries = routes[project];
+    for (var i=0; i<queries.length; i++) {
+        var query = queries[i];
+        if ( query.type === req.params.type ) {
+            for (var p=0; p<query.params.length; p++) {
+                query.params[p] = query.params[p] || null;
+            }
+
+            var cmdline = 'reports/build-report.sh '+query.file+' '+query.params.join(' ');
+
+            exec(cmdline, function (err, stdout, stderr) {
+                if (err) {
+                    console.log(err);
+                    return res.json(500, {error: 'report_building_failed'});
+                }
+
+                res.type('application/pdf');
+                res.sendfile(stdout, function (err) {
+                    fs.unlink(stdout);
+                });
+            });
+
+            return;
+        }
+    }
+
+    res.json(400, {error: 'invalid_params'});
+}
diff --git a/web/server.js b/web/server.js
index 989e495..10ce519 100755
--- a/web/server.js
+++ b/web/server.js
@@ -22,6 +22,8 @@ app.all('/api/points/count', db.connect, points.count);
 
 app.all('/api/telecenters/:state?/:city_id?/:telecenter_id?', db.connect, telecenters.list);
 
+app.get('/api/reports/:project/:type/:id_city', db.connect, charts.get_report);
+
 app.get('/api/:project/:type', db.connect, charts.get_data);
 app.get('/api/:project/:type/:region', db.connect, charts.get_data);
 app.get('/api/:project/:type/:region/:state', db.connect, charts.get_data);
-- 
GitLab