Skip to content

Commit

Permalink
Global chain ledger history (#13)
Browse files Browse the repository at this point in the history
* working initial history

* moved to dict for infomation passing
  • Loading branch information
bbartlett-beamio authored and chriswilley committed Nov 22, 2019
1 parent 43319f3 commit d400ca1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions algorithm_toolkit/atk_default_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from collections import OrderedDict

ATK_MANAGEMENT_API_KEY = None
CORS_ORIGIN_WHITELIST = []
DEFAULT_WORKING_ROOT = '/tmp'

CHAIN_HISTORY = OrderedDict()
CHAIN_HISTORY_LENGTH = 0

CHAIN_DATA = OrderedDict()
30 changes: 23 additions & 7 deletions algorithm_toolkit/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,29 @@ def main():
iter_type = request.args.get('iter_type', None)
iter_value = request.args.get('iter_value', None)

if status_key is None:
status_key = create_random_string(http_safe=True)

if chain is None:
return make_response('Missing chain parameter in request', 400)

try:
chain = json.loads(chain)
except ValueError:
return make_response('Chain parameter not properly formatted', 400)
if chain.lower() == 'from_global':
try:
chain = app.config['CHAIN_DATA'].pop(status_key)
except ValueError:
return make_response('Chain parameter not present in app configuration', 400)
else:
try:
chain = json.loads(chain)
except ValueError:
return make_response('Chain parameter not properly formatted', 400)

if 'chain_name' not in chain:
return make_response('Chain name not defined', 400)

if 'algorithms' not in chain:
return make_response('Algorithms not defined', 400)

if status_key is None:
status_key = create_random_string(http_safe=True)

c_obj = AlgorithmChain(path, chain)
if c_obj.chain_definition == {}:
return make_response('Chain name not found', 404)
Expand All @@ -200,13 +206,23 @@ def main():
if run_mode == 'single':
response = c_obj.call_chain_algorithms()
save_fname = status_key + '.json'

if 'CHAIN_LEDGER_HISTORY_PATH' in app.config:
save_path = os.path.join(
app.config['CHAIN_LEDGER_HISTORY_PATH'], save_fname)
else:
make_dir_if_not_exists(os.path.join(path, 'history'))
save_path = os.path.join(path, 'history', save_fname)
c_obj.chain_ledger.save_history_to_json(save_path, pretty=True)

if 'CHAIN_HISTORY' in app.config:
if app.config['CHAIN_HISTORY_LENGTH'] > 0:
ch = app.config['CHAIN_HISTORY']

if len(ch) > app.config['CHAIN_HISTORY_LENGTH']:
ch.popitem(last=False)

ch[status_key] = c_obj.chain_ledger
else:
response = c_obj.call_batch(iter_param, iter_type, iter_value)

Expand Down

0 comments on commit d400ca1

Please sign in to comment.