Exemplo n.º 1
0
 def get_db_statistics_payload(self, ifo, req_method, full_uri):
     # Instantiate objects.
     dao = DAO.DAOHandle()
     # Define expected payload.
     d = {
         "results": {
             "earliest_known_segment_start_time":
             dao.get_segment_boundaries('known', False, ifo, req_method,
                                        full_uri),
             "latest_known_segment_stop_time":
             dao.get_segment_boundaries('known', True, ifo, req_method,
                                        full_uri),
             "total_known_segments":
             dao.get_segment_totals('known', ifo, req_method, full_uri),
             "earliest_active_segment_start_time":
             dao.get_segment_boundaries('active', False, ifo, req_method,
                                        full_uri),
             "latest_active_segment_stop_time":
             dao.get_segment_boundaries('active', True, ifo, req_method,
                                        full_uri),
             "total_active_segments":
             dao.get_segment_totals('active', ifo, req_method, full_uri),
             "total_flags":
             dao.get_flag_totals(ifo, req_method, full_uri),
             "total_versions":
             dao.get_flag_version_totals(ifo, req_method, full_uri),
             "last_segment_insert_time":
             dao.get_last_segment_insert_time(ifo, req_method, full_uri)
         }
     }
     # Return.
     return d
Exemplo n.º 2
0
 def get_available_resources(self, req_method, full_uri):
     # Instantiate.
     dao = DAO.DAOHandle();
     # Set resource array.
     d = {"results" : ['/report/flags', '/report/coverage', '/report/known', '/report/active', '/report/all', '/report/db', '/report/process']}
     # Get IFO array.
     ifos = dao.get_value_list(1, req_method, full_uri)
     # Loop IFO array.
     for ifo in ifos['Ifos']:
         # Append resource to list.
         d['results'].append('/report/db/' + ifo)
     # Return.
     return d
Exemplo n.º 3
0
 def check_simple_connection(self, run_id):
     # Init.
     l = 'Failure'
     n = 'simple connection check'
     m = ''
     r = 1
     # Log event.
     log.info('Starting ' + n)
     # Instantiate.
     constant = Constants.ConstantsHandle()
     dao = DAO.DAOHandle()
     util = Utils.UtilHandle()
     # Set call URI.
     uri = constant.dqsegdb_host + '/dq'
     # Attempt to open url and convert JSON payload to dictionary..
     try:
         reply = util.convert_uri_json_response_to_dict(n, uri)
     except:
         # Log event.
         m = 'Unable to convert payload returned by ' + n + ': ' + uri + ' to dictionary'
         log.error(m)
     else:
         # If payload reply is not good.
         if not reply['r'] == True:
             # Log event.
             m = 'Problem in payload conversion ' + n + ': ' + uri + ' to dictionary'
             log.error(m)
         else:
             m = reply['m']
             d = reply['d']
             # If the Ifos key is not available in the dictionary.
             if not 'Ifos' in d:
                 # Log event.
                 m = 'Ifos key not available in payload returned by ' + n + ': ' + uri + ' to dictionary'
                 log.error(m)
             # Otherwise, if key exists.
             else:
                 # Log event.
                 m = n + ' completed successfully. Resource returned ' + str(
                     d['Ifos'])
                 log.info(m)
                 # Set.
                 l = 'Success'
                 r = 0
     # Write result to database.
     dao.insert_test_result(run_id, n, l, m)
     # Return.
     return r
Exemplo n.º 4
0
 def run_test_suite(self):
     # Init.
     failures = 0
     # Instantiate.
     constant = Constants.ConstantsHandle()
     dao = DAO.DAOHandle()
     # If connection established.
     if dao.connect_to_db():
         # Set time.
         t_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
         # Insert test run to database.
         run_id = dao.insert_test_run(constant.dqsegdb_host, t_start)
         # If no run ID returned.
         if run_id == 0:
             # Log event.
             log.error('Unable to write test run to DB.')
         else:
             # Check simple connection.
             failures += self.check_simple_connection(run_id)
             # Check flag versions.
             failures += self.check_flag_versions(run_id)
             # Check active flag-version coverage.
             failures += self.check_flag_version_coverage(run_id, 'active')
             # Check known flag-version coverage.
             failures += self.check_flag_version_coverage(run_id, 'known')
             # Check flag version active segment boundaries.
             failures += self.check_flag_version_boundaries(
                 run_id, 'active')
             # Check flag version known segment boundaries.
             failures += self.check_flag_version_boundaries(run_id, 'known')
             # Set time.
             t_stop = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
             # Close test run in database.
             dao.close_test_run(run_id, t_stop, failures)
         # Close database connection.
         dao.close_db_connection()
     # Otherwise, problem with database connection. stop everything.
     else:
         # Log event.
         log.critical('No database connection available.')
