-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubgraph.pl
executable file
·89 lines (76 loc) · 2.4 KB
/
subgraph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/local/bin/perl -w
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. - Philip Paeps
# ----------------------------------------------------------------------------
#
# Generates a graph of submissions per day from the mtimes of the keys.
# This could be done much simpler, but it would be even scarier to read.
#
use strict;
use DirHandle;
use POSIX qw/strftime/;
use RRDs;
use Time::Local;
my $year = "2016"; # Current FOSDEM edition.
my $startdate = "20151204"; # Start date of submissions.
my $basedir = "/home/ksp"; # Absolute location of files.
# Bail out if submissions are closed!
if (-e "$basedir/kspd.lock") {
exit 0;
}
# Get timestamps for all files in the given directory.
sub getmtimes($) {
my $dir = shift;
my $d = DirHandle->new($dir);
return map { $_ => (stat($_))[9]}
map { "$dir/$_" }
grep { ! m/^\.|\!/ }
$d->read();
}
# Create the rrdtool database.
sub createrrd() {
unlink "$basedir/keys.rrd";
RRDs::create("$basedir/keys.rrd", "--start", $startdate,
"--step", "1days", "DS:keys:GAUGE:86400:0:U",
"RRA:MAX:0.5:86400:365");
my $err = RRDs::error;
if ($err) {
warn "Error creating RRD: $err\n";
}
}
# Create the rrdtool graph.
sub creategraph() {
RRDs::graph("$basedir/graphs/submissions.png", "--start", $startdate,
"-w", 512, "-t", "Key Submissions per Day ($year)", "-v",
"# of keys", "DEF:keys=$basedir/keys.rrd:keys:MAX",
"LINE:keys#ff0000:Keys submitted");
my $err = RRDs::error;
if ($err) {
warn "Error creating graph: $err\n";
}
}
# Create a fresh rrdtool database.
createrrd();
# Get the number of submissions per day.
my %days = ();
my %mtimes = getmtimes("$basedir/keys");
foreach my $keys (sort{$mtimes{$a} <=> $mtimes{$b}} keys %mtimes) {
$days{strftime("%Y-%m-%d", localtime($mtimes{$keys}))}++;
}
# Let rrdtool have them.
foreach my $day (sort(keys %days)) {
my ($y, $m, $d) = split(/-/, $day);
my $time = timelocal(0, 0, 0, $d, $m-1, $y-1900);
my $err;
#printf "$day - $days{$day}\n";
RRDs::update("$basedir/keys.rrd", "$time:$days{$day}");
$err = RRDs::error;
if ($err) {
warn "Error updating RRD: $err\n";
}
}
# Graph the results.
creategraph();