-
-
Notifications
You must be signed in to change notification settings - Fork 203
feat(api): add /api/v2/user-reports/
endpoint for superusers DEV-232 DEV-899
#6243
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
Open
RuthTurk
wants to merge
55
commits into
main
Choose a base branch
from
dev-899-optimize-queries-for-subscriptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
1dd21ae
inital draft of using a materialized view to query user report data
RuthTurk e48582b
clean up materalized view migration and add SKIP_HEAVY_MIGRATION, add…
RuthTurk a163c57
Draft implementation of optimized usage data calculation for user report
rajpatel24 f9a90b3
Optimization for the cross database values
rajpatel24 4f6f0f8
add documentation for /user-reports endpoint
RuthTurk c688f58
Refactor billing and usage snapshot table
rajpatel24 9780930
Improvements
rajpatel24 9649670
Merge refactored endpoint logic into original implementation
rajpatel24 9304d87
add 'account_restricted' field
RuthTurk 20896e5
Refactor snapshot and celery task logic
rajpatel24 061f595
Add comments
rajpatel24 dbd88e6
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk 1227002
fix migration dependency errors
RuthTurk 33f3bde
only run 0070 if Stripe is enabled and add error messages to the endp…
RuthTurk 4d87545
Cleanup for filters
rajpatel24 38e1965
add tests
RuthTurk 75e5a66
fix spelling
RuthTurk 757b22f
skip tests if stripe is not enabled
RuthTurk 3f421d1
fix tests and add openapi schema generated by management command
RuthTurk a54b789
remove newline at end of schema_v2.json
RuthTurk bf3bc31
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk 817905d
clean up and organize code
RuthTurk 9be8ae7
fix darker issues
RuthTurk 8d7441b
move everything for this endpoint into a new django app called user_r…
RuthTurk e33b454
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk 859243e
remove newline
RuthTurk bc57655
Update logic to retry failed Celery job from where it left off
rajpatel24 881712b
Fix linter issues
rajpatel24 71334ff
Minor fixes
rajpatel24 e698dde
Refactor user report snapshot task with resumable runs and add migrat…
rajpatel24 be0906b
Fix redis locks and simplify codes
noliveleger ccb0664
Refactor models and migrations to follow conventions and remove redun…
rajpatel24 0d3418b
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk c58e1b8
update audit logs tag to server logs and fix error message with corre…
RuthTurk a556695
Remove filtering logic from the branch
rajpatel24 e867928
fix darker and orval issues
RuthTurk 7f87d61
rename materialized view 'user_reports_mv' to 'user_reports_userrepor…
RuthTurk fa632e4
don't include anonymous users in reports, create utility function to …
RuthTurk ffb30d7
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk 6485dc3
fix tests
RuthTurk 1232d5e
Update materialized view field names for consistency
rajpatel24 6e4fd44
Minor fixes
rajpatel24 aa84ad4
Fix linter issues
rajpatel24 b8bd62d
Add organization balance calculations to celery task and materialized…
rajpatel24 57212ef
Make QueryParse work with integer,float in JSONBfield
noliveleger 76a38a1
Merge branch 'dev-899-optimize-queries-for-subscriptions' of github.c…
noliveleger f7cdc9a
Add unit test for process_value of QueryParser
noliveleger 214b7ad
Rename 'organizations' to 'organization' and simplified field names
rajpatel24 a851aac
Fix failing tests and refactoring
rajpatel24 5713681
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk a8342bc
fix tests and add require_stripe to refresh_user_report_snapshots
RuthTurk 10ba98b
Merge branch 'main' of github.com:kobotoolbox/kpi into dev-899-optimi…
RuthTurk 1034ed2
use f string instead of replace for filtering out anonymous users
RuthTurk eed9c41
Remove redundant fields from the materialized view
rajpatel24 67d4d96
Refactor user reports logic to ensure compatibility with stripe-disab…
rajpatel24 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
File renamed without changes.
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
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UserReportsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'kobo.apps.user_reports' |
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,212 @@ | ||
from django.db import migrations, models | ||
from django.db.models import Q | ||
|
||
import kpi.models.abstract_models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='BillingAndUsageSnapshotRun', | ||
fields=[ | ||
( | ||
'id', | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name='ID' | ||
) | ||
), | ||
('uid', kpi.fields.kpi_uid.KpiUidField(_null=False, uid_prefix='busr')), | ||
( | ||
'status', | ||
models.CharField( | ||
choices=[ | ||
('in_progress', 'In Progress'), | ||
('completed', 'Completed'), | ||
('aborted', 'Aborted') | ||
], | ||
default='in_progress', | ||
max_length=32 | ||
) | ||
), | ||
('last_processed_org_id', models.CharField(blank=True, null=True)), | ||
('details', models.JSONField(blank=True, null=True)), | ||
('singleton', models.BooleanField(default=True, editable=False)), | ||
( | ||
'date_created', | ||
models.DateTimeField( | ||
default=kpi.models.abstract_models._get_default_datetime | ||
), | ||
), | ||
( | ||
'date_modified', | ||
models.DateTimeField( | ||
default=kpi.models.abstract_models._get_default_datetime | ||
), | ||
), | ||
], | ||
options={ | ||
'ordering': ['-date_created'], | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='BillingAndUsageSnapshot', | ||
fields=[ | ||
( | ||
'id', | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name='ID' | ||
) | ||
), | ||
( | ||
'effective_user_id', | ||
models.IntegerField(blank=True, null=True, db_index=True) | ||
), | ||
( | ||
'last_snapshot_run', | ||
models.ForeignKey( | ||
on_delete=models.deletion.CASCADE, | ||
related_name='snapshots', | ||
to='user_reports.billingandusagesnapshotrun', | ||
), | ||
), | ||
( | ||
'organization', | ||
models.OneToOneField( | ||
on_delete=models.deletion.CASCADE, | ||
to='organizations.organization', | ||
), | ||
), | ||
('total_storage_bytes', models.BigIntegerField(default=0)), | ||
('total_submission_count_all_time', models.BigIntegerField(default=0)), | ||
( | ||
'total_submission_count_current_period', | ||
models.BigIntegerField(default=0) | ||
), | ||
('billing_period_start', models.DateTimeField(blank=True, null=True)), | ||
('billing_period_end', models.DateTimeField(blank=True, null=True)), | ||
( | ||
'date_created', | ||
models.DateTimeField( | ||
default=kpi.models.abstract_models._get_default_datetime | ||
), | ||
), | ||
( | ||
'date_modified', | ||
models.DateTimeField( | ||
default=kpi.models.abstract_models._get_default_datetime | ||
), | ||
), | ||
('submission_limit', models.BigIntegerField(blank=True, null=True)), | ||
('storage_bytes_limit', models.BigIntegerField(blank=True, null=True)), | ||
('asr_seconds_limit', models.BigIntegerField(blank=True, null=True)), | ||
('mt_characters_limit', models.BigIntegerField(blank=True, null=True)), | ||
], | ||
), | ||
migrations.AddIndex( | ||
model_name='billingandusagesnapshot', | ||
index=models.Index(fields=['effective_user_id'], name='idx_bau_user'), | ||
), | ||
migrations.AddIndex( | ||
model_name='billingandusagesnapshot', | ||
index=models.Index(fields=['date_created'], name='idx_bau_created'), | ||
), | ||
# Add index for runs | ||
migrations.AddIndex( | ||
model_name='billingandusagesnapshotrun', | ||
index=models.Index( | ||
fields=['status', 'date_modified'], | ||
name='idx_bau_run_status_expires' | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name='billingandusagesnapshotrun', | ||
constraint=models.UniqueConstraint( | ||
fields=('singleton',), | ||
condition=Q(status='in_progress'), | ||
name='uniq_run_in_progress' | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name='billingandusagesnapshot', | ||
constraint=models.UniqueConstraint( | ||
fields=('organization',), | ||
name='uniq_snapshot_per_org' | ||
), | ||
), | ||
# Register the materialized-view model state (unmanaged) so Django knows | ||
# the model exists for ORM queries, but doesn't try to create a migration | ||
# file for it. The actual materialized view is created by the subsequent | ||
# migration `0002_create_user_reports_mv.py`. | ||
migrations.SeparateDatabaseAndState( | ||
database_operations=[], | ||
state_operations=[ | ||
migrations.CreateModel( | ||
name='UserReports', | ||
fields=[ | ||
( | ||
'id', | ||
models.BigIntegerField(primary_key=True, serialize=False) | ||
), | ||
( | ||
'extra_details_uid', | ||
models.CharField(max_length=255, null=True, blank=True) | ||
), | ||
('username', models.CharField(max_length=150)), | ||
('first_name', models.CharField(max_length=150)), | ||
('last_name', models.CharField(max_length=150)), | ||
('email', models.CharField(max_length=254)), | ||
('is_superuser', models.BooleanField()), | ||
('is_staff', models.BooleanField()), | ||
('is_active', models.BooleanField()), | ||
('date_joined', models.CharField(max_length=64)), | ||
( | ||
'last_login', | ||
models.CharField(max_length=64, null=True, blank=True) | ||
), | ||
('validated_email', models.BooleanField()), | ||
('validated_password', models.BooleanField()), | ||
('mfa_is_active', models.BooleanField()), | ||
('sso_is_active', models.BooleanField()), | ||
('accepted_tos', models.BooleanField()), | ||
('social_accounts', models.JSONField(null=True, blank=True)), | ||
('organizations', models.JSONField(null=True, blank=True)), | ||
('metadata', models.JSONField(null=True, blank=True)), | ||
('subscriptions', models.JSONField(null=True, blank=True)), | ||
('asset_count', models.IntegerField(default=0)), | ||
('deployed_asset_count', models.IntegerField(default=0)), | ||
( | ||
'current_period_start', | ||
models.DateTimeField(null=True, blank=True) | ||
), | ||
( | ||
'current_period_end', | ||
models.DateTimeField(null=True, blank=True) | ||
), | ||
( | ||
'organization_id', | ||
models.IntegerField(null=True, blank=True) | ||
), | ||
( | ||
'service_usage', | ||
models.JSONField(null=True, blank=True), | ||
), | ||
], | ||
options={ | ||
'managed': False, | ||
'db_table': 'user_reports_userreportsmv', | ||
}, | ||
), | ||
], | ||
), | ||
] |
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.