Skip to content
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

Update codes with new CC db structure #43

Merged
merged 23 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5294c14
Merge pull request #41 from TogetherCrew/development
cyri113 Nov 21, 2023
788b033
update: Code update with latest db schema!
amindadgar Nov 29, 2023
8ab6bfb
feat: Added guild id fetching for platform!
amindadgar Nov 29, 2023
cf67fcd
feat: Updating tests with new cc db structure!
amindadgar Nov 29, 2023
a9a1dc5
update: Fixed failing tests!
amindadgar Nov 29, 2023
1d80d04
fix: linter issue based on superlinter rules!
amindadgar Nov 29, 2023
1508967
fix: linter issues based on superlinter!
amindadgar Nov 29, 2023
8b4347c
fix: superlinter errors!
amindadgar Nov 30, 2023
6d02242
fix: linter issues!
amindadgar Nov 30, 2023
2aa5a1a
feat: Update action param type!
amindadgar Nov 30, 2023
5a5acc6
fix: linter issues based on superlinter!
amindadgar Nov 30, 2023
6a4496d
update: the saga data!
amindadgar Nov 30, 2023
54bde23
fix: superlinter issues!
amindadgar Nov 30, 2023
3e32ee5
feat: update platformId in sagas to bson.ObjectId!
amindadgar Nov 30, 2023
3e9e944
fix: update action param of tests!
amindadgar Nov 30, 2023
c1b00f7
fix: tests based on new action type!
amindadgar Nov 30, 2023
4408e2c
update: based on new window param type!
amindadgar Nov 30, 2023
8be9d99
fix: test errors!
amindadgar Nov 30, 2023
fdc9004
feat: Added community node creation!
amindadgar Nov 30, 2023
c85d05f
update: More assertion to the tests for community
amindadgar Nov 30, 2023
e40ce74
fix: Forgot to change back to subtracting!
amindadgar Nov 30, 2023
f30553d
fix: linter issues based on superlinter rules!
amindadgar Nov 30, 2023
02a664a
fix: linter flake8 whitespace error!
amindadgar Nov 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions analyzer_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class AnalyzerInit:
initialize the analyzer with its configs
"""

def __init__(self) -> None:
pass
def __init__(self, community_id: str) -> None:
self.community_id = community_id

def get_analyzer(self) -> tuple[RnDaoAnalyzer, dict[str, Any]]:
"""
Expand All @@ -23,7 +23,7 @@ def get_analyzer(self) -> tuple[RnDaoAnalyzer, dict[str, Any]]:
analyzer : RnDaoAnalyzer
mongo_creds : dict[str, Any]
"""
analyzer = RnDaoAnalyzer()
analyzer = RnDaoAnalyzer(self.community_id)

# credentials
mongo_creds = get_mongo_credentials()
Expand Down
12 changes: 10 additions & 2 deletions discord_analyzer/DB_operations/mongo_neo4j_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def set_mongo_db_ops(
self.mongoOps.set_mongo_db_access()

def store_analytics_data(
self, analytics_data, remove_memberactivities=False, remove_heatmaps=False
self,
analytics_data: dict,
community_id: str,
remove_memberactivities: bool = False,
remove_heatmaps: bool = False,
):
"""
store the analytics data into database
Expand All @@ -65,6 +69,8 @@ def store_analytics_data(
heatmaps is also a list of dictinoaries
and memberactivities is a tuple of memberactivities dictionary list
and memebractivities networkx object dictionary list
community_id : str
the community id to save the data for
remove_memberactivities : bool
remove the whole memberactivity data and insert
default is `False` which means don't delete the existing data
Expand Down Expand Up @@ -98,7 +104,9 @@ def store_analytics_data(
and memberactivities_networkx_data != []
):
queries_list = make_neo4j_networkx_query_dict(
networkx_graphs=memberactivities_networkx_data, guildId=guildId
networkx_graphs=memberactivities_networkx_data,
guildId=guildId,
community_id=community_id,
)
self.run_operations_transaction(
guildId=guildId,
Expand Down
62 changes: 3 additions & 59 deletions discord_analyzer/DB_operations/mongodb_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,12 @@ def __init__(self) -> None:
"""
pass

def _check_inputs(
self,
acc_names,
channels,
dates,
variable_aggregation_type="and",
value_aggregation_type="or",
):
"""
just check whether the inputs are correctly entered or not
"""
# checking the length of arrays
if len(acc_names) < 1:
raise ValueError("acc_names array is empty!")
if len(channels) < 1:
raise ValueError("channels array is empty!")
if len(dates) < 1:
raise ValueError("dates array is empty!")

# checking the variable aggregation_type variable
if variable_aggregation_type not in ["and", "or"]:
raise ValueError(
f"variable aggregation type must be either `and` or \
`or`!\nentered value is:{variable_aggregation_type}"
)

# checking the value aggregation_type variable
if value_aggregation_type not in ["and", "or"]:
raise ValueError(
f"value aggregation type must be either `and` or \
`or`!\nentered value is:{value_aggregation_type}"
)

def create_query_filter_account_channel_dates(
self,
acc_names,
channels,
dates,
variable_aggregation_type="and",
value_aggregation_type="or",
date_key="date",
channel_key="channelId",
account_key="account_name",
Expand Down Expand Up @@ -94,35 +60,13 @@ def create_query_filter_account_channel_dates(
query : dictionary
the query to get access
"""

# creating each part of query seperately

# creating date query
date_query = []
for date in dates:
date_query.append({date_key: {"$regex": date}})

# creating channels query
channel_query = []

for ch in channels:
channel_query.append({channel_key: ch})

# creating the account_name query
account_query = []

for account in acc_names:
account_query.append({account_key: account})

# creating the query
query = {
"$"
+ variable_aggregation_type: [
{"$" + value_aggregation_type: account_query},
{"$" + value_aggregation_type: channel_query},
# for time we should definitly use `or` because
# `and` would result in nothing!
{"$or": date_query},
{account_key: {"$in": acc_names}},
{channel_key: {"$in": channels}},
{date_key: {"$in": dates}},
]
}

Expand Down
91 changes: 77 additions & 14 deletions discord_analyzer/DB_operations/network_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import networkx


def make_neo4j_networkx_query_dict(networkx_graphs, guildId):
def make_neo4j_networkx_query_dict(
networkx_graphs: dict[datetime.datetime, networkx.classes.graph.Graph],
guildId: str,
community_id: str,
):
"""
make a list of queries to store networkx graphs into the neo4j

Expand All @@ -17,6 +21,8 @@ def make_neo4j_networkx_query_dict(networkx_graphs, guildId):
are the actual networkx graphs
guildId : str
the guild that the members belong to
community_id : str
the community id to save the data for

Returns:
-----------
Expand All @@ -34,6 +40,7 @@ def make_neo4j_networkx_query_dict(networkx_graphs, guildId):
networkx_graphs=graph_list,
networkx_dates=graph_dates,
guildId=guildId,
community_id=community_id,
toGuildRelation="IS_MEMBER",
)

Expand All @@ -44,6 +51,7 @@ def make_graph_list_query(
networkx_graphs: networkx.classes.graph.Graph,
networkx_dates: list[datetime.datetime],
guildId: str,
community_id: str,
toGuildRelation: str = "IS_MEMBER",
):
"""
Expand All @@ -59,6 +67,8 @@ def make_graph_list_query(
guildId : str
the guild that the members belong to
default is `None` meaning that it wouldn't be belonged to any guild
community_id : str
the community id to save the data for
toGuildRelation : str
the relationship label that connect the users to guilds
default value is `IS_MEMBER`
Expand All @@ -81,13 +91,46 @@ def make_graph_list_query(
guildId=guildId,
toGuildRelation=toGuildRelation,
)
community_query = create_community_node_query(community_id, guildId)

final_queries.extend(node_queries)
final_queries.extend(query_relations)
final_queries.append(community_query)

return final_queries


def create_community_node_query(
community_id: str,
guild_id: str,
community_node: str = "Community",
) -> str:
"""
create the community node

Parameters
------------
community_id : str
the community id to create its node
guild_id : str
the guild node to attach to community
"""
date_now_timestamp = get_timestamp()

query = f"""
MERGE (g:Guild {{guildId: '{guild_id}'}})
ON CREATE SET g.createdAt = {int(date_now_timestamp)}
WITH g
MERGE (c:{community_node} {{id: '{community_id}'}})
ON CREATE SET c.createdAt = {int(date_now_timestamp)}
WITH g, c
MERGE (g) -[r:IS_WITHIN]-> (c)
ON CREATE SET r.createdAt = {int(date_now_timestamp)}
"""

return query


def create_network_query(
nodes_dict: networkx.classes.reportviews.NodeDataView,
edge_dict: networkx.classes.reportviews.EdgeDataView,
Expand Down Expand Up @@ -127,19 +170,8 @@ def create_network_query(
the list of MERGE queries for creating all relationships
"""
# getting the timestamp `date`
graph_date_timestamp = (
graph_date.replace(
hour=0, minute=0, second=0, microsecond=0, tzinfo=datetime.timezone.utc
).timestamp()
* 1000
)
date_now_timestamp = (
datetime.datetime.now()
.replace(
hour=0, minute=0, second=0, microsecond=0, tzinfo=datetime.timezone.utc
)
.timestamp()
) * 1000
graph_date_timestamp = get_timestamp(graph_date)
date_now_timestamp = get_timestamp()

# initializiation of queries
rel_queries = []
Expand Down Expand Up @@ -208,3 +240,34 @@ def create_network_query(
rel_queries.append(rel_str_query + ";")

return node_queries, rel_queries


def get_timestamp(time: datetime.datetime | None = None) -> float:
"""
get the timestamp of the given time or just now

Parameters
------------
time : datetime.datetime
the time to get its timestamp
default is `None` meaning to send the time of now

Returns
--------
timestamp : float
the timestamp of the time multiplied to 1000
"""
using_time: datetime.datetime
if time is not None:
using_time = time
else:
using_time = datetime.datetime.now()

timestamp = (
using_time.replace(
hour=0, minute=0, second=0, microsecond=0, tzinfo=datetime.timezone.utc
).timestamp()
* 1000
)

return timestamp
11 changes: 6 additions & 5 deletions discord_analyzer/analysis/compute_interaction_matrix_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Author Ene SS Rawa / Tjitse van der Molen

from discord_analyzer.analysis.utils.activity import Activity
from discord_analyzer.DB_operations.mongodb_access import DB_access
from discord_analyzer.DB_operations.mongodb_query import MongodbQuery
from numpy import ndarray

Expand All @@ -16,10 +17,10 @@


def compute_interaction_matrix_discord(
acc_names,
dates,
channels,
db_access,
acc_names: list[str],
dates: list[str],
channels: list[str],
db_access: DB_access,
activities: list[str] = [Activity.Mention, Activity.Reply, Activity.Reaction],
) -> dict[str, ndarray]:
"""
Expand Down Expand Up @@ -63,7 +64,7 @@ def compute_interaction_matrix_discord(
query_dict = query.create_query_filter_account_channel_dates(
acc_names=acc_names,
channels=channels,
dates=dates,
dates=list(dates),
date_key="date",
channel_key="channelId",
account_key="account_name",
Expand Down
Loading