Skip to content
Closed
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
28 changes: 28 additions & 0 deletions dandiapi/api/migrations/0031_applicationstats_asset_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.2.7 on 2026-03-25 14:12
from __future__ import annotations

from django.db import migrations, models


def populate_asset_count(apps, schema_editor):
Asset = apps.get_model('api.Asset')
ApplicationStats = apps.get_model('api.ApplicationStats')

asset_count = Asset.objects.filter(versions__isnull=False).distinct().count()

ApplicationStats.objects.update(asset_count=asset_count)


class Migration(migrations.Migration):
dependencies = [
('api', '0030_alter_asset_path'),
]

operations = [
migrations.AddField(
model_name='applicationstats',
name='asset_count',
field=models.PositiveBigIntegerField(default=0),
),
migrations.RunPython(code=populate_asset_count, reverse_code=migrations.RunPython.noop),
]
4 changes: 4 additions & 0 deletions dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,7 @@ def total_size(cls):
or 0
for cls in (AssetBlob, ZarrArchive)
)

@classmethod
def total_count(cls):
return cls.objects.filter(versions__isnull=False).distinct().count()
1 change: 1 addition & 0 deletions dandiapi/api/models/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApplicationStats(models.Model): # noqa: DJ008
published_dandiset_count = models.PositiveIntegerField()
user_count = models.PositiveIntegerField()
size = models.PositiveBigIntegerField()
asset_count = models.PositiveBigIntegerField(default=0)

class Meta:
verbose_name_plural = 'Application Stats'
Expand Down
1 change: 1 addition & 0 deletions dandiapi/api/tasks/scheduled.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def compute_application_stats() -> None:
published_dandiset_count=Dandiset.published_count(),
user_count=User.objects.filter(metadata__status=UserMetadata.Status.APPROVED).count(),
size=Asset.total_size(),
asset_count=Asset.total_count(),
)


Expand Down
15 changes: 15 additions & 0 deletions dandiapi/api/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_stats_baseline(api_client):
'published_dandiset_count': 0,
'user_count': 0,
'size': 0,
'asset_count': 0,
}


Expand Down Expand Up @@ -77,6 +78,20 @@ def test_stats_asset(api_client, version, asset):
stats = api_client.get('/api/stats/').data

assert stats['size'] == asset.size
assert stats['asset_count'] == 1


@pytest.mark.django_db
def test_stats_asset_count(api_client, version, asset_factory):
"""Test that asset_count reflects the number of distinct assets in any version."""
assets = [asset_factory() for _ in range(3)]
for a in assets:
version.assets.add(a)

compute_application_stats()

stats = api_client.get('/api/stats/').data
assert stats['asset_count'] == 3


@pytest.mark.django_db
Expand Down
7 changes: 5 additions & 2 deletions web/src/views/HomeView/StatsBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
>
<v-col
class="py-0 flex-grow-1"
md="4"
sm="4"
md="3"
sm="3"
cols="12"
>
<SingleStat
Expand All @@ -35,6 +35,7 @@ import SingleStat from '@/views/HomeView/SingleStat.vue';
const dandisets = ref(0);
const users = ref(0);
const size = ref(0);
const files = ref(0);

const stats = computed(() => [
{
Expand All @@ -43,6 +44,7 @@ const stats = computed(() => [
description: 'A DANDI dataset including files and dataset-level metadata',
href: '/dandiset',
},
{ name: 'files', value: files.value },
{ name: 'users', value: users.value },
{ name: 'total data size', value: filesize(size.value, { round: 0, base: 10, standard: 'iec' }) },
]);
Expand All @@ -52,5 +54,6 @@ dandiRest.stats().then((data) => {
dandisets.value = data.dandiset_count;
users.value = data.user_count;
size.value = data.size;
files.value = data.asset_count;
});
</script>