Changeset - 6173267a5536
[Not reviewed]
Dennis Fink - 7 years ago 2017-10-17 12:33:36
dennis.fink@c3l.lu
Update
6 files changed with 9 insertions and 252 deletions:
0 comments (0 inline, 0 general)
ennstatus/data/views.py
Show inline comments
 
# Ënnstatus
 
# Copyright (C) 2015  Dennis Fink
 
#
 
# This program 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 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import json
 

	
 
from collections import defaultdict
 

	
 
from flask import Blueprint, jsonify
 

	
 
from ennstatus.status.functions import split_all_servers_to_types
 
from ennstatus.donate.functions import all_csv_to_dict, year_sum, month_sum
 

	
 
data_page = Blueprint('data', __name__)
 

	
 

	
 
@data_page.route('/worldmap')
 
def worldmap():
 
    servers = split_all_servers_to_types()
 
    countries = {}
 

	
 
    for key, value in servers.items():
 
        for server in value:
 
            if server.country not in countries:
 
                countries[server.country] = defaultdict(int)
 
            countries[server.country][server.type] += 1
 
            countries[server.country]['total'] += 1
 

	
 
    maximum = max(i['total'] for i in countries.values())
 

	
 
    countries['max'] = maximum
 

	
 
    return jsonify(countries)
 

	
 

	
 
@data_page.route('/year')
 
def yeardata():
 
    data = all_csv_to_dict()
 
    data = year_sum(data)
 
    datalist = []
 

	
 
    for key, values in data.items():
 
        datalist.append({"key": key, "value": float(values)})
 
    datalist.sort(key=lambda x: x["key"])
 

	
 
    return json.dumps(datalist)
 

	
 

	
 
@data_page.route('/month')
 
def monthdata():
 
    data = all_csv_to_dict()
 
    data = month_sum(data)
 
    data_list = []
 

	
 
    for key, values in data.items():
 
        data_list.append({"key": key, "value": float(values)})
 

	
 
    data_list.sort(key=lambda x: x["key"])
 

	
 
    return json.dumps(data_list)
ennstatus/donate/functions.py
Show inline comments
 
# Ënnstatus
 
# Copyright (C) 2015  Dennis Fink
 
#
 
# This program 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 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import os
 
import os.path
 
import csv
 
import statistics
 

	
 
from collections import defaultdict
 

	
 
from babel.numbers import parse_decimal
 

	
 

	
 
def load_csv_file(file):
 

	
 
    with open(file, encoding='utf-8', newline='') as csvfile:
 
        csvreader = csv.reader(csvfile, delimiter=',')
 
        rows = list(csvreader)
 
    return rows
 

	
 

	
 
def load_csv(date):
 

	
 
    filename = '.'.join([date, 'csv'])
 
    path = os.path.join('donations', filename)
 

	
 
    for row in load_csv_file(path):
 
        yield row
 
    with open(path, encoding='utf-8', newline='') as csvfile:
 
        csvreader = csv.reader(csvfile, delimiter=',')
 

	
 
        for row in csvreader:
 
            yield row
 

	
 

	
 
def get_choices():
 

	
 
    files = os.listdir('donations')
 

	
 
    for file in files:
 
        if not file.startswith('.') \
 
           and file.endswith('.csv'):
 
            yield os.path.splitext(file)[0]
 

	
 

	
 
def get_all_csv():
 

	
 
    for file in os.listdir('donations'):
 
        path = os.path.join('donations', file)
 
        yield load_csv_file(path)
 

	
 

	
 
def get_all_rows():
 

	
 
    for csvfile in get_all_csv():
 
        for row in csvfile:
 
            yield row
 

	
 

	
 
def all_csv_to_dict():
 

	
 
    data = defaultdict(set)
 

	
 
    for row in get_all_rows():
 
        data[row[0]].add(parse_decimal(row[2], locale='de'))
 
    return data
 

	
 

	
 
