From d2561018ab53d6bd5aa787aefbac82b55a5f88ca Mon Sep 17 00:00:00 2001
From: "Eduardo L. Buratti" <elb09@c3sl.ufpr.br>
Date: Thu, 31 Oct 2013 11:32:59 -0200
Subject: [PATCH] web: Fix urls with spaces and other invalid characters

To avoid the problem of urls with spaces and weird characters (like
"/api/tlbr/avail_hist/SUL/PR/LAGOA D'ANTA"), use id_city and id_point
instead of city and telecenter names. Now queries return an "id", which
is used to build the urls, and a "cat" representing the full name to be
displayed.

Signed-off-by: Eduardo L. Buratti <elb09@c3sl.ufpr.br>
---
 web/assets/js/attendance.js                   | 24 +++++++++----------
 web/queries/tlbr/avail/current.sql            | 14 +++++------
 web/queries/tlbr/avail/hist.sql               |  4 ++--
 web/queries/tlbr/avail/sub_cities.sql         | 13 +++++-----
 web/queries/tlbr/avail/sub_regions.sql        |  3 ++-
 web/queries/tlbr/avail/sub_states.sql         |  3 ++-
 web/queries/tlbr/avail/sub_telecenters.sql    | 19 ++++++++-------
 web/queries/tlbr/net_usage/sub_cities.sql     |  3 ++-
 web/queries/tlbr/net_usage/sub_regions.sql    |  3 ++-
 web/queries/tlbr/net_usage/sub_states.sql     |  3 ++-
 .../tlbr/net_usage/sub_telecenters.sql        |  5 ++--
 web/queries/tlbr/net_usage/telecenter.sql     |  9 ++++---
 web/routes/charts.js                          | 10 ++++----
 web/server.js                                 |  3 ++-
 14 files changed, 62 insertions(+), 54 deletions(-)

