databasetastic

This commit is contained in:
foo 2014-11-07 19:59:40 +00:00
parent f786b04efb
commit 68e1e6539c

29
new.pl
View File

@ -26,11 +26,16 @@ sub newer($$) {
}
my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr;
sub setlastrecord($$) {
my ($id, $time) = @_;
my $recordlevel = $db->prepare("INSERT INTO levels (stationid, timestamp, level) VALUES (?,?,?)") or die $db->errstr;
sub recordlevel($$$) {
my ($id, $time, $level) = @_;
$recordlevel->execute($id, $time, $level);
$setlastrecord->execute($id, $time);
}
my $rememberstation = $db->prepare("INSERT INTO stations (id, name, watercourse, datum) VALUES (?,?,?,?)") or die $db->errstr;
# First thing on the line should be the current time
print time, "\t" unless $ENV{R_GPLOT};
@ -43,7 +48,9 @@ foreach my $station (@stations) {
my $level;
my $stationname;
my $lastupdate;
if ($res->is_success) {
my $watercourse;
my $datum;
if ($res->is_success and $res->content =~ m~Current level:~) {
$res->content =~ m~<h1>([^<]+)</h1>~;
$stationname = $1;
@ -54,12 +61,24 @@ foreach my $station (@stations) {
$lastupdate = "$4-$3-$2T$1";
if($ENV{R_GPLOT}) {
$res->content =~ m~Watercourse:(&nbsp;|\s)+([^<]+)~;
$watercourse = $2;
$watercourse =~ s~\s*$~~;
$watercourse =~ s~^\s*~~;
$res->content =~ m~Site datum:(&nbsp;|\s)+([^<]+) m AOD~;
$datum = $2;
$datum =~ s~\s*$~~;
$datum =~ s~^\s*~~;
print " \"data.tsv\" using 1:$c with linespoints title \"$stationname\", \\\n";
$rememberstation->execute($station, $stationname, $watercourse, $datum);
} else {
if($level) {
if(newer($station, $lastupdate)) {
print $level;
setlastrecord($station, $lastupdate);
recordlevel($station, $lastupdate, $level);
} else {
print "-";
}
@ -68,7 +87,7 @@ foreach my $station (@stations) {
}
print "\t";
}
}
} else { print "-\t";}
$c++;
}