Exemplo n.º 5
0
 def get_user_id(self, u, req_method, full_uri):
     # Instantiate objects.
     dao = DAO.DAOHandle()
     # Init.
     i = 0
     # Attempt to get ID.
     try:
         i = dao.get_value_details(2, u, req_method, full_uri)
     except:
         pass
     # If ID has not been found.
     if i == None or i == 0:
         # Attempt username insert.
         try:
             dao.insert_user(u, req_method, full_uri)
         except:
             pass
         else:
             # Retrieve ID.
             i = dao.get_value_details(2, u, req_method, full_uri)
     # Return.
     return i
Exemplo n.º 6
0
 def serve_put_or_patch_uri(self, req_method, full_uri, script_url, qs, j):
     # Init.
     r = {}
     e = []
     request_array = []
     # Instantiate objects.
     admin = Admin.AdminHandle()
     dao = DAO.DAOHandle()
     # If trailing slash found.
     if admin.check_for_last_str_char(full_uri, '/'):
         # Set HTTP code and log.
         e = admin.log_and_set_http_code(400, 16, req_method, None,
                                         full_uri)
     # Otherwise, trailing slash not found at end of URI.
     else:
         # Load JSON to array.
         try:
             a = json.loads(j)
         except:
             # Set HTTP code and log.
             e = admin.log_and_set_http_code(400, 1, req_method, None,
                                             full_uri)
         else:
             # Check the payload.
             payload_check = admin.check_json_payload(a, req_method)
             # If problem found.
             if payload_check:
                 # Set HTTP code and log.
                 e = admin.log_and_set_http_code(400, 33, req_method,
                                                 str(payload_check),
                                                 full_uri)
             else:
                 # Get server request timestamp.
                 server_start_time = time.time()
                 # Split the script URL to a list.
                 o = script_url.split('/')
                 # Filter out empty values.
                 f = filter(None, o)
                 # Get list size.
                 l = len(f)
                 # Start serving requests.
                 if l > 2:
                     ifo = f[1]
                     ifo_id = dao.get_value_details(1, ifo, req_method,
                                                    full_uri)
                     # If IFO does not exist.
                     if ifo_id == None:
                         # Set HTTP code and log.
                         e = admin.log_and_set_http_code(
                             404, 5, req_method, None, full_uri)
                     else:
                         flag = f[2]
                         # Add query info to passed JSON.
                         a.update(
                             admin.add_query_info_to_flag_resource(
                                 full_uri, a, 0, 0, request_array,
                                 server_start_time))
                         # Put new flag.
                         if l == 3:
                             # If PUT.
                             if req_method == 'PUT':
                                 e = dao.insert_flag(
                                     req_method, full_uri, ifo_id, ifo,
                                     flag, a)
                             else:
                                 # Set HTTP code and log.
                                 e = admin.log_and_set_http_code(
                                     400, 31, req_method, None, full_uri)
                         # Otherwise, prepare more complex requests.
                         if l > 3:
                             # Set version.
                             version = f[3]
                             # Get flag ID.
                             flag_id = dao.get_flag_id(
                                 ifo_id, flag, req_method, full_uri)
                             # If flag ID does not exist.
                             if flag_id == None:
                                 # Set HTTP code and log.
                                 e = admin.log_and_set_http_code(
                                     404, 8, req_method, None, full_uri)
                             else:
                                 # Insert version.
                                 if l == 4:
                                     # If PUT.
                                     if req_method == 'PUT':
                                         # Insert new version.
                                         e = dao.insert_flag_version(
                                             req_method, full_uri, ifo_id,
                                             ifo, flag_id, flag, version, a)
                                     # Get version ID.
                                     version_id = dao.get_flag_version_id(
                                         flag_id, version, req_method,
                                         full_uri)
                                     # If version does not exist.
                                     if version_id == None:
                                         # Set HTTP code and log.
                                         e = admin.log_and_set_http_code(
                                             404, 10, req_method, None,
                                             full_uri)
                                     else:
                                         # If putting and this version already has Known segments associated to it.
                                         if req_method == 'PUT' and dao.get_flag_version_segment_total(
                                                 'known', version_id,
                                                 req_method, full_uri) > 0:
                                             # Set HTTP code and log.
                                             e = admin.log_and_set_http_code(
                                                 400, 18, req_method, None,
                                                 full_uri)
                                         else:
                                             # Put new 'known' segments.
                                             known_insert = dao.insert_segments(
                                                 'known', req_method,
                                                 full_uri, ifo_id, ifo,
                                                 flag_id, flag, version_id,
                                                 version, a)
                                             # Set error info.
                                             e = known_insert['error_info']
                                             # If no error code returned following known insert.
                                             if not e:
                                                 # Put new 'active' segments.
                                                 active_insert = dao.insert_segments(
                                                     'active', req_method,
                                                     full_uri, ifo_id, ifo,
                                                     flag_id, flag,
                                                     version_id, version, a)
                                                 # Set error info.
                                                 e = active_insert[
                                                     'error_info']
                                                 # If no error code returned following known insert and insert set to true.
                                                 if not e and known_insert[
                                                         'inserted']:
                                                     # Update version segment global values.
                                                     dao.update_segment_global_values(
                                                         version_id,
                                                         known_insert[
                                                             'seg_tot'],
                                                         known_insert[
                                                             'seg_first_gps'],
                                                         known_insert[
                                                             'seg_last_gps'],
                                                         active_insert[
                                                             'seg_tot'],
                                                         active_insert[
                                                             'seg_first_gps'],
                                                         active_insert[
                                                             'seg_last_gps'],
                                                         req_method,
                                                         full_uri)
                                                     # Insert process.
                                                     dao.insert_process(
                                                         a, version_id,
                                                         known_insert[
                                                             'uid'],
                                                         known_insert[
                                                             'seg_tot'],
                                                         known_insert[
                                                             'seg_first_gps'],
                                                         known_insert[
                                                             'seg_last_gps'],
                                                         active_insert[
                                                             'seg_tot'],
                                                         active_insert[
                                                             'seg_first_gps'],
                                                         active_insert[
                                                             'seg_last_gps'],
                                                         req_method,
                                                         full_uri)
                                                     # Commit the transaction to the DB.
                                                     dao.commit_transaction_to_db(
                                                     )
                                                 else:
                                                     admin.log_and_set_http_code(
                                                         500, 43,
                                                         req_method, None,
                                                         full_uri)
                                 # Otherwise, URI too long.
                                 elif l > 4:
                                     # Set HTTP code and log.
                                     e = admin.log_and_set_http_code(
                                         414, 19, req_method, None,
                                         full_uri)
     # If errors have been found.
     if e:
         # Set the reply as an error.
         r = e
     # Otherwise, set as completed.
     else:
         # Set HTTP code and log.
         r = admin.log_and_set_http_code(200, 4, req_method, None, full_uri)
     # Return content.
     return r
