Skip to content

Commit

Permalink
Adds method that returns narrative in search doc format
Browse files Browse the repository at this point in the history
  • Loading branch information
charleshtrenholm committed Oct 7, 2021
1 parent c41e7d2 commit 377c43b
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 3 deletions.
83 changes: 83 additions & 0 deletions NarrativeService.spec
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,87 @@ module NarrativeService {
If all this goes off well, the new narrative UPA is returned.
*/
funcdef rename_narrative(RenameNarrativeParams params) returns (RenameNarrativeResult result) authentication required;

/*
narrative_upa - UPA of the narrative to be requested in search doc format.
*/
typedef structure {
string narrative_upa;
} SearchDocNarrativeParams;

/*
desc - a brief description of the narrative cell.
cell_type - the type of cell.
count - the number of instances this cell appears within a given narrative.
*/
typedef structure {
string desc;
string cell_type;
int count;
} DocCell;

/*
name - The name of the data object.
obj_type - The type of data object.
readableType - The data object type in a human readable format for displays.
*/
typedef structure {
string name;
string obj_type;
string readableType;
} DocDataObject;

/*
access_group - A numeric ID which corresponds to the ownership group.
cells - A list of each cell's metadata within a given narrative.
copied - Indicates whether this narrative is a copy.
creation_date - The date this narrative was created.
creator - The username of the creator of a given narrative.
data_objects - A list of each data object used in a given narrative.
is_narratorial - Whether or not the doc item is narratorial.
is_public - Whether or not a given narrative is publicly shared.
is_temporary - Whether or not a given narrative exists permanently.
modified_at - The date a given narrative was last updated according to the version provided in the UPA param.
narrative_title - The title of a given narrative.
obj_id - The id of a given narrative
obj_name - The name of a given narrative
obj_type_module -
obj_type_version -
owner - The username of the current owner of a given narrative.
shared_users - A list of users who are allowed access to a given narrative.
tags - A list of all tagged versions of a given narrative ???
timestamp - The time that a given narrative was last saved, regardless of version.
total_cells - The total number of cells in a given narrative.
version - The version of the narrative requested
*/
typedef structure {
int access_group;
list<DocCell> cells;
boolean copied;
string creation_date;
string creator;
list<DocDataObject> data_objects;
boolean is_narratorial;
boolean is_public;
boolean is_temporary;
int modified_at;
string narrative_title;
int obj_id;
string obj_name;
string obj_type_module;
string obj_type_version;
string owner;
list<string> shared_users;
list<string> tags;
int timestamp;
int total_cells;
int version;
} SearchDocResult;

/*
Intended to return data of previous versions of a given narrative in the same format returned from Search.
Formats a call to workspace service to fit the appropriate schema that is intended for use in UI displays
in the narrative navigator.
*/
funcdef get_narrative_doc(SearchDocNarrativeParams params) returns (SearchDocResult result) authentication required;
};
73 changes: 73 additions & 0 deletions lib/NarrativeService/NarrativeManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,79 @@ def __init__(self, config, user_id, set_api_client, data_palette_client, workspa
self.ws = workspace_client
self.intro_cell_file = config["intro-cell-file"]

def get_narrative_doc(self, ws_id, narrative_upa):
# data = self.ws.get_objects2({'objects': [{'ref': narrative_upa}]})['data'][0]
obj_data = self.ws.get_objects2({'objects': [{'ref': narrative_upa}]})
data_objects = self.ws.list_objects({'ids': [ws_id]})

permissions = self.ws.get_permissions_mass({'workspaces': [{'id': ws_id}]})['perms']
shared_users, is_public = self._fmt_doc_permissions(permissions)

doc = {
'access_group': obj_data['orig_wsid'],
'cells': [self._get_doc_cell(c) for c in obj_data['data'][0]['data']['cells']],
'total_cells': len(obj_data['data'][0]['data']['cells']),
'data_objects': [{'name': o[1], 'obj_type': o[2]} for o in data_objects],
'creator': obj_data['creator'],
'shared_users': shared_users,
'is_public': is_public,
'timestamp': obj_data['epoch'],
'creation_date': obj_data['created']
}

return doc

def _fmt_doc_permissions(self, permissions):
# get list of users and whether a narrative is public
is_public = False
shared_users = []
for permission in permissions:
k, v = permission.popitem()
if k == '*':
is_public = (v != 'n')
elif v != 'n':
shared_users.append(k)
return shared_users, is_public

def _get_doc_cell(self, cell):
# get the appropriate cell format for search result doc
meta = cell['metadata']['kbase']
if cell['cell_type'] == 'markdown':
# type markdown
return {
'cell_type': 'markdown',
'desc': meta['attributes']['title']
}
elif meta['type'] == 'output':
# type widget
return {
'cell_type': 'widget',
'desc': meta['outputCell']['widget']['name']
}
elif meta['type'] == 'data':
# type data
return {
'cell_type': 'data',
'desc': meta['dataCell']['objectInfo']['name']
}
elif meta['type'] == 'app':
# type kbase_app
return {
'cell_type': 'kbase_app',
'desc': meta['appCell']['app']['spec']['info']['name']
}
elif meta['type'] == 'code':
# type code_cell
return {
'cell_type': 'code_cell',
'desc': cell['source']
}
else:
return {
'cell_type': '',
'desc': ''
}

def copy_narrative(self, newName, workspaceRef, workspaceId):
time_ms = int(round(time.time() * 1000))
newWsName = self.user_id + ':narrative_' + str(time_ms)
Expand Down
74 changes: 71 additions & 3 deletions lib/NarrativeService/NarrativeServiceImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class NarrativeService:
# state. A method could easily clobber the state set by another while
# the latter method is running.
######################################### noqa
VERSION = "0.3.2"
GIT_URL = "[email protected]:kbaseapps/NarrativeService.git"
GIT_COMMIT_HASH = "8d3dee255ebcac308a221a0a3cb1a2e9cfde18bd"
VERSION = "0.3.3"
GIT_URL = "[email protected]:charleshtrenholm/NarrativeService.git"
GIT_COMMIT_HASH = "c41e7d2f4a4be8b28742c743b5c512f7fb122cd5"

#BEGIN_CLASS_HEADER
def _nm(self, ctx):
Expand Down Expand Up @@ -850,6 +850,74 @@ def rename_narrative(self, ctx, params):
'result is not type dict as required.')
# return the results
return [result]

