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 = EmbeddedAnalysisResultLog() log.level = level log.message = msg log.datetime = now self.current_task.log.append(log)
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 # Validate user can add service results to this TLO. 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 results to." return results # Update analysis log le = EmbeddedAnalysisResultLog() le.message = log_message le.level = level le.datetime = str(datetime.datetime.now()) ar = AnalysisResult.objects(analysis_id=analysis_id).first() if ar: AnalysisResult.objects(id=ar.id).update_one(push__log=le) results['success'] = True else: results['message'] = "Could not find task to add log to." return results
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. found = False ar = AnalysisResult.objects(analysis_id=task.task_id).first() if ar: found = True if not found: logger.warning("Tried to update a task that didn't exist.") insert_analysis_results(task) else: # Otherwise, update it. tdict = task.to_dict() tdict['analysis_id'] = tdict['id'] del tdict['id'] #TODO: find a better way to do this. new_dict = {} for k in tdict.iterkeys(): new_dict['set__%s' % k] = tdict[k] try: AnalysisResult.objects(id=ar.id).update_one(**new_dict) except Exception as e: # assume bad data in 'results' task.status = 'error' new_dict['set__results'] = [] le = EmbeddedAnalysisResultLog() le.message = 'DB Update Failed: %s' % e le.level = 'error' le.datetime = str(datetime.datetime.now()) new_dict['set__log'].append(le) try: AnalysisResult.objects(id=ar.id).update_one(**new_dict) except: # don't know what's wrong, try writing basic log only AnalysisResult.objects(id=ar.id).update_one(set__log=[le])