:PROPERTIES: :ID: e6a2c650-ff59-4b72-b073-970731796888 :END: #+title: Legacy search segment construction Thoughts on constructing a segment representation from legacy data in the [[id:11edd6c9-b976-403b-a419-b5542ddedaae][Subscriber Search Service]], referencing a [[https://gitlab.aweber.io/CP/Services/subscriber-search/-/merge_requests/15/diffs?commit_id=4d203dfec800cf49825d32a7ea663c4b862693bb][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: #+begin_src python 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 ] ], ), ), ) #+end_src 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.