-
Notifications
You must be signed in to change notification settings - Fork 20
KeyFactors v3.1 #3564
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
hlbmtc
wants to merge
33
commits into
main
Choose a base branch
from
feat/3558-key-factors-cleanup-votes
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
KeyFactors v3.1 #3564
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
bdc8cd5
KeyFactors votes migration
hlbmtc 2f95278
Votes strength generation
hlbmtc 982bccd
Added no votes log
hlbmtc 446b86f
Added extra comments
hlbmtc be7cef3
Small fix
hlbmtc a2a5dd5
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc f13189b
Refactored KeyFactor vote serialization and calculations
hlbmtc 97dbf12
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 558b59b
Fixed conflicts
hlbmtc 85026ec
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc fda1edd
Fixed ImpactDirection type
hlbmtc e25bc08
Small fix
hlbmtc 46f4cf8
Fixed migration and adjusted votes_unique_user_key_factor constraint
hlbmtc 17e1d38
Adjusted strength formula
hlbmtc cf7275e
Added Driver.certainty
hlbmtc 7c26829
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc f0a4927
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 940314e
PR review changes
hlbmtc 6392669
Added extra logging
hlbmtc 0740667
Small fix
hlbmtc 2074e02
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 5e709bc
Key Factors: new backend endpoints (#3595)
hlbmtc 9a8e064
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 4a005e9
Small fix
hlbmtc 98ffa98
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 0c9fbf6
Fixed Driver creation validation
hlbmtc db3a77b
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc 52be0f9
Small fix
hlbmtc 00c6fb7
Key Factors V3 Frontend (#3626)
hlbmtc 8601488
Simplified `KeyFactorDraft`
hlbmtc 924a3ed
Adjust ai suggested key factors (#3669)
hlbmtc 0a476db
Merge branch 'main' into feat/3558-key-factors-cleanup-votes
hlbmtc dc18c83
Key Factors that are migrated from the old system display empty stren…
hlbmtc 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
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
143 changes: 143 additions & 0 deletions
143
comments/migrations/0020_keyfactors_votes_migration_and_cleanup.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,143 @@ | ||
| # Generated by Django 5.1.10 on 2025-10-02 19:31 | ||
| import logging | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def calculate_votes_strength(scores: list[int]): | ||
| """ | ||
| Calculates overall strengths of the KeyFactor | ||
| """ | ||
|
|
||
| return (sum(scores) + 2 * max(0, 3 - len(scores))) / max(3, len(scores)) | ||
|
|
||
|
|
||
| def migrate_strength_vote_score(score: int): | ||
| # Converting (-5, -3, -2, 0, 2, 3, 5) scale to (0, 1, 2, 5) | ||
| score = abs(score) | ||
|
|
||
| return { | ||
| 0: 0, # No | ||
| 2: 1, # Low | ||
| 3: 2, # Medium | ||
| 5: 5, # High | ||
| }[score] | ||
|
|
||
|
|
||
| def votes_migration(apps, schema_editor): | ||
| KeyFactorVote = apps.get_model("comments", "KeyFactorVote") | ||
| KeyFactor = apps.get_model("comments", "KeyFactor") | ||
| KeyFactorDriver = apps.get_model("comments", "KeyFactorDriver") | ||
|
|
||
| # Drop unused a_updown votes | ||
| KeyFactorVote.objects.filter(vote_type="a_updown").delete() | ||
|
|
||
| # There could be cases when users might have multiple vote types against one KeyFactor | ||
| # So deleting other duplicates | ||
| votes_qs = KeyFactorVote.objects.order_by( | ||
| "key_factor_id", "user_id", "-created_at" | ||
| ).distinct("key_factor_id", "user_id") | ||
|
|
||
| to_delete = KeyFactorVote.objects.exclude(pk__in=votes_qs) | ||
| logger.info(f"Deleting {to_delete.count()} duplicated KeyFactor votes") | ||
|
|
||
| to_delete.delete() | ||
|
|
||
| # Migrate other votes | ||
| key_factors = list(KeyFactor.objects.prefetch_related("votes").all()) | ||
| update_votes = [] | ||
| update_drivers = [] | ||
|
|
||
| for kf in key_factors: | ||
| votes = kf.votes.all() | ||
|
|
||
| if not votes: | ||
| logger.info(f"KeyFactor {kf.id} has no votes") | ||
| continue | ||
|
|
||
| direction = sum([v.score for v in votes]) | ||
|
|
||
| if direction == 0: | ||
| logger.info(f"KeyFactor {kf.id} has direction = 0") | ||
| else: | ||
| # Update driver direction | ||
| kf.driver.impact_direction = 1 if direction > 0 else 0 | ||
| update_drivers.append(kf.driver) | ||
|
|
||
| for vote in votes: | ||
| # Update vote type | ||
| vote.vote_type = "strength" | ||
|
|
||
| if ( | ||
| # TODO: double-check 0 case | ||
| direction == 0 | ||
| or (direction > 0 and vote.score < 0) | ||
| or (direction < 0 and vote.score > 0) | ||
| ): | ||
| # votes that disagree with the new direction get strength 0 | ||
| vote.score = 0 | ||
| else: | ||
| # votes that agree keep their abs strength, | ||
| # but are converted to the (0, 1, 2, 5) scale | ||
| vote.score = migrate_strength_vote_score(vote.score) | ||
|
|
||
| update_votes.append(vote) | ||
|
|
||
| # Calculate strength | ||
| kf.votes_score = ( | ||
| calculate_votes_strength([v.score for v in votes]) if votes else 0 | ||
| ) | ||
|
|
||
| logger.info(f"Updating {len(update_votes)} votes") | ||
| KeyFactorVote.objects.bulk_update(update_votes, ["score", "vote_type"]) | ||
| logger.info(f"Updating {len(update_drivers)} drivers") | ||
| KeyFactorDriver.objects.bulk_update(update_drivers, ["impact_direction"]) | ||
| KeyFactor.objects.bulk_update(key_factors, ["votes_score"]) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("comments", "0019_keyfactors_refactor"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="keyfactor", | ||
| name="votes_score", | ||
| field=models.FloatField(db_index=True, default=0, editable=False), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="keyfactordriver", | ||
| name="impact_direction", | ||
| field=models.SmallIntegerField( | ||
| blank=True, choices=[(1, "Increase"), (-1, "Decrease")], null=True | ||
| ), | ||
| ), | ||
| migrations.RunPython(votes_migration, reverse_code=migrations.RunPython.noop), | ||
| migrations.RemoveConstraint( | ||
| model_name="keyfactorvote", | ||
| name="votes_unique_user_key_factor", | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="keyfactorvote", | ||
| constraint=models.UniqueConstraint( | ||
| fields=("user_id", "key_factor_id"), name="votes_unique_user_key_factor" | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="keyfactorvote", | ||
| name="vote_type", | ||
| field=models.CharField( | ||
| choices=[("strength", "Strength"), ("direction", "Direction")], | ||
| default="direction", | ||
| max_length=20, | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="keyfactordriver", | ||
| name="certainty", | ||
| field=models.FloatField(blank=True, null=True), | ||
| ), | ||
| ] |
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
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.