Form db backend query


In [1]:
from tinydb import TinyDB, Query
import glob
import json
from pprint import pprint
from dkrz_forms.config import settings

db = TinyDB("/home/stephan/Forms/db.json")
Form = Query()
# to do: pycodestyle --show-source --show-pep8 dkrz_forms/form_handler.py

In [3]:
json_files = glob.glob(settings.SUBMISSION_REPO+"/test/"+"*.json")
print json_files


['/home/stephan/Forms/submission_repo/test/test_testsuite_123.json', '/home/stephan/Forms/submission_repo/test/test_testsuite_12.json', '/home/stephan/Forms/submission_repo/test/test_testsuite_1.json', '/home/stephan/Forms/submission_repo/test/test_testsuite_1234.json']

In [4]:
def clean_dict(a_dict):
    if '__doc__' in a_dict.keys():
       a_dict.pop('__doc__')
    for a,b in a_dict.iteritems():
        if isinstance(b,dict):
            clean_dict(b)

json_dicts = []
for json_file in json_files:
    with open(json_file) as j_file:
        fdict = json.load(j_file)
        clean_dict(fdict)
        json_dicts.append(fdict)

In [8]:
db.purge()

In [5]:
for my_dict in json_dicts:
    db.insert(my_dict)

In [11]:
db.insert_multiple(json_dicts)


Out[11]:
[1, 2, 3, 4]

Query the status flags

Query expressions:

  • Form.status = ".." # global status
  • Form.sub.activity.status = ".." # status of submission (anlog for pub,qua,ing,.. steps)

In [14]:
# Examples: 

db.search(Form.sub.activity.status = 'sub')

db.search(Form.sub.activity.status = 'started')

Query expressions:

  • submission agent related:
    • Form.sub.agent.email = ".."
    • Form.sub.agent.last_name = ".." # first_name, key_work
  • submission entity related:
    • Form.sub.entity_out.check_status = ".."
    • Form.sub.entity_out.report.project_specific_keywords = ".."

In [ ]:
# examples

Query submission activity info

Query expressions:

  • Form.sub.activity.status = ".."
  • Form.sub.activity.time_start = ".." # or time_end
  • ..

In [ ]:


In [19]:
pprint(results1)
pprint(results2)


