diff --git a/TODO b/TODO index 850c988d0f60a87664d4b5afd4b666a7c6ea23f3..cef75ed80516f31a4c2d6f013e57258d01a1028f 100644 --- a/TODO +++ b/TODO @@ -7,4 +7,4 @@ usage related queries: * Use Math module to perform calculations smadmin: -* Update or possibly ignore entries instead of deleting +* Update or possibly ignore entries instead of deleting (topup summaries) diff --git a/smradius/modules/accounting/mod_accounting_sql.pm b/smradius/modules/accounting/mod_accounting_sql.pm index 1e19f891567b135d099497f1aa0d10817598682d..28c1a15e3a1efc82853bbe2c4e3af5d312f1540d 100644 --- a/smradius/modules/accounting/mod_accounting_sql.pm +++ b/smradius/modules/accounting/mod_accounting_sql.pm @@ -683,37 +683,97 @@ sub cleanup my @insertArray; for (my $i = 0; $i < $index; $i++) { - @insertArray = ( - $allRecords[$i]->{'Username'}, + + # Check if this record exists + my $sth = DBSelect(' + SELECT + COUNT(*) as rowCount + FROM + @TP@accounting + WHERE + PeriodKey = ? + AND Username = ? + ', $allRecords[$i]->{'PeriodKey'}, - $allRecords[$i]->{'SessionTime'}, - $allRecords[$i]->{'InputOctets'}, - $allRecords[$i]->{'InputGigawords'}, - $allRecords[$i]->{'OutputOctets'}, - $allRecords[$i]->{'OutputGigawords'} + $allRecords[$i]->{'Username'} ); - @dbDoParams = (' - INSERT INTO - @TP@accounting_summary - ( - Username, - PeriodKey, - AcctSessionTime, - AcctInputOctets, - AcctInputGigawords, - AcctOutputOctets, - AcctOutputGigawords - ) - VALUES - (?,?,?,?,?,?,?) - ', - @insertArray + if (!$sth) { + $server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to check for existing record: ". + awitpt::db::dblayer::Error()); + return; + } + + my $recordCheck = $sth->fetchrow_hashref(); + $recordCheck = hashifyLCtoMC( + $recordCheck, + qw(rowCount) ); - if ($sth) { - # Do query - $sth = DBDo(@dbDoParams); + if (defined($recordCheck->{'rowCount'}) && $recordCheck->{'rowCount'} > 0) { + @insertArray = ( + $allRecords[$i]->{'SessionTime'}, + $allRecords[$i]->{'InputOctets'}, + $allRecords[$i]->{'InputGigawords'}, + $allRecords[$i]->{'OutputOctets'}, + $allRecords[$i]->{'OutputGigawords'}, + $allRecords[$i]->{'Username'}, + $allRecords[$i]->{'PeriodKey'} + ); + + @dbDoParams = (' + UPDATE + @TP@accounting_summary + SET + AcctSessionTime = ?, + AcctInputOctets = ?, + AcctInputGigawords = ?, + AcctOutputOctets = ?, + AcctOutputGigawords = ? + WHERE + Username = ? + AND PeriodKey = ? + ', + @insertArray + ); + + if ($sth) { + # Do query + $sth = DBDo(@dbDoParams); + } + } else { + @insertArray = ( + $allRecords[$i]->{'Username'}, + $allRecords[$i]->{'PeriodKey'}, + $allRecords[$i]->{'SessionTime'}, + $allRecords[$i]->{'InputOctets'}, + $allRecords[$i]->{'InputGigawords'}, + $allRecords[$i]->{'OutputOctets'}, + $allRecords[$i]->{'OutputGigawords'} + ); + + @dbDoParams = (' + INSERT INTO + @TP@accounting_summary + ( + Username, + PeriodKey, + AcctSessionTime, + AcctInputOctets, + AcctInputGigawords, + AcctOutputOctets, + AcctOutputGigawords + ) + VALUES + (?,?,?,?,?,?,?) + ', + @insertArray + ); + + if ($sth) { + # Do query + $sth = DBDo(@dbDoParams); + } } } } @@ -721,7 +781,7 @@ sub cleanup # Rollback with error if failed if (!$sth) { DBRollback(); - $server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to insert accounting record: ". + $server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to insert or update accounting record: ". awitpt::db::dblayer::Error()); return; }