append results to tracer

so a tracer could have:

[(timestamp, data), ...] # result from yesterday
[(timestamp, data), ...] # result from today

Change-Id: Ief409c1401e9972da576657db74a3388c9422356
This commit is contained in:
Kun Huang 2015-11-19 17:03:54 +08:00
parent c960bba7a0
commit 03ce2e4901
8 changed files with 57 additions and 21 deletions

View File

@ -30,7 +30,8 @@ class TracerEndpoint(object):
ret.append({"name":tr.name, ret.append({"name":tr.name,
"tpl":tr.template, "tpl":tr.template,
"running":tr.is_running, "running":tr.is_running,
"pid":tr.pid}) "pid":tr.pid,
"results":tr.results})
return ret return ret
def start_tracers(self, ctx, tracers): def start_tracers(self, ctx, tracers):

View File

@ -5,17 +5,29 @@
from scalpels.client.api import api as agent_api from scalpels.client.api import api as agent_api
from scalpels.client.utils import generate_multiple_result_html from scalpels.client.utils import generate_multiple_result_html
from scalpels.client.utils import pprint_result from scalpels.client.utils import pprint_result
from scalpels.client.actions.start import _parse_agents_from_args
def run(config): def run(config):
task = agent_api.try_get_task_from_config(config) if config["uuid"] and config["agent"]:
raise ValueError("Can't stop both a task and a tracer")
print "reporting task: <%s>" % task["uuid"] elif config["agent"]:
rets = [] req_tr = _parse_agents_from_args(config)
for ret_uuid in task["results"]: all_tr = {t["name"]:t for t in agent_api.get_tracer_list()}
ret = agent_api.get_result(ret_uuid) for tr in req_tr:
rets.append(ret) for ret_uuid in all_tr[tr]["results"]:
if config.get("html"): ret = agent_api.get_result(ret_uuid)
generate_multiple_result_html(rets) pprint_result(ret)
else: else:
map(pprint_result, rets)
task = agent_api.try_get_task_from_config(config)
print "reporting task: <%s>" % task["uuid"]
rets = []
for ret_uuid in task["results"]:
ret = agent_api.get_result(ret_uuid)
rets.append(ret)
if config.get("html"):
generate_multiple_result_html(rets)
else:
map(pprint_result, rets)

View File

@ -9,10 +9,11 @@ from scalpels.client.actions.start import _parse_agents_from_args
def run(config): def run(config):
print "command stop: %s" % config print "command stop: %s" % config
if config["uuid"] and config["agent"]: if config["uuid"] and config["agent"]:
raise ValueError("") raise ValueError("Can't stop both a task and a tracer")
elif config["uuid"]:
task = agent_api.try_get_task_from_config(config)
agent_api.stop_task(task["uuid"])
elif config["agent"]: elif config["agent"]:
agents = _parse_agents_from_args(config) agents = _parse_agents_from_args(config)
agent_api.stop_tracers(agents) agent_api.stop_tracers(agents)
else:
# no tracers are specified so trying to find a task
task = agent_api.try_get_task_from_config(config)
agent_api.stop_task(task["uuid"])

View File

@ -37,6 +37,7 @@ def main():
report.add_argument("--last", action="store_true", dest="last", help="report the last task") report.add_argument("--last", action="store_true", dest="last", help="report the last task")
report.add_argument("--html", action="store_true", dest="html", help="report html to stdout instead of pretty print") report.add_argument("--html", action="store_true", dest="html", help="report html to stdout instead of pretty print")
report.add_argument("uuid", type=str, default="", nargs="?", help="report the last task") report.add_argument("uuid", type=str, default="", nargs="?", help="report the last task")
report.add_argument("-a", "--agent", action="append", dest="agent", help="agent(s) to stop", required=False)
# setup sca result --list # setup sca result --list
result = subparsers.add_parser("result") result = subparsers.add_parser("result")

View File

@ -60,14 +60,12 @@ def save_result_to_task():
co.start() co.start()
lock = co.get_lock("task_update_lock") lock = co.get_lock("task_update_lock")
with lock: with lock:
task = db_api.task_get(task_uuid)
results = copy(task.results)
for ret in parse_func(out): for ret in parse_func(out):
ret = db_api.result_create(**ret) ret = db_api.result_create(**ret)
print "[LOG] appending result with id %s" % ret.uuid print "[LOG] appending result with id %s" % ret.uuid
results.append(ret.uuid) db_api.task_append_result(task_uuid, ret.uuid)
db_api.tracer_append_result(ag, ret.uuid)
print "[LOG] update tas with result %s" % task_uuid print "[LOG] update tas with result %s" % task_uuid
db_api.task_update(task_uuid, results=results)
time.sleep(2) time.sleep(2)
co.stop() co.stop()

View File

@ -39,6 +39,9 @@ def task_get(task_uuid, fuzzy=False):
def task_update(task_uuid, results=None, pids=None): def task_update(task_uuid, results=None, pids=None):
return IMPL.task_update(task_uuid, results, pids) return IMPL.task_update(task_uuid, results, pids)
def task_append_result(task_uuid, result_uuid):
return IMPL.task_append_result(task_uuid, result_uuid)
def task_get_last(): def task_get_last():
return IMPL.task_get_last() return IMPL.task_get_last()
@ -77,3 +80,6 @@ def tracer_get(tracer):
def tracer_update(tracer, running=None, pid=None): def tracer_update(tracer, running=None, pid=None):
return IMPL.tracer_update(tracer, running, pid) return IMPL.tracer_update(tracer, running, pid)
def tracer_append_result(tracer, result_uuid):
return IMPL.tracer_append_result(tracer, result_uuid)

View File

@ -77,7 +77,14 @@ def task_update(task_uuid, results=None, pids=None):
task.save(session=session) task.save(session=session)
return task return task
def task_append_result(task_uuid, result_uuid):
session = get_session()
task = model_query(models.Task, session=session).filter_by(uuid=task_uuid).first()
new = copy(task.results)
new.append(result_uuid)
task.update({"results":new})
task.save(session=session)
return task
def task_get(task_uuid, fuzzy=False): def task_get(task_uuid, fuzzy=False):
if not fuzzy: if not fuzzy:
@ -119,7 +126,7 @@ def get_all_results():
def register_tracer(name, template): def register_tracer(name, template):
tracer = models.Tracer() tracer = models.Tracer()
tracer.update({"name":name, "template": template}) tracer.update({"name":name, "template": template, "results":[]})
tracer.save() tracer.save()
return tracer return tracer
@ -155,3 +162,12 @@ def tracer_update(tracer_name, running=None, pid=None):
tracer.update(_update) tracer.update(_update)
tracer.save(session=session) tracer.save(session=session)
return tracer return tracer
def tracer_append_result(tracer_name, result_uuid):
session = get_session()
tracer = model_query(models.Tracer, session=session).filter_by(name=tracer_name).first()
new = copy(tracer.results)
new.append(result_uuid)
tracer.update({"results":new})
tracer.save(session=session)
return tracer

View File

@ -57,4 +57,5 @@ class Tracer(BASE, ScalpelsBase):
name = Column(String(20), nullable=False, unique=True) name = Column(String(20), nullable=False, unique=True)
template = Column(String(20), nullable=False) template = Column(String(20), nullable=False)
is_running = Column(Boolean(), default=False) is_running = Column(Boolean(), default=False)
results = Column(JSONEncodedData, nullable=False)
pid = Column(Integer(), default=-1) pid = Column(Integer(), default=-1)