Skip to content

Commit

Permalink
fix: jira syncing issue for N3C datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
newgene committed Jun 26, 2023
1 parent 4ae09df commit a2280f5
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions discovery/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def adfy(item):
return doc

# override
def to_ADF(self): # noqa N802
def to_ADF(self): # noqa N802
"""
Attach dataset metadata details at the end of the message.
Use pyadf to build complex document structures. See documentation:
Expand Down Expand Up @@ -233,7 +233,6 @@ def __init__(self, settings):

@staticmethod
def get_portal_image(guide=""):

for portal, url in {
"outbreak": "https://discovery.biothings.io/static/img/outbreak.png",
"niaid": "https://discovery.biothings.io/static/img/niaid.png",
Expand All @@ -246,36 +245,43 @@ def get_portal_image(guide=""):
def add(self, _id, doc, user, **meta):
"""return an iterator of HTTPRequest instances during add event"""
for channel in self.channels:

if isinstance(channel, N3CChannel):
if meta.get("class_id") == "n3c::n3c:Dataset":
client = HTTPClient()
# check if the user has a registered account first
results = yield channel.sends_query(user)
# results = yield channel.sends_query(user)
results = client.fetch(channel.sends_query(user))
results = json.loads(results.body)

if results:
userid = results[0]["accountId"]
elif "@" in user:
# register this user by email if not registered yet
yield
userid = yield channel.sends_signup(user)
# yield
# userid = yield channel.sends_signup(user)
userid = client.fetch(channel.sends_signup(user))
userid = json.loads(userid.body)
userid = userid["accountId"]

else: # no email address, cannot register
userid = None
yield # this will return None for requests.send(response)
response = yield from channel.send(
DatasetMessage(
{
"title": "External Dataset Request", # customized title
"body": f'A new dataset "{doc.get("name")}" has been submitted by {user} on Data Discovery Engine.',
"url": f"http://discovery.biothings.io/dataset/{_id}",
"url_text": "View this dataset on Data Discovery Engine", # since details already included below
"reporter": userid, # registered user id basing on email
"doc": doc, # produce additional jira ticket content
}
)
# yield # this will return None for requests.send(response)
# response = yield from channel.send(
response = client.fetch(
list(
channel.send(
DatasetMessage(
{
"title": "External Dataset Request", # customized title
"body": f'A new dataset "{doc.get("name")}" has been submitted by {user} on Data Discovery Engine.',
"url": f"http://discovery.biothings.io/dataset/{_id}",
"url_text": "View this dataset on Data Discovery Engine", # since details already included below
"reporter": userid, # registered user id basing on email
"doc": doc, # produce additional jira ticket content
}
)
)
)[0]
)

if response.code == 201:
Expand All @@ -291,7 +297,7 @@ def add(self, _id, doc, user, **meta):
dataset.update(_n3c={"url": url})
except Exception as exc:
logging.error(str(exc))
yield # this will return None for requests.send(response)
# yield # this will return None for requests.send(response)
else: # all other channels
yield from channel.send(
DatasetMessage(
Expand All @@ -305,10 +311,9 @@ def add(self, _id, doc, user, **meta):
}
)
)
yield # this will return None for requests.send(response)
# yield # this will return None for requests.send(response)

def delete(self, _id, name, user):

message = DatasetMessage(
{
"title": "Dataset Metadata Deleted",
Expand All @@ -318,7 +323,6 @@ def delete(self, _id, name, user):
return self.broadcast(message)

def update(self, _id, name, version, user):

message = DatasetMessage(
{
"title": "Dataset Metadata Updated",
Expand Down Expand Up @@ -390,9 +394,11 @@ def update_n3c_status(_id):


def update_n3c_routine():
from discovery.model.dataset import Dataset

logger = logging.getLogger("update_n3c")
logger.info("Updating status for all N3C datasets...")
datasets = ESDataset.search().query("exists", field="_n3c.url")
datasets = Dataset.search().query("exists", field="_n3c.url")
datasets = datasets.source(False).scan()

_cnt = 0
Expand Down

0 comments on commit a2280f5

Please sign in to comment.