Skip to content

Commit 1366b80

Browse files
authored
Added functionality to add aggregated data to get requests (#1214)
1 parent 997900f commit 1366b80

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ protected function getFeatures(): array
120120
return $features;
121121
}
122122

123+
/**
124+
* Overidable function to aggregate data in the object. Currently only used for Tasks
125+
* returns the aggregated data in key value pairs
126+
*/
127+
public static function aggregateData(object $object): array {
128+
return [];
129+
}
130+
123131
/**
124132
* Take all the dba features and converts them to a list.
125133
* It uses the data from the generator and replaces the keys with the aliasses.
@@ -517,6 +525,9 @@ protected function obj2Resource(object $obj, array $expandResult = [])
517525
$attributes[$feature['alias']] = $apiClass::db2json($feature, $kv[$name]);
518526
}
519527

528+
//TODO: only aggregate data when it has been included
529+
$aggregatedData = $apiClass::aggregateData($obj);
530+
$attributes = array_merge($attributes, $aggregatedData);
520531

521532
/* Build JSON::API relationship resource */
522533
$toManyRelationships = $apiClass::getToManyRelationships();
@@ -890,6 +901,7 @@ protected function getPrimaryKey(): string
890901
return $key;
891902
}
892903
}
904+
throw new HTException("Internal error: no primary key found");
893905
}
894906

895907
function getFilters(Request $request) {
@@ -899,7 +911,6 @@ function getFilters(Request $request) {
899911
/**
900912
* Check for valid filter parameters and build QueryFilter
901913
*/
902-
// protected function makeFilter(Request $request, array $features): array
903914
protected function makeFilter(array $filters, object $apiClass): array
904915
{
905916
$qFs = [];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ protected function getPrimaryKeyOther(string $dbaClass): string
159159
return $key;
160160
}
161161
}
162+
throw new HTException("Internal error: no primary key found");
162163
}
163164

164165
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use DBA\Speed;
1212
use DBA\Task;
1313
use DBA\TaskWrapper;
14+
use Util;
1415

1516
require_once(dirname(__FILE__) . "/../common/AbstractModelAPI.class.php");
1617

@@ -125,6 +126,13 @@ protected function createObject(array $data): int {
125126
return $object->getId();
126127
}
127128

129+
static function aggregateData(object $object): array {
130+
$aggregatedData["Dispatched"] = Util::showperc($object->getKeyspaceProgress(), $object->getKeyspace());
131+
$aggregatedData["Searched"] = Util::showperc(TaskUtils::getTaskProgress($object), $object->getKeyspace());
132+
133+
return $aggregatedData;
134+
}
135+
128136
protected function deleteObject(object $object): void {
129137
TaskUtils::deleteTask($object);
130138
}

src/inc/utils/TaskUtils.class.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use DBA\TaskDebugOutput;
2626
use DBA\Factory;
2727
use DBA\Speed;
28+
use DBA\Aggregation;
2829

2930
class TaskUtils {
3031
/**
@@ -1393,4 +1394,14 @@ public static function isSaturatedByOtherAgents($task, $agent) {
13931394
return ($task->getIsSmall() == 1 && $numAssignments > 0) || // at least one agent is already assigned here
13941395
($task->getMaxAgents() > 0 && $numAssignments >= $task->getMaxAgents()); // at least maxAgents agents are already assigned
13951396
}
1397+
1398+
public static function getTaskProgress($task) {
1399+
$qF1 = new QueryFilter(Chunk::TASK_ID, $task->getId(), "=");
1400+
1401+
$agg1 = new Aggregation(Chunk::CHECKPOINT, Aggregation::SUM);
1402+
$agg2 = new Aggregation(Chunk::SKIP, Aggregation::SUM);
1403+
$results = Factory::getChunkFactory()->multicolAggregationFilter([Factory::FILTER => $qF1], [$agg1, $agg2]);
1404+
$progress = $results[$agg1->getName()] - $results[$agg2->getName()];
1405+
return $progress;
1406+
}
13961407
}

0 commit comments

Comments
 (0)