Skip to content

Commit

Permalink
Show map with anonymous volunteers for whole site
Browse files Browse the repository at this point in the history
  • Loading branch information
teleyinex committed Jul 15, 2013
1 parent c8a1d4b commit 9a5cd60
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
53 changes: 53 additions & 0 deletions pybossa/templates/stats/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@
{% import "privacy/locked.html" as privacy %}

{% block content %}
<!-- Leaflet maps -->
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.ie.css" />
<![endif]-->

<link href="{{url_for('static', filename='css/stats/stats.css')}}" rel="stylesheet" type="text/css">
<script src="{{url_for('static', filename='js/stats/flotr2.min.js')}}" type="text/javascript"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>
<link href="{{url_for('static', filename='css/stats/MarkerCluster.css')}}" rel="stylesheet" type="text/css">
<link href="{{url_for('static', filename='css/stats/MarkerCluster.Default.css')}}" rel="stylesheet" type="text/css">
<link href="{{url_for('static', filename='css/stats/stats.css')}}" rel="stylesheet" type="text/css">
<script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>
<script src="{{url_for('static', filename='js/stats/flotr2.min.js')}}" type="text/javascript"></script>
<script src="{{url_for('static', filename='js/stats/leaflet.markercluster.js')}}" type="text/javascript"></script>

<style>
h3 {
font-size: 20px;
Expand Down Expand Up @@ -146,6 +160,45 @@ <h3>{{ _('Number of Task Runs') }}: <small>{{stats.n_task_runs}}</small></h3>
</div>
</div>
<!-- END Users Stats CARD -->
<!-- START Anonymous Users Map CARD -->
{% if locs | count > 0 %}
<div id="card" class="row-fluid">
<div class="span12 well">
<h2>{{ _('Anonymous Volunteers') }}</h2>
<div id="map" style="height:250px;"></div>
<p class="note">{{_('This page includes GeoLite data created by MaxMind, available from')}}
<a href="http://www.maxmind.com">http://www.maxmind.com</a></p>
</div>
<script>
(function(){
var map = L.map('map');
map.fitWorld();
map.setZoom(1);
var url = 'http://{s}.tile.cloudmade.com/f9ed30886bf3454691669c63a45490f4/997/256/{z}/{x}/{y}.png'
L.tileLayer(url,
{
attribution: 'Map data Cloudmade.com &copy;',
maxZoom: 18,
minZoom:1
}).addTo(map);

var i = 0;
var locations = {{locs|safe}};
var l = locations.length;
var markers = new L.MarkerClusterGroup();
for (i;i<l;i++) {
if (locations[i].loc != null) {
var lat = parseFloat(locations[i].loc.latitude);
var lng = parseFloat(locations[i].loc.longitude);
markers.addLayer(L.marker([lat,lng]));
}
}
map.addLayer(markers);
})();
</script>
</div>
{% endif %}
<!-- END Anonymous Users Map CARD -->
{% endif %}
</div>
</div>
Expand Down
19 changes: 18 additions & 1 deletion pybossa/view/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with PyBOSSA. If not, see <http://www.gnu.org/licenses/>.
import json
from flask import Blueprint
import pygeoip
from flask import Blueprint, current_app
from flask import render_template
from sqlalchemy.sql import text

Expand Down Expand Up @@ -91,6 +92,21 @@ def index():
n_answers=row.n_answers)
top5_users_24_hours.append(user)

# All IP addresses from anonymous users to create a map
if current_app.config['GEO']:
sql = '''SELECT DISTINCT(user_ip) from task_run WHERE user_ip IS NOT NULL;'''
results = db.engine.execute(sql)
locs = []

geolite = current_app.root_path + '/../dat/GeoLiteCity.dat'
gic = pygeoip.GeoIP(geolite)
for row in results:
loc = gic.record_by_addr(row.user_ip)
if (len(loc.keys()) == 0):
loc['latitude'] = 0
loc['longitude'] = 0
locs.append(dict(loc=loc))

stats = dict(n_total_users=n_total_users, n_auth=n_auth, n_anon=n_anon,
n_published_apps=n_published_apps,
n_draft_apps=n_draft_apps,
Expand All @@ -117,6 +133,7 @@ def index():
users=json.dumps(users),
apps=json.dumps(apps),
tasks=json.dumps(tasks),
locs=json.dumps(locs),
top5_users_24_hours=top5_users_24_hours,
top5_apps_24_hours=top5_apps_24_hours,
stats=stats)

0 comments on commit 9a5cd60

Please sign in to comment.