#!/usr/bin/perl -w
#
# Wine FIXME report generator
#
# Copyright 2010 Michael Stefaniuc
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;
use DBI;

my $dbname = shift;
#my $lastid = shift;
my $lastid = 3403;

my @types = ("fixme", "err");
my $limit = " LIMIT 10";

my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", {RaiseError => 1});

my $logs = $dbh->selectrow_array("SELECT count(*) FROM source WHERE id>$lastid", undef);
my $percent = int($logs / 100);

printf("Files (logs) with FIXME/ERR/WARN: %d\n", $logs);

my @total = @{$dbh->selectall_arrayref("SELECT type, SUM(count) AS lines FROM message, fixmes WHERE source_id>$lastid AND message.id=message_id GROUP BY type ORDER BY lines DESC")};
my $unique = $dbh->selectall_hashref("SELECT type, COUNT(type) AS uniq FROM message, fixmes WHERE source_id>$lastid AND message.id=message_id GROUP BY type", "type");
print("  Type  |  Total  | Unique\n");
print("  ------+---------+-------\n");
foreach my $ref (@total) {
    printf("  %-6s| %7s | %5s\n", $$ref[0], $$ref[1], $$unique{$$ref[0]}{"uniq"});
}
print("\n\n");


my $title = "Most Frequent Messages By Logs";
print($title, "\n", "-" x length($title), "\n");
print("Messages that are present in most Wine output logs collected.\n\n");
foreach my $t (@types) {
    print(uc($t), ":\n");
    print("   Logs | Lines |  Mean  | Channel:Function Text\n");
    print("  ------+-------+--------+----------------------\n");
    my $sth = $dbh->prepare("SELECT COUNT(count) AS logs, SUM(count) AS lines, channel, function, text FROM message, fixmes WHERE source_id>$lastid AND type=\"$t\" AND message.id=message_id GROUP BY type, channel, function, text ORDER BY logs DESC $limit");
    $sth->execute;
    my ($logs, $lines, $chan, $func, $text);
    $sth->bind_columns(\$logs, \$lines, \$chan, \$func, \$text);
    while($sth->fetch) {
        printf("  %5s | %5s | %6.2f | %s:%s %s\n", $logs, $lines, $lines / $logs, $chan, $func, $text);
    }
    print("\n");
}
print("\n");


$title = "Most Frequent Messages By Mean Lines Per Logs";
print($title, "\n", "-" x length($title), "\n");
print("This are the messages which show up in at least 1% of the logs and have\nthe highest mean lines per logs ratio.\n\n");
foreach my $t (@types) {
    print(uc($t), ":\n");
    print("   Logs | Lines |  Mean  | Channel:Function Text\n");
    print("  ------+-------+--------+----------------------\n");
    my $sth = $dbh->prepare("SELECT * FROM (SELECT COUNT(count) AS logs, SUM(count) AS lines, channel, function, text FROM message, fixmes WHERE source_id>$lastid AND type=\"$t\" AND message.id=message_id GROUP BY type, channel, function, text) WHERE logs>$percent ORDER BY lines/logs DESC $limit");
    $sth->execute;
    my ($logs, $lines, $chan, $func, $text);
    $sth->bind_columns(\$logs, \$lines, \$chan, \$func, \$text);
    while($sth->fetch) {
        printf("  %5s | %5s | %6.2f | %s:%s %s\n", $logs, $lines, $lines / $logs, $chan, $func, $text);
    }
    print("\n");
}
print("\n");


$title = "Functions With Most Messages By Mean Lines Per Logs";
print($title, "\n", "-" x length($title), "\n");
print("This are the functions which show up in at least 1% of the logs and have\nthe highest mean lines per logs ratio.\n\n");
foreach my $t (@types) {
    print(uc($t), ":\n");
    print("   Logs | Lines |  Mean  | Channel:Function\n");
    print("  ------+-------+--------+-----------------\n");
    my $sth = $dbh->prepare("SELECT * FROM (SELECT COUNT(count) AS logs, SUM(count) AS lines, channel, function FROM message, fixmes WHERE source_id>$lastid AND type=\"$t\" AND message.id=message_id GROUP BY type, channel, function) WHERE logs>$percent ORDER BY lines/logs DESC $limit");
    $sth->execute;
    my ($logs, $lines, $chan, $func);
    $sth->bind_columns(\$logs, \$lines, \$chan, \$func);
    while($sth->fetch) {
        printf("  %5s | %5s | %6.2f | %s:%s\n", $logs, $lines, $lines / $logs, $chan, $func);
    }
    print("\n");
}
print("\n");


$title = "Messages With The Highest Repetition Count";
print($title, "\n", "-" x length($title), "\n");
print("The messages with the highest repetition count in a single log file.\n\n");
foreach my $t (@types) {
    print(uc($t), ":\n");
    print("   Logs | Lines |  Mean  | Channel:Function Text\n");
    print("  ------+-------+--------+----------------------\n");
    my $sth = $dbh->prepare("SELECT COUNT(count) AS logs, MAX(count) AS lines, channel, function, text from message, fixmes WHERE source_id>$lastid AND type=\"$t\" AND message.id=message_id GROUP BY type, channel, function, text ORDER BY lines DESC $limit");
    $sth->execute;
    my ($logs, $lines, $chan, $func, $text);
    $sth->bind_columns(\$logs, \$lines, \$chan, \$func, \$text);
    while($sth->fetch) {
        printf("  %5s | %5s | %6.1f | %s:%s %s\n", $logs, $lines, $lines / $logs, $chan, $func, $text);
    }
    print("\n");
}
print("\n");
