Browse Source

added update job

cjs3
Stephen Lorenz 3 years ago
parent
commit
57d2e66afa
  1. 13
      cjs/database.py
  2. BIN
      cjsd/.app/database/steve/database.db
  3. 3
      cjsd/app.py
  4. 38
      cjsd/core/database.py
  5. 1
      cjsd/models/database.py
  6. 44
      cjsd/resources/automate.py
  7. 9
      cjsd/resources/database.py

13
cjs/database.py

@ -43,8 +43,6 @@ def new(ctx, database_name):
# send (POST) the dictonary to the host and store the response
address = '%s/database/new' % ctx.obj['host']
response = rest_post(address, payload)
print(response.content.decode('utf-8'))
@cli.command()
@click.argument('database_name')
@ -67,7 +65,6 @@ def clone(ctx, database_name, new_database):
# send (POST) the dictonary to the host and store the response
address = '%s/database/clone' % ctx.obj['host']
response = rest_post(address, payload)
print(response.content.decode('utf-8'))
@cli.command()
@click.argument('database_name')
@ -88,7 +85,6 @@ def delete(ctx, database_name):
# send (POST) the dictonary to the host and store the response
address = '%s/database/delete' % ctx.obj['host']
response = rest_post(address, payload)
print(response.content.decode('utf-8'))
@cli.command(name='list')
@click.pass_context
@ -99,7 +95,9 @@ def my_list(ctx):
Args:
ctx: Click command-line interface context.
'''
response = rest_get('http://127.0.0.1:8000/database/list')
address = '%s/database/list' % ctx.obj['host']
response = rest_get(address)
print(response.content.decode('utf-8'))
@cli.command()
@ -152,8 +150,6 @@ def clear(ctx, database_name):
# send (POST) the dictonary to the host and store the response
address = '%s/database/clear' % ctx.obj['host']
response = rest_post(address, payload)
print(response.content.decode('utf-8'))
@cli.command()
@click.argument('database_name')
@ -170,13 +166,12 @@ def stage(ctx, database_name, batch_name):
'''
payload = {
'database': database_name,
'batch': batch_name
}
# send (POST) the dictonary to the host and store the response
address = '%s/database/stage' % ctx.obj['host']
response = rest_post(address, payload)
print(response.content.decode('utf-8'))
@cli.command()
@click.argument('database_name')

BIN
cjsd/.app/database/steve/database.db

3
cjsd/app.py

@ -31,7 +31,8 @@ ROUTES = {
'/database/upload': database.UploadResource,
'/database/status': database.StatusResource,
'/database/stage': database.StageResource,
'/automate/next_job': automate.NextJobResource
'/automate/next_job': automate.NextJobResource,
'/automate/update_job': automate.UpdateJobResource
}
def create_api():

38
cjsd/core/database.py

@ -60,7 +60,13 @@ class Database:
def initialize(self, sess):
# NOTE: this method was created because I was unable to get sqlalchemy's
# initial values to work
names = ['Pending', 'Processing', 'Complete', 'Failed']
names = [
'Pending',
'Processing',
'Complete',
'Failed'
]
events = [EventTable(n) for n in names]
self.add_tables(events)
@ -453,17 +459,37 @@ def next_job(name):
next_comparison = ds.select_tables(ComparisonTable,
ComparisonTable.id_ == next_test.comparison_id)
return next_evidence[0], next_comparison[0]
return first_job, next_evidence[0], next_comparison[0]
except:
# TODO: implement fine-grained handling
raise
def set_job_status(name, job_id, event_id):
try:
_database_update(name,
JobTable,
JobTable.id_ == job_id,
{'event_id': event_id})
database_update(name,
JobTable,
JobTable.id_ == job_id,
{'event_id': event_id})
except:
# TODO: implement fine-grained handling
raise
def find_job(database_name, evidence_name, comparison_name):
try:
db = _get_database(database_name)
sess = db._get_session()
evidence_table = db.first_record(EvidenceTable,
EvidenceTable.name == evidence_name)
comparison_table = db.first_record(ComparisonTable,
ComparisonTable.name == comparison_name)
test_table = db.first_record(
TestTable,
((TestTable.comparison_id == comparison_table.id_) and (TestTable.evidence_id == comparison_table.id_))
)
job_table = db.first_record(JobTable, JobTable.test_id == test_table.id_)
return job_table
except:
# TODO: implement fine-grained handling
raise

1
cjsd/models/database.py

@ -110,6 +110,7 @@ class ResultTable(DatabaseBase):
id_ = Column('id', Integer, primary_key=True)
name = Column('name', String(32))
path = Column('path', String(260))
label = Column('label', String(64))
batch_id = Column('batch_id', ForeignKey('Batch.id'))
def __init__(self, name, path, batch_id):

44
cjsd/resources/automate.py

@ -3,9 +3,13 @@
import falcon
import resources.database
from resources.database import get_context
from resources.database import get_database_context
import core.database
from core.database import next_job
from core.database import set_job_status
from core.database import find_job
import utils.io
from utils.io import read_json
@ -19,29 +23,41 @@ class NextJobResource:
job_list = next_job(database_name)
# split the job list into separate objects
evidence_table, comparison_table = job_list
job, evidence_table, comparison_table = job_list
# load the data from both
evidence_data = read_json(evidence_table.path)
comparison_data = read_json(comparison_table.path)
# store name in variable for error checking purposes
evidence_name = evidence_table.name
comparison_name = comparison_table.name
resp.media = {
'evidence': {
'name': evidence_name,
'data': evidence_data
},
'comparison': {
'name': comparison_name,
'data': comparison_data
}
'evidence': evidence_data,
'comparison': comparison_data
}
# where 2 = 'Processing'
set_job_status(database_name, job.id_, 2)
except IntegrityError:
raise falcon.HTTPBadRequest('No jobs',
'Unable to get next job.')
except Exception as e:
raise falcon.HTTPInternalServerError()
class UpdateJobResource:
@falcon.before(get_context, 'evidence')
@falcon.before(get_context, 'comparison')
@falcon.before(get_context, 'event')
@falcon.before(get_database_context)
def on_post(self, req, resp):
database_name = req.context['database']
try:
evidence_name = req.context['evidence']
comparison_name = req.context['comparison']
event_id = req.context['event']
job = find_job(database_name, evidence_name, comparison_name)
set_job_status(database_name, job.id_, event_id)
except IntegrityError:
raise falcon.HTTPBadRequest('No jobs',
'Unable to get next job.')
except Exception as e:
raise falcon.HTTPInternalServerError()
raise falcon.HTTPInternalServerError()

9
cjsd/resources/database.py

@ -69,6 +69,12 @@ def database_skeleton(root):
# BEGIN: REST API Resources
# BEGIN: Falcon hooks
def get_context(req, resp, Resource, params, key):
try:
req.context[key] = req.media[key]
except KeyError:
raise falcon.HTTPMissingParam(key)
def get_database_context(req, resp, Resource, params):
try:
req.context['database'] = req.media['database']
@ -192,7 +198,8 @@ class ClearResource:
raise falcon.HTTPInternalServerError()
# initialize a temporary function to resolve input file paths
input_path = lambda root, filename: '%s/input/%s.json' % (root, filename)
def input_path(root, filename):
return '%s/input/%s.json' % (root, filename)
def upload_evidence(database_name, database_path, data):
# separate the comparison(s) from evidence data

Loading…
Cancel
Save