2.4 KiB
2.4 KiB
Legacy search segment construction
Thoughts on constructing a segment representation from legacy data in the Subscriber Search Service, referencing a WIP commit loading legacy segments from the database.
- The legacy segment stores field and operator information encoded in a search box ID, and a match value in a list view criteria field.
- Legacy segments only support a top-level AND grouping of criteria
Therefore, given a list of criteria on a segment, a Search can be formed as follows:
import dataclasses
from subscribersearch import legacy, search
@dataclasses.dataclass
class Segment:
"""A saved search on a list representing a segment of subscribers.
Likely belongs in the search module.
"""
id: typing.Optional[int]
name: str
search: search.Search
@staticmethod
def from_dict(value: dict) -> Search:
return Segment(
id=value.get("id"),
name=value["name"],
search=search.from_dict(value["search"]),
)
def to_dict(self) -> typing.Dict[str, typing.Any]:
return {
"id": self.id,
"name": self.name,
"search": self.search.to_dict(),
}
# segment = fetch legacy segment info
# rows = fetch legacy segment criteria
segment = Segment(
id=segment["id"],
name=segment["name"],
search=search.Search(
list_id=segment["list_id"],
group=search.Group(
group_type=search.GroupType.AND,
conditions=[
search.Condition(
field=filter.field,
operator=filter.operator,
match=match,
)
for filter, match in [
(legacy.filter_from_id(row["sb_id"]), row["lvc_criteria"]) in rows
]
],
),
),
)
By having functions that can retrieve a Segment object from the database and save one back to it, we have an interface that we can then copy later for storing and retrieving segments in an updated schema, and migrate between the two.