def data_to_year(data):
 

	
 
    year_data = defaultdict(set)
 

	
 
    for date, values in data.items():
 
        year = date.split('-')[0]
 
        for value in values:
 
            year_data[year].add(value)
 

	
 
    return year_data
 

	
 

	
 
def data_to_month(data):
 

	
 
    month_data = defaultdict(set)
 

	
 
    for date, values in data.items():
 
        month = date.rsplit('-', maxsplit=1)[0]
 
        for value in values:
 
            month_data[month].add(value)
 

	
 
    return month_data
 

	
 

	
 
def year_sum(data):
 

	
 
    year_data = data_to_year(data)
 

	
 
    for year, values in year_data.items():
 
        year_data[year] = sum(values)
 

	
 
    return year_data
 

	
 

	
 
def month_sum(data):
 

	
 
    month_data = data_to_month(data)
 

	
 
    for month, values in month_data.items():
 
        month_data[month] = sum(values)
 

	
 
    return month_data
 

	
 

	
 
def get_all_years_mean(data):
 

	
 
    mean = set()
 
    year_data = year_sum(data)
 

	
 
    for year, values in year_data.items():
 
        mean.add(values)
 

	
 
    return statistics.mean(mean)
 

	
 

	
 
def get_months_mean(data):
 

	
 
    mean = set()
 
    month_data = month_sum(data)
 

	
 
    for year, values in month_data.items():
 
        mean.add(values)
 

	
 
    return statistics.mean(mean)
 

	
 

	
 
def get_best_year(data):
 

	
 
    year_data = year_sum(data)
 
    return max(year_data.items(), key=lambda x: x[1])
 

	
 

	
 
def get_best_month(data):
 

	
 
    month_data = month_sum(data)
 
    return max(month_data.items(), key=lambda x: x[1])
 

	
 

	
 
def get_highest_donation():
 

	
 
    data = all_csv_to_dict()
 

	
 
    for date, values in data.items():
 
        data[date] = max(values)
 

	
 
    return max(
 
        sorted(
 
            data.items(),
 
            key=lambda x: x[0],
 
            reverse=True
 
        ),
 
        key=lambda x: x[1]
 
    )
 

	
 

	
 
def get_median_donation():
 
    data = all_csv_to_dict()
 
    donations = []
 

	
 
    for value in data.values():
 
        donations.extend(value)
 

	
 
    return statistics.median(donations)
 

	
 

	
 
def get_mode_donation():
 
    data = all_csv_to_dict()
 
    donations = []
 

	
 
    for value in data.values():
 
        donations.extend(value)
 

	
 
    return statistics.mode(donations)
ennstatus/donate/views.py
Show inline comments
 
# Ënnstatus
 
# Copyright (C) 2015  Dennis Fink
 
#
 
# This program 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 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import json
 

	
 
from flask import (Blueprint, render_template, request,
 
                   redirect, url_for, current_app)
 

	
 
from babel.numbers import parse_decimal, format_decimal
 

	
 
from ennstatus.donate.forms import DateForm
 
from ennstatus.donate.functions import (load_csv,
 
                                        get_choices,
 
                                        all_csv_to_dict,
 
                                        get_all_years_mean,
 
                                        get_months_mean,
 
                                        get_best_year,
 
                                        get_best_month,
 
                                        get_highest_donation,
 
                                        get_median_donation,
 
                                        get_mode_donation,
 
                                        )
 
from ennstatus.donate.functions import load_csv, get_choices
 

	
 
donate_page = Blueprint('donate', __name__)
 

	
 

	
 
@donate_page.route('/')
 
def index():
 
    return render_template('donate/index.html')
 

	
 

	
 
@donate_page.route('/received',
 
                   methods=('GET', 'POST'))
 
def received():
 

	
 
    current_app.logger.info('Handling received')
 
    form = DateForm()
 

	
 
    current_app.logger.debug('Creating choices')
 

	
 
    files = [name for name in get_choices()]
 
    files.sort()
 

	
 
    year_choices = list({name.split('-')[0] for name in files})
 
    year_choices.sort()
 
    form.year.choices = [(name, name) for name in year_choices]
 
