Skip to content

Replace live/batch Rating calculation with denormalized RatingSum/RatingCount counters on School #519 - #512

Merged
sanaderi merged 8 commits into
GamaEdtech:stagingfrom
farsica:fariborz-stg
Jul 24, 2026
Merged

Replace live/batch Rating calculation with denormalized RatingSum/RatingCount counters on School #519#512
sanaderi merged 8 commits into
GamaEdtech:stagingfrom
farsica:fariborz-stg

Conversation

@farsica

@farsica farsica commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Pull Request Template

Description

  1. undo and refactor school Rate/Rank decoupling, and Score to RankScore rename Docs system, school Rate/Rank decoupling, and Score to RankScore rename #500
  2. Replace live/batch Rating calculation with denormalized RatingSum/RatingCount counters on School Replace live/batch Rating calculation with denormalized RatingSum/RatingCount counters on School #519

Fixes #(issue number)

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Other (please describe):

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings/errors
  • I have added tests that prove my fix is effective or that my feature works

Additional Notes

Any other information or context here.

@github-actions

Copy link
Copy Markdown

🤖 AI Generated Review

This review was generated automatically and does not replace human review.

Review chunk 1/4

Summary

The pull request primarily focuses on undoing the previous decoupling of school Rating/Rank and renaming the "RankScore" to "Score" across various parts of the codebase. It updates data transfer objects (DTOs), domain entities, specifications, and also adjusts migration scripts to reflect this change.

Potential Issues

  1. Query Performance: Commented lines related to calculating average ratings (t.SchoolComments.Average) should be considered for removal if not needed, as their presence might lead to future confusion.
  2. Data Consistency: Ensure that renaming in the migration file does not result in lost data or incorrect mappings during the database migration.

Suggestions

  1. Review of Commented Code: Evaluate if entirely removing the commented sections related to "Rating" calculations is more appropriate than commenting them out, reducing dead code.
  2. Testing Migration: Run thorough database tests to ensure RankScore to Score renaming does not affect existing data and functionality, specifically focusing on rollback procedures.

Positive Feedback

  1. Comprehensive Updates: The changes reflect a comprehensive update across service layer, DTOs, domain entities, and migration files, ensuring uniformity of the renamed attribute throughout the system.
  2. Migration Inclusion: Including migration files demonstrates attention to syncing schema changes with the database state.

Review chunk 2/4

Summary

This pull request reverts changes related to the decoupling of school Rate/Rank fields and renames a property from Score to RankScore. The changes primarily involve updating the Entity Framework model configurations for entities like ApplicationUser, School, and others in the domain model.

Potential Issues

  1. Security Concern: Make sure the sample data added for the ApplicationUsers table, such as the PasswordHash, are not real production credentials to prevent security breaches.
  2. Data Integrity: Removing or renaming columns may have cascading effects on the application. Ensure that the application logic and database interactions are correctly handling these changes.

Suggestions

  1. Documentation: Update any related documentation to reflect these changes, especially if APIs or public access layers were affected by the Score to RankScore modification.
  2. Validation: Perform a test migration and rollback scenario to verify data consistency and to handle any potential runtime exceptions related to schema changes.

Positive Feedback

  • The use of IsUnique() and specific data types for properties ensures database integrity and optimizes query performance.
  • The HasDatabaseName() method used for indexes can improve clarity and maintainability regarding how indexes are named and referenced in database scripts.

Review chunk 3/4

Summary

This pull request primarily focuses on undoing the decoupling of school Rate/Rank and renaming Score to RankScore. It involves changes to multiple entity configurations and database mappings within the GamaEdtech application.

Potential Issues

  1. Identity Column Usage: The UseIdentityColumn method is being used consistently across entities. Ensure that this aligns with your migration strategy and data flow since it can lead to issues if IDs are not managed as expected in distributed environments.

  2. Nullable Properties: There are several properties such as Nullable last modification dates and users. Ensure proper handling of null values when retrieving these properties to prevent runtime exceptions.

  3. String Length Limits: Check that the HasMaxLength constraints are sufficient for your application’s needs, as underestimated limits could lead to data truncation.

  4. Precision for Decimal Types: Precision for Price and other numeric fields have been set to 36, 18. Validate these precision scales against business requirements to avoid precision loss.

