Skip to content

Commit

Permalink
Chunck load device point
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Hamann <git@arne.email>
  • Loading branch information
tacruc committed Feb 13, 2023
1 parent e7f8f72 commit 901bc2e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/Controller/DevicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
21 changes: 18 additions & 3 deletions lib/Service/DevicesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 = [];
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions src/views/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
},
Expand Down

0 comments on commit 901bc2e

Please sign in to comment.