Skip to content

Commit

Permalink
fix: updated data source selector modules db query!
Browse files Browse the repository at this point in the history
  • Loading branch information
amindadgar committed May 6, 2024
1 parent fe7fd5f commit cbd2380
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 96 deletions.
100 changes: 24 additions & 76 deletions tests/integration/test_data_source_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,29 @@ def setUp(self) -> None:
self.client = MongoSingleton.get_instance().get_client()
self.community_id = "6579c364f1120850414e0dc4"
self.client["Core"].drop_collection("modules")
self.client["Core"].drop_collection("platforms")

def test_no_community(self):
"""
test if no community selected hivemind modeules
"""
selector = DataSourceSelector()
data_sources = selector.select_data_source(community_id=self.community_id)
self.assertEqual(data_sources, {})
with self.assertRaises(ValueError):
selector.select_data_source(community_id=self.community_id)

def test_single_platform(self):
platform_id = "6579c364f1120850414e0da1"
self.client["Core"]["platforms"].insert_one(
{
"_id": ObjectId(platform_id),
"name": "discord",
"metadata": {
"name": "TEST",
"channels": ["1234", "4321"],
"roles": ["111", "222"],
},
"community": ObjectId(self.community_id),
"disconnectedAt": None,
"connectedAt": datetime(2023, 12, 1),
"createdAt": datetime(2023, 12, 1),
"updatedAt": datetime(2023, 12, 1),
}
)

self.client["Core"]["modules"].insert_one(
{
"name": "hivemind",
"communityId": ObjectId(self.community_id),
"community": ObjectId(self.community_id),
"options": {
"platforms": [
{
"platformId": ObjectId(platform_id),
"fromDate": datetime(2024, 1, 1),
"options": {},
"platform": ObjectId(platform_id),
"name": "discord",
"metadata": {
"fromDate": datetime(2024, 1, 1),
},
}
]
},
Expand All @@ -68,69 +52,33 @@ def test_multiple_platform(self):
platform_id1 = "6579c364f1120850414e0da1"
platform_id2 = "6579c364f1120850414e0da2"
platform_id3 = "6579c364f1120850414e0da3"
self.client["Core"]["platforms"].insert_many(
[
{
"_id": ObjectId(platform_id1),
"name": "discord",
"metadata": {
"name": "TEST",
"channels": ["1234", "4321"],
"roles": ["111", "222"],
},
"community": ObjectId(self.community_id),
"disconnectedAt": None,
"connectedAt": datetime(2023, 12, 1),
"createdAt": datetime(2023, 12, 1),
"updatedAt": datetime(2023, 12, 1),
},
{
"_id": ObjectId(platform_id2),
"name": "github",
"metadata": {
"organizationId": 12345,
},
"community": ObjectId(self.community_id),
"disconnectedAt": None,
"connectedAt": datetime(2023, 12, 1),
"createdAt": datetime(2023, 12, 1),
"updatedAt": datetime(2023, 12, 1),
},
{
"_id": ObjectId(platform_id3),
"name": "discourse",
"metadata": {
"some_id": 133445,
},
"community": ObjectId(self.community_id),
"disconnectedAt": None,
"connectedAt": datetime(2023, 12, 1),
"createdAt": datetime(2023, 12, 1),
"updatedAt": datetime(2023, 12, 1),
},
]
)

self.client["Core"]["modules"].insert_one(
{
"name": "hivemind",
"communityId": ObjectId(self.community_id),
"community": ObjectId(self.community_id),
"options": {
"platforms": [
{
"platformId": ObjectId(platform_id1),
"fromDate": datetime(2024, 1, 1),
"options": {},
"platform": ObjectId(platform_id1),
"name": "discord",
"metadata": {
"fromDate": datetime(2024, 1, 1),
},
},
{
"platformId": ObjectId(platform_id2),
"fromDate": datetime(2024, 1, 1),
"options": {},
"platform": ObjectId(platform_id2),
"name": "github",
"metadata": {
"fromDate": datetime(2024, 1, 1),
},
},
{
"platformId": ObjectId(platform_id3),
"fromDate": datetime(2024, 1, 1),
"options": {},
"platform": ObjectId(platform_id3),
"name": "discourse",
"metadata": {
"fromDate": datetime(2024, 1, 1),
},
},
]
},
Expand Down
32 changes: 12 additions & 20 deletions utils/data_source_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,25 @@ def select_data_source(self, community_id: str) -> dict[str, bool]:
for the given community
"""
db_results = self._query_modules_db(community_id)
platforms = list(map(lambda data: data["platform"]["name"], db_results))
platforms = list(map(lambda data: data["name"], db_results))
data_sources = dict.fromkeys(platforms, True)
return data_sources

def _query_modules_db(self, community_id: str) -> list[dict]:
client = MongoSingleton.get_instance().get_client()

pipeline = [
{"$match": {"name": "hivemind", "communityId": ObjectId(community_id)}},
{"$unwind": "$options.platforms"},
hivemind_module = client["Core"]["modules"].find_one(
{
"$lookup": {
"from": "platforms",
"localField": "options.platforms.platformId",
"foreignField": "_id",
"as": "platform",
}
"community": ObjectId(community_id),
"name": "hivemind",
},
{"$unwind": "$platform"},
{
"$project": {
"_id": 0,
"platform.name": 1,
}
"options.platforms.name": 1,
},
]
cursor = client["Core"]["modules"].aggregate(pipeline)
)
if hivemind_module is None:
raise ValueError(
f"No hivemind modules set for the given community id: {community_id}"
)
platforms = hivemind_module["options"]["platforms"]

data_sources = list(cursor)
return data_sources
return platforms

0 comments on commit cbd2380

Please sign in to comment.