Do not update alert timestamp when updating a triggered alert (#10907)

* Do not update alert timestamp when updating a triggered alert

* Only update alerts timestamp if alert is triggered or recovered

* db schema

* db schema

* cleanup timestamp update
This commit is contained in:
louis-oui
2020-01-22 22:01:25 +01:00
committed by Kevin Krumm
parent 1c043069b1
commit a384fc2830
3 changed files with 31 additions and 3 deletions

View File

@@ -112,7 +112,7 @@ class AlertRules
if (is_null($current_state)) {
dbInsert(array('state' => 1, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1,'alerted' => 0), 'alerts');
} else {
dbUpdate(['state' => 1, 'open' => 1], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
dbUpdate(['state' => 1, 'open' => 1, 'timestamp' => array('NOW()')], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
}
c_echo(PHP_EOL . 'Status: %rALERT');
}
@@ -125,7 +125,7 @@ class AlertRules
if (is_null($current_state)) {
dbInsert(['state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0], 'alerts');
} else {
dbUpdate(['state' => 0, 'open' => 1, 'note' => ''], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
dbUpdate(['state' => 0, 'open' => 1, 'note' => '', 'timestamp' => array('NOW()')], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
}
c_echo(PHP_EOL . 'Status: %gOK');

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AlertsDisableOnUpdateCurrentTimestamp extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\DB::statement("ALTER TABLE `alerts` CHANGE `timestamp` `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP;");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
\DB::statement("ALTER TABLE `alerts` CHANGE `timestamp` `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP;");
}
}

View File

@@ -28,7 +28,7 @@ alerts:
- { Field: alerted, Type: int(11), 'Null': false, Extra: '' }
- { Field: open, Type: int(11), 'Null': false, Extra: '' }
- { Field: note, Type: text, 'Null': true, Extra: '' }
- { Field: timestamp, Type: timestamp, 'Null': false, Extra: 'on update CURRENT_TIMESTAMP', Default: CURRENT_TIMESTAMP }
- { Field: timestamp, Type: timestamp, 'Null': false, Extra: '', Default: CURRENT_TIMESTAMP }
- { Field: info, Type: text, 'Null': false, Extra: '' }
Indexes:
PRIMARY: { Name: PRIMARY, Columns: [id], Unique: true, Type: BTREE }