<commit_before>#-*- coding: utf-8 -*-

##########################################################
# © 2013/2014 Daniel 'grindhold' Brendle with torservers.net
#
# This file is part of torcollect
#
# torcollect is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later
# version.
#
# torcollect 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with torcollect.
# If not, see http://www.gnu.org/licenses/.
###########################################################

import torcollect.database
import torcollect.graphs
import datetime
import pygal
import StringIO

class MonthlyReport(object):
    """ Generates a Monthly report of the bridge-usage in HTML-format
    """
    _USAGE_STMNT = "SELECT COALESCE(TT.USAGE, 0) FROM \
                        (SELECT SUM(TRP_USERS) AS USAGE, REP_DATE \
                         FROM Report INNER JOIN TransportReport \
                            ON (REP_ID = TRP_REP_ID) \
                         WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                         GROUP BY REP_DATE) AS TT\
                    RIGHT JOIN \
                        (SELECT CURRENT_DATE + i AS DATE FROM GENERATE_SERIES( \
                            DATE %(start_date)s - CURRENT_DATE, \
                            DATE %(end_date)s - CURRENT_DATE) i) AS DT \
                    ON (DT.DATE = TT.REP_DATE) \
                    ORDER BY DT.DATE;"

    _TRFFC_STMNT = "SELECT COALESCE(TT.RECEIVED, 0), COALESCE(TT.SENT, 0) FROM \
                        (SELECT SUM(REP_TRAFFIC_RECEIVED) AS RECEIVED, \
                                SUM(REP_TRAFFIC_SENT) AS SENT, \
                                REP_DATE \
                         FROM Report \
                         WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                         GROUP BY REP_DATE) AS TT \
                    RIGHT JOIN \
                        (SELECT CURRENT_DATE + i AS DATE FROM GENERATE_SERIES( \
                            DATE %(start_date)s - CURRENT_DATE, \
                            DATE %(end_date)s - CURRENT_DATE) i) AS DT \
                    ON (DT.DATE = TT.REP_DATE) \
                    ORDER BY DT.DATE;"

    _CNMAP_STMNT = "SELECT TC.CCO_SHORT, COALESCE(TT.USAGE, 0) FROM \
                        (SELECT CCO_SHORT, SUM(CRP_USERS) AS USAGE \
                        FROM Countryreport INNER JOIN CountryCode \
                            ON (CCO_ID = CRP_CCO_ID) \
                        INNER JOIN Report \
                            ON (REP_ID = CRP_REP_ID) \
                        WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                        GROUP BY CCO_SHORT ) AS TT \
                    RIGHT JOIN CountryCode as TC \
                        ON (TT.CCO_SHORT = TC.CCO_SHORT) \
                    ORDER BY TC.CCO_SHORT ASC;"

    _CNHIS_STMNT = "SELECT CT.CCO_SHORT, RT.USAGE, CT.DATE FROM \
                        (SELECT CCO_SHORT, DT.DATE AS DATE FROM \
                            (SELECT CURRENT_DATE + i AS DATE FROM \
                                GENERATE_SERIES(DATE %(start_date)s - CURRENT_DATE, \
                                                DATE %(end_date)s - CURRENT_DATE) i) \
                            AS DT \
                            CROSS JOIN CountryCode) AS CT \
                        LEFT JOIN \
                        (SELECT CCO_SHORT, SUM(CRP_USERS) AS USAGE, REP_DATE  \
                         FROM CountryReport INNER JOIN CountryCode \
                            ON (CCO_ID = CRP_CCO_ID)  \
                        INNER JOIN Report \
                            ON (REP_ID = CRP_REP_ID) \
                        WHERE REP_DATE >= %(start_date)s  AND REP_DATE <= %(end_date)s \
                        GROUP BY CCO_SHORT, REP_DATE \
                        ORDER BY CCO_SHORT, REP_DATE ASC) \
                    AS RT ON (RT.CCO_SHORT = CT.CCO_SHORT AND RT.REP_DATE = CT.DATE);"
    
    _HTML_FRAME = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>%(title)s</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="/torcollect.css" type="text/css">
    </head>
    <body>
        <h1> %(title)s </title>
        <p> This report is concering data from %(start_date)s to %(end_date)s </p>
        %(overall_usage_graph)s
        %(overall_traffic_graph)s
        <h2> Distribution by Country </h2>
        %(worldmap)s
        %(country_graph)s
    </body>
    </html>
    """

    def __init__(self, year=None, month=None):
        """ The Monthly Report is initialized by giving it a Month it should represent """
        # if no date and year given, assume its this date and year
        if month is None or year is None:
            date = datetime.datetime.now()
            date.day = 1
        else:
            date = datetime.datetime(year, month, 1)

        self.start_date = date
        
        # determine end date
        self.days_of_month = (datetime.datetime(date.year, date.month+1, 1) - date).days
        self.end_date = datetime.datetime(date.year, date.month, self.days_of_month)

        # define structures to save data in
        self.usage_data = []
        self.traffic_sent = []
        self.traffic_received = []
        self.country_overall_data = {}<commit_msg>made report prettier<commit_after>#-*- coding: utf-8 -*-

##########################################################
# © 2013/2014 Daniel 'grindhold' Brendle with torservers.net
#
# This file is part of torcollect
#
# torcollect is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later
# version.
#
# torcollect 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with torcollect.
# If not, see http://www.gnu.org/licenses/.
###########################################################

import torcollect.database
import torcollect.graphs
import datetime
import pygal
import StringIO

class MonthlyReport(object):
    """ Generates a Monthly report of the bridge-usage in HTML-format
    """
    _USAGE_STMNT = "SELECT COALESCE(TT.USAGE, 0) FROM \
                        (SELECT SUM(TRP_USERS) AS USAGE, REP_DATE \
                         FROM Report INNER JOIN TransportReport \
                            ON (REP_ID = TRP_REP_ID) \
                         WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                         GROUP BY REP_DATE) AS TT\
                    RIGHT JOIN \
                        (SELECT CURRENT_DATE + i AS DATE FROM GENERATE_SERIES( \
                            DATE %(start_date)s - CURRENT_DATE, \
                            DATE %(end_date)s - CURRENT_DATE) i) AS DT \
                    ON (DT.DATE = TT.REP_DATE) \
                    ORDER BY DT.DATE;"

    _TRFFC_STMNT = "SELECT COALESCE(TT.RECEIVED, 0), COALESCE(TT.SENT, 0) FROM \
                        (SELECT SUM(REP_TRAFFIC_RECEIVED) AS RECEIVED, \
                                SUM(REP_TRAFFIC_SENT) AS SENT, \
                                REP_DATE \
                         FROM Report \
                         WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                         GROUP BY REP_DATE) AS TT \
                    RIGHT JOIN \
                        (SELECT CURRENT_DATE + i AS DATE FROM GENERATE_SERIES( \
                            DATE %(start_date)s - CURRENT_DATE, \
                            DATE %(end_date)s - CURRENT_DATE) i) AS DT \
                    ON (DT.DATE = TT.REP_DATE) \
                    ORDER BY DT.DATE;"

    _CNMAP_STMNT = "SELECT TC.CCO_SHORT, COALESCE(TT.USAGE, 0) FROM \
                        (SELECT CCO_SHORT, SUM(CRP_USERS) AS USAGE \
                        FROM Countryreport INNER JOIN CountryCode \
                            ON (CCO_ID = CRP_CCO_ID) \
                        INNER JOIN Report \
                            ON (REP_ID = CRP_REP_ID) \
                        WHERE REP_DATE >= %(start_date)s AND REP_DATE <= %(end_date)s \
                        GROUP BY CCO_SHORT ) AS TT \
                    RIGHT JOIN CountryCode as TC \
                        ON (TT.CCO_SHORT = TC.CCO_SHORT) \
                    ORDER BY TC.CCO_SHORT ASC;"

    _CNHIS_STMNT = "SELECT CT.CCO_SHORT, RT.USAGE, CT.DATE FROM \
                        (SELECT CCO_SHORT, DT.DATE AS DATE FROM \
                            (SELECT CURRENT_DATE + i AS DATE FROM \
                                GENERATE_SERIES(DATE %(start_date)s - CURRENT_DATE, \
                                                DATE %(end_date)s - CURRENT_DATE) i) \
                            AS DT \
                            CROSS JOIN CountryCode) AS CT \
                        LEFT JOIN \
                        (SELECT CCO_SHORT, SUM(CRP_USERS) AS USAGE, REP_DATE  \
                         FROM CountryReport INNER JOIN CountryCode \
                            ON (CCO_ID = CRP_CCO_ID)  \
                        INNER JOIN Report \
                            ON (REP_ID = CRP_REP_ID) \
                        WHERE REP_DATE >= %(start_date)s  AND REP_DATE <= %(end_date)s \
                        GROUP BY CCO_SHORT, REP_DATE \
                        ORDER BY CCO_SHORT, REP_DATE ASC) \
                    AS RT ON (RT.CCO_SHORT = CT.CCO_SHORT AND RT.REP_DATE = CT.DATE);"
    
    _HTML_FRAME = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>%(title)s</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" type="text/css">
        <link rel="stylesheet" href="/bootstrap/css/bootstrap-theme.min.css" type="text/css">
        <link rel="stylesheet" href="/torcollect.css" type="text/css">
    </head>
    <body>
        <h1 style="text-align:center;"> %(title)s </h1>
        <br>
        <p> This report is concering data from %(start_date)s to %(end_date)s </p>
        <h2> Usage Information </h2>
        <p> The numbers displayed to you in this graph represent the number of TOR
        circuits that have been set up through the monitored bridges. Be aware that to 
        minimize the risk of de-anonymization, TOR rounds the actual figure up to 
        multiples of 8.
        %(overall_usage_graph)s
        <h2> Traffic Information </h2>
        %(overall_traffic_graph)s
        <h2> Distribution by Country </h2>
        %(worldmap)s
        %(country_graph)s
    </body>
    </html>
    """

    def __init__(self, year=None, month=None):
        """ The Monthly Report is initialized by giving it a Month it should represent """
        # if no date and year given, assume its this date and year
        if month is None or year is None:
            date = datetime.datetime.now()
            date.day = 1
        else:
            date = datetime.datetime(year, month, 1)

        self.start_date = date
        
        # determine end date
        self.days_of_month = (datetime.datetime(date.year, date.month+1, 1) - date).days
        self.end_date = datetime.datetime(date.year, date.month, self.days_of_month)

        # define structures to save data in
        self.usage_data = []
        self.traffic_sent = []
        self.traffic_received = []
        self.country_overall_data = {}
{'fraction_non_alphanumeric': 0.10361028684470822, 'fraction_numerical': 0.004121332014507088, 'mean_word_length': 1.5624076029567053, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3927436', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3386, 'n_words': 1017}