Suggestions

  1. Index Review: The unique indexes placed on properties like UserId, TestId, and FeatureId should be reviewed to ensure that they reflect the necessary uniqueness constraints without impacting performance adversely.

  2. Security: Review the access to table entities, especially those involving user data (UserSubscriptions, Transactions) to ensure data security and compliance standards are met, possibly by implementing additional security measures or access controls.

  3. Performance Optimization: If this schema is associated with high-volume transactional data, consider reviewing and optimizing indexing strategy to ensure query performance is not degraded over time.

Positive Feedback

  1. Consistent Entity Configuration: The pull request maintains a consistent approach to entity configuration, ensuring clarity and uniformity in the application’s data layer.

  2. Proper Navigation Properties: Many-to-one relationships are set up with clear navigation properties, facilitating easier access to related data and maintaining referential integrity.

  3. JSON Storage for Complex Types: The use of ToJson for IdentityPasskeyData and QuestionOptions allows for efficient storage of complex types within a single column, enhancing flexibility without requiring additional tables.

These changes are well-organized and adhere to best practices for using Entity Framework Core in terms of setting up relationships and database mappings.

Review chunk 4/4

Summary

This pull request addresses several changes: it undoes the decoupling of school Rate/Rank, renames the RankScore to Score, and applies related adjustments across various files, including database migrations, model snapshot updates, controller logic, and view models.

Potential Issues

  1. Migration Rollback Risk: The migration script only renames the column and index, which should perform as expected. However, ensure that the database contains data compatible with the name change, or data retrieval might be disrupted. Ensure that existing systems expecting RankScore are fully updated at deployment.

  2. Data Inconsistencies: The renaming could lead to potential mismatches with data integrations or third-party tools expecting RankScore as a field.

  3. Specifications Naming: The specifications classes are renamed corresponding to the field changes. Ensure that these class changes align with all references in the codebase to avoid name resolution issues.

Suggestions

  1. Comprehensive Testing: Conduct end-to-end testing, including testing with real data, to confirm there are no hidden data retrieval or processing issues due to the column renaming or specifications changes.

  2. Communication Strategy: If RankScore is referenced externally (e.g., documentation, API consumers), ensure thorough communication with external stakeholders regarding these changes.

  3. Review Naming Consistency: Ensure all instances and documentation, both hardcoded and dynamic, now reference Score rather than RankScore to maintain consistency.

Positive Feedback

  1. Systematic and Consistent Changes: The pull request shows a carefully managed alteration across the codebase, maintaining domain-driven naming conventions.

  2. Database Handling: The migration and snapshot changes appear correct and adhere to best practices for database schema evolution.

@farsica farsica changed the title undo school Rate/Rank decoupling, and Score to RankScore rename #500 undo and refactor school Rate/Rank decoupling, and Score to RankScore rename #500 Jul 17, 2026
@github-actions

Copy link
Copy Markdown

🤖 AI Generated Review

This review was generated automatically and does not replace human review.

Review chunk 1/4

Summary

The pull request updates the existing school rate/rank feature to undo a previous decoupling of rate and rank, and renames "Score" to "RankScore". It involves changes across multiple files, including several database migrations and modifications to service classes, DTOs, and controllers to reflect these changes.

Potential Issues

  1. Data Migration Risks: The database migration renames the RankScore column to Rating. Care should be taken to ensure that data transformation through this migration preserves data integrity.

  2. Query Performance: Some queries, like those involving score and rank calculations, depend on aggregations and may have performance implications, especially for large datasets. This might necessitate a review to ensure that indexes support the new calculations effectively.

  3. Null Handling: Previous logic checking SchoolComments.Any() to conditionally calculate ratings has been removed. Depending on database state, this might lead to null reference errors if not correctly handled elsewhere.