@@ -84,75 +72,24 @@ def received():
 
                current_app.logger.warn('Date %s not found' % filename)
 
                return render_template('donate/received.html',
 
                                       form=form, csv_file=None,
 
                                       year=year, month=month, total=None)
 
        else:
 
            filename = files[-1]
 
            current_app.logger.info('Showing last date %s' % filename)
 
            year, month = filename.split('-')
 
            form.year.data = year
 
            form.month.data = '{:02d}'.format(int(month))
 
            csv_file = load_csv(filename)
 

	
 
        total = format_decimal(
 
            sum(
 
                parse_decimal(row[2], locale='de') for row in csv_file
 
            ),
 
            locale='de'
 
        )
 
        csv_file = load_csv(filename)
 

	
 
        current_app.logger.info('Return result')
 
        return render_template('donate/received.html',
 
                               form=form, csv_file=csv_file,
 
                               year=year, month=month, total=total)
 

	
 

	
 
@donate_page.route('/statistics')
 
def statistics():
 
    data = all_csv_to_dict()
 

	
 
    all_years_mean = format_decimal(
 
        get_all_years_mean(data),
 
        locale='de'
 
    )
 
    month_mean = format_decimal(
 
        get_months_mean(data),
 
        locale='de'
 
    )
 
    best_year = get_best_year(data)
 
    best_year = (best_year[0], format_decimal(best_year[1], locale='de'))
 

	
 
    best_month = get_best_month(data)
 
    best_month = (best_month[0], format_decimal(best_month[1], locale='de'))
 

	
 
    highest_donation = get_highest_donation()
 
    highest_donation = (highest_donation[0], format_decimal(highest_donation[1], locale='de'))
 

	
 
    median_donation = format_decimal(
 
        get_median_donation(),
 
        locale='de'
 
    )
 

	
 
    mode_donation = format_decimal(
 
        get_mode_donation(),
 
        locale='de'
 
    )
 

	
 
    dates = [name for name in get_choices()]
 
    dates.sort()
 

	
 
    last_default_date = dates[-1]
 
    first_default_date = dates[-12]
 

	
 
    return render_template('donate/statistics.html',
 
                           all_years_mean=all_years_mean,
 
                           month_mean=month_mean,
 
                           best_year=best_year,
 
                           best_month=best_month,
 
                           highest_donation=highest_donation,
 
                           median_donation=median_donation,
 
                           mode_donation=mode_donation,
 
                           dates=dates,
 
                           first_default_date=first_default_date,
 
                           last_default_date=last_default_date
 
                           )
ennstatus/static/js/barcharts.js
Show inline comments
 
@@ -128,35 +128,35 @@ function draw(ywidth, yheight, mwidth, m
 
			.attr("x", function(d) {
 
				return xscale(d.key);
 
			})
 
			.attr("width", xscale.rangeBand())
 
			.attr("y", function(d) {
 
				return yscale(d.value);
 
			})
 
			.attr("height", function(d) {
 
				return mheight - yscale(d.value);
 
			})
 
			.attr("fill", function(d) {
 
				return "#00ae18";
 
			})
 
			.on("mouseover", tip.show)
 
			.on("mouseout", tip.hide)
 
			
 
	});	
 
};
 

	
 
function redraw() {
 
	ywidth = document.getElementById('yearchart').offsetWidth - 40 -30;
 
	yheight = (ywidth / 2) - 20 - 30;
 
	mwidth = document.getElementById('monthchart').offsetWidth - 40 -30;
 
	mheight = (mwidth / 2) - 20 - 30;
 
	d3.select('svg').remove();
 
	d3.selectAll('svg').remove();
 
	setup(ywidth, yheight, mwidth, mheight);
 
}
 

	
 
var throttleTimer;
 
function throttle() {
 
	  window.clearTimeout(throttleTimer);
 
	      throttleTimer = window.setTimeout(function() {
 
		            redraw();
 
			        }, 200);
 
}
ennstatus/templates/base.html
Show inline comments
 
