Skip to content

Commit cd61bff

Browse files
committed
Added sparse fieldset support for the aggregated data
1 parent d897121 commit cd61bff

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

src/inc/apiv2/common/AbstractBaseAPI.class.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ protected function getUpdateHandlers($id, $current_user): array {
151151
* Overridable function to aggregate data in the object. Currently only used for Tasks
152152
* returns the aggregated data in key value pairs
153153
*/
154-
public static function aggregateData(object $object): array {
154+
public static function aggregateData(object $object, array $sparseFieldsets = null): array {
155155
return [];
156156
}
157157

@@ -591,8 +591,7 @@ protected function obj2Resource(object $obj, array $expandResult = [], array $sp
591591
$attributes[$feature['alias']] = $apiClass::db2json($feature, $kv[$name]);
592592
}
593593

594-
//TODO: only aggregate data when it has been included
595-
$aggregatedData = $apiClass::aggregateData($obj);
594+
$aggregatedData = $apiClass::aggregateData($obj, $sparseFieldsets);
596595
$attributes = array_merge($attributes, $aggregatedData);
597596

598597
/* Build JSON::API relationship resource */

src/inc/apiv2/model/tasks.routes.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,36 +160,47 @@ protected function createObject(array $data): int {
160160
return $task->getId();
161161
}
162162

163-
//TODO make aggregate data queryable and not included by default
164-
static function aggregateData(object $object): array {
163+
//TODO make aggregate data queryable
164+
static function aggregateData(object $object, array $sparseFieldsets = null): array {
165+
$aggregatedData = [];
165166
$keyspace = $object->getKeyspace();
166167
$keyspaceProgress = $object->getKeyspaceProgress();
167168

168-
$aggregatedData["dispatched"] = Util::showperc($keyspaceProgress, $keyspace);
169-
$aggregatedData["searched"] = Util::showperc(TaskUtils::getTaskProgress($object), $keyspace);
170-
171-
$qF = new QueryFilter(Chunk::TASK_ID, $object->getId(), "=");
172-
$chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]);
173-
174-
$activeAgents = [];
175-
foreach ($chunks as $chunk) {
176-
if (time() - max($chunk->getSolveTime(), $chunk->getDispatchTime()) < SConfig::getInstance()->getVal(DConfig::CHUNK_TIMEOUT) && $chunk->getProgress() < 10000) {
177-
$activeAgents[$chunk->getAgentId()] = true;
178-
}
169+
if(is_array($sparseFieldsets) && array_key_exists('task', $sparseFieldsets) && in_array("dispatched", $sparseFieldsets['task']) ) {
170+
$aggregatedData["dispatched"] = Util::showperc($keyspaceProgress, $keyspace);
179171
}
180-
181-
//status 1 is running, 2 is idle and 3 is completed
182-
$status = 2;
183-
if ($keyspaceProgress >= $keyspace && $keyspaceProgress > 0) {
184-
$status = 3;
172+
173+
if(is_array($sparseFieldsets) && array_key_exists('task', $sparseFieldsets) && in_array("searched", $sparseFieldsets['task']) ) {
174+
$aggregatedData["searched"] = Util::showperc(TaskUtils::getTaskProgress($object), $keyspace);
175+
}
176+
177+
if(is_array($sparseFieldsets) && array_key_exists('task', $sparseFieldsets) && in_array("activeAgents", $sparseFieldsets['task']) ) {
178+
$qF = new QueryFilter(Chunk::TASK_ID, $object->getId(), "=");
179+
$chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]);
180+
181+
$activeAgents = [];
182+
foreach ($chunks as $chunk) {
183+
if (time() - max($chunk->getSolveTime(), $chunk->getDispatchTime()) < SConfig::getInstance()->getVal(DConfig::CHUNK_TIMEOUT) && $chunk->getProgress() < 10000) {
184+
$activeAgents[$chunk->getAgentId()] = true;
185+
}
186+
}
187+
188+
$aggregatedData["activeAgents"] = array_keys($activeAgents);
185189
}
186-
elseif (count($activeAgents) > 0) {
187-
$status = 1;
190+
191+
if(is_array($sparseFieldsets) && array_key_exists('task', $sparseFieldsets) && in_array("searched", $sparseFieldsets['task']) ) {
192+
//status 1 is running, 2 is idle and 3 is completed
193+
$status = 2;
194+
if ($keyspaceProgress >= $keyspace && $keyspaceProgress > 0) {
195+
$status = 3;
196+
}
197+
elseif (count($activeAgents) > 0) {
198+
$status = 1;
199+
}
200+
201+
$aggregatedData["status"] = $status;
188202
}
189203

190-
$aggregatedData["activeAgents"] = array_keys($activeAgents);
191-
$aggregatedData["status"] = $status;
192-
193204
return $aggregatedData;
194205
}
195206

0 commit comments

Comments
 (0)