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

Return list of synapse ids instead of dataframe #32

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__
*.egg-info
dist
build
.vscode
69 changes: 45 additions & 24 deletions synapsemonitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,53 @@ def _render_fileview(
return viewdf


def _find_modified_entities_fileview(
syn: Synapse, syn_id: str, days: int = 1
) -> pd.DataFrame:
"""Performs query to find modified entities in id and render columns
These modified entities include newly uploaded ones
def _find_modified_entities_fileview(syn: Synapse, syn_id: str, days: int = 1) -> list:
"""Finds entities scoped in a fileview modified in the past N number of days
Args:
syn: Synapse connection
view_id: Synapse View Id
epochtime: Epoch time in milliseconds
syn_id: Synapse Fileview Id
days: N number of days
Returns:
Dataframe with updated entities
List of synapse ids
"""
# Update the view
# _force_update_view(syn, view_id)
query = (
"select id, name, currentVersion, modifiedOn, modifiedBy, "
f"createdOn, projectId, type from {syn_id} where "
f"select id from {syn_id} where "
f"modifiedOn > unix_timestamp(NOW() - INTERVAL {days} DAY)*1000"
)
results = syn.tableQuery(query)
resultsdf = results.asDataFrame()
return _render_fileview(syn, viewdf=resultsdf)
return resultsdf["id"].tolist()


def _find_modified_entities_file(
syn: Synapse, syn_id: str, days: int = 1
) -> pd.DataFrame:
def _find_modified_entities_file(syn: Synapse, syn_id: str, days: int = 1) -> list:
"""Determines if entity was modified in the past N number of days
Args:
syn: Synapse connection
syn_id: Synapse File Id
days: N number of days
Returns:
List of synapse ids
"""
raise NotImplementedError("Files not supported yet")


def _find_modified_entities_container(
syn: Synapse, syn_id: str, days: int = 1
) -> pd.DataFrame:
def _find_modified_entities_container(syn: Synapse, syn_id: str, days: int = 1) -> list:
"""Finds entities in a folder or project modified in the past N number of days
Args:
syn: Synapse connection
syn_id: Synapse Folder or Project Id
days: N number of days
Returns:
List of synapse ids
"""
raise NotImplementedError("Projects and folders not supported yet")


Expand Down Expand Up @@ -135,8 +147,17 @@ def _get_user_ids(syn: Synapse, users: list = None):
return user_ids


def find_modified_entities(syn: Synapse, syn_id: str, days: int) -> pd.DataFrame:
"""Find modified entities based on the type of the input"""
def find_modified_entities(syn: Synapse, syn_id: str, days: int) -> list:
"""Find modified entities based on the type of the input
Args:
syn: Synapse connection
syn_id: Synapse Entity Id
days: N number of days
Returns:
List of synapse ids
"""
entity = syn.get(syn_id, downloadFile=False)
if isinstance(entity, synapseclient.EntityViewSchema):
return _find_modified_entities_fileview(syn=syn, syn_id=syn_id, days=days)
Expand Down Expand Up @@ -170,21 +191,21 @@ def monitoring(
Dataframe with files modified within last N days
"""
# get dataframe of files
filesdf = find_modified_entities(syn=syn, syn_id=syn_id, days=days)
modified_entities = find_modified_entities(syn=syn, syn_id=syn_id, days=days)
# Filter out projects and folders
print(f"Total number of entities = {len(filesdf.index)}")
print(f"Total number of entities = {len(modified_entities)}")
thomasyu888 marked this conversation as resolved.
Show resolved Hide resolved

# get user ids
user_ids = _get_user_ids(syn, users)

# TODO: Add function to beautify email message

# Prepare and send Message
if not filesdf.empty:
if modified_entities:
syn.sendMessage(
user_ids,
email_subject,
filesdf.to_html(index=False),
", ".join(modified_entities),
contentType="text/html",
)
return filesdf
return modified_entities
thomasyu888 marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 12 additions & 13 deletions test/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,21 @@ def test__find_modified_entities_fileview(self):
with patch.object(self.syn, "tableQuery",
return_value=self.table_query_results) as patch_q,\
patch.object(self.table_query_results, "asDataFrame",
return_value=self.query_resultsdf) as patch_asdf,\
patch.object(monitor, "_render_fileview",
return_value=self.expecteddf) as patch_render:
resultdf = monitor._find_modified_entities_fileview(
return_value=self.query_resultsdf) as patch_asdf:
# patch.object(monitor, "_render_fileview",
# return_value=self.expecteddf) as patch_render:
modified_list = monitor._find_modified_entities_fileview(
self.syn, "syn44444", days=2
)
patch_q.assert_called_once_with(
"select id, name, currentVersion, modifiedOn, modifiedBy, "
"createdOn, projectId, type from syn44444 where "
"select id from syn44444 where "
"modifiedOn > unix_timestamp(NOW() - INTERVAL 2 DAY)*1000"
)
patch_asdf.assert_called_once_with()
assert resultdf.equals(self.expecteddf)
patch_render.assert_called_once_with(
self.syn, viewdf=self.query_resultsdf
)
assert modified_list == ["syn23333"]
# patch_render.assert_called_once_with(
# self.syn, viewdf=self.query_resultsdf
# )


def test__get_user_ids_none():
Expand Down Expand Up @@ -148,9 +147,9 @@ def setup_method(self):

def test_monitoring_fail_integration(self):
"""Test all monitoring functions are called"""
returndf = pd.DataFrame({"test": ["foo"]})
modified_list = ["syn2222", "syn33333"]
with patch.object(monitor, "find_modified_entities",
return_value=returndf) as patch_find,\
return_value=modified_list) as patch_find,\
patch.object(monitor, "_get_user_ids",
return_value=[111]) as patch_get_user,\
patch.object(self.syn, "sendMessage") as patch_send:
Expand All @@ -159,5 +158,5 @@ def test_monitoring_fail_integration(self):
patch_find.assert_called_once_with(syn=self.syn, syn_id="syn12345", days=15)
patch_get_user.assert_called_once_with(self.syn, ["2222", "fooo"])
patch_send.assert_called_once_with([111], "new subject",
returndf.to_html(index=False),
"syn2222, syn33333",
contentType='text/html')