def process_event_query(self): """ This is the main function in this class. This calls several functions to validate input, set match clauses, set filter clauses set sort clauses and finally resolve any nested documents if needed. Finally this function returns the data as a json or json_lines joined by a '\n' """ valid_input = self.validate_input() if not valid_input: err_json = dict() err_json['message'] = "Please enter valid query params. Fields must exist for the given project. " \ "If not sure, please access http://mydigurl/projects/<project_name>/fields " \ "API for reference" return rest.bad_request(err_json) try: resp = self.cquery.process() if resp[1] == 400: logger.warning( "Request generated 4xx response. Check request again") return resp else: resp = resp[0] if resp is not None and len( resp['aggregations'][self.field]['buckets']) > 0: if "." not in self.field and self.config[ self.field]['type'] == "date": ts, dims = DigOutputProcessor( resp['aggregations'][self.field], self.agg_field, True).process() ts_obj = TimeSeries( ts, {}, dims, percent_change=self.percent_change, impute_method=self.impute_method).to_dict() else: ts, dims = DigOutputProcessor( resp['aggregations'][self.field], self.agg_field, False).process() ts_obj = TimeSeries( ts, {}, dims, percent_change=self.percent_change, impute_method=self.impute_method).to_dict() return rest.ok(ts_obj) else: return rest.not_found("No Time series found for query") except Exception as e: logger.exception( "Exception encountered while performing Event query") return rest.internal_error("Internal Error occured")
def setUp(self): str_ts1 = """[["2012-08-01T00:00:00.000Z",1,null],["2012-09-01T00:00:00.000Z",1,416.1], ["2012-10-01T00:00:00.000Z",1,426.4],["2012-11-01T00:00:00.000Z",1,450], ["2012-12-01T00:00:00.000Z",1,472.5],["2013-01-01T00:00:00.000Z",1,121.6], ["2013-02-01T00:00:00.000Z",null],["2013-03-01T00:00:00.000Z",1,129.9], ["2013-04-01T00:00:00.000Z",1,127.2],["2013-05-01T00:00:00.000Z",1,127],["2013-06-01T00:00:00.000Z",1,122.3], ["2013-07-01T00:00:00.000Z",1,119.8],["2013-08-01T00:00:00.000Z",1,119.1]]""" self.ts_obj1 = TimeSeries(json.loads(str_ts1), None, None, impute_method='previous') self.ts_obj2 = TimeSeries(json.loads(str_ts1), None, None, impute_method='average')
def test_percent_change_2(self): ts1 = [['a', 0.5], ['b', 0.25], ['c', 1], ['d', None], ['e', 4.75]] ts_obj1 = TimeSeries(ts1, None, None, percent_change=True) ts = ts_obj1.ts expected_ts = [["b", -50.0], ["c", 300.0], ["d", 0.0], ["e", 375.0]] self.assertTrue(len(ts) == 4) for i in range(len(ts)): self.assertEqual(ts[i][len(ts[i]) - 1], expected_ts[i][len(expected_ts[i]) - 1])
def test_percent_change_3(self): ts1 = [['a', 0.5], ['b', 0.25], ['c', 1], ['d', None], ['e', 4.75]] ts_obj1 = TimeSeries(ts1, None, None, percent_change=True, impute_method='average') ts = ts_obj1.ts print ts expected_ts = [["b", -50.0], ["c", 300.0], ["d", 187.5], ["e", 65.21739130434783]] self.assertTrue(len(ts) == 4) for i in range(len(ts)): self.assertEqual(ts[i][len(ts[i]) - 1], expected_ts[i][len(expected_ts[i]) - 1])
def process_ts_query(self): """ This is the main function in this class. This calls several functions to validate input, set match clauses, set filter clauses set sort clauses and finally resolve any nested documents if needed. Finally this function returns the data as a json or json_lines joined by a '\n' """ valid_input = self.validate_input() if not valid_input: err_json = dict() err_json['message'] = "Please enter valid query params. Fields must exist for the given project. " \ "If not sure, please access http://mydigurl/projects/<project_name>/fields API for " \ "reference" return rest.bad_request(err_json) resp = self.cquery.process() if resp is None or resp[1] == 400: return resp try: resp = resp[0] if len(resp['hits']['hits']) > 0 and 'doc_id' in resp['hits'][ 'hits'][0]['_source'].keys(): docid = resp['hits']['hits'][0]['_source']['doc_id'] argmap = dict() argmap['measure/value'] = docid argmap['_group-by'] = 'event_date' argmap['_interval'] = self.agg self.myargs = argmap newquery = ConjunctiveQueryProcessor(self.project_name, self.config, self.project_root_name, self.es, myargs=self.myargs) resp = newquery.process() if resp[1] == 400: return resp else: resp = resp[0] logger.debug("Response for query is {}".format(resp)) isDateAggregation = True if "." not in self.field and self.config[ self.field]['type'] == "date" else False ts, dims = DigOutputProcessor(resp['aggregations'][self.field], self.agg_field, isDateAggregation).process() ts_obj = TimeSeries( ts, dict(), dims, percent_change=self.percent_change, impute_method=self.impute_method).to_dict() return rest.ok(ts_obj) else: return rest.not_found("Time series not found") except Exception as e: logger.exception( "Exception encountered while performing time series query") return rest.bad_request("Enter valid query")
def process_ts_query(self): ''' This is the main function in this class. This calls several functions to validate input, set match clauses, set filter clauses set sort clauses and finally resolve any nested documents if needed. Finally this function returns the data as a json or json_lines joined by a '\n' ''' # valid_input = self.validate_input() # if not valid_input: # err_json = {} # err_json['message'] = "Please enter valid query params. Fields must exist for the given project. If not sure, please access http://mydigurl/projects/<project_name>/fields API for reference" # return rest.bad_request(err_json) resp = self.cquery.process()[0] try: if len(resp['hits']['hits']) > 0 and 'doc_id' in resp['hits'][ 'hits'][0]['_source'].keys(): docid = resp['hits']['hits'][0]['_source']['doc_id'] argmap = {} argmap['measure/value'] = docid argmap['_group-by'] = 'event_date' argmap['_interval'] = self.agg #meta = resp['hits']['hits'][0]['_source']['measure']['metadata'] self.request.args = argmap newquery = ConjunctiveQueryProcessor(self.request, self.project_name, self.config, self.project_root_name, self.es) resp = newquery.process()[0] print resp ts, dims = DigOutputProcessor( resp['aggregations'][self.field]).process() dimensions = [] dimensions.append("DATE") dimensions.append(dims) ts_obj = TimeSeries(ts, dict(), dimensions).to_dict() return rest.ok(ts_obj) else: return rest.not_found("Time series not found") except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) lines = ''.join(lines) print lines return rest.bad_request("Enter valid query for measure document")
def process_event_query(self): ''' This is the main function in this class. This calls several functions to validate input, set match clauses, set filter clauses set sort clauses and finally resolve any nested documents if needed. Finally this function returns the data as a json or json_lines joined by a '\n' ''' valid_input = self.validate_input() if not valid_input: err_json = {} err_json[ 'message'] = "Please enter valid query params. Fields must exist for the given project. If not sure, please access http://mydigurl/projects/<project_name>/fields API for reference" return rest.bad_request(err_json) resp = self.cquery.process()[0] ts, dims = DigOutputProcessor(resp['aggregations'][self.field], self.agg_field).process() ts_obj = TimeSeries(ts, {}, dims).to_dict() return rest.ok(ts_obj)