Exemplo n.º 7
0
 def serve_get_uri(self, req_method, full_uri, script_url, qs):
     # Init.
     r = {}
     e = []
     t1 = None
     t2 = None
     request_array = []
     meta = []
     a_segs = []
     k_segs = []
     i_hist = []
     arg = []
     # Instantiate objects.
     admin = Admin.AdminHandle()
     dao = DAO.DAOHandle()
     # If trailing slash found.
     if admin.check_for_last_str_char(full_uri, '/'):
         # Set HTTP code and log.
         e = admin.log_and_set_http_code(400, 16, req_method, None,
                                         full_uri)
     # Otherwise, trailing slash not found at end of URI.
     else:
         # Get server request timestamp.
         server_start_time = time.time()
         # Split the script URL to a list.
         o = script_url.split('/')
         # Filter out empty values.
         f = filter(None, o)
         # Get list size.
         l = len(f)
         # Start serving requests.
         if f[0] == 'dq':
             # Get ifo list.
             if l == 1:
                 r = dao.get_value_list(1, req_method, full_uri)
                 # If list not supplied.
                 if not r:
                     # Set HTTP code and log.
                     e = admin.log_and_set_http_code(
                         200, 21, req_method, None, full_uri)
             # Otherwise, prepare more complex requests.
             elif l > 1:
                 ifo = f[1]
                 ifo_id = dao.get_value_details(1, ifo, req_method,
                                                full_uri)
                 # Check IFO exists in database.
                 if ifo_id == None:
                     # Set HTTP code and log.
                     e = admin.log_and_set_http_code(
                         404, 5, req_method, None, full_uri)
                 else:
                     # Get flag list.
                     if l == 2:
                         r = dao.get_flag_list(ifo, req_method, full_uri)
                         # If list not supplied.
                         if not r:
                             # Set HTTP code and log.
                             admin.log_and_set_http_code(
                                 200, 22, req_method, None, full_uri)
                     # Otherwise, prepare more complex requests.
                     elif l > 2:
                         flag = f[2]
                         flag_id = dao.get_flag_id(ifo_id, flag, req_method,
                                                   full_uri)
                         # If flag does not exist in database.
                         if flag_id == None:
                             # Set HTTP code and log.
                             e = admin.log_and_set_http_code(
                                 404, 8, req_method, None, full_uri)
                         else:
                             # If three URI array elements supplied.
                             if l == 3:
                                 # Get version list.
                                 v_l = dao.get_flag_version_list(
                                     ifo, flag, req_method, full_uri)
                                 # If list not supplied.
                                 if not v_l:
                                     # Set HTTP code and log.
                                     e = admin.log_and_set_http_code(
                                         409, 38, req_method, None,
                                         full_uri)
                                 # Otherwise.
                                 else:
                                     # Put list into response dictionary.
                                     r = admin.put_version_list_into_dict(
                                         v_l)
                             # Otherwise, get version and segment info. Ensuring URI is not ended with a trailing slash.
                             elif l == 4:
                                 version = f[3]
                                 # If query string being passed.
                                 if qs:
                                     arg = parse_qs(
                                         urlparse(full_uri).query)
                                     #                               print 'Args as tuples: [%s]' % ', '.join(map(str, arg))
                                     # If include exists.
                                     try:
                                         arg['include']
                                     except:
                                         pass
                                     else:
                                         request_array = arg['include'][
                                             0].split(',')
                                     # Get t1 and t2.
                                     try:
                                         arg['s'][0]
                                     except:
                                         pass
                                     else:
                                         t1 = arg['s'][0]
                                     try:
                                         arg['e'][0]
                                     except:
                                         pass
                                     else:
                                         t2 = arg['e'][0]
                                 # Get version ID.
                                 version_id = dao.get_flag_version_id(
                                     flag_id, version, req_method, full_uri)
                                 # If version ID found.
                                 if version_id == None:
                                     # Set HTTP code and log.
                                     e = admin.log_and_set_http_code(
                                         404, 10, req_method, None,
                                         full_uri)
                                 else:
                                     # If metadata or everything included.
                                     if 'metadata' in request_array or not request_array:
                                         # Get metadata.
                                         meta = dao.get_flag_version_metadata(
                                             'metadata', ifo, flag, version,
                                             version_id, req_method,
                                             full_uri)
                                         # If metadata not built.
                                         if not meta:
                                             # Set HTTP code and log.
                                             e = admin.log_and_set_http_code(
                                                 404, 24, req_method, None,
                                                 full_uri)
                                     # If 'active' segments or everything included.
                                     if 'active' in request_array or not request_array:
                                         # Get segments.
                                         a_segs = dao.get_flag_version_segments(
                                             'active', version_id, t1, t2,
                                             req_method, full_uri)
                                     # If 'known' segments or everything included.
                                     if 'known' in request_array or not request_array:
                                         # Get segments.
                                         k_segs = dao.get_flag_version_segments(
                                             'known', version_id, t1, t2,
                                             req_method, full_uri)
                                     # If insert history or everything included.
                                     if 'insert_history' in request_array or not request_array:
                                         # Get insert history.
                                         i_hist = dao.get_flag_version_insert_history(
                                             version_id, req_method,
                                             full_uri)
                                         # If insert history not built.
                                         if not i_hist:
                                             # Set HTTP code and log.
                                             e = admin.log_and_set_http_code(
                                                 404, 25, req_method, None,
                                                 full_uri)
                                     # Set segments as result so far, with any insert history that has been set.
                                     r.update(meta)
                                     r.update(k_segs)
                                     r.update(a_segs)
                                     r.update(i_hist)
                             # Otherwise, request is too long.
                             elif l > 4:
                                 # Set HTTP code and log.
                                 e = admin.log_and_set_http_code(
                                     414, 19, req_method, None, full_uri)
         # Reports.
         elif f[0] == 'report':
             # Get report options.
             if l == 1:
                 r = admin.get_available_resources(req_method, full_uri)
                 # If dictionary not supplied.
                 if not r:
                     # Set HTTP code and log.
                     e = admin.log_and_set_http_code(
                         404, 26, req_method, None, full_uri)
             # Otherwise, prepare more complex requests. Ensuring URI is not ended with a trailing slash.
             elif l > 1:
                 request = f[1]
                 if l == 2:
                     # If request is flags.
                     if request == 'flags':
                         # Get list of all flags.
                         r = dao.get_flags_with_versions_for_report(
                             req_method, full_uri)
                         # If dictionary not supplied.
                         if not r:
                             # Set HTTP code and log.
                             admin.log_and_set_http_code(
                                 200, 27, req_method, None, full_uri)
                     # If request is coverage.
                     elif request == 'coverage':
                         # Get list of all flags.
                         r = dao.get_flag_version_coverage(
                             req_method, full_uri)
                         # If dictionary not supplied.
                         if not r:
                             # Set HTTP code and log.
                             admin.log_and_set_http_code(
                                 200, 42, req_method, None, full_uri)
                     # If request is db.
                     elif request == 'db':
                         # Get DB-related statistics.
                         r = admin.get_db_statistics_payload(
                             None, req_method, full_uri)
                         # If dictionary not supplied.
                         if not r:
                             # Set HTTP code and log.
                             admin.log_and_set_http_code(
                                 200, 39, req_method, None, full_uri)
                     # If request within acceptable range, i.e. 'active', 'known', etc., get list of all flags over period requested by args.
                     elif admin.check_request(
                             'seg',
                             request) == False and not request == 'all':
                         # Set HTTP code and log.
                         e = admin.log_and_set_http_code(
                             404, 11, req_method, None, full_uri)
                     # Otherwise, it must be 'known' or 'active'.
                     else:
                         # If query string being passed.
                         if qs:
                             arg = parse_qs(urlparse(full_uri).query)
                             # If include exists.
                             try:
                                 arg['include']
                             except:
                                 pass
                             else:
                                 request_array = arg['include'][0].split(
                                     ',')
                             # Get t1 and t2.
                             try:
                                 arg['s'][0]
                             except:
                                 pass
                             else:
                                 t1 = arg['s'][0]
                             try:
                                 arg['e'][0]
                             except:
                                 pass
                             else:
                                 t2 = arg['e'][0]
                         # Get report segments.
                         r = dao.get_report_segments(
                             request, t1, t2, request_array, req_method,
                             full_uri)
                         # If no segments supplied.
                         if not r:
                             # Set HTTP code and log.
                             admin.log_and_set_http_code(
                                 200, 28, req_method, None, full_uri)
                 # Otherwise, if handling DB-statistics request.
                 elif l == 3:
                     # Report error if not in report.
                     if not request == 'db':
                         # Set HTTP code and log.
                         admin.log_and_set_http_code(
                             200, 13, req_method, None, full_uri)
                     else:
                         # Get IFO id.
                         ifo = f[2]
                         ifo_id = dao.get_value_details(
                             1, ifo, req_method, full_uri)
                         # Check IFO exists in database.
                         if ifo_id == None:
                             # Set HTTP code and log.
                             e = admin.log_and_set_http_code(
                                 404, 5, req_method, None, full_uri)
                         else:
                             # Get DB-related statistics for this IFO.
                             r = admin.get_db_statistics_payload(
                                 ifo_id, req_method, full_uri)
                 # Otherwise, request is too long.
                 elif l > 3:
                     # Set HTTP code and log.
                     e = admin.log_and_set_http_code(
                         414, 19, req_method, None, full_uri)
     # If results have been found and there are no errors.
     if r and not e:
         # Add query info to results.
         r.update(
             admin.add_query_info_to_flag_resource(full_uri, r, t1, t2,
                                                   request_array,
                                                   server_start_time))
         # Encode final dictionary to JSON.
         r = json.dumps(r)
         # Incorporate into list to pass back to application.
         r = ['200 OK', r]
         # Set HTTP code and log.
         admin.log_and_set_http_code(200, 4, req_method, None, full_uri)
     # Otherwise, if errors have been found.
     elif e:
         # Set the reply as an error.
         r = e
     # Otherwise, complete error.
     else:
         # Set HTTP code and log.
         r = admin.log_and_set_http_code(404, 20, req_method, None,
                                         full_uri)
     # Return content.
     return r
