diff --git a/src/app/Http/Controllers/StatisticsController.php b/src/app/Http/Controllers/StatisticsController.php index e9d58be..f90e67a 100644 --- a/src/app/Http/Controllers/StatisticsController.php +++ b/src/app/Http/Controllers/StatisticsController.php @@ -5,10 +5,10 @@ use App\Http\Controllers\ResponseController; use App\Http\Resources\StatisticsResource; use App\Models\SummaryStatsView; +use Illuminate\Database\Query\Builder; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -// use Illuminate\Support\Collection; -// use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\DB; class StatisticsController extends ResponseController { @@ -37,4 +37,49 @@ public function index(Request $request): JsonResponse return $this->sendError('Invalid data', $exception->getMessage(), 400); } } + + public function alltimeIndex(): JsonResponse + { + try { + $items = DB::table('Item')->select('CompletionStatusId'); + $scores = DB::table('Score')->select('ScoreTypeId', 'UserId', 'Amount'); + + $data = [ + 'ActiveUsers' => $this->countDistinctUsers($scores), + 'ItemsNotStarted' => $this->countByCompletionStatusId($items, 1), + 'ItemsEdited' => $this->countByCompletionStatusId($items, 2), + 'ItemsReviewed' => $this->countByCompletionStatusId($items, 3), + 'ItemsCompleted' => $this->countByCompletionStatusId($items, 4), + 'ManualTranscriptions' => $this->sumByScoreTypeId($scores, 2), + 'HTRTranscriptions' => $this->sumByScoreTypeId($scores, 5), + 'Locations' => $this->sumByScoreTypeId($scores, 1), + 'Enrichments' => $this->sumByScoreTypeId($scores, 3), + 'Descriptions' => $this->sumByScoreTypeId($scores, 4) + ]; + + $resource = new StatisticsResource($data); + + return $this->sendResponse($resource, 'Statistics fetched.'); + } catch (\Exception $exception) { + return $this->sendError('Invalid data', $exception->getMessage(), 400); + } + } + + private function sumByScoreTypeId(Builder $query, int $scoreTypeId): int + { + $cloned = clone $query; + return intval($cloned->where('ScoreTypeId', '=', $scoreTypeId)->sum('Amount')); + } + + private function countByCompletionStatusId(Builder $query, int $completionStatusId): int + { + $cloned = clone $query; + return $cloned->where('CompletionStatusId', '=', $completionStatusId)->count(); + } + + private function countDistinctUsers(Builder $query): int + { + $cloned = clone $query; + return $cloned->distinct()->count('UserId'); + } } diff --git a/src/routes/api.php b/src/routes/api.php index 261455b..1e4ff69 100644 --- a/src/routes/api.php +++ b/src/routes/api.php @@ -108,4 +108,5 @@ Route::delete('/projects/{id}', [ProjectController::class, 'destroy']); Route::get('/statistics', [StatisticsController::class, 'index']); + Route::get('/statistics/alltime', [StatisticsController::class, 'alltimeIndex']); }); diff --git a/src/storage/api-docs/api-docs.yaml b/src/storage/api-docs/api-docs.yaml index 1a09644..856994e 100644 --- a/src/storage/api-docs/api-docs.yaml +++ b/src/storage/api-docs/api-docs.yaml @@ -1,7 +1,7 @@ openapi: 3.0.3 info: - version: 1.37.2 + version: 1.38.0 title: Transcribathon Platform API v2 description: This is the documentation of the Transcribathon API v2 used by [https:transcribathon.eu](https://transcribathon.eu/).
For authorization you can use the the bearer token you are provided with. @@ -54,6 +54,8 @@ tags: paths: /statistics: $ref: 'statistics-path.yaml' + /statistics/alltime: + $ref: 'statistics-alltime-path.yaml' /stories/{StoryId}/autoenrichments: $ref: 'stories-storyId-autoenrichments-path.yaml' @@ -209,5 +211,7 @@ components: $ref: 'scores-schema.yaml#/ScoresPostRequestSchema' StatisticsGetResponseSchema: $ref: 'statistics-schema.yaml#/StatisticsGetResponseSchema' + StatisticsAlltimeGetResponseSchema: + $ref: 'statistics-alltime-schema.yaml#/StatisticsAlltimeGetResponseSchema' HealthGetResponseSchema: $ref: 'health-schema.yaml#/HealthGetResponseSchema' diff --git a/src/storage/api-docs/statistics-alltime-path.yaml b/src/storage/api-docs/statistics-alltime-path.yaml new file mode 100644 index 0000000..fd93fa7 --- /dev/null +++ b/src/storage/api-docs/statistics-alltime-path.yaml @@ -0,0 +1,18 @@ +get: + tags: + - statistics + summary: Get summarized alltime statistics + description: Get summarized alltime statistics + responses: + 200: + description: Ok + content: + application/json: + schema: + allOf: + - $ref: 'responses.yaml#/BasicSuccessResponse' + - properties: + data: + $ref: 'statistics-alltime-schema.yaml#/StatisticsAlltimeGetResponseSchema' + 401: + $ref: 'responses.yaml#/401ErrorResponse' diff --git a/src/storage/api-docs/statistics-alltime-schema.yaml b/src/storage/api-docs/statistics-alltime-schema.yaml new file mode 100644 index 0000000..992b2ad --- /dev/null +++ b/src/storage/api-docs/statistics-alltime-schema.yaml @@ -0,0 +1,45 @@ +StatisticsAlltimeGetResponseSchema: + allOf: + - type: object + - description: The data object of a single response entry + properties: + ActiveUsers: + type: integer + description: Amount of users who partizipated in transctiption or enrichment + example: 2549 + ItemsNotStarted: + type: integer + description: Amount of items whose Transctiption/Enrichment has not yet started + example: 31563 + ItemsEdited: + type: integer + description: Amount of items whose status »Edit« + example: 315642 + ItemsReviewed: + type: integer + description: Amount of items whose are reviewed + example: 1186 + ItemsCompleted: + type: integer + description: Amount of items whose are completed + example: 358 + ManualTranscriptions: + type: integer + description: Amount of manual transcribed chars + example: 2642433 + HTRTranscriptions: + type: integer + description: Amount of corrected chars in the HTR editor + example: 2642433 + Locations: + type: integer + description: Amount of geo-located locations + example: 36845 + Enrichments: + type: integer + description: Amount of done enrichments + example: 84433 + Descriptions: + type: integer + description: Amount of written descriptions + example: 8510