Skip to content

Commit

Permalink
Final fixes for billing
Browse files Browse the repository at this point in the history
  • Loading branch information
laf committed Jul 17, 2016
1 parent 47f7c37 commit 5a9a963
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions includes/billing.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ function getValue($host, $port, $id, $inout) {

}//end getValue()

function getLastPortCounter($port_id) {
function getLastPortCounter($port_id, $bill_id) {
$return = array();
$row = dbFetchRow("SELECT timestamp, in_counter, in_delta, out_counter, out_delta FROM bill_port_counters WHERE `port_id` = ?", array($port_id));
$row = dbFetchRow("SELECT timestamp, in_counter, in_delta, out_counter, out_delta FROM bill_port_counters WHERE `port_id` = ? AND `bill_id` = ?", array($port_id, $bill_id));
if (!is_null($row)) {
$return[timestamp] = $row['timestamp'];
$return[in_counter] = $row['in_counter'];
Expand Down
9 changes: 5 additions & 4 deletions poll-billing.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function CollectData($bill_id) {
$port_data['in_measurement'] = getValue($port_data['hostname'], $port_data['port'], $port_data['ifIndex'], 'In');
$port_data['out_measurement'] = getValue($port_data['hostname'], $port_data['port'], $port_data['ifIndex'], 'Out');

$last_counters = getLastPortCounter($port_id);
$last_counters = getLastPortCounter($port_id, $bill_id);
if ($last_counters['state'] == 'ok') {
$port_data['last_in_measurement'] = $last_counters[in_counter];
$port_data['last_in_delta'] = $last_counters[in_delta];
Expand All @@ -75,7 +75,7 @@ function CollectData($bill_id) {

$tmp_period = dbFetchCell("SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP('".mres($last_counters['timestamp'])."')");

if (delta_to_bits($port_data['in_measurement'], $tmp_period)-$port_data['last_in_measurement'] > $port_data['ifSpeed']) {
if ((delta_to_bits($port_data['in_measurement'], $tmp_period)-delta_to_bits($port_data['last_in_measurement'], $tmp_period)) > $port_data['ifSpeed']) {
$port_data['in_delta'] = $port_data['last_in_delta'];
}
elseif ($port_data['in_measurement'] >= $port_data['last_in_measurement']) {
Expand All @@ -85,7 +85,7 @@ function CollectData($bill_id) {
$port_data['in_delta'] = $port_data['last_in_delta'];
}

if (delta_to_bits($port_data['out_measurement'], $tmp_period)-$port_data['last_out_measurement'] > $port_data['ifSpeed']) {
if ((delta_to_bits($port_data['out_measurement'], $tmp_period)-delta_to_bits($port_data['last_out_measurement'], $tmp_period)) > $port_data['ifSpeed']) {
$port_data['out_delta'] = $port_data['last_out_delta'];
}
elseif ($port_data['out_measurement'] >= $port_data['last_out_measurement']) {
Expand All @@ -101,7 +101,8 @@ function CollectData($bill_id) {
}

$fields = array('timestamp' => $now, 'in_counter' => $port_data['in_measurement'], 'out_counter' => $port_data['out_measurement'], 'in_delta' => $port_data['in_delta'], 'out_delta' => $port_data['out_delta']);
if (dbUpdate($fields, 'bill_port_counters', "`port_id`='" . mres($port_id) . "'") == 0) {
if (dbUpdate($fields, 'bill_port_counters', "`port_id`='" . mres($port_id) . "' AND `bill_id`='$bill_id'") == 0) {
$fields['bill_id'] = $bill_id;
$fields['port_id'] = $port_id;
dbInsert($fields, 'bill_port_counters');
}
Expand Down
2 changes: 2 additions & 0 deletions sql-schema/124.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `bill_port_counters` ADD `bill_id` INT NOT NULL ;
ALTER TABLE `bill_port_counters` DROP PRIMARY KEY, ADD PRIMARY KEY ( `port_id` , `bill_id` );

0 comments on commit 5a9a963

Please sign in to comment.