#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

require "time"
require "qwik/qp"

class LogGraph
  def initialize(name)
    @name = name
    @param = {}
    @stat = {}
  end
  attr_reader :param, :stat

  def generate_graph
    file = @name+".gp"
    open(file, "wb") {|out|
      print_begin(out)
      add_graph(out)
    }
    system("gnuplot #{file}")
  end


end

class LogStat
  def initialize
  end

  def fatal_error(msg)
    p "Fatal Error: "+msg
    exit
  end

  def parse_date(date)
    if /\A(\d\d(\d\d)-(\d\d)-(\d\d)T(\d\d)):\d\d:\d\d\Z/ =~ date
      date = Time.parse($1)
      timestr =  $2 + $3 + $4 + $5
      return date, timestr
    end
    fatal_error("what?"+date)
  end

  def calc_stat_qwik
    access = 0
    @qwik_access = {}
    users = Hash.new(0)
    @qwik_user = {}
    groups = Hash.new(0)
    @qwik_group = {}
    
    @logs.each {|date, user, group, page, cmd|
      #date, timestr = parse_date(date)
      timestr = date
      if cmd != "html"
	access += 1
	@qwik_access[timestr] = access
      end
      users[user] += 1
      @qwik_user[timestr] = users.length
      groups[group] += 1
      @qwik_group[timestr] = groups.length
    }
  end

  def calc_stat_quickml
    access = 0
    @quickml_access = {}
    users = Hash.new(0)
    @quickml_user = {}
    groups = Hash.new(0)
    @quickml_group = {}
    @ml_logs.each {|date, group, cmd, dest|
      date, timestr = parse_date(date)
      case cmd
      when "Add"
	users[dest] += 1
	@quickml_user[timestr] = users.length
      when "Remove"
	users[dest] -= 1
	@quickml_user[timestr] = users.length
      when "Send"
	access += 1
	@quickml_access[timestr] = access
      when "NewML"
	groups[group] += 1
	@quickml_group[timestr] = groups.length
      when "MLClosed"
	groups[group] -= 1
	@quickml_group[timestr] = groups.length
      end
    }
  end

  def write_stat(filename, stat)
    File.open(filename, "w") {|f|
      stat.keys.sort.each {|date|
	f.puts "#{date} #{stat[date]}"
      }
    }
  end

  def print_begin(f)
    f.print "
set grid
set timefmt \"%y%m%d%H\"
set xdata time
set size ratio 0.76
set linestyle 1
set linestyle 2
set linestyle 3
set linestyle 4
set linestyle 5
set linestyle 6
"
  end

  def add_graph(f, name, param)
    mode = "png"
    #mode = "eps"
    terminal = "png" if mode == "png"
    #terminal = "postscript eps color \"Helvetica\" 40" if mode == "eps"
    terminal = "postscript eps color \"Helvetica\"" if mode == "eps"
    xlabel = param["xlabel"]
    title = param["title"]
    output = name+".png" if mode == "png"
    output = name+".eps" if mode == "eps"
    str = "
set size 0.7
set terminal #{terminal}
set format x \"%m\"
set xlabel \"#{xlabel}\"
set title  \"#{title}\"
set output \"#{output}\"
"
    f.print str
  end

  def add_plot(stat, title, num)
    "\"#{stat}\" using 1:2 title \"#{title}\" with lines ls #{num}"
  end

  def make_gnuplot(title, base)
    param = {}
    param["xlabel"] = "Month"
    param["title"] = title
    file = base+".gp"
    open(file, "wb") {|f|
      print_begin(f)
      add_graph(f, base, param)
      plots = []
      yield(plots)
      pp = "plot "+plots.join(",\\\n     ")+"\n"
      f.print pp
    }
    system("gnuplot #{file}")
  end

  def make_stat
    @logs = []
    #open("qwik1.log") {|f|
    open("access1.log") {|f|
      while line = f.gets
	line.chomp!
	date, user, group, page, cmd = line.split
	@logs << [date, user, group, page, cmd]
      end
    }

    @ml_logs = []
    open("quickml1.log") {|f|
      while line = f.gets
	line.chomp!
	date, group, cmd, dest = line.split
	@ml_logs << [date, group, cmd, dest]
      end
    }

    calc_stat_qwik
    write_stat("qwik_access.stat", @qwik_access)
    write_stat("qwik_user.stat", @qwik_user)
    write_stat("qwik_group.stat", @qwik_group)
  end

  def process
    make_stat

#    calc_stat_quickml
#    write_stat("quickml_access.stat", @quickml_access)
#    write_stat("quickml_user.stat", @quickml_user)
#    write_stat("quickml_group.stat", @quickml_group)

    make_gnuplot("Access number of qwikWeb", "access") {|plots|
#      plots << add_plot("quickml_access.stat", "Mail Send", 1)
#      plots << add_plot("quickml_access.stat", "Mail Send", 1)
      plots << add_plot("stat.submit", "Mail Submit", 1)
      plots << add_plot("qwik_access.stat", "Wiki access", 2)
    }

    make_gnuplot("Group number of qwikWeb", "group") {|plots|
#      plots << add_plot("quickml_group.stat", "Group Created", 1)
#      plots << add_plot("stat.ml", "maling list", 1)
      plots << add_plot("stat.ml.created", "maling list", 1)
      plots << add_plot("qwik_group.stat", "Wiki access", 2)
    }

    make_gnuplot("User number of qwikWeb", "user") {|plots|
#      plots << add_plot("quickml_user.stat", "User", 1)
      plots << add_plot("stat.user", "User", 1)
      plots << add_plot("stat.user.uniq", "Unique User", 2)
      plots << add_plot("qwik_user.stat", "Wiki access user", 3)
    }
  end
end

class StatLog
  def main
    logstat = LogStat.new
    logstat.process
  end
end