diff --git a/web/assets/js/attendance.js b/web/assets/js/attendance.js
index aab5542..1d019ab 100644
--- a/web/assets/js/attendance.js
+++ b/web/assets/js/attendance.js
@@ -82,7 +82,7 @@ angular.module('datasid.attendance', []).
             }).
 
             state('attendance.telecentrosbr.network_usage-telecenter', {
-                url: '/network_usage/:region/:state/:city/:id_telecenter',
+                url: '/network_usage/:region/:state/:city/:id_point',
                 controller: 'NetworkUsageTelecenterCtrl',
                 templateUrl: 'partials/attendance.network_usage.telecenter.html',
                 section: 'attendance'
@@ -91,7 +91,7 @@ angular.module('datasid.attendance', []).
     }).
 
     factory('AvailFactory', function($resource) {
-        return $resource('/api/tlbr/:type/:region/:state/:city/:id_telecenter');
+        return $resource('/api/tlbr/:type/:region/:state/:city/:id_point');
     }).
 
     controller('AttendanceCtrl', function ($scope, $rootScope) {
@@ -105,7 +105,7 @@ angular.module('datasid.attendance', []).
                 config.plotOptions.series.events = {
                     click: function(event) {
                         $scope.$apply(function () {
-                            $location.path($location.path() + '/' + event.point.category);
+                            $location.path($location.path() + '/' + event.point.category[0]);
                         });
                     }
                 };
@@ -119,7 +119,7 @@ angular.module('datasid.attendance', []).
                     region: $state.params.region || null,
                     state: $state.params.state || null,
                     city: $state.params.city || null,
-                    id_telecenter: $state.params.id_telecenter || null
+                    id_point: $state.params.id_point || null
                 };
 
                 if (typeof $state.params.city !== 'undefined')
@@ -153,7 +153,7 @@ angular.module('datasid.attendance', []).
 
                     for (var i=0; i<data.length; i++) {
                         var d = data[i];
-                        categories.push(d.cat);
+                        categories.push([d.id, d.cat]);
                         green.push(parseInt(d.green));
                         yellow.push(parseInt(d.yellow));
                         red.push(parseInt(d.red));
@@ -185,7 +185,7 @@ angular.module('datasid.attendance', []).
                     region: $state.params.region || null,
                     state: $state.params.state || null,
                     city: $state.params.city || null,
-                    id_telecenter: $state.params.id_telecenter || null
+                    id_point: $state.params.id_point || null
                 };
 
                 AvailFactory.query(options, function (data) {
@@ -213,7 +213,7 @@ angular.module('datasid.attendance', []).
                     region: $state.params.region || null,
                     state: $state.params.state || null,
                     city: $state.params.city || null,
-                    id_telecenter: $state.params.id_telecenter || null
+                    id_point: $state.params.id_point || null
                 };
 
                 AvailFactory.query(options, function (data) {
@@ -241,7 +241,7 @@ angular.module('datasid.attendance', []).
 
     /* NETWORK USAGE */
     factory('NetworkUsageFactory', function($resource) {
-        return $resource('/api/tlbr/:type/:region/:state/:city/:id_telecenter');
+        return $resource('/api/tlbr/:type/:region/:state/:city/:id_point');
     }).
 
     controller('NetworkUsageCtrl', function ($scope, $rootScope, $state, $location, NetworkUsageFactory) {
@@ -254,7 +254,7 @@ angular.module('datasid.attendance', []).
                     {name: 'Upload'}
                 ];
                 config.tooltip.formatter = function() {
-                    var s = '<b>'+ this.x +'</b>';
+                    var s = '<b>'+ this.x[1] +'</b>';
 
                     for (var i = 0; i < this.points.length; i++) {
                         var point = this.points[i];
@@ -267,7 +267,7 @@ angular.module('datasid.attendance', []).
                 config.plotOptions.series.events = {
                     click: function(event) {
                         $scope.$apply(function () {
-                            $location.path($location.path() + '/' + event.point.category);
+                            $location.path($location.path() + '/' + event.point.category[0]);
                         });
                     }
                 };
@@ -281,7 +281,7 @@ angular.module('datasid.attendance', []).
                     region: $state.params.region || null,
                     state: $state.params.state || null,
                     city: $state.params.city || null,
-                    id_telecenter: $state.params.id_telecenter || null
+                    id_point: $state.params.id_point || null
                 };
 
                 if (typeof $state.params.city !== 'undefined')
@@ -314,7 +314,7 @@ angular.module('datasid.attendance', []).
 
                     for (var i=0; i<data.length; i++) {
                         var d = data[i];
-                        categories.push(d.cat);
+                        categories.push([d.id, d.cat]);
                         down.push(parseInt(d.down));
                         up.push(parseInt(d.up));
                     }
diff --git a/web/queries/tlbr/avail/current.sql b/web/queries/tlbr/avail/current.sql
index 450ab79..bd23296 100644
--- a/web/queries/tlbr/avail/current.sql
+++ b/web/queries/tlbr/avail/current.sql
@@ -1,11 +1,11 @@
 SELECT
-    SUM(is_green) AS green,
-    SUM(is_yellow) AS yellow,
-    SUM(is_red) AS red
+    SUM(a.is_green) AS green,
+    SUM(a.is_yellow) AS yellow,
+    SUM(a.is_red) AS red
 FROM
-    aggr_availability
+    aggr_availability a JOIN point p ON a.id_point = p.id
 WHERE
     base_date = (SELECT max(base_date) FROM aggr_availability) AND
-    ($1::text IS NULL OR region = $1::text) AND
-    ($2::text IS NULL OR state = $2::text) AND
-    ($3::text IS NULL OR city = $3::text);
\ No newline at end of file
+    ($1::text IS NULL OR a.region = $1::text) AND
+    ($2::text IS NULL OR a.state = $2::text) AND
+    ($3::bigint IS NULL OR p.id_city = $3::bigint)
\ No newline at end of file
diff --git a/web/queries/tlbr/avail/hist.sql b/web/queries/tlbr/avail/hist.sql
index 373d24b..64385cc 100644
--- a/web/queries/tlbr/avail/hist.sql
+++ b/web/queries/tlbr/avail/hist.sql
@@ -4,12 +4,12 @@ SELECT
     SUM(is_yellow) AS yellow,
     SUM(is_red) AS red
 FROM
-    aggr_availability
+    aggr_availability a JOIN point p ON a.id_point = p.id
 WHERE
     base_date = (SELECT max(base_date) FROM aggr_availability) AND
     ($1::text IS NULL OR region = $1::text) AND
     ($2::text IS NULL OR state = $2::text) AND
-    ($3::text IS NULL OR city = $3::text)
+    ($3::bigint IS NULL OR p.id_city = $3::bigint)
 GROUP BY
     base_date
 ORDER BY
diff --git a/web/queries/tlbr/avail/sub_cities.sql b/web/queries/tlbr/avail/sub_cities.sql
index c88677d..5728d2e 100644
--- a/web/queries/tlbr/avail/sub_cities.sql
+++ b/web/queries/tlbr/avail/sub_cities.sql
@@ -1,13 +1,14 @@
 SELECT
-    city AS cat,
-    SUM(is_green) AS green,
-    SUM(is_yellow) AS yellow,
-    SUM(is_red) AS red
+    p.id_city AS id,
+    INITCAP(a.city) AS cat,
+    SUM(a.is_green) AS green,
+    SUM(a.is_yellow) AS yellow,
+    SUM(a.is_red) AS red
 FROM
-    aggr_availability
+    aggr_availability a JOIN point p ON a.id_point = p.id
 WHERE
     base_date = (SELECT max(base_date) FROM aggr_availability) AND
     ($1::text IS NULL OR region = $1::text) AND
     ($2::text IS NULL OR state = $2::text)
 GROUP BY
-    city;
+    p.id_city, a.city;
diff --git a/web/queries/tlbr/avail/sub_regions.sql b/web/queries/tlbr/avail/sub_regions.sql
index 05ee84f..d9c3706 100644
--- a/web/queries/tlbr/avail/sub_regions.sql
+++ b/web/queries/tlbr/avail/sub_regions.sql
@@ -1,5 +1,6 @@
 SELECT
-    region AS cat,
+    UPPER(region) AS id,
+    INITCAP(region) AS cat,
     SUM(is_green) AS green,
     SUM(is_yellow) AS yellow,
     SUM(is_red) AS red
diff --git a/web/queries/tlbr/avail/sub_states.sql b/web/queries/tlbr/avail/sub_states.sql
index 2bdb215..f3f7d66 100644
--- a/web/queries/tlbr/avail/sub_states.sql
+++ b/web/queries/tlbr/avail/sub_states.sql
@@ -1,5 +1,6 @@
 SELECT
-    state AS cat,
+    UPPER(state) AS id,
+    UPPER(state) AS cat,
     SUM(is_green) AS green,
     SUM(is_yellow) AS yellow,
     SUM(is_red) AS red
diff --git a/web/queries/tlbr/avail/sub_telecenters.sql b/web/queries/tlbr/avail/sub_telecenters.sql
index a4d2c74..682783e 100644
--- a/web/queries/tlbr/avail/sub_telecenters.sql
+++ b/web/queries/tlbr/avail/sub_telecenters.sql
@@ -1,14 +1,15 @@
 SELECT
-    tc_name AS cat,
-    SUM(is_green) AS green,
-    SUM(is_yellow) AS yellow,
-    SUM(is_red) AS red
+    p.id AS id,
+    a.tc_name AS cat,
+    SUM(a.is_green) AS green,
+    SUM(a.is_yellow) AS yellow,
+    SUM(a.is_red) AS red
 FROM
-    aggr_availability
+    aggr_availability a JOIN point p ON a.id_point = p.id
 WHERE
     base_date = (SELECT max(base_date) FROM aggr_availability) AND
-    ($1::text IS NULL OR region = $1::text) AND
-    ($2::text IS NULL OR state = $2::text) AND
-    ($3::text IS NULL OR city = $3::text)
+    ($1::text IS NULL OR a.region = $1::text) AND
+    ($2::text IS NULL OR a.state = $2::text) AND
+    ($3::bigint IS NULL OR p.id_city = $3::bigint)
 GROUP BY
-    tc_name;
+    p.id, tc_name;
\ No newline at end of file
diff --git a/web/queries/tlbr/net_usage/sub_cities.sql b/web/queries/tlbr/net_usage/sub_cities.sql
index e51dbf3..4d4f5d9 100644
--- a/web/queries/tlbr/net_usage/sub_cities.sql
+++ b/web/queries/tlbr/net_usage/sub_cities.sql
@@ -1,5 +1,6 @@
 SELECT
-    city AS cat
+    id_city AS id
+    , INITCAP(city) AS cat
     , AVG(bytes_five_min_to_kbits_sec(down)) AS down
     , AVG(bytes_five_min_to_kbits_sec(up)) AS up
 FROM
diff --git a/web/queries/tlbr/net_usage/sub_regions.sql b/web/queries/tlbr/net_usage/sub_regions.sql
index 6b9d8ff..06de16f 100644
--- a/web/queries/tlbr/net_usage/sub_regions.sql
+++ b/web/queries/tlbr/net_usage/sub_regions.sql
@@ -1,6 +1,7 @@
 -- Compute the peak for every telecenter and than the average for each region
 SELECT
-    f.region AS cat
+    UPPER(f.region) AS id
+    , INITCAP(f.region) AS cat
     , AVG(bytes_five_min_to_kbits_sec(f.down)) AS down
     , AVG(bytes_five_min_to_kbits_sec(f.up)) AS up
 FROM
diff --git a/web/queries/tlbr/net_usage/sub_states.sql b/web/queries/tlbr/net_usage/sub_states.sql
index 0398328..092fd51 100644
--- a/web/queries/tlbr/net_usage/sub_states.sql
+++ b/web/queries/tlbr/net_usage/sub_states.sql
@@ -1,6 +1,7 @@
 -- Compute the peak for every telecenter and than the average for each state
 SELECT
-    state AS cat
+    UPPER(state) AS id
+    , UPPER(state) AS cat
     , AVG(bytes_five_min_to_kbits_sec(down)) AS down
     , AVG(bytes_five_min_to_kbits_sec(up)) AS up
 FROM
diff --git a/web/queries/tlbr/net_usage/sub_telecenters.sql b/web/queries/tlbr/net_usage/sub_telecenters.sql
index 675ef4f..411d56e 100644
--- a/web/queries/tlbr/net_usage/sub_telecenters.sql
+++ b/web/queries/tlbr/net_usage/sub_telecenters.sql
@@ -1,12 +1,13 @@
 SELECT
-    telecenter AS cat
+    id_point AS id
+    , telecenter AS cat
     , id_point
     , MAX(bytes_five_min_to_kbits_sec(down_kbits)) AS down
     , MAX(bytes_five_min_to_kbits_sec(up_kbits)) AS up
 FROM
     fact_net_usage_telecenter
 WHERE
-    city = $1::text
+    id_city = $1::bigint
 GROUP BY
     id_point
     , telecenter
diff --git a/web/queries/tlbr/net_usage/telecenter.sql b/web/queries/tlbr/net_usage/telecenter.sql
index e42b218..ebd8a02 100644
--- a/web/queries/tlbr/net_usage/telecenter.sql
+++ b/web/queries/tlbr/net_usage/telecenter.sql
@@ -1,13 +1,12 @@
 SELECT
-    telecenter AS cat
-    , bytes_five_min_to_kbits_sec(down_kbits) AS down
-    , bytes_five_min_to_kbits_sec(up_kbits) AS up
-    , collect_date + collect_time AS timestamp
+    bytes_five_min_to_kbits_sec(down_kbits)::real AS down
+    , bytes_five_min_to_kbits_sec(up_kbits)::real AS up
+    , EXTRACT('epoch' FROM collect_date + collect_time) AS timestamp
 FROM
     fact_net_usage_telecenter
 WHERE
     id_point = $1::integer
 ORDER BY
-    id_date
+    collect_date
     , collect_time
 ;
diff --git a/web/routes/charts.js b/web/routes/charts.js
index a230980..1c22f22 100644
--- a/web/routes/charts.js
+++ b/web/routes/charts.js
@@ -13,7 +13,7 @@ exports.get_data = function(req, res) {
             /* Availability */
             {
                 type: 'avail_current', file: 'queries/tlbr/avail/current.sql'
-                , params: [ req.params.region, req.params.state, req.params.city ]
+                , params: [ req.params.region, req.params.state, req.params.id_city ]
             },
             {
                 type: 'avail_sub_regions' , file: 'queries/tlbr/avail/sub_regions.sql'
@@ -29,11 +29,11 @@ exports.get_data = function(req, res) {
             },
             {
                 type: 'avail_sub_telecenters', file: 'queries/tlbr/avail/sub_telecenters.sql'
-                , params: [ req.params.region, req.params.state, req.params.city ]
+                , params: [ req.params.region, req.params.state, req.params.id_city ]
             },
             {
                 type: 'avail_hist', file: 'queries/tlbr/avail/hist.sql'
-                , params: [ req.params.region, req.params.state, req.params.city ]
+                , params: [ req.params.region, req.params.state, req.params.id_city ]
             },
 
             /* Net Usage */
@@ -51,11 +51,11 @@ exports.get_data = function(req, res) {
             },
             {
                 type: 'net_usage_sub_telecenters', file: 'queries/tlbr/net_usage/sub_telecenters.sql'
-                , params: [ req.params.city ]
+                , params: [ req.params.id_city ]
             },
             {
                 type: 'net_usage_telecenter', file: 'queries/tlbr/net_usage/telecenter.sql'
-                , params: [ req.params.id_telecenter ]
+                , params: [ req.params.id_point ]
             }
         ]
     };
diff --git a/web/server.js b/web/server.js
index 635945c..989e495 100755
--- a/web/server.js
+++ b/web/server.js
@@ -25,6 +25,7 @@ app.all('/api/telecenters/:state?/:city_id?/:telecenter_id?', db.connect, telece
 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);
-app.get('/api/:project/:type/:region/:state/:city', db.connect, charts.get_data);
+app.get('/api/:project/:type/:region/:state/:id_city', db.connect, charts.get_data);
+app.get('/api/:project/:type/:region/:state/:id_city/:id_point', db.connect, charts.get_data);
 
 app.listen(port);
-- 
GitLab