#!/usr/bin/env python
#
# Copyright (C) 2008 by Alexander Koenig
# Copyright (C) 2008, 2009 by Adrian Reber
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 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.

import sys, pylab, time, getopt

start = time.clock()

logfile = None
dest = None
offset = 0


def usage():
    print "mirrorlist_statistics.py analyzes mirrorlist_server.py logfiles"
    print "and draws piecharts"
    print
    print "Usage:"
    print "  mirrorlist_statistics.py [OPTION]..."
    print
    print "Options:"
    print "  -l, --log=LOGFILE     logfile which should be used as input"
    print "  -d, --dest=DIRECTORY  output directory"
    print "  -o, --offset=DAYS     number of days which should be subtracted"
    print "                        from today's date and be used as basis for log analysis"
    print "  -h, --help            show this help; then exit"
    print


def parse_args():
    global logfile
    global dest
    global offset
    opts, args = getopt.getopt(sys.argv[1:], "l:d:ho:",
                ["log=", "dest=", "help", "offset"])
    for option, argument in opts:
        if option in ("-l", "--log"):
            logfile = argument
        if option in ("-d", "--dest"):
            dest = argument
        if option in ("-o", "--offset"):
            offset = int(argument)
        if option in ("-h", "--help"):
            usage()
            sys.exit(0)


parse_args()


if logfile is None or dest is None:
    usage()
    sys.exit(-1)


def sort_dict(dict):
    """ Sort dictionary by values and reverse. """
    items=dict.items()
    sorteditems=[[v[1],v[0]] for v in items ]
    sorteditems.sort()
    sorteditems.reverse()
    return sorteditems


y1, m1, d1, x1, x2, x3, x4, x5, x6 = time.localtime()


# this works only if offset < d1
if d1 > offset:
    d1 = d1 - offset

countries = {}
accesses = 0
repositories = {}
archs = {}
i = 0


for line in open(logfile):
    arguments = line.split()
    try:
        y, m, d = arguments[3][:10].split('-')
    except:
        continue
        if not ((int(y) == y1) and (int(m) == m1) and (int(d) == d1)):
            continue
    try:
        countries[arguments[5][:2]] += 1
    except:
        countries[arguments[5][:2]] = 1
    try:
        archs[arguments[9]] += 1
    except:
        archs[arguments[9]] = 1
    try:
        repositories[arguments[7][:len(arguments[7])-1]] += 1
    except:
        repositories[arguments[7][:len(arguments[7])-1]] = 1
    accesses += 1
    continue


def do_pie(prefix, dict, accesses):
    pylab.figure(1, figsize=(8,8))
    ax =  pylab.axes([0.1, 0.1, 0.8, 0.8])

    labels = []