-
Notifications
You must be signed in to change notification settings - Fork 10
First pass at stats_api #440
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c58dd9d
First pass at stats_api
duckduckgrayduck 8df4b6a
Fix test
duckduckgrayduck 412c21b
Move upload stats from using a signal to explicit calls in document a…
duckduckgrayduck 7918eaf
Enrich detail view, remove leftover util add user/org stats to INSTAL…
duckduckgrayduck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
documentcloud/organizations/migrations/0023_organizationstats_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| # Generated by Django 5.2.15 on 2026-08-03 15:06 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("organizations", "0022_organization_created_at_organization_updated_at"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="OrganizationStats", | ||
| fields=[ | ||
| ( | ||
| "organization", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| primary_key=True, | ||
| related_name="stats", | ||
| serialize=False, | ||
| to=settings.SQUARELET_ORGANIZATION_MODEL, | ||
| ), | ||
| ), | ||
| ( | ||
| "last_upload_at", | ||
| models.DateTimeField(blank=True, db_index=True, null=True), | ||
| ), | ||
| ], | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="organization", | ||
| name="document_language", | ||
| field=models.CharField( | ||
| blank=True, | ||
| choices=[ | ||
| ("afr", "Afrikaans"), | ||
| ("amh", "Amharic"), | ||
| ("ara", "Arabic"), | ||
| ("asm", "Assamese"), | ||
| ("aze", "Azerbaijani"), | ||
| ("aze_cyrl", "Azerbaijani - Cyrillic"), | ||
| ("bel", "Belarusian"), | ||
| ("ben", "Bengali"), | ||
| ("bod", "Tibetan"), | ||
| ("bos", "Bosnian"), | ||
| ("bul", "Bulgarian"), | ||
| ("cat", "Catalan; Valencian"), | ||
| ("ceb", "Cebuano"), | ||
| ("ces", "Czech"), | ||
| ("zho", "chi_sim"), | ||
| ("tra", "chi_tra"), | ||
| ("chr", "Cherokee"), | ||
| ("cym", "Welsh"), | ||
| ("dan", "Danish"), | ||
| ("deu", "German"), | ||
| ("dzo", "Dzongkha"), | ||
| ("ell", "Greek"), | ||
| ("eng", "English"), | ||
| ("enm", "Middle English"), | ||
| ("epo", "Esperanto"), | ||
| ("est", "Estonian"), | ||
| ("eus", "Basque"), | ||
| ("fas", "Persian"), | ||
| ("fin", "Finnish"), | ||
| ("fra", "French"), | ||
| ("frk", "German Fraktur"), | ||
| ("frm", "Middle French"), | ||
| ("gle", "Irish"), | ||
| ("glg", "Galician"), | ||
| ("grc", "Ancient Greek"), | ||
| ("guj", "Gujarati"), | ||
| ("hat", "Haitian; Haitian Creole"), | ||
| ("heb", "Hebrew"), | ||
| ("hin", "Hindi"), | ||
| ("hrv", "Croatian"), | ||
| ("hun", "Hungarian"), | ||
| ("iku", "Inuktitut"), | ||
| ("ind", "Indonesian"), | ||
| ("isl", "Icelandic"), | ||
| ("ita", "Italian"), | ||
| ("ita_old", "Italian - Old"), | ||
| ("jav", "Javanese"), | ||
| ("jpn", "Japanese"), | ||
| ("kan", "Kannada"), | ||
| ("kat", "Georgian"), | ||
| ("kat_old", "Georgian - Old"), | ||
| ("kaz", "Kazakh"), | ||
| ("khm", "Central Khmer"), | ||
| ("kir", "Kirghiz; Kyrgyz"), | ||
| ("kor", "Korean"), | ||
| ("kur", "Kurdish"), | ||
| ("lao", "Lao"), | ||
| ("lat", "Latin"), | ||
| ("lav", "Latvian"), | ||
| ("lit", "Lithuanian"), | ||
| ("mal", "Malayalam"), | ||
| ("mar", "Marathi"), | ||
| ("mkd", "Macedonian"), | ||
| ("mlt", "Maltese"), | ||
| ("msa", "Malay"), | ||
| ("mya", "Burmese"), | ||
| ("nep", "Nepali"), | ||
| ("nld", "Dutch; Flemish"), | ||
| ("nor", "Norwegian"), | ||
| ("ori", "Oriya"), | ||
| ("pan", "Panjabi; Punjabi"), | ||
| ("pol", "Polish"), | ||
| ("por", "Portuguese"), | ||
| ("pus", "Pushto; Pashto"), | ||
| ("ron", "Romanian; Moldavian; Moldovan"), | ||
| ("rus", "Russian"), | ||
| ("san", "Sanskrit"), | ||
| ("sin", "Sinhala; Sinhalese"), | ||
| ("slk", "Slovak"), | ||
| ("slv", "Slovenian"), | ||
| ("spa", "Spanish; Castilian"), | ||
| ("spa_old", "Spanish; Castilian - Old"), | ||
| ("sqi", "Albanian"), | ||
| ("srp", "Serbian"), | ||
| ("srp_latn", "Serbian - Latin"), | ||
| ("swa", "Swahili"), | ||
| ("swe", "Swedish"), | ||
| ("syr", "Syriac"), | ||
| ("tam", "Tamil"), | ||
| ("tel", "Telugu"), | ||
| ("tgk", "Tajik"), | ||
| ("tgl", "Tagalog"), | ||
| ("tha", "Thai"), | ||
| ("tir", "Tigrinya"), | ||
| ("tur", "Turkish"), | ||
| ("uig", "Uighur; Uyghur"), | ||
| ("ukr", "Ukrainian"), | ||
| ("urd", "Urdu"), | ||
| ("uzb", "Uzbek"), | ||
| ("uzb_cyrl", "Uzbek - Cyrillic"), | ||
| ("vie", "Vietnamese"), | ||
| ("yid", "Yiddish"), | ||
| ], | ||
| default="eng", | ||
| help_text="The default document language for user's in this organization", | ||
| max_length=8, | ||
| verbose_name="document language", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="organization", | ||
| name="language", | ||
| field=models.CharField( | ||
| blank=True, | ||
| choices=[ | ||
| ("afr", "Afrikaans"), | ||
| ("amh", "Amharic"), | ||
| ("ara", "Arabic"), | ||
| ("asm", "Assamese"), | ||
| ("aze", "Azerbaijani"), | ||
| ("aze_cyrl", "Azerbaijani - Cyrillic"), | ||
| ("bel", "Belarusian"), | ||
| ("ben", "Bengali"), | ||
| ("bod", "Tibetan"), | ||
| ("bos", "Bosnian"), | ||
| ("bul", "Bulgarian"), | ||
| ("cat", "Catalan; Valencian"), | ||
| ("ceb", "Cebuano"), | ||
| ("ces", "Czech"), | ||
| ("zho", "chi_sim"), | ||
| ("tra", "chi_tra"), | ||
| ("chr", "Cherokee"), | ||
| ("cym", "Welsh"), | ||
| ("dan", "Danish"), | ||
| ("deu", "German"), | ||
| ("dzo", "Dzongkha"), | ||
| ("ell", "Greek"), | ||
| ("eng", "English"), | ||
| ("enm", "Middle English"), | ||
| ("epo", "Esperanto"), | ||
| ("est", "Estonian"), | ||
| ("eus", "Basque"), | ||
| ("fas", "Persian"), | ||
| ("fin", "Finnish"), | ||
| ("fra", "French"), | ||
| ("frk", "German Fraktur"), | ||
| ("frm", "Middle French"), | ||
| ("gle", "Irish"), | ||
| ("glg", "Galician"), | ||
| ("grc", "Ancient Greek"), | ||
| ("guj", "Gujarati"), | ||
| ("hat", "Haitian; Haitian Creole"), | ||
| ("heb", "Hebrew"), | ||
| ("hin", "Hindi"), | ||
| ("hrv", "Croatian"), | ||
| ("hun", "Hungarian"), | ||
| ("iku", "Inuktitut"), | ||
| ("ind", "Indonesian"), | ||
| ("isl", "Icelandic"), | ||
| ("ita", "Italian"), | ||
| ("ita_old", "Italian - Old"), | ||
| ("jav", "Javanese"), | ||
| ("jpn", "Japanese"), | ||
| ("kan", "Kannada"), | ||
| ("kat", "Georgian"), | ||
| ("kat_old", "Georgian - Old"), | ||
| ("kaz", "Kazakh"), | ||
| ("khm", "Central Khmer"), | ||
| ("kir", "Kirghiz; Kyrgyz"), | ||
| ("kor", "Korean"), | ||
| ("kur", "Kurdish"), | ||
| ("lao", "Lao"), | ||
| ("lat", "Latin"), | ||
| ("lav", "Latvian"), | ||
| ("lit", "Lithuanian"), | ||
| ("mal", "Malayalam"), | ||
| ("mar", "Marathi"), | ||
| ("mkd", "Macedonian"), | ||
| ("mlt", "Maltese"), | ||
| ("msa", "Malay"), | ||
| ("mya", "Burmese"), | ||
| ("nep", "Nepali"), | ||
| ("nld", "Dutch; Flemish"), | ||
| ("nor", "Norwegian"), | ||
| ("ori", "Oriya"), | ||
| ("pan", "Panjabi; Punjabi"), | ||
| ("pol", "Polish"), | ||
| ("por", "Portuguese"), | ||
| ("pus", "Pushto; Pashto"), | ||
| ("ron", "Romanian; Moldavian; Moldovan"), | ||
| ("rus", "Russian"), | ||
| ("san", "Sanskrit"), | ||
| ("sin", "Sinhala; Sinhalese"), | ||
| ("slk", "Slovak"), | ||
| ("slv", "Slovenian"), | ||
| ("spa", "Spanish; Castilian"), | ||
| ("spa_old", "Spanish; Castilian - Old"), | ||
| ("sqi", "Albanian"), | ||
| ("srp", "Serbian"), | ||
| ("srp_latn", "Serbian - Latin"), | ||
| ("swa", "Swahili"), | ||
| ("swe", "Swedish"), | ||
| ("syr", "Syriac"), | ||
| ("tam", "Tamil"), | ||
| ("tel", "Telugu"), | ||
| ("tgk", "Tajik"), | ||
| ("tgl", "Tagalog"), | ||
| ("tha", "Thai"), | ||
| ("tir", "Tigrinya"), | ||
| ("tur", "Turkish"), | ||
| ("uig", "Uighur; Uyghur"), | ||
| ("ukr", "Ukrainian"), | ||
| ("urd", "Urdu"), | ||
| ("uzb", "Uzbek"), | ||
| ("uzb_cyrl", "Uzbek - Cyrillic"), | ||
| ("vie", "Vietnamese"), | ||
| ("yid", "Yiddish"), | ||
| ], | ||
| default="eng", | ||
| help_text="The default interface language for user's in this organization", | ||
| max_length=8, | ||
| verbose_name="language", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Django | ||
| from django.db import models | ||
|
|
||
|
|
||
| class OrganizationStats(models.Model): | ||
| organization = models.OneToOneField( | ||
| "organizations.Organization", | ||
| on_delete=models.CASCADE, | ||
| related_name="stats", | ||
| primary_key=True, | ||
| ) | ||
| last_upload_at = models.DateTimeField(null=True, blank=True, db_index=True) | ||
|
|
||
| class Meta: | ||
| app_label = "organizations" | ||
|
|
||
| def __str__(self): | ||
| return f"Stats for organization {self.organization_id}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Django | ||
| from django.utils import timezone | ||
| from rest_framework import serializers | ||
|
|
||
| # DocumentCloud | ||
| from documentcloud.organizations.stats_api.models import OrganizationStats | ||
|
|
||
|
|
||
| class OrganizationStatsSerializer(serializers.ModelSerializer): | ||
| uuid = serializers.UUIDField(source="organization.uuid", read_only=True) | ||
| total_documents = serializers.IntegerField(read_only=True) | ||
| days_since_last_upload = serializers.SerializerMethodField() | ||
| recent_upload_count = serializers.SerializerMethodField( | ||
| help_text="Documents uploaded by the org within the configured window " | ||
| "(UPLOAD_WINDOW_DAYS, defaults to 90)." | ||
| ) | ||
|
|
||
| class Meta: | ||
| model = OrganizationStats | ||
| fields = [ | ||
| "uuid", | ||
| "total_documents", | ||
| "last_upload_at", | ||
| "days_since_last_upload", | ||
| "recent_upload_count", | ||
| ] | ||
| read_only_fields = fields | ||
|
|
||
| def get_days_since_last_upload(self, obj): | ||
| if obj.last_upload_at is None: | ||
| return None | ||
| return (timezone.now() - obj.last_upload_at).days | ||
|
|
||
| def get_recent_upload_count(self, obj): | ||
| return getattr(obj, "recent_upload_count", None) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.