Skip to content

Commit

Permalink
もろもろ
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Nov 24, 2011
1 parent f0649b7 commit 5661c38
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 95 deletions.
81 changes: 81 additions & 0 deletions eg/alter_for_primary.pl
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/perl

use strict;
use warnings;
use FindBin;
use utf8;
use DBIx::Sunny;
use Encode;
use File::Copy;
use Digest::MD5 qw/md5_hex/;

my $root_dir = "$FindBin::Bin/..";
my $dbh = DBIx::Sunny->connect_cached('dbi:SQLite:dbname='.$root_dir.'/data.o/gforecast.db','','',{
sqlite_use_immediate_transaction => 1,
});

$dbh->do(<<EOF);
CREATE TABLE IF NOT EXISTS graphs_new (
id INTEGER NOT NULL PRIMARY KEY,
service_name VARCHAR(255) NOT NULL,
section_name VARCHAR(255) NOT NULL,
graph_name VARCHAR(255) NOT NULL,
number INT NOT NULL DEFAULT 0,
description VARCHAR(255) NOT NULL DEFAULT '',
sort UNSIGNED INT NOT NULL DEFAULT 0,
gmode VARCHAR(255) NOT NULL DEFAULT 'gauge',
color VARCHAR(255) NOT NULL DEFAULT '#00CC00',
ulimit INT NOT NULL DEFAULT 1000000000,
llimit INT NOT NULL DEFAULT 0,
sulimit INT NOT NULL DEFAULT 100000,
sllimit INT NOT NULL DEFAULT 0,
type VARCHAR(255) NOT NULL DEFAULT 'AREA',
stype VARCHAR(255) NOT NULL DEFAULT 'AREA',
meta TEXT NOT NULL DEFAULT '',
created_at UNSIGNED INT NOT NULL,
updated_at UNSIGNED INT NOT NULL,
UNIQUE (service_name, section_name, graph_name)
)
EOF

$dbh->do(<<EOF);
CREATE TABLE IF NOT EXISTS prev_graphs_new (
graph_id INT NOT NULL,
number INT NOT NULL DEFAULT 0,
subtract INT,
updated_at UNSIGNED INT NOT NULL,
PRIMARY KEY (graph_id)
)
EOF

my $rows = $dbh->select_all('SELECT * FROM graphs');
foreach my $old ( @$rows ) {
$dbh->query(
'INSERT INTO graphs_new (service_name, section_name, graph_name,
number, description, sort, gmode, color, ulimit, llimit,
sulimit, sllimit, type, stype, created_at, updated_at)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
map { $old->{$_} }
qw/service_name section_name graph_name number description sort gmode color ulimit llimit sulimit sllimit type stype created_at updated_at/
);
my $id = $dbh->last_insert_id;
my $old_prev = $dbh->select_row('SELECT * FROM prev_graphs WHERE service_name=? AND section_name=? AND graph_name=?',
$old->{service_name}, $old->{section_name}, $old->{graph_name} );
if ($old_prev) {
$dbh->query('INSERT INTO prev_graphs_new (graph_id, number, subtract, updated_at) VALUES (?,?,?,?)',
$id, $old_prev->{number}, $old_prev->{subtract}, $old_prev->{updated_at});
}
}

$dbh->query('DROP TABLE graphs');
$dbh->query('DROP TABLE prev_graphs');

$dbh->query('ALTER TABLE graphs_new RENAME to graphs');
$dbh->query('ALTER TABLE prev_graphs_new RENAME to prev_graphs');

my $rows_new = $dbh->select_all('SELECT * FROM graphs');
foreach my $row ( @$rows_new ) {
my $old = md5_hex( join(':',map { Encode::encode_utf8($_) } $row->{service_name},$row->{section_name},$row->{graph_name}) );
my $new = md5_hex( Encode::encode_utf8($row->{id}) );
move( "$root_dir/data.o/$old.rrd","$root_dir/data.o/$new.rrd");
}
114 changes: 66 additions & 48 deletions lib/GrowthForecast/Data.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Time::Piece;
use Digest::MD5 qw/md5_hex/; use Digest::MD5 qw/md5_hex/;
use List::Util; use List::Util;
use Encode; use Encode;
use JSON;
use Log::Minimal; use Log::Minimal;


