- my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
-
- my $sum = "
- SELECT COALESCE(SUM(value),0) FROM srvexport AS other
- WHERE other.serviceid IN ($sin)
- AND ABS( $cur - $other_date ) <= 60
- ";
-
- my $isql = "
- INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
- VALUES ( ?, ?, ?, ($sum), ? )
- ";
- my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
- time2str('%X', $cur), #srv_time
- "$serviceid$dir",
- 300, #intvl ...
+ #if we're more than a day behind, call native torrus srvderive
+ my $last_srv_date = $torrus_srvderive->last_srv_date;
+ my $currdate = time2str('%Y-%m-%d', time);
+ if ( $last_srv_date ne $currdate
+ && time - str2time($last_srv_date) > (24)*60*60
+ ) {
+ warn "serviceid $serviceid last_srv_date != $currdate\n" if $DEBUG;
+
+ #delete records from last_srv_date
+ my $dsql =
+ 'DELETE FROM srvexport WHERE srv_date = ? AND serviceid IN(?,?)';
+ my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
+ warn "$dsql $last_srv_date ${serviceid}_IN ${serviceid}_OUT\n"
+ if $DEBUG;
+ $dsth->execute($last_srv_date, $serviceid.'_IN', $serviceid.'_OUT')
+ or die $dsth->errstr;
+ dbh->commit or die dbh->errstr;
+
+ foreach my $dir ('_IN','_OUT') {
+
+ #XX TORRUS_PATH
+ my @cmd = ( '/usr/local/bin/torrus', 'srvderive',
+ "--start=$last_srv_date", "--end=$currdate",
+ "--out=$serviceid$dir", "--func=SUM",
+ map "$_$dir", @serviceids