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
 
@@ -14,14 +14,11 @@
 
# 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__)
 

	
 
@@ -43,30 +40,3 @@ def worldmap():
 
    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
 
@@ -17,19 +17,6 @@
 
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):
 
@@ -37,8 +24,11 @@ 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():
 
@@ -49,141 +39,3 @@ def get_choices():
 
        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
 
@@ -14,25 +14,13 @@
 
# 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__)
 

	
 
@@ -105,54 +93,3 @@ def received():
 
        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
 
@@ -149,7 +149,7 @@ function redraw() {
 
	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);
 
}
 

	
ennstatus/templates/base.html
Show inline comments
 
@@ -94,8 +94,6 @@
 
            <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>
ennstatus/templates/donate/statistics.html
Show inline comments
 
@@ -61,13 +61,13 @@
 
        <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 %}
0 comments (0 inline, 0 general)