Skip to content

Commit

Permalink
Merge pull request #32 from Sage-Bionetworks/gh-30-return-list
Browse files Browse the repository at this point in the history
Return list of synapse ids instead of dataframe
  • Loading branch information
thomasyu888 authored Jan 20, 2022
2 parents 3ae5794 + adf0a94 commit 448fe67
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
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)}")

# 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
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')

0 comments on commit 448fe67

Please sign in to comment.