Suggestions

  1. Testing Database Migrations: Ensure extensive testing around the newly added migration scripts to confirm they migrate existing data properly and that any rollback functionalities are effective.

  2. Performance Testing: Consider performance testing the revised SQL queries focusing on large datasets. If performance degradation is observed, indexing or query restructuring might be necessary.

  3. Validate Null Handling: Verify that all instances where ratings are used assume that they might be null, especially where aggregations like AVERAGE are used without initial checks done by SchoolComments.Any().

Positive Feedback

  1. Consistency in Naming: The renaming of RankScore to Rating across various layers introduces consistent terminology, which improves maintainability and readability.

  2. Targeted and Comprehensive Changes: The changes are well-focused on achieving the goal of renaming and feature decoupling, which means the PR sticks to its intended purpose without introducing unrelated modifications.

  3. Migration Scripting: The migration scripts are thorough, addressing both the schema changes and adjustments in related logic, providing a robust approach to the database update process.

Review chunk 2/4

Summary

The pull request includes the addition of multiple entity configurations for the GamaEdtech domain in a model builder setup. The changes related to entity creation are primarily focused on identity and associated domain entities like School, Location, Payment, and more. These entities seem to add functionality around user identity, school ranking, payments, and comments.

Potential Issues

  1. Security Concerns: Ensure that sensitive data fields (e.g., passwords, tokens) are not inadvertently logged or exposed. Current changes don't show encryption methods for sensitive data.
  2. Data Integrity: Since many entities are interconnected, make sure foreign key constraints are effectively maintained during migrations.
  3. Unique Indexes: Several indices are set as unique. Verify that the application logic ensures data consistency and unique constraint violations are handled gracefully.

Suggestions

  1. Data Validation: Implement data validation at the entity level, using annotations or a validation framework, to preemptively catch issues before they hit the database.
  2. Index Performance: Consider carefully if all indices, especially unique ones, are necessary. Indices can impact write performance, so ensure they are justified by query patterns.
  3. Nullable Fields: Review nullable fields and verify if nullable properties are necessary, using HasColumnType() and setting IsRequired() appropriately for improved data integrity.

Positive Feedback

  • The schema design is well-structured and makes use of appropriate data types and configurations like precision on numeric fields and IsRequired for mandatory fields.
  • The use of "HasMaxLength" and data types such as nvarchar and varchar for string fields optimizes storage and performance.
  • Unique constraints and indexes suggest a focus on maintaining efficient data retrieval and ensuring data integrity.

Review chunk 3/4

Summary

This pull request involves modifications related to the database schema mapping in Entity Framework. New entities and associations are defined for various domain models, including SubscriptionPlanFeatures, SubscriptionPlanGatewayMapping, SubscriptionPlanPrice, Tag, TestSubmission, among others. Additionally, there are references to historical logs and audit entities. This likely extends database capabilities for tracking and managing various subscriptions, user interactions, and content within the GamaEdtech platform.

Potential Issues

  1. Data Integrity: There are several associations with OnDelete(DeleteBehavior.NoAction). Ensure this behavior aligns with expected data lifecycle management, as it might lead to orphaned records.
  2. Index Management: The uniqueness of indices on multiple columns (e.g., SubscriptionPlanPriceId, Gateway) requires careful monitoring. Changes to these fields are costly operations.

Suggestions

  1. Constraints Validation: Confirm that all entities with IsRequired properties enforce these constraints at the database level to prevent potential NULL value insertion.
  2. Currency and Precision: The decimal properties enforce high precision for financial transactions. Ensure that client-side logic or services interacting with these values can handle high precision decimals without errors.
  3. String Column Types: Consistently use nvarchar over varchar for better support of internationalization unless storage size is a critical constraint.

Positive Feedback

  1. Use of Multi-column Indexes: Use of composite keys and multi-field indexes demonstrates thoughtfulness toward query performance and data entry integrity.
  2. Entity Relations: Proper usage of foreign key constraints improves data integrity and relationships among entities.
  3. Precision and Data Types: Correct use of HasPrecision for decimal types across entities ensures precise financial calculations, important in transactional contexts.