Exemplo n.º 8
0
 def check_flag_version_boundaries(self, run_id, ak):
     # Init.
     l = 'Failure'
     n = ak + ' flag version boundary check'
     m = ''
     r = 1
     defaut_earliest_boundary = 999999999999999
     # Log event.
     log.info('Starting ' + n)
     # Instantiate.
     constant = Constants.ConstantsHandle()
     dao = DAO.DAOHandle()
     util = Utils.UtilHandle()
     # Set call URI.
     uri = constant.dqsegdb_host + '/report/coverage'
     # Attempt to open url and convert JSON payload to dictionary..
     try:
         reply = util.convert_uri_json_response_to_dict(n, uri)
     except:
         # Log event.
         m = 'Unable to convert payload returned by ' + n + ' ' + uri + ' to dictionary'
         log.error(m)
     else:
         # If payload reply is not good.
         if not reply['r'] == True:
             # Log event.
             m = 'Problem in payload conversion ' + n + ' ' + uri + ' to dictionary'
             log.error(m)
         else:
             m = reply['m']
             d = reply['d']
             # If the Ifos key is not available in the dictionary.
             if not 'results' in d:
                 # Log event.
                 m = 'results key not available in payload returned by ' + n + ' ' + uri + ' to dictionary'
                 log.error(m)
             # Otherwise, if key exists.
             else:
                 # Set error list.
                 l_error = []
                 # Loop through and get flags, versions and totals.
                 for f_v, totals in d['results'].iteritems():
                     # Set flag-version URI.
                     f_v_uri = constant.dqsegdb_host + f_v + '?include=' + ak
                     # Check resource returns response.
                     try:
                         reply_b = util.convert_uri_json_response_to_dict(
                             n, f_v_uri)
                     except:
                         # Log event.
                         m = 'Unable to convert payload returned by ' + n + ' ' + f_v_uri + ' to dictionary'
                         log.error(m)
                     else:
                         # If payload reply is not good.
                         if not reply_b['r'] == True:
                             # Append to error list.
                             l_error.append(
                                 f_v_uri +
                                 ' - problem with returned payload')
                         else:
                             # Set first start/stop boundaries
                             earliest_boundary = defaut_earliest_boundary
                             latest_boundary = 0
                             # Loop through segments in list.
                             for start, stop in reply_b['d'][ak]:
                                 # If start lower than earliest boundary set so far.
                                 if start < earliest_boundary:
                                     # Reset earliest boundary.
                                     earliest_boundary = start
                                 # If stop higher than latest boundary set so far.
                                 if stop < latest_boundary:
                                     # Reset earliest boundary.
                                     latest_boundary = stop
                             # If the earliest boundary hasn't changed from when first set.
                             if earliest_boundary == defaut_earliest_boundary:
                                 # Set the earliest boundary to zero.
                                 earliest_boundary = 0
                             # If earliest boundary different to coverage totals.
                             if not earliest_boundary == totals[
                                     'earliest_' + ak + '_segment']:
                                 # Append to error list.
                                 l_error.append(
                                     f_v_uri + ' - ' + ak +
                                     ' boundaries are incongruent - DB: ' +
                                     str(totals['earliest_' + ak +
                                                '_segment']) + '; JSON: ' +
                                     str(earliest_boundary))
                             print f_v + ' - ' + ak + '; DB: ' + str(
                                 totals['earliest_' + ak + '_segment']
                             ) + '; JSON: ' + str(earliest_boundary)
                 # If reply error messages have been set.
                 if l_error:
                     m = n + ' produced failures. Incorrect responses for following URI: ' + str(
                         l_error)
                     log.error(m)
                 # Otherwise, error messages returned.
                 else:
                     # Log event.
                     m = n + ' completed successfully.'
                     log.info(m)
                     # Set.
                     l = 'Success'
                     r = 0
     # Write result to database.
     dao.insert_test_result(run_id, n, l, m)
     # Return.
     return r
