def query(self): if self._result is not None: return self._result es = self.get_es() trc = TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at) rc = RangeClause( 'backend_status_code', min_val=self.status_code_class, max_val=self.status_code_class + 100 ) body = { 'filter': { 'bool': { 'filter': [ trc.get_clause(), rc.get_clause() ] } } } result = es.count(index=self.get_index_name(), body=body) logger.info(result) self._result = result.get('count', 0) return self._result
def test_when_min_val_and_max_val(self): rc = RangeClause('a_field', min_val=1, max_val=10) self.assertEqual(rc.get_clause(), {'range': { 'a_field': { 'gte': 1, 'lt': 10 } }})
def test_call(self): self.assertEqual(self.sccq.query(), 1) trc = TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at) rc = RangeClause('backend_status_code', min_val=200, max_val=300) self.mock_es.count.assert_called_with( index='logstash-2016.01.01', body={ 'filter': { 'bool': {'filter': [trc.get_clause(), rc.get_clause()]} } } )
def query(self): if self._result is not None: return self._result field = 'backend_processing_time' conditions = [ TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at), ExistClause('rails.controller#action'), TermClause('domain_name', 'api.thekono.com'), RangeClause(field, 0) ] body = { 'query': { 'bool': { 'filter': [c.get_clause() for c in conditions] } }, 'size': 0, 'aggs': { 'avg_resp_time': { 'avg': { 'field': field } } } } result = self.get_es().search(index=self.get_index_name(), body=body) logger.info(result) self._result = result['aggregations']['avg_resp_time']['value'] return self._result
def get_satisfied_request_count(self): trc = TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at) rc = RangeClause( 'backend_processing_time', max_val=self.apdex_threshold) body = { 'filter': { 'bool': { 'filter': [trc.get_clause(), rc.get_clause()] } } } result = self.get_es().count(index=self.get_index_name(), body=body) logger.info('satisifed: %s', result) return result.get('count', 0)
def query(self): if self._result is not None: return self._result conds = [ TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at), ExistClause('rails.controller#action'), TermClause('domain_name', 'api.thekono.com'), RangeClause('backend_processing_time', 0), RangeClause('backend_status_code', min_val=self.status_code_class, max_val=self.status_code_class + 100) ] body = {'query': {'bool': {'filter': [c.get_clause() for c in conds]}}} result = self.get_es().count(index=self.get_index_name(), body=body) logger.info(result) self._result = result.get('count', 0) return self._result
def query_server_error_records(begin_time, end_time): es_api = Elasticsearch(setting.get('elasticsearch', 'url')) indices = [ index_name(begin_time.date() + timedelta(n)) for n in xrange((end_time.date() - begin_time.date()).days) ] begin_at = timegm(begin_time.timetuple()) * 1000 end_at = timegm(end_time.timetuple()) * 1000 index = ','.join(indices) if indices else index_name(begin_time.date()) offset = 0 results = [] body = { 'query': { 'bool': { 'filter': [ RangeClause('timestamp', begin_at, end_at).get_clause(), RangeClause('elb_status_code', 500).get_clause(), # Filter out /robots.txt requests ExistClause('rails.controller#action').get_clause(), # Filter out https://52.197.62.134 requests TermClause('domain_name', 'api.thekono.com').get_clause() ] } } } while True: body['from'] = offset args = dict(index=index, body=body, sort='timestamp:asc', size=100) result = es_api.search(**args) logger.debug(result) hits = result.get('hits', {}).get('hits', []) if not hits: break results.extend([h['_source'] for h in hits]) offset += len(hits) return results
def get_tolerating_request_count(self): conditions = [ TimeRangeClause(begin_time=self.begin_at, end_time=self.end_at), ExistClause('rails.controller#action'), TermClause('domain_name', 'api.thekono.com'), RangeClause('backend_processing_time', min_val=self.apdex_threshold, max_val=self.apdex_threshold * 4) ] body = {'query': {'bool': {'filter': []}}} body['query']['bool']['filter'] = [c.get_clause() for c in conditions] result = self.get_es().count(index=self.get_index_name(), body=body) logger.info('tolerating: %s', result) return result.get('count', 0)
def test_when_only_max_val(self): rc = RangeClause('a_field', max_val=10) self.assertEqual(rc.get_clause(), {'range': {'a_field': {'lt': 10}}})
def test_when_only_min_val(self): rc = RangeClause('a_field', min_val=1) self.assertEqual(rc.get_clause(), {'range': {'a_field': {'gte': 1}}})