Skip to content

Commit

Permalink
WIP - add index
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Aug 14, 2024
1 parent d78783a commit 192134c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 3.2.25 on 2024-08-13 20:04
# Generated by Django 3.2.25 on 2024-08-14 11:11

import django.contrib.postgres.indexes
from django.db import migrations, models
import django.db.models.deletion

Expand All @@ -25,4 +26,8 @@ class Migration(migrations.Migration):
('environment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='environments.environment')),
],
),
migrations.AddIndex(
model_name='edgeidentitymeta',
index=django.contrib.postgres.indexes.GinIndex(fields=['searchable_traits'], name='edge_api_id_searcha_816c05_gin'),
),
]
13 changes: 9 additions & 4 deletions api/edge_api/identities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing
from contextlib import suppress

from django.contrib.postgres.indexes import GinIndex
from django.db import models
from django.db.models import Prefetch, Q
from flag_engine.features.models import FeatureStateModel
Expand Down Expand Up @@ -314,11 +315,15 @@ class EdgeIdentityMeta(models.Model):
_SEPARATOR = "<separator>"
_EQUALITY_CHARS = "<equals>"

class Meta:
indexes = [GinIndex(fields=["searchable_traits"])]

def update_searchable_traits(self, traits: typing.List[TraitModel]) -> None:
searchable_trait_keys = self.environment.trait_meta_items.filter(
is_searchable=True
).values_list("trait_key", flat=True)
# searchable_trait_keys = self.environment.trait_meta_items.filter(
# is_searchable=True
# ).values_list("trait_key", flat=True)
self.searchable_traits = self._SEPARATOR.join(
f"{trait.trait_key}{self._EQUALITY_CHARS}{trait.trait_value}"
for trait in filter(lambda t: t.trait_key in searchable_trait_keys, traits)
# for trait in filter(lambda t: t.trait_key in searchable_trait_keys, traits)
for trait in traits
)

0 comments on commit 192134c

Please sign in to comment.