Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce alltime statistics endpoint #108

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/app/Http/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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');
}
}
1 change: 1 addition & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@
Route::delete('/projects/{id}', [ProjectController::class, 'destroy']);

Route::get('/statistics', [StatisticsController::class, 'index']);
Route::get('/statistics/alltime', [StatisticsController::class, 'alltimeIndex']);
});
6 changes: 5 additions & 1 deletion src/storage/api-docs/api-docs.yaml
Original file line number Diff line number Diff line change
@@ -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/).<br />
For authorization you can use the the bearer token you are provided with.
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
18 changes: 18 additions & 0 deletions src/storage/api-docs/statistics-alltime-path.yaml
Original file line number Diff line number Diff line change
@@ -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'
45 changes: 45 additions & 0 deletions src/storage/api-docs/statistics-alltime-schema.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading