From 901bc2e011320441c321f1aa322cce5284313442 Mon Sep 17 00:00:00 2001 From: Arne Hamann Date: Mon, 13 Feb 2023 19:06:22 +0100 Subject: [PATCH] Chunck load device point Signed-off-by: Arne Hamann --- lib/Controller/DevicesController.php | 4 ++-- lib/Service/DevicesService.php | 21 ++++++++++++++++++--- src/network.js | 4 +++- src/views/App.vue | 16 ++++++++++++---- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/Controller/DevicesController.php b/lib/Controller/DevicesController.php index 39451d6c0..87864e8c7 100644 --- a/lib/Controller/DevicesController.php +++ b/lib/Controller/DevicesController.php @@ -107,8 +107,8 @@ public function getDevices(): DataResponse { * @param int $pruneBefore * @return DataResponse */ - public function getDevicePoints($id, int $pruneBefore=0): DataResponse { - $points = $this->devicesService->getDevicePointsFromDB($this->userId, $id, $pruneBefore); + public function getDevicePoints($id, ?int $pruneBefore=0, ?int $limit=10000, ?int $offset=0): DataResponse { + $points = $this->devicesService->getDevicePointsFromDB($this->userId, $id, $pruneBefore, $limit, $offset); return new DataResponse($points); } diff --git a/lib/Service/DevicesService.php b/lib/Service/DevicesService.php index 0d2731c31..4cb71dc7f 100644 --- a/lib/Service/DevicesService.php +++ b/lib/Service/DevicesService.php @@ -75,7 +75,16 @@ public function getDevicesFromDB($userId) { return $devices; } - public function getDevicePointsFromDB($userId, $deviceId, $pruneBefore=0) { + /** + * @param $userId + * @param $deviceId + * @param int|null $pruneBefore + * @param int|null $limit + * @param int|null $offset + * @return array + * @throws \OCP\DB\Exception + */ + public function getDevicePointsFromDB($userId, $deviceId, ?int $pruneBefore=0, ?int $limit=null, ?int $offset=null) { $qb = $this->qb; // get coordinates $qb->select('p.id', 'lat', 'lng', 'timestamp', 'altitude', 'accuracy', 'battery') @@ -92,7 +101,13 @@ public function getDevicePointsFromDB($userId, $deviceId, $pruneBefore=0) { $qb->expr()->gt('timestamp', $qb->createNamedParameter($pruneBefore, IQueryBuilder::PARAM_INT)) ); } - $qb->orderBy('timestamp', 'ASC'); + if (!is_null($offset)) { + $qb->setFirstResult($offset); + } + if (!is_null($limit)) { + $qb->setMaxResults($limit); + } + $qb->orderBy('timestamp', 'DESC'); $req = $qb->execute(); $points = []; @@ -110,7 +125,7 @@ public function getDevicePointsFromDB($userId, $deviceId, $pruneBefore=0) { $req->closeCursor(); $qb = $qb->resetQueryParts(); - return $points; + return array_reverse($points); } public function getOrCreateDeviceFromDB($userId, $userAgent) { diff --git a/src/network.js b/src/network.js index 2a405e707..bb6f34048 100644 --- a/src/network.js +++ b/src/network.js @@ -390,10 +390,12 @@ export function getDevices(myMapId = null) { return axios.get(url, conf) } -export async function getDevice(id, myMapId = null) { +export async function getDevice(id, myMapId = null, limit= null, offset = null) { const conf = { params: { myMapId, + limit, + offset, }, } const url = generateUrl('/apps/maps/devices/' + id) diff --git a/src/views/App.vue b/src/views/App.vue index 314bcb9c9..c4872bd08 100644 --- a/src/views/App.vue +++ b/src/views/App.vue @@ -2017,9 +2017,17 @@ export default { }, getDevice(device, enable = false, save = true, zoom = false) { device.loading = true - network.getDevice(device.id, this.myMapId).then(async (response) => { - this.$set(device, 'points', response.data /* .sort((p1, p2) => (p1.timestamp || 0) - (p2.timestamp || 0)) */) - + network.getDevice(device.id, this.myMapId, 100000, device.points?.length || 0).then((response) => { + //There are too many points making it responsiv crashes most browsers + // this.$set(device, 'points', response.data /* .sort((p1, p2) => (p1.timestamp || 0) - (p2.timestamp || 0)) */) + if (device.points) { + device.points = response.data.concat(device.points) + } else { + device.points = response.data + } + if (response.data.length >= 100000) { + this.getDevice(device, false, false, false) + } if (enable) { device.enabled = true } @@ -2127,7 +2135,7 @@ export default { // Fixme showInfo('Adding device to map not supported yet') }, - onToggleDeviceHistory(device) { + async onToggleDeviceHistory(device) { device.historyEnabled = !device.historyEnabled this.saveEnabledDeviceLines() },