Review chunk 4/4

Summary

The pull request involves significant changes related to the renaming of RankScore to Rating within the Schools entity in a database context, along with some navigational configuration updates for various entities. It includes a migration script for renaming and updating the relevant indexes, as well as updates to related API controllers and view models to reflect the renaming.

Potential Issues

  1. Foreign Key Constraints:
    The changes include setting and updating foreign key constraints. There is a mix of DeleteBehavior.Cascade and DeleteBehavior.NoAction. It's important to ensure that all indicated relationships align with the intended data lifecycle to avoid unintentional deletions or constraint conflicts.

  2. Controller Logic:
    In the Api/Controllers/SchoolsController.cs, there's a commented out Rating assignment, which might lead to inconsistency if Rating is expected to be part of the serialized response in all cases.

  3. Backward Compatibility:
    The renaming of RankScore to Rating might introduce issues if any consumers of this data are expecting the old field names. Ensure that all integrations are aware of and prepared for this change.

Suggestions

  1. Audit Foreign Key Changes:
    Verify that all foreign key changes, especially those affecting DeleteBehavior, align with the intended design and do not result in unexpected behaviors.

  2. Code Consistency:
    Ensure that Rating is consistently included in controller responses across all relevant controllers to maintain uniform API behavior.

  3. Documentation Updates:
    Consider updating any related documentation to reflect the renaming from RankScore to Rating to assist developers and maintainers in understanding the changes.

Positive Feedback

  1. Migration Handling:
    The migration script effectively handles both Up and Down migrations, ensuring a clean rollback path if needed.

  2. Entity Configuration:
    The entity configuration is clear, with well-defined navigation properties that enhance ORM readability and maintainability.

@gamadev1 gamadev1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: #512 — requesting changes

Thanks for looking into Rating's performance — that's a legitimate concern (the live .Average() in GetSchoolsListAsync does get evaluated for every matching row before pagination, since it's part of the same projection that OrderBy/Skip/Take run on top of).

But this PR does more than optimize Rating — it merges Rating back into RankScore (renaming the column, reusing it for both the public rating and the internal ranking formula). That reverses the split made on 2026-07-10 (docs/business/school-scoring-analysis.md) for a real reason: Rating is meant to be "what reviewers actually said" (0–5), while RankScore is a 0–150 blend of reviews and profile completeness (website, coordinates, contact info, photos) used only for ordering. Combining them means a school with zero reviews but a filled-out profile would show a nonzero "rating" it didn't earn from anyone.

It also has a concrete bug as written: UpdateSchoolScoreAsync's final UPDATE sets s.Rating = rc.Rating, but rc never produces a Rating column (only CommentRating and Score exist in the CTE) — this throws Invalid column name 'Rating' every time the ranking job runs, which will break CountryRank/StateRank/CityRank recalculation entirely once merged.

Could we split this into two separate concerns?

  1. Keep Rating and RankScore as separate, independent values — don't rename/merge the column.
  2. Solve the performance problem on its own terms: e.g. add a persisted, indexed Rating column that's just the plain review average, updated incrementally whenever a SchoolComment is added/edited/deleted (not via the periodic RankScore batch job) — that gets you the O(1) read you're after without reintroducing staleness or conflating it with ranking.

Happy to pair on the incremental-update approach if useful. Would rather not merge the Rating/RankScore recombination as-is — can you confirm with whoever's driving #500 whether that merge is actually the intended fix, or if it was really just the performance angle that mattered?

@farsica farsica changed the title undo and refactor school Rate/Rank decoupling, and Score to RankScore rename #500 Replace live/batch Rating calculation with denormalized RatingSum/RatingCount counters on School #519 Jul 22, 2026
@farsica
farsica requested a review from sanaderi July 23, 2026 10:42
@sanaderi
sanaderi merged commit 393bf7c into GamaEdtech:staging Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants