- # unknown field
- del testcase[key]
-
- if len(mandatory_fields) > 0:
- # some mandatory fields are missing
- logger.info("Skipping testcase with mongo _id '{}' because the testcase was missing"
- " mandatory field(s) '{}'".format(mongo_id, mandatory_fields))
- return False
- elif len(mandatory_fields_to_modify) > 0:
- # some mandatory fields are missing
- logger.info("Skipping testcase with mongo _id '{}' because the testcase was missing"
- " mandatory field(s) '{}'".format(mongo_id, mandatory_fields_to_modify.keys()))
- return False
- else:
- if len(fields_to_swap_or_add) > 0:
- for key, swap_key in fields_to_swap_or_add.iteritems():
- testcase[key] = testcase[swap_key]
-
- return True
-
-
-def format_document(testcase):
- # 1. verify and identify the testcase
- # 2. if modification is implemented, then use that
- # 3. if not, try to use default
- # 4. if 2 or 3 is successful, return True, otherwise return False
- if verify_document(testcase):
- project = testcase['project_name']
- case_name = testcase['case_name']
- fmt = conf_utils.get_format(project, case_name)
- if fmt:
- try:
- logger.info("Processing %s/%s using format %s" % (project, case_name, fmt))
- return vars(mongo2elastic_format)[fmt](testcase)
- except Exception:
- logger.error("Fail in format testcase[%s]\nerror message: %s" % (testcase, traceback.format_exc()))
- return False
- else:
- return False
-
-
-def export_documents(days):
- cmd = ['mongoexport', '--db', 'test_results_collection', '-c', 'results']
- if days > 0:
- past_time = datetime.datetime.today() - datetime.timedelta(days=days)
- cmd += ['--query', '{{"start_date":{{$gt:"{}"}}}}'.format(past_time)]
- cmd += [ '--out', '{}'.format(tmp_docs_file)]
-
- try:
- subprocess.check_call(cmd)
- except Exception, err:
- logger.error("export mongodb failed: %s" % err)
- exit(-1)
-
-
-def publish_document(document, es_creds, to):
- status, data = shared_utils.publish_json(document, es_creds, to)
- if status > 300:
- logger.error('Publish record[{}] failed, due to [{}]'
- .format(document, json.loads(data)['error']['reason']))
-
-
-def publish_nonexist_documents(elastic_docs, es_creds, to):
- try:
- with open(tmp_docs_file) as fdocs:
- for doc_line in fdocs:
- doc = json.loads(doc_line)
- if format_document(doc) and doc not in elastic_docs:
- publish_document(doc, es_creds, to)
- finally:
- fdocs.close()
+ query = '''{{
+ "project_name": "{}",
+ "case_name": "{}"
+ }}'''.format(self.project, self.case)
+ cmd = ['mongoexport',
+ '--db', 'test_results_collection',
+ '--collection', 'results',
+ '--query', '{}'.format(query),
+ '--out', '{}'.format(tmp_docs_file)]
+ try:
+ subprocess.check_call(cmd)
+ return self
+ except Exception, err:
+ logger.error("export mongodb failed: %s" % err)
+ self._remove()
+ exit(-1)
+
+ def get_existed_docs(self):
+ self.existed_docs = shared_utils.get_elastic_docs_by_days(self.elastic_url, self.creds, days)
+ return self
+
+ def publish(self):
+ try:
+ with open(tmp_docs_file) as fdocs:
+ for doc_line in fdocs:
+ DocumentPublisher(json.loads(doc_line),
+ self.fmt,
+ self.existed_docs,
+ self.creds,
+ self.to).format().publish()
+ finally:
+ fdocs.close()
+ self._remove()
+
+ def _remove(self):