Skip to content

Fix rating aggregation: use video_ratings counter table instead of writing to videos #20

@pmcfadin

Description

@pmcfadin

Problem

_update_video_aggregate_rating() in app/services/rating_service.py (lines 31-71) tries to $set three camelCase columns on the videos table that don't exist in the Cassandra schema:

  • averageRating
  • totalRatingsCount
  • updatedAt

This produces an UNKNOWN_TABLE_COLUMNS error on every rating submission:

WARNING:astrapy.utils.api_commander:APICommander about to raise from: 
[{'errorCode': 'UNKNOWN_TABLE_COLUMNS', 'message': 'The table killrvideo.videos defines the columns: 
videoid, added_date, ... views, youtube_id. The update included the following unknown columns: 
"averageRating", "totalRatingsCount", "updatedAt".'}]

The error is silently caught (lines 64-71), so ratings return 200 — but aggregated values are never persisted, and get_video_ratings_summary() always returns averageRating=None, totalRatingsCount=0.

Root Cause

The schema has a dedicated counter table for this purpose that isn't being used:

CREATE TABLE IF NOT EXISTS killrvideo.video_ratings (
    videoid uuid PRIMARY KEY,
    rating_counter counter,
    rating_total counter
);

Required Changes

Write path (_update_video_aggregate_rating)

Replace the $set on videos with counter operations on video_ratings:

  • On new rating: increment rating_counter by 1, increment rating_total by the rating value
  • On updated rating: increment rating_total by (new_rating - old_rating) (counter stays the same)
  • Note: Counters use $inc, not $set

Read path (get_video_ratings_summary)

Instead of reading averageRating/totalRatingsCount from the Video model (which are always None/0):

  1. Query the video_ratings table for the video's rating_counter and rating_total
  2. Compute average = rating_total / rating_counter on the fly
  3. Return the computed values

Files to modify

  • app/services/rating_service.py — rewrite _update_video_aggregate_rating() and get_video_ratings_summary()
  • app/models/video.py — consider removing averageRating, totalRatingsCount, updatedAt fields (or keeping them as API-only computed fields)
  • tests/services/test_rating_service.py — update tests for the new approach

Acceptance criteria

  • Rating submission uses video_ratings counter table (not videos)
  • GET /videos/{id}/ratings returns correct averageRating and totalRatingsCount
  • No more UNKNOWN_TABLE_COLUMNS warnings in logs
  • Existing tests pass, new tests cover counter logic

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions