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

Add API endpoint for stats #849

Merged
merged 2 commits into from
Feb 29, 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
14 changes: 14 additions & 0 deletions isic/stats/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.http.request import HttpRequest
from ninja import Router

from isic.stats.views import get_archive_stats

stats_router = Router()


@stats_router.get("/", response=dict, summary="Return ISIC Archive statistics.")
def stats(request: HttpRequest):
archive_stats = get_archive_stats()

del archive_stats["engagement"]["30_day_sessions_per_country"]
return archive_stats
2 changes: 1 addition & 1 deletion isic/stats/templates/stats/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<canvas id="canvas" width="1200" height="400"></canvas>
</div>

{{ sessions_last_30_days_per_country|json_script:"country_data" }}
{{ 30_day_sessions_per_country|json_script:"country_data" }}

<script type="text/javascript">
const country_values = JSON.parse(document.getElementById("country_data").textContent);
Expand Down
8 changes: 8 additions & 0 deletions isic/stats/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls.base import reverse
import pytest


@pytest.mark.django_db
def test_api_stats(client):
r = client.get(reverse("api:stats"))
assert r.status_code == 200
81 changes: 40 additions & 41 deletions isic/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,69 @@
from django.shortcuts import render

from isic.core.models import Collection, Image
from isic.ingest.models import Contributor, ZipUpload
from isic.ingest.models import ZipUpload
from isic.stats.models import GaMetrics
from isic.studies.models import Annotation, Markup, Response, Study
from isic.studies.models import Annotation, Study


def get_archive_stats():
latest_ga_metrics = GaMetrics.objects.last()

ctx = {
"annotations_count": Annotation.objects.count(),
"collections_count": Collection.objects.count(),
"contributors_count": Contributor.objects.count(),
"images_count": Image.objects.count(),
"markups_count": Markup.objects.count(),
"public_images_count": Image.objects.public().count(),
"public_studies_count": Study.objects.public().count(),
"responses_count": Response.objects.count(),
"studies_count": Study.objects.count(),
"annotated_images_count": Annotation.objects.values("image").distinct().count(),
# TODO: keep?
"uploaders_count": ZipUpload.objects.values("creator").distinct().count(),
"annotating_users_count": Annotation.objects.values("annotator").distinct().count(),
"total_users_count": User.objects.count(),
"image": {
"images_count": Image.objects.count(),
"public_images_count": Image.objects.public().count(),
"annotated_images_count": Annotation.objects.values("image").distinct().count(),
},
"collection": {
"collections_count": Collection.objects.count(),
"public_collections_count": Collection.objects.public().count(),
},
"study": {
"studies_count": Study.objects.count(),
"public_studies_count": Study.objects.public().count(),
"study_annotations_count": Annotation.objects.count(),
},
"user": {
"users_count": User.objects.count(),
"uploading_users_count": ZipUpload.objects.values("creator").distinct().count(),
"annotating_users_count": Annotation.objects.values("annotator").distinct().count(),
},
"engagement": {
"30_day_sessions_count": latest_ga_metrics.num_sessions if latest_ga_metrics else 0,
"30_day_sessions_per_country": (
latest_ga_metrics.sessions_per_country if latest_ga_metrics else []
),
},
}

if latest_ga_metrics:
ctx["sessions_last_30_days_count"] = latest_ga_metrics.num_sessions
ctx["sessions_last_30_days_per_country"] = latest_ga_metrics.sessions_per_country
else:
ctx["sessions_last_30_days_count"] = 0
ctx["sessions_last_30_days_per_country"] = []

return ctx


def stats(request):
archive_stats = get_archive_stats()
ctx = {
"sessions_last_30_days_per_country": archive_stats["sessions_last_30_days_per_country"],
"30_day_sessions_per_country": archive_stats["engagement"]["30_day_sessions_per_country"],
"stats": [
[
("Users", archive_stats["total_users_count"]),
("Sessions (Last 30 days)", archive_stats["sessions_last_30_days_count"]),
("Annotating Users", archive_stats["annotating_users_count"]),
],
[
("Contributing Institutions", archive_stats["contributors_count"]),
("", ""),
("Collections", archive_stats["collections_count"]),
("Users", archive_stats["user"]["users_count"]),
("Sessions (Last 30 days)", archive_stats["engagement"]["30_day_sessions_count"]),
("Collections", archive_stats["collection"]["collections_count"]),
],
[
("Images", archive_stats["images_count"]),
("Public Images", archive_stats["public_images_count"]),
("Annotated Images", archive_stats["annotated_images_count"]),
("Uploading Users", archive_stats["user"]["uploading_users_count"]),
("Public Collections", archive_stats["collection"]["public_collections_count"]),
("Annotating Users", archive_stats["user"]["annotating_users_count"]),
],
[
("Studies", archive_stats["studies_count"]),
("Public Studies", archive_stats["public_studies_count"]),
("", ""),
("Images", archive_stats["image"]["images_count"]),
("Public Images", archive_stats["image"]["public_images_count"]),
("Annotated Images", archive_stats["image"]["annotated_images_count"]),
],
[
("Annotations", archive_stats["annotations_count"]),
("Markups", archive_stats["markups_count"]),
("Responses", archive_stats["responses_count"]),
("Studies", archive_stats["study"]["studies_count"]),
("Public Studies", archive_stats["study"]["public_studies_count"]),
("Annotations", archive_stats["study"]["study_annotations_count"]),
],
],
}
Expand Down
2 changes: 2 additions & 0 deletions isic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
lesion_router,
metadata_file_router,
)
from isic.stats.api import stats_router
from isic.studies.api import annotation_router, study_router, study_task_router
from isic.zip_download.api import zip_router

Expand All @@ -46,6 +47,7 @@
api.add_router("/lesions/", lesion_router, tags=["lesions"])
api.add_router("/metadata-files/", metadata_file_router, tags=["metadata-files"])
api.add_router("/quickfind/", quickfind_router, tags=["quickfind"])
api.add_router("/stats/", stats_router, tags=["stats"])
api.add_router("/studies/", study_router, tags=["studies"])
api.add_router("/study-tasks/", study_task_router, tags=["study-tasks"])
api.add_router("/users/", user_router, tags=["users"])
Expand Down
Loading