sub new { sub new {
Expand All @@ -20,6 +21,7 @@ my $_on_connect = sub {
my $dbh = shift; my $dbh = shift;
$dbh->do(<<EOF); $dbh->do(<<EOF);
CREATE TABLE IF NOT EXISTS graphs ( CREATE TABLE IF NOT EXISTS graphs (
id INTEGER NOT NULL PRIMARY KEY,
service_name VARCHAR(255) NOT NULL, service_name VARCHAR(255) NOT NULL,
section_name VARCHAR(255) NOT NULL, section_name VARCHAR(255) NOT NULL,
graph_name VARCHAR(255) NOT NULL, graph_name VARCHAR(255) NOT NULL,
Expand All @@ -34,21 +36,20 @@ CREATE TABLE IF NOT EXISTS graphs (
sllimit INT NOT NULL DEFAULT 0, sllimit INT NOT NULL DEFAULT 0,
type VARCHAR(255) NOT NULL DEFAULT 'AREA', type VARCHAR(255) NOT NULL DEFAULT 'AREA',
stype VARCHAR(255) NOT NULL DEFAULT 'AREA', stype VARCHAR(255) NOT NULL DEFAULT 'AREA',
meta TEXT NOT NULL DEFAULT '',
created_at UNSIGNED INT NOT NULL, created_at UNSIGNED INT NOT NULL,
updated_at UNSIGNED INT NOT NULL, updated_at UNSIGNED INT NOT NULL,
PRIMARY KEY (service_name, section_name, graph_name) UNIQUE (service_name, section_name, graph_name)
) )
EOF EOF


$dbh->do(<<EOF); $dbh->do(<<EOF);
CREATE TABLE IF NOT EXISTS prev_graphs ( CREATE TABLE IF NOT EXISTS prev_graphs (
service_name VARCHAR(255) NOT NULL, graph_id INT NOT NULL,
section_name VARCHAR(255) NOT NULL,
graph_name VARCHAR(255) NOT NULL,
number INT NOT NULL DEFAULT 0, number INT NOT NULL DEFAULT 0,
subtract INT, subtract INT,
updated_at UNSIGNED INT NOT NULL, updated_at UNSIGNED INT NOT NULL,
PRIMARY KEY (service_name, section_name, graph_name) PRIMARY KEY (graph_id)
) )
EOF EOF
return; return;
Expand All @@ -65,50 +66,72 @@ sub dbh {
$self->{dbh}; $self->{dbh};
} }


sub inflate_row {
my ($self, $row) = @_;
$row->{created_at} = localtime($row->{created_at})->strftime('%Y/%m/%d %T');
$row->{updated_at} = localtime($row->{updated_at})->strftime('%Y/%m/%d %T');
$row->{md5} = md5_hex( Encode::encode_utf8($row->{id}) );
my $ref = decode_json($row->{meta}||'{}');
$ref->{adjust} = '*' if ! exists $ref->{adjust};
$ref->{adjustval} = '1' if ! exists $ref->{adjustval};
$ref->{unit} = '' if ! exists $ref->{unit};
my %result = (
%$ref,
%$row
);
\%result
}

sub get { sub get {
my ($self, $service, $section, $graph) = @_; my ($self, $service, $section, $graph) = @_;
my $row = $self->dbh->select_row( my $row = $self->dbh->select_row(
'SELECT * FROM graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'SELECT * FROM graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?',
$service, $section, $graph $service, $section, $graph
); );
return unless $row; return unless $row;
$row->{created_at} = localtime($row->{created_at})->strftime('%Y/%m/%d %T'); $self->inflate_row($row);
$row->{updated_at} = localtime($row->{updated_at})->strftime('%Y/%m/%d %T');
$row->{md5} = md5_hex( join(':',map { Encode::encode_utf8($_) } $row->{service_name},$row->{section_name},$row->{graph_name}) );
$row;
} }


sub get_for_rrdupdate { sub get_by_id {
my ($self, $service, $section, $graph) = @_; my ($self, $id) = @_;
my $row = $self->dbh->select_row(
'SELECT * FROM graphs WHERE id = ?',
$id
);
return unless $row;
$self->inflate_row($row);
}

sub get_by_id_for_rrdupdate {
my ($self, $id) = @_;
my $dbh = $self->dbh; my $dbh = $self->dbh;


my $data = $dbh->select_row( my $data = $dbh->select_row(
'SELECT * FROM graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'SELECT * FROM graphs WHERE id = ?',
$service, $section, $graph $id
); );
return if !$data; return if !$data;


$dbh->begin_work; $dbh->begin_work;
my $prev = $dbh->select_row( my $prev = $dbh->select_row(
'SELECT * FROM prev_graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'SELECT * FROM prev_graphs WHERE graph_id = ?',
$service, $section, $graph $data->{id}
); );


my $subtract; my $subtract;
if ( !$prev ) { if ( !$prev ) {
$subtract = 'U'; $subtract = 'U';
$dbh->query( $dbh->query(
'INSERT INTO prev_graphs (service_name, section_name, graph_name, number, subtract, updated_at) 'INSERT INTO prev_graphs (graph_id, number, subtract, updated_at)
VALUES (?,?,?,?,?,?)', VALUES (?,?,?,?)',
$service, $section, $graph, $data->{number}, undef, $data->{updated_at}); $data->{id}, $data->{number}, undef, $data->{updated_at});


} }
elsif ( $data->{updated_at} != $prev->{updated_at} ) { elsif ( $data->{updated_at} != $prev->{updated_at} ) {
$subtract = $data->{number} - $prev->{number}; $subtract = $data->{number} - $prev->{number};
$dbh->query( $dbh->query(
'UPDATE prev_graphs SET number=?, subtract=?, updated_at=? WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'UPDATE prev_graphs SET number=?, subtract=?, updated_at=? WHERE graph_id = ?',
$data->{number}, $subtract, $data->{updated_at}, $data->{number}, $subtract, $data->{updated_at}, $data->{id}
$service, $section, $graph,
); );
} }
else { else {
Expand All @@ -117,12 +140,8 @@ sub get_for_rrdupdate {
} }


$dbh->commit; $dbh->commit;

$data->{created_at} = localtime($data->{created_at})->strftime('%Y/%m/%d %T');
$data->{updated_at} = localtime($data->{updated_at})->strftime('%Y/%m/%d %T');
$data->{md5} = md5_hex( join(':',map { Encode::encode_utf8($_) } $data->{service_name},$data->{section_name},$data->{graph_name}) );
$data->{subtract} = $subtract; $data->{subtract} = $subtract;
$data; $self->inflate_row($data);
} }


sub update { sub update {
Expand All @@ -136,18 +155,17 @@ sub update {
$number += $data->{number}; $number += $data->{number};
} }
$dbh->query( $dbh->query(
'UPDATE graphs SET number=?, updated_at=? WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'UPDATE graphs SET number=?, updated_at=? WHERE id = ?',
$number, time, $number, time, $data->{id}
$service, $section, $graph,
); );
} }
else { else {
my @colors = List::Util::shuffle(qw/33 66 99 cc/); my @colors = List::Util::shuffle(qw/33 66 99 cc/);
my $color = '#' . join('', splice(@colors,0,3)); my $color = '#' . join('', splice(@colors,0,3));
$dbh->query( $dbh->query(
'INSERT INTO graphs (service_name, section_name, graph_name, number, description, color, created_at, updated_at) 'INSERT INTO graphs (service_name, section_name, graph_name, number, color, created_at, updated_at)
VALUES (?,?,?,?,?,?,?,?)', VALUES (?,?,?,?,?,?,?)',
$service, $section, $graph, $number, "", $color, time, time $service, $section, $graph, $number, $color, time, time
); );
} }
my $row = $self->get($service, $section, $graph); my $row = $self->get($service, $section, $graph);
Expand All @@ -157,13 +175,14 @@ sub update {
} }


sub update_graph { sub update_graph {
my ($self, $service, $section, $graph, $description, $sort, $gmode, $color, $type, $stype, $llimit, $ulimit, $sllimit, $sulimit ) = @_; my ($self, $id, $args) = @_;
my @update = map { delete $args->{$_} } qw/description sort gmode color type stype llimit ulimit sllimit sulimit/;
my $meta = encode_json($args);
my $dbh = $self->dbh; my $dbh = $self->dbh;
$dbh->query( $dbh->query(
'UPDATE graphs SET description=?, sort=?, gmode=?, color=?, type=?, stype=?, llimit=?, ulimit=?, sllimit=?, sulimit=? 'UPDATE graphs SET description=?, sort=?, gmode=?, color=?, type=?, stype=?,
WHERE service_name = ? AND section_name = ? AND graph_name = ?', llimit=?, ulimit=?, sllimit=?, sulimit=?, meta=? WHERE id = ?',
$description, $sort, $gmode, $color, $type, $stype, $llimit, $ulimit, $sllimit, $sulimit, @update, $meta, $id
$service, $section, $graph,
); );
return 1; return 1;
} }
Expand Down Expand Up @@ -197,32 +216,31 @@ sub get_graphs {
); );
my @ret; my @ret;
for my $row ( @$rows ) { for my $row ( @$rows ) {
$row->{created_at} = localtime($row->{created_at})->strftime('%Y/%m/%d %T'); push @ret, $self->inflate_row($row);
$row->{updated_at} = localtime($row->{updated_at})->strftime('%Y/%m/%d %T');
$row->{md5} = md5_hex( join(':', map { Encode::encode_utf8($_) } $row->{service_name},$row->{section_name},$row->{graph_name}) );
push @ret, $row;
} }
\@ret; \@ret;
} }


