def update_analysis_results(task): """ Update analysis results for this task. """ # If the task does not currently exist for the given sample in the # database, add it. obj_class = class_from_type(task.obj._meta['crits_type']) obj = obj_class.objects(id=task.obj.id).first() obj_id = obj.id found = False for a in obj.analysis: if str(a.analysis_id) == task.task_id: found = True break if not found: logger.warning("Tried to update a task that didn't exist.") insert_analysis_results(task) else: # Otherwise, update it. ear = EmbeddedAnalysisResult() tdict = task.to_dict() tdict['analysis_id'] = tdict['id'] del tdict['id'] ear.merge(arg_dict=tdict) obj_class.objects(id=obj_id, analysis__id=task.task_id).update_one(set__analysis__S=ear)
def insert_analysis_results(task): """ Insert analysis results for this task. """ obj_class = class_from_type(task.obj._meta['crits_type']) ear = EmbeddedAnalysisResult() tdict = task.to_dict() tdict['analysis_id'] = tdict['id'] del tdict['id'] ear.merge(arg_dict=tdict) obj_class.objects(id=task.obj.id).update_one(push__analysis=ear)
def _insert_analysis_results(self, task): """ Insert analysis results for this task. """ obj_class = class_from_type(task.context.crits_type) query = self.get_db_query(task.context) ear = EmbeddedAnalysisResult() tdict = task.to_dict() tdict['analysis_type'] = tdict['type'] tdict['analysis_id'] = tdict['id'] del tdict['type'] del tdict['id'] ear.merge(arg_dict=tdict) ear.config = AnalysisConfig(**tdict['config']) obj_class.objects(__raw__=query).update_one(push__analysis=ear)
def _update_analysis_results(self, task): """ Update analysis results for this task. """ # If the task does not currently exist for the given sample in the # database, add it. obj_class = class_from_type(task.context.crits_type) query = self.get_db_query(task.context) obj = obj_class.objects(__raw__=query).first() obj_id = obj.id found = False c = 0 for a in obj.analysis: if str(a.analysis_id) == task.task_id: found = True break c += 1 if not found: logger.warning("Tried to update a task that didn't exist.") self._insert_analysis_results(task) else: # Otherwise, update it. ear = EmbeddedAnalysisResult() tdict = task.to_dict() tdict['analysis_type'] = tdict['type'] tdict['analysis_id'] = tdict['id'] del tdict['type'] del tdict['id'] ear.merge(arg_dict=tdict) ear.config = AnalysisConfig(**tdict['config']) obj_class.objects( id=obj_id, analysis__id=task.task_id).update_one(set__analysis__S=ear)
def _update_analysis_results(self, task): """ Update analysis results for this task. """ # If the task does not currently exist for the given sample in the # database, add it. obj_class = class_from_type(task.context.crits_type) query = self.get_db_query(task.context) obj = obj_class.objects(__raw__=query).first() obj_id = obj.id found = False c = 0 for a in obj.analysis: if str(a.analysis_id) == task.task_id: found = True break c += 1 if not found: logger.warning("Tried to update a task that didn't exist.") self._insert_analysis_results(task) else: # Otherwise, update it. ear = EmbeddedAnalysisResult() tdict = task.to_dict() tdict['analysis_type'] = tdict['type'] tdict['analysis_id'] = tdict['id'] del tdict['type'] del tdict['id'] ear.merge(arg_dict=tdict) ear.config = AnalysisConfig(**tdict['config']) obj_class.objects(id=obj_id, analysis__id=task.task_id).update_one(set__analysis__S=ear)
def add_log(object_type, object_id, analysis_id, log_message, level, analyst): """ Add a log entry to an analysis task. :param object_type: The top-level object type. :type object_type: str :param object_id: The ObjectId to search for. :type object_id: str :param analysis_id: The ID of the task to update. :type analysis_id: str :param log_message: The log entry to append. :type log_message: dict :param level: The log level. :type level: str :param analyst: The user updating the log. :type analyst: str :returns: dict with keys "success" (boolean) and "message" (str) if failed. """ results = {'success': False} if not object_type or not object_id or not analysis_id: results['message'] = "Must supply object id/type and analysis id." return results klass = class_from_type(object_type) sources = user_sources(analyst) obj = klass.objects(id=object_id, source__name__in=sources).first() if not obj: results['message'] = "Could not find object to add log to." return results found = False c = 0 for a in obj.analysis: if str(a.analysis_id) == analysis_id: found = True break c += 1 if not found: results['message'] = "Could not find an analysis task to update." return results le = EmbeddedAnalysisResult.EmbeddedAnalysisResultLog() le.message = log_message le.level = level le.datetime = str(datetime.datetime.now()) klass.objects( id=object_id, analysis__id=analysis_id).update_one(push__analysis__S__log=le) results['success'] = True return results
def _log(self, level, msg): """ Add a log entry for this task. :param level: The log level for this entry. :type level: str ('debug', 'info', 'warning', 'error', 'critical') :param msg: The log message. :type msg: str """ self.ensure_current_task() now = str(datetime.now()) log = EmbeddedAnalysisResult.EmbeddedAnalysisResultLog() log.level = level log.message = msg log.datetime = now self.current_task.log.append(log)