def get_narrative_doc(self, ctx, params):
"""
Intended to return data of previous versions of a given narrative in the same format returned from Search.
Formats a call to workspace service to fit the appropriate schema that is intended for use in UI displays
in the narrative navigator.
:param params: instance of type "SearchDocNarrativeParams"
(narrative_upa - UPA of the narrative to be requested in search
doc format.) -> structure: parameter "narrative_upa" of String
:returns: instance of type "SearchDocResult" (access_group - A
numeric ID which corresponds to the ownership group. cells - A
list of each cell's metadata within a given narrative. copied -
Indicates whether this narrative is a copy. creation_date - The
date this narrative was created. creator - The username of the
creator of a given narrative. data_objects - A list of each data
object used in a given narrative. is_narratorial - Whether or not
the doc item is narratorial. is_public - Whether or not a given
narrative is publicly shared. is_temporary - Whether or not a
given narrative exists permanently. modified_at - The date a given
narrative was last updated according to the version provided in
the UPA param. narrative_title - The title of a given narrative.
obj_id - The id of a given narrative obj_name - The name of a
given narrative obj_type_module - obj_type_version - owner - The
username of the current owner of a given narrative. shared_users -
A list of users who are allowed access to a given narrative. tags
- A list of all tagged versions of a given narrative ??? timestamp
- The time that a given narrative was last saved, regardless of
version. total_cells - The total number of cells in a given
narrative. version - The version of the narrative requested) ->
structure: parameter "access_group" of Long, parameter "cells" of
list of type "DocCell" (desc - a brief description of the
narrative cell. cell_type - the type of cell. count - the number
of instances this cell appears within a given narrative.) ->
structure: parameter "desc" of String, parameter "cell_type" of
String, parameter "count" of Long, parameter "copied" of type
"boolean" (@range [0,1]), parameter "creation_date" of String,
parameter "creator" of String, parameter "data_objects" of list of
type "DocDataObject" (name - The name of the data object. obj_type
- The type of data object. readableType - The data object type in
a human readable format for displays.) -> structure: parameter
"name" of String, parameter "obj_type" of String, parameter
"readableType" of String, parameter "is_narratorial" of type
"boolean" (@range [0,1]), parameter "is_public" of type "boolean"
(@range [0,1]), parameter "is_temporary" of type "boolean" (@range
[0,1]), parameter "modified_at" of Long, parameter
"narrative_title" of String, parameter "obj_id" of Long, parameter
"obj_name" of String, parameter "obj_type_module" of String,
parameter "obj_type_version" of String, parameter "owner" of
String, parameter "shared_users" of list of String, parameter
"tags" of list of String, parameter "timestamp" of Long, parameter
"total_cells" of Long, parameter "version" of Long
"""
# ctx is the context object
# return variables are: result
#BEGIN get_narrative_doc
try:
ws_id = int(params['narrative_upa'].split('/')[0])
except ValueError as e:
raise ValueError('Incorrect upa format: required format is <worspace_id>/<object_id>/<version>')
restult = self._nm(ctx).get_narrative_doc(ws_id, params['narrative_upa'])
#END get_narrative_doc

# At some point might do deeper type checking...
if not isinstance(result, dict):
raise ValueError('Method get_narrative_doc return value ' +
'result is not type dict as required.')
# return the results
return [result]
def status(self, ctx):
#BEGIN_STATUS
returnVal = {'state': "OK",
Expand Down
4 changes: 4 additions & 0 deletions lib/NarrativeService/NarrativeServiceServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ def __init__(self):
name='NarrativeService.rename_narrative',
types=[dict])
self.method_authentication['NarrativeService.rename_narrative'] = 'required' # noqa
self.rpc_service.add(impl_NarrativeService.get_narrative_doc,
name='NarrativeService.get_narrative_doc',
types=[dict])
self.method_authentication['NarrativeService.get_narrative_doc'] = 'required' # noqa
self.rpc_service.add(impl_NarrativeService.status,
name='NarrativeService.status',
types=[dict])
Expand Down

0 comments on commit 377c43b

Please sign in to comment.