Record Debree optimalization

This commit is contained in:
JonatanRek 2020-10-19 16:22:17 +02:00
parent 6b7bd911f7
commit 9dfaeb554d

View File

@ -24,11 +24,18 @@ class RecordManager{
if ($subDeviceId == '') { if ($subDeviceId == '') {
return false; return false;
}; };
//Ochrana proti duplicitním hodnotám zapisují se jen změny
if (self::getLastRecord($subDeviceId, 1)['value'] === $value){
return false;
}
try { try {
$record = [ $record = [
'execuded' => 1, 'execuded' => 1,
]; ];
Db::edit ('records', $record, 'WHERE subdevice_id = ?', array ($subDeviceId)); Db::edit ('records', $record, 'WHERE subdevice_id = ?', array ($subDeviceId));
$record = array ( $record = array (
'subdevice_id' => $subDeviceId, 'subdevice_id' => $subDeviceId,
'value' => $value, 'value' => $value,
@ -40,7 +47,6 @@ class RecordManager{
} }
} }
public static function setExecuted($recordId) { public static function setExecuted($recordId) {
try { try {
Db::edit ('records', ['execuded' => 1], 'WHERE record_id = ?', array($recordId)); Db::edit ('records', ['execuded' => 1], 'WHERE record_id = ?', array($recordId));
@ -72,11 +78,12 @@ class RecordManager{
return Db::loadAll('SELECT * FROM records WHERE subdevice_id = ? AND time >= ? AND time <= ? AND value != ? ORDER BY time;', array($subDeviceId, $timeFrom, $timeTo, 999)); return Db::loadAll('SELECT * FROM records WHERE subdevice_id = ? AND time >= ? AND time <= ? AND value != ? ORDER BY time;', array($subDeviceId, $timeFrom, $timeTo, 999));
} }
//TODO: Zeptat se @Patrik jestli je secure pak používat periodu přímo do SQL a pak pře url SQL Injection
public static function getAllRecordForGraph($subDeviceId, $period = "day", $groupBy = "hour") { public static function getAllRecordForGraph($subDeviceId, $period = "day", $groupBy = "hour") {
$periodLocal = '- 1 ' . strtoupper($period); $periodLocal = '- 1' . strtoupper($period);
$dateTime = new DateTime(); $dateTime = new DateTime();
$dateTime = $dateTime->modify($periodLocal); $dateTime = $dateTime->modify($periodLocal);
$dateTime = $dateTime->format('Y-m-d'); $dateTime = $dateTime->format('Y-m-d H:i:s');
$groupBy = strtoupper($groupBy).'(time)'; $groupBy = strtoupper($groupBy).'(time)';
$sql = 'SELECT value, time FROM records $sql = 'SELECT value, time FROM records
WHERE WHERE
@ -86,7 +93,7 @@ class RecordManager{
AND AND
time > ? time > ?
GROUP BY '.$groupBy.' GROUP BY '.$groupBy.'
ORDER BY time ASC'; ORDER BY time Desc';
//TODO: Prasárna Opravit //TODO: Prasárna Opravit
return Db::loadAll($sql, array($subDeviceId, $dateTime)); return Db::loadAll($sql, array($subDeviceId, $dateTime));
} }
@ -97,7 +104,6 @@ class RecordManager{
} }
} }
//TODO: zkontrolovat jestli neco nezbilo po smazaní //TODO: zkontrolovat jestli neco nezbilo po smazaní
public static function cleanSubdeviceRecords ($subDeviceId) { public static function cleanSubdeviceRecords ($subDeviceId) {
Db::command ('DELETE FROM records WHERE subdevice_id = ?);', array($subDeviceId)); Db::command ('DELETE FROM records WHERE subdevice_id = ?);', array($subDeviceId));