@@ -73,50 +73,48 @@
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.about') }}">Organization</a></li>
 
            <li><a href="{{ url_for('root.partners') }}">Partners</a></li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Services <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('status.index') }}">Tor Servers</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.mirrors') }}">Mirrors</a></li>
 
            <li><a href="//{{ wiki_url }}">Wiki</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.ennstatus') }}">About Ënnstatus</a></li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Support <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('donate.index') }}">Donate</a></li>
 
            <li><a href="{{ url_for('donate.received') }}">Donation history</a></li>
 
            <li><a href="{{ url_for('root.bridgeprogram')}}">Adopt a Bridge</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.member') }}">Join Us</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('donate.statistics') }}">Donation statistics</a></li>
 
            <li class="dropdown-submenu">
 
            </li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Contact <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('root.contact') }}">General</a></li>
 
            <li><a href="{{ url_for('root.abuse') }}">Abuse</a></li>
 
            <li class="divider"></li>
 
            <li><a href="https://twitter.com/FrennVunDerEnn" target="blank">Twitter</a></li>
 
            <li><a href="http://lists.enn.lu/listinfo/discuss" target="blank">Mailing List</a></li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Statistics <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="https://stats.enn.lu/">Traffic</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('statistics.worldmap') }}">Worldmap</a></li>
 
          </ul>
 
        </li>
 
      </ul>
 
    </div>
ennstatus/templates/donate/statistics.html
Show inline comments
 
@@ -40,49 +40,49 @@
 
      <dd>{{ best_month[0] }} {{ best_month[1] }}</dd>
 
      <dt>Highest donation:</dt>
 
      <dd>{{ highest_donation[0] }} {{ highest_donation[1] }}</dd>
 
      <dt>Median donation:</dt>
 
      <dd>{{ median_donation }}</dd>
 
      <dt>Mode donation:</dt>
 
      <dd>{{ mode_donation }}</dd>
 
    </dl>
 
  </div>
 
  <div class="col-md-12">
 
    <div class="panel panel-default">
 
      <div class="panel-heading">Donations per year</div>
 
      <div class="panel-body">
 
        <div id="yearchart" class="center-block"></div>
 
      </div>
 
    </div>
 
  </div>
 
  <div class="col-md-12">
 
    <div class="panel panel-default">
 
      <div class="panel-heading">Donations per month</div>
 
      <div class="panel-body">
 
        <form class="form-inline" role="form">
 
          <div class="form-group">
 
            <label for="monthfirstdate">First date</label>
 
            <select class="form-control input-sm" id="monthfirstdate" name="monthfirstdate">
 
            <select class="form-control input-sm" id="monthfirstdate" name="monthfirstdate" onchange="redraw()">
 
              {% for date in dates %}
 
                <option {% if date == first_default_date %}selected{% endif %} value="{{ date }}">{{ date }}</option>
 
              {% endfor %}
 
            </select>
 
            <label for="monthlastdate">Last date</label>
 
            <select class="form-control input-sm" id="monthlastdate" name="monthlastdate">
 
            <select class="form-control input-sm" id="monthlastdate" name="monthlastdate" onchange="redraw()">
 
              {% for date in dates %}
 
                <option {% if date == last_default_date %}selected{% endif %} value="{{ date }}">{{ date }}</option>
 
              {% endfor %}
 
            </select>
 
          </div>
 
        </form>
 
        <div id="monthchart"></div>
 
      </div>
 
    </div>
 
  </div>
 
{% endblock %}
 

	
 
{% block scripts %}
 
  {{ super() }}
 
  <script src="{{ url_for('static', filename='js/d3/d3.min.js') }}"></script>
 
  <script src="{{ url_for('static', filename='js/d3-tip/index.js') }}"></script>
 
  <script src="{{ url_for('static', filename='js/barcharts.js') }}"></script>
 
{% endblock %}
0 comments (0 inline, 0 general)