|
Revision 38, 1.2 kB
(checked in by astro, 3 years ago)
|
chart: 300px width & axis labels
|
| Line | |
|---|
| 1 |
#!/usr/bin/ruby |
|---|
| 2 |
|
|---|
| 3 |
require 'dbi' |
|---|
| 4 |
require 'yaml' |
|---|
| 5 |
require 'gruff' |
|---|
| 6 |
|
|---|
| 7 |
config = YAML::load File.new('config.yaml') |
|---|
| 8 |
timeout = config['settings']['timeout'].to_i |
|---|
| 9 |
sizelimit = config['settings']['size limit'].to_i |
|---|
| 10 |
dbi = DBI::connect(config['db']['driver'], config['db']['user'], config['db']['password']) |
|---|
| 11 |
|
|---|
| 12 |
class StatsPerCollection |
|---|
| 13 |
attr_reader :days |
|---|
| 14 |
|
|---|
| 15 |
def initialize |
|---|
| 16 |
@collections = {} |
|---|
| 17 |
@days = [] |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
def add_one(collection, day) |
|---|
| 21 |
@days << day unless @days.index(day) |
|---|
| 22 |
|
|---|
| 23 |
c = @collections[collection] || {} |
|---|
| 24 |
c[day] = (c[day] || 0) + 1 |
|---|
| 25 |
@collections[collection] = c |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def each |
|---|
| 29 |
@collections.each { |n,c| |
|---|
| 30 |
v = [] |
|---|
| 31 |
@days.each { |d| |
|---|
| 32 |
v << c[d].to_i |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
yield n, v |
|---|
| 36 |
} |
|---|
| 37 |
end |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
c = StatsPerCollection.new |
|---|
| 41 |
dbi.select_all("select date(items.date) as date,sources.collection from items left join sources on sources.rss=items.rss where date > now() - interval '14 days' order by date") { |date,collection| |
|---|
| 42 |
c.add_one(collection, date.day) |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
g = Gruff::Line.new(300) |
|---|
| 46 |
g.title = "Harvested items per day" |
|---|
| 47 |
g.x_axis_label = "Days" |
|---|
| 48 |
g.y_axis_label = "Items" |
|---|
| 49 |
|
|---|
| 50 |
c.each(&g.method(:data)) |
|---|
| 51 |
|
|---|
| 52 |
labels = {} |
|---|
| 53 |
c.days.each_with_index do |d,i| |
|---|
| 54 |
labels[i] = d.to_s |
|---|
| 55 |
end |
|---|
| 56 |
g.labels = labels |
|---|
| 57 |
|
|---|
| 58 |
g.write("#{config['settings']['output']}/chart.jpg") |
|---|
| 59 |
|
|---|
| 60 |
|
|---|