Exemplo n.º 9
0
 def check_flag_version_coverage(self, run_id, ak):
     # Init.
     l = 'Failure'
     n = ak + ' flag version coverage check'
     m = ''
     r = 1
     # Log event.
     log.info('Starting ' + n)
     # Instantiate.
     constant = Constants.ConstantsHandle()
     dao = DAO.DAOHandle()
     util = Utils.UtilHandle()
     # Set call URI.
     uri = constant.dqsegdb_host + '/report/coverage'
     # Attempt to open url and convert JSON payload to dictionary..
     try:
         reply = util.convert_uri_json_response_to_dict(n, uri)
     except:
         # Log event.
         m = 'Unable to convert payload returned by ' + n + ' ' + uri + ' to dictionary'
         log.error(m)
     else:
         # If payload reply is not good.
         if not reply['r'] == True:
             # Log event.
             m = 'Problem in payload conversion ' + n + ' ' + uri + ' to dictionary'
             log.error(m)
         else:
             m = reply['m']
             d = reply['d']
             # If the Ifos key is not available in the dictionary.
             if not 'results' in d:
                 # Log event.
                 m = 'results key not available in payload returned by ' + n + ' ' + uri + ' to dictionary'
                 log.error(m)
             # Otherwise, if key exists.
             else:
                 # Set error list.
                 l_error = []
                 # Loop through and get flags, versions and totals.
                 for f_v, totals in d['results'].iteritems():
                     # Set flag-version URI.
                     f_v_uri = constant.dqsegdb_host + f_v + '?include=' + ak
                     # Check resource returns response.
                     try:
                         reply_b = util.convert_uri_json_response_to_dict(
                             n, f_v_uri)
                     except:
                         # Log event.
                         m = 'Unable to convert payload returned by ' + n + ' ' + f_v_uri + ' to dictionary'
                         log.error(m)
                     else:
                         # If payload reply is not good.
                         if not reply_b['r'] == True:
                             # Append to error list.
                             l_error.append(
                                 f_v_uri +
                                 ' - problem with returned payload')
                         else:
                             # Count active/known segments in payload.
                             tot = len(reply_b['d'][ak])
                             # If counted active/known total different to coverage totals.
                             if not tot == totals['total_' + ak +
                                                  '_segments']:
                                 # Append to error list.
                                 l_error.append(
                                     f_v_uri + ' - ' + ak +
                                     ' totals are incongruent - DB: ' +
                                     str(totals['total_' + ak +
                                                '_segments']) + '; JSON: ' +
                                     str(tot))
                             print f_v + ' - ' + ak + '; DB: ' + str(totals[
                                 'total_' + ak +
                                 '_segments']) + '; JSON: ' + str(tot)
                 # If reply error messages have been set.
                 if l_error:
                     m = n + ' produced failures. Incorrect responses for following URI: ' + str(
                         l_error)
                     log.error(m)
                 # Otherwise, error messages returned.
                 else:
                     # Log event.
                     m = n + ' completed successfully.'
                     log.info(m)
                     # Set.
                     l = 'Success'
                     r = 0
     # Write result to database.
     dao.insert_test_result(run_id, n, l, m)
     # Return.
     return r
Exemplo n.º 10
0
 def check_flag_versions(self, run_id):
     # Init.
     l = 'Failure'
     n = 'flag version check'
     m = ''
     r = 1
     # Log event.
     log.info('Starting ' + n)
     # Instantiate.
     constant = Constants.ConstantsHandle()
     dao = DAO.DAOHandle()
     util = Utils.UtilHandle()
     # Set call URI.
     uri = constant.dqsegdb_host + '/report/flags'
     # Attempt to open url and convert JSON payload to dictionary..
     try:
         reply = util.convert_uri_json_response_to_dict(n, uri)
     except:
         # Log event.
         m = 'Unable to convert payload returned by ' + n + ': ' + uri + ' to dictionary'
         log.error(m)
     else:
         # If payload reply is not good.
         if not reply['r'] == True:
             # Log event.
             m = 'Problem in payload conversion ' + n + ': ' + uri + ' to dictionary'
             log.error(m)
         else:
             m = reply['m']
             d = reply['d']
             # If the Ifos key is not available in the dictionary.
             if not 'results' in d:
                 # Log event.
                 m = 'results key not available in payload returned by ' + n + ': ' + uri + ' to dictionary'
                 log.error(m)
             # Otherwise, if key exists.
             else:
                 # Set error list.
                 l_error = []
                 # Loop through and get flags and versions.
                 for f_v in d['results']:
                     # Set flag-version URI.
                     f_v_uri = constant.dqsegdb_host + f_v + '?include=metadata'
                     # Check resource returns response.
                     try:
                         reply = util.convert_uri_json_response_to_dict(
                             n, f_v_uri)
                     except:
                         # Log event.
                         m = 'Unable to convert payload returned by ' + n + ': ' + f_v_uri + ' to dictionary'
                         log.error(m)
                     else:
                         # If payload reply is not good.
                         if not reply['r'] == True:
                             # Set reply message
                             l_error.append(f_v_uri)
                 # If reply error messages have been set.
                 if l_error:
                     m = n + ' produced failures. Incorrect responses for following URI: ' + str(
                         l_error)
                     log.error(m)
                 # Otherwise, error messages returned.
                 else:
                     # Log event.
                     m = n + ' completed successfully. Resource returned ' + str(
                         d['results'])
                     log.info(m)
                     # Set.
                     l = 'Success'
                     r = 0
     # Write result to database.
     dao.insert_test_result(run_id, n, l, m)
     # Return.
     return r
