kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate UserSyncStatusViewSet to serializer-derived values pattern

Open rtibbles opened this issue 9 months ago • 2 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Migrate UserSyncStatusViewSet from explicit values/field_map to serializer-derived pattern as part of the BaseValuesViewset refactor (#14036).

Complexity: Medium Target branch: develop

Context

UserSyncStatusViewSet in kolibri/core/device/api.py has an explicit values tuple (9 fields) and a field_map = {"status": map_status}. It has no serializer_class or consolidate(). It has a substantial annotate_queryset() — 8 of the 9 fields are computed via subquery annotations (sync status, transfer status, device status, download tracking). The get_queryset() returns .none() on subset-of-users devices with no sync peers.

The Change

Create a serializer for UserSyncStatusViewSet that defines all read-path fields including the annotated fields. Remove the explicit values tuple and field_map. The map_status transformation should be handled by the serializer field's to_representation.

See #14036 for the overall pattern and FacilityUserViewSet in kolibri/core/auth/api.py for a concrete migration example.

Relocation

After the migration commits, extract the UserSyncStatus domain into kolibri/core/device/viewsets/user_sync_status.py as a pure-move commit: the viewset(s), their serializers (inline above the viewsets), and any domain-specific filters and permission classes (SyncStatusFilter, map_status). Update imports (api_urls.py, tests, cross-references).

Moving code breaks GitHub blame (local git blame -C -C still works), so digest the history the move obscures:

  1. Run blame over the code being moved; flag hunks whose purpose isn't evident from the code or tests (odd guards, magic orderings, special cases).
  2. Chase each flagged hunk to its originating PR/issue; where the rationale is non-obvious and load-bearing, carry it forward as a code comment citing the reference.
  3. Where the rationale turns out to be obsolete, remove the code as part of the migration commits rather than annotating it.

This is the last issue touching kolibri/core/device/api.py and serializers.py: also move the remaining non-migrating code into the package — DevicePermissionsViewSet, FreeSpaceView, DeviceInfoView, DeviceSettingsView, DeviceNameView, PluginsViewSet, DeviceRestartView, DriveInfoViewSet, PathPermissionView, InitializeAppView, CheckMeteredConnectionView, with the serializers.py contents (DevicePermissionsSerializer, NoFacilityFacilityUserSerializer, DeviceSerializerMixin, PathListField, DeviceSettingsSerializer) inline next to their consumers. Then delete api.py and serializers.py.

Out of Scope

  • Changes to the API response shape — the API contract must not change
  • Modifications to existing tests — if tests need to change, the migration is wrong
  • Write-path changes — only the read serialization path is affected
  • Changes to annotate_queryset() logic

Acceptance Criteria

  • [ ] Explicit values tuple and field_map removed from UserSyncStatusViewSet

  • [ ] Serializer has all read-path fields with correct handling of annotated fields

  • [ ] map_status transformation handled via serializer field

  • [ ] Existing API tests pass unchanged

  • [ ] Benchmark comparison passes (data hash match + no performance regression)

  • [ ] The migrated domain lives in kolibri/core/device/viewsets/user_sync_status.py, moved as a pure-move commit separate from import updates

  • [ ] Comments citing the originating PR/issue added where blame digestion found non-obvious, load-bearing rationale

  • [ ] Remaining non-migrating code moved; kolibri/core/device/api.py/serializers.py deleted

Testing

  • Before migrating: verify that existing test coverage is adequate. If the viewset has insufficient API tests, write them first to capture the current behavior. Tests must pass before and after migration without modification.
  • Benchmark: run the benchmark script before and after the migration using the same data:
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.device.api.UserSyncStatusViewSet \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.device.api.UserSyncStatusViewSet \
        --inherit-kolibri-home --compare baseline.json
    
  • The benchmark comparison must pass — this validates that the data output is identical and there is no performance regression.
  • Include the benchmark before/after comparison in the pull request description.

Benchmark data setup (run via kolibri manage shell before benchmarking):

from kolibri.core.auth.models import Facility, FacilityUser

# generateuserdata creates users; UserSyncStatus records are created
# automatically when users sync. For benchmarking, ensure the facility
# has users:
# kolibri manage generateuserdata

References

  • Tracking issue: #14036
  • Benchmark script: integration_testing/scripts/viewset_serialization_benchmark.py
  • Example migration: FacilityUserViewSet in kolibri/core/auth/api.py

AI usage

This issue was drafted with Claude Code assistance, reviewed and directed by @rtibbles.

  • After the move, the benchmark comparison run uses the new dotted path (e.g. kolibri.core.device.viewsets.user_sync_status.UserSyncStatusViewSet).

rtibbles avatar Mar 02 '26 04:03 rtibbles

Watching for resolution on behalf of learningequality/kolibri#14302.

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

I was unable to complete this task after multiple attempts. Manual review and intervention may be needed.

rtibblesbot avatar Jun 11 '26 04:06 rtibblesbot