-
Notifications
You must be signed in to change notification settings - Fork 265
Extract content urls #4604
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
Merged
akolson
merged 21 commits into
learningequality:search-recommendations
from
akolson:embed-content-logic
Aug 12, 2024
Merged
Extract content urls #4604
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a93c0c9
add docstring to embed methods
akolson df86440
updates embed_content to use methods as building blocks
akolson b8fdb18
Implements extract content urls
akolson 8c10f77
Runs recommendations cache migrations
akolson 63de9d9
clean up and tests
akolson 61e8b62
Remove extra migrations
akolson b920691
Adds new migration
akolson 8c23f5f
implements feedback
akolson 9dbbc5c
Use values to query dictionary
akolson c7c598a
Improves cache implementation
akolson a8cca1e
Adjusts docstring description
akolson 2ebd5ee
Adds unique constraint to cache model
akolson b7af67f
Adds indexes for optimized data querying
akolson 50fc1d8
Adds migrations
akolson 0def99e
Writes queries to get main_tree_id
akolson 374da3f
Reruns migrations
akolson d70e978
adds more tests
akolson bd5c018
Fixes failing tests
akolson fa5253b
adds more tests
akolson cb2f47c
cte implementation initial commit
akolson 902875e
Implements CTE to get recommendations
akolson 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Generated by Django 3.2.24 on 2024-08-05 21:23 | ||
| import uuid | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations | ||
| from django.db import models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ('kolibri_public', '0003_alter_file_preset'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='RecommendationsCache', | ||
| fields=[ | ||
| ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, | ||
| serialize=False)), | ||
| ('request_hash', models.CharField(max_length=32, null=True)), | ||
| ('rank', models.FloatField(default=0.0, null=True)), | ||
| ('override_threshold', models.BooleanField(default=False)), | ||
| ('timestamp', models.DateTimeField(auto_now_add=True)), | ||
| ('contentnode', models.ForeignKey(blank=True, null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name='recommendations', | ||
| to='kolibri_public.contentnode')), | ||
| ], | ||
| ), | ||
| migrations.AddIndex( | ||
| model_name='recommendationscache', | ||
| index=models.Index(fields=['request_hash'], name='request_hash_idx'), | ||
| ), | ||
| migrations.AddIndex( | ||
| model_name='recommendationscache', | ||
| index=models.Index(fields=['contentnode'], name='contentnode_idx'), | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name='recommendationscache', | ||
| unique_together={('request_hash', 'contentnode')}, | ||
| ), | ||
| ] | ||
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 |
|---|---|---|
| @@ -1,3 +1,30 @@ | ||
| # from django.db import models | ||
| import uuid | ||
|
|
||
| # Create your models here. | ||
| from django.db import models | ||
| from kolibri_public.models import ContentNode | ||
|
|
||
|
|
||
| REQUEST_HASH_INDEX_NAME = "request_hash_idx" | ||
| CONTENTNODE_INDEX_NAME = "contentnode_idx" | ||
|
|
||
|
|
||
| class RecommendationsCache(models.Model): | ||
| id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
| request_hash = models.CharField(max_length=32, null=True) | ||
| contentnode = models.ForeignKey( | ||
| ContentNode, | ||
| null=True, | ||
| blank=True, | ||
| related_name='recommendations', | ||
| on_delete=models.CASCADE, | ||
| ) | ||
| rank = models.FloatField(default=0.0, null=True) | ||
| override_threshold = models.BooleanField(default=False) | ||
| timestamp = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| class Meta: | ||
| unique_together = ('request_hash', 'contentnode') | ||
| indexes = [ | ||
| models.Index(fields=['request_hash'], name=REQUEST_HASH_INDEX_NAME), | ||
| models.Index(fields=['contentnode'], name=CONTENTNODE_INDEX_NAME), | ||
| ] |
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
64 changes: 64 additions & 0 deletions
64
contentcuration/automation/tests/test_recommendations_cache_model.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,64 @@ | ||
| import uuid | ||
|
|
||
| from automation.models import RecommendationsCache | ||
| from django.db import IntegrityError | ||
| from kolibri_public.models import ContentNode | ||
|
|
||
| from contentcuration.tests.base import StudioTestCase | ||
|
|
||
|
|
||
| class TestRecommendationsCache(StudioTestCase): | ||
|
|
||
| def setUp(self): | ||
| self.content_node = ContentNode.objects.create( | ||
| id=uuid.uuid4(), | ||
| title='Test Content Node', | ||
| content_id=uuid.uuid4(), | ||
| channel_id=uuid.uuid4(), | ||
| ) | ||
| self.cache = RecommendationsCache.objects.create( | ||
| request_hash='test_hash', | ||
| contentnode=self.content_node, | ||
| rank=1.0, | ||
| override_threshold=False | ||
| ) | ||
|
|
||
| def test_cache_creation(self): | ||
| self.assertIsInstance(self.cache, RecommendationsCache) | ||
| self.assertEqual(self.cache.request_hash, 'test_hash') | ||
| self.assertEqual(self.cache.contentnode, self.content_node) | ||
| self.assertEqual(self.cache.rank, 1.0) | ||
| self.assertFalse(self.cache.override_threshold) | ||
|
|
||
| def test_cache_retrieval(self): | ||
| retrieved_cache = RecommendationsCache.objects.get(request_hash='test_hash') | ||
| self.assertEqual(retrieved_cache, self.cache) | ||
|
|
||
| def test_cache_uniqueness(self): | ||
| with self.assertRaises(IntegrityError): | ||
| RecommendationsCache.objects.create( | ||
| request_hash='test_hash', | ||
| contentnode=self.content_node, | ||
| rank=2.0, | ||
| override_threshold=True | ||
| ) | ||
|
|
||
| def test_bulk_create_ignore_conflicts_true(self): | ||
| initial_count = RecommendationsCache.objects.count() | ||
| try: | ||
| RecommendationsCache.objects.bulk_create( | ||
| [self.cache, self.cache], | ||
| ignore_conflicts=True | ||
| ) | ||
| except IntegrityError: | ||
| self.fail("bulk_create raised IntegrityError unexpectedly!") | ||
|
|
||
| final_count = RecommendationsCache.objects.count() | ||
| self.assertEqual(initial_count, final_count) | ||
|
|
||
| def test_bulk_create_ignore_conflicts_false(self): | ||
| with self.assertRaises(IntegrityError): | ||
| RecommendationsCache.objects.bulk_create( | ||
| [self.cache, self.cache], | ||
| ignore_conflicts=False | ||
| ) |
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
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.
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.