sub get_all_graphs { sub get_all_graph_id {
my $self = shift; my $self = shift;
$self->dbh->select_all( $self->dbh->select_all(
'SELECT service_name, section_name, graph_name FROM graphs', 'SELECT id FROM graphs',
); );
} }


sub remove { sub remove {
my ($self, $service, $section, $graph ) = @_; my ($self, $id ) = @_;
my $dbh = $self->dbh; my $dbh = $self->dbh;
$dbh->begin_work;
$dbh->query( $dbh->query(
'DELETE FROM graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'DELETE FROM graphs WHERE id = ?',
$service, $section, $graph $id
); );
$dbh->query( $dbh->query(
'DELETE FROM prev_graphs WHERE service_name = ? AND section_name = ? AND graph_name = ?', 'DELETE FROM prev_graphs WHERE graph_id = ?',
$service, $section, $graph $id
); );
$dbh->commit;


} }


Expand Down
32 changes: 19 additions & 13 deletions lib/GrowthForecast/RRD.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ sub update {
my @jp_fonts = grep { -f $_ } zglob("/usr/share/fonts/**/sazanami-gothic.ttf"); my @jp_fonts = grep { -f $_ } zglob("/usr/share/fonts/**/sazanami-gothic.ttf");
sub graph { sub graph {
my $self = shift; my $self = shift;
my ($gmode, $span, $from, $to, $data) = @_; my $data = shift;
my $args = shift;
my ($gmode, $span, $from, $to, $width, $height) = map { $args->{$_} } qw/gmode t from to width height/;
$span ||= 'd'; $span ||= 'd';
$width ||= 390;
$height ||= 110;


my $period_title; my $period_title;
my $period; my $period;
Expand Down Expand Up @@ -126,40 +130,42 @@ sub graph {


if ( $gmode eq 'subtract' ) { $period_title = "[subtract] $period_title" } if ( $gmode eq 'subtract' ) { $period_title = "[subtract] $period_title" }
my ($tmpfh, $tmpfile) = File::Temp::tempfile(UNLINK => 0, SUFFIX => ".png"); my ($tmpfh, $tmpfile) = File::Temp::tempfile(UNLINK => 0, SUFFIX => ".png");
my @args = ( my @opt = (
$tmpfile, $tmpfile,
'-w', 390, '-w', $width,
'-h', 110, '-h', $height,
'-a', 'PNG', '-a', 'PNG',
'-t', "$period_title", '-t', "$period_title",
'-l', 0, #minimum '-l', 0, #minimum
'-u', 2, #maximum '-u', 2, #maximum
'-x', $xgrid, '-x', $xgrid,
'-s', $period, '-s', $period,
'-e', $end, '-e', $end,
'--slope-mode',
'--disable-rrdtool-tag',
); );

push @opt, '--only-graph' if $args->{graphonly};
push @args, '--font', "DEFAULT:0:".$jp_fonts[0] if @jp_fonts; push @opt, '--font', "DEFAULT:0:".$jp_fonts[0] if @jp_fonts;


my $i=0; my $i=0;
my $type = ( $gmode eq 'subtract' ) ? $data->{stype} : $data->{type}; my $type = ( $gmode eq 'subtract' ) ? $data->{stype} : $data->{type};
my $gdata = ( $gmode eq 'subtract' ) ? 'sub' : 'num'; my $gdata = ( $gmode eq 'subtract' ) ? 'sub' : 'num';
my $llimit = ( $gmode eq 'subtract' ) ? $data->{sllimit} : $data->{llimit}; my $llimit = ( $gmode eq 'subtract' ) ? $data->{sllimit} : $data->{llimit};
my $ulimit = ( $gmode eq 'subtract' ) ? $data->{sulimit} : $data->{ulimit}; my $ulimit = ( $gmode eq 'subtract' ) ? $data->{sulimit} : $data->{ulimit};
my $file = $self->path($data); my $file = $self->path($data);
push @args, push @opt,
sprintf('DEF:%s%dt=%s:%s:AVERAGE', $gdata, $i, $file, $gdata), sprintf('DEF:%s%dt=%s:%s:AVERAGE', $gdata, $i, $file, $gdata),
sprintf('CDEF:%s%d=%s%dt,%s,%s,LIMIT', $gdata, $i, $gdata, $i, $llimit, $ulimit), sprintf('CDEF:%s%d=%s%dt,%s,%s,LIMIT,%d,%s', $gdata, $i, $gdata, $i, $llimit, $ulimit, $data->{adjustval}, $data->{adjust}),
sprintf('%s:%s%d%s:%s ', $type, $gdata, $i, $data->{color}, $data->{graph_name}), sprintf('%s:%s%d%s:%s ', $type, $gdata, $i, $data->{color}, $data->{graph_name}),
sprintf('GPRINT:%s%d:LAST:Cur\: %%4.1lf%%s', $gdata, $i), sprintf('GPRINT:%s%d:LAST:Cur\: %%4.1lf%%s%s', $gdata, $i, $data->{unit}),
sprintf('GPRINT:%s%d:AVERAGE:Avg\: %%4.1lf%%s', $gdata, $i), sprintf('GPRINT:%s%d:AVERAGE:Avg\: %%4.1lf%%s%s', $gdata, $i, $data->{unit}),
sprintf('GPRINT:%s%d:MAX:Max\: %%4.1lf%%s', $gdata, $i), sprintf('GPRINT:%s%d:MAX:Max\: %%4.1lf%%s%s', $gdata, $i, $data->{unit}),
sprintf('GPRINT:%s%d:MIN:Min\: %%4.1lf%%s\l', $gdata, $i); sprintf('GPRINT:%s%d:MIN:Min\: %%4.1lf%%s%s\l', $gdata, $i, $data->{unit});
$i++; $i++;




eval { eval {
RRDs::graph(map { Encode::encode_utf8($_) } @args); RRDs::graph(map { Encode::encode_utf8($_) } @opt);
my $ERR=RRDs::error; my $ERR=RRDs::error;
die $ERR if $ERR; die $ERR if $ERR;
}; };
Expand Down
Loading

0 comments on commit 5661c38

Please sign in to comment.