Exemplo n.º 11
0
def application(environ, start_response):
    # Instantiate logger.
    constant = Constants.ConstantsHandle()
    logging.basicConfig(filename=constant.log_file_location +
                        time.strftime("%Y-%m-%d", time.localtime()) + '.log',
                        format="%(asctime)s:%(levelname)s:%(message)s",
                        level=logging.DEBUG)
    # Instantiate objects.
    admin = Admin.AdminHandle()
    dao = DAO.DAOHandle()
    reqhan = Request.RequestHandle()
    ldbdwauth = LDBDWAuth.GridmapAuthorization()
    # Set HTTP code and log.
    res = admin.log_and_set_http_code(400, 0, environ['REQUEST_METHOD'], None,
                                      environ['REQUEST_URI'])
    # Connect to DB.
    if dao.connect_to_db(environ['REQUEST_METHOD'], environ['REQUEST_URI']):
        # Respond to a GET request.
        if environ['REQUEST_METHOD'] == 'GET':
            # Authenticate.
            res = ldbdwauth.check_authorization_gridmap(
                environ, environ['REQUEST_METHOD'], environ['REQUEST_URI'],
                False)
            # If authentication successful.
            if res[0] == 200:
                # Get content for output.
                res = reqhan.serve_get_uri(environ['REQUEST_METHOD'],
                                           environ['REQUEST_URI'],
                                           environ['PATH_INFO'],
                                           environ['QUERY_STRING'])
        # Respond to a PUT request.
        elif environ['REQUEST_METHOD'] == 'PUT' or environ[
                'REQUEST_METHOD'] == 'PATCH':
            # Authorise.
            res = ldbdwauth.check_authorization_gridmap(
                environ, environ['REQUEST_METHOD'], environ['REQUEST_URI'],
                True)
            # If authorisation successful.
            if res[0] == 200:
                # Get the size of the requested JSON.
                try:
                    request_body_size = int(environ.get('CONTENT_LENGTH', 0))
                except:
                    request_body_size = 0
                # Process PUT or PATCH request.
                res = reqhan.serve_put_or_patch_uri(
                    environ['REQUEST_METHOD'], environ['REQUEST_URI'],
                    environ['PATH_INFO'], environ['QUERY_STRING'],
                    environ['wsgi.input'].read(request_body_size))
    # Check first character to check content is Python dictionary converted to JSON.
    if not res[1][:1] == '{':
        # Handle error.
        content_type = 'text/html'
        res[1] = admin.get_http_msg_for_display(res[0], res[1])
    else:
        content_type = 'application/json'
    # Set headers.
    response_headers = [('Content-type', content_type),
                        ('Content-Length', str(len(res[1])))]
    # Start response - Status / Headers.
    start_response(res[0], response_headers)
    # Return.
    return [res[1]]