[{u'ing': {u'activity': {u'comment': u'',
                         u'end_time': u'',
                         u'i_name': u'ingest_activity',
                         u'report': {u'comment': u''},
                         u'start_time': u'',
                         u'status': u'',
                         u'ticket_id': 0},
           u'agent': {u'i_name': u'ingest_agent',
                      u'responsible_person': u''},
           u'entity_in': {u'comment': u'',
                          u'date': u'',
                          u'i_name': u'review_report',
                          u'repo': u'',
                          u'report': {},
                          u'status': u'',
                          u'tag': u''},
           u'entity_out': {u'comment': u'',
                           u'date': u'',
                           u'i_name': u'ingest_report',
                           u'report': {},
                           u'status': u'',
                           u'tag': u''},
           u'i_name': u'data_ingest'},
  u'project': u'test',
  u'pub': {u'activity': {u'comment': u'',
                         u'end_time': u'',
                         u'follow_up_ticket': u'',
                         u'i_name': u'publication_activity',
                         u'report': {},
                         u'start_time': u'',
                         u'status': u'',
                         u'ticket_id': u''},
           u'agent': {u'i_name': u'publication_agent',
                      u'publication_tool': u'',
                      u'responsible_person': u''},
           u'entity_in': {u'comment': u'',
                          u'date': u'',
                          u'i_name': u'qua_report',
                          u'repo': u'',
                          u'report': {},
                          u'status': u'',
                          u'tag': u''},
           u'entity_out': {u'comment': u'',
                           u'date': u'',
                           u'facet_string': u'# e.g. project=A&model=B& ....',
                           u'i_name': u'publication_report',
                           u'repo': u'',
                           u'report': {},
                           u'status': u'',
                           u'tag': u''},
           u'i_name': u'data_publication'},
  u'qua': {u'activity': {u'comment': u'',
                         u'end_time': u'',
                         u'follow_up_ticket': u'',
                         u'i_name': u'qua_activity',
                         u'report': {},
                         u'start_time': u'',
                         u'status': u'',
                         u'ticket_id': u''},
           u'agent': {u'i_name': u'qua_agent', u'responsible_person': u''},
           u'entity_in': {u'comment': u'',
                          u'date': u'',
                          u'i_name': u'ingest_report',
                          u'report': {},
                          u'status': u'',
                          u'tag': u''},
           u'entity_out': {u'comment': u'',
                           u'date': u'',
                           u'i_name': u'qua_report',
                           u'repo': u'',
                           u'report': {},
                           u'status': u'',
                           u'tag': u''},
           u'i_name': u'data_quality_assurance'},
  u'rev': {u'activity': {u'comment': u'',
                         u'end_time': u'',
                         u'i_name': u'review_activity',
                         u'report': {},
                         u'start_time': u'',
                         u'status': u'',
                         u'ticket_id': u'0',
                         u'ticket_url': u'https://dm-rt.dkrz.de/Ticket/Display.html?'},
           u'agent': {u'i_name': u'review_agent',
                      u'responsible_person': u''},
           u'entity_in': {u'checks_done': u'',
                          u'form_dir': u'',
                          u'form_json': u'',
                          u'form_name': u'',
                          u'form_path': u'',
                          u'form_repo': u'',
                          u'form_repo_path': u'',
                          u'i_name': u'submission_form_entity',
                          u'repo': u'',
                          u'report': {},
                          u'status': u'',
                          u'tag': u''},
           u'entity_out': {u'comment': u'',
                           u'date': u'',
                           u'i_name': u'review_report',
                           u'repo': u'',
                           u'report': {},
                           u'status': u'',
                           u'tag': u''},
           u'i_name': u'data_submission_review'},
  u'status': u'sub_start',
  u'sub': {u'activity': {u'commit_hash': u'ee8acab4267e071e6d3ae33719c8101c89274e36',
                         u'i_name': u'submission_activity',
                         u'keyword': u'123',
                         u'pwd': u'3G1YAO',
                         u'status': u'completed',
                         u'submission_comment': u'',
                         u'submission_method': u''},
           u'agent': {u'email': u'stephan.kindermann@gmail.com',
                      u'first_name': u'unit_tester',
                      u'i_name': u'submission_agent',
                      u'key_word': u'',
                      u'last_name': u'testsuite'},
           u'entity_in': {u'form_json': u'/home/stephan/form_notebooks/test/test_testsuite_123.json',
                          u'form_path': u'/home/stephan/form_notebooks/test/test_testsuite_123.ipynb',
                          u'form_template_version': u'',
                          u'i_name': u'submission_form_template_entity',
                          u'source_path': u'/home/stephan/submission_forms/dkrz_forms/Templates/test_submission_form.ipynb',
                          u'tag': u''},
           u'entity_out': {u'check_status': u'consistency_checked',
                           u'checks_done': u'',
                           u'form_dir': u'',
                           u'form_json': u'/home/stephan/Forms/local_repo/test/test_testsuite_123.json',
                           u'form_name': u'test_testsuite_123',
                           u'form_path': u'',
                           u'form_repo': u'/home/stephan/Forms/local_repo/test',
                           u'form_repo_path': u'/home/stephan/Forms/local_repo/test/test_testsuite_123.ipynb',
                           u'i_name': u'submission_form_entity',
                           u'pwd': u'3G1YAO',
                           u'repo': u'',
                           u'report': {u'comment': u'',
                                       u'project': u'test'},
                           u'status': u'checked',
                           u'tag': u''},
           u'form_dir': u'/home/stephan/form_notebooks/test',
           u'i_name': u'data_submission',
           u'id': u'62e4a61d-693c-11e7-8a44-080027a5bc96',
           u'submission_repo': u'/home/stephan/Forms/submission_repo/test',
           u'timestamp': u'2017-07-15 11:02:53.694575'},
  u'workflow': [[u'sub', u'data_submission'],
                [u'rev', u'data_submission_review'],
                [u'ing', u'data_ingest'],
                [u'qua', u'data_quality_assurance'],
                [u'pub', u'data_publication']]}]
[]

In [ ]:
db.all()

In [ ]: