전단 로그 모니터링 - Nodejs Elastic Search 를 통 해 데이터 프로 세 스 조회

1. es 클 라 이언 트 연결 만 들 기
var elasticsearch = require('elasticsearch');
const Service = require('egg').Service;

let esClient;
class EsBaseService extends Service {
   getEsHost() {
       let esHost = '172.21.101.22:9200';
       if (this.ctx.helper.isPro()) {
           esHost = '172.2.101.2:93200';
       }
       return esHost
   }

   getEsClient(argument) {
       if (!esClient) {
           this.ctx.logger.info('esClient init' + this.getEsHost())
           esClient = new elasticsearch.Client({
               host: this.getEsHost(),
               log: 'info'
           });
       }
       return esClient;
   }

   searchBucketsAndCountFromFsBugInfo(opts) {
       return new Promise((resolve, reject) => {
           this.searchFromFsBugInfo({
               body: opts.body
           }).then(function (body) {
               let data = {
                   name: opts.name,
                   count: body.hits.total.value,
                   buckets: body.aggregations[2].buckets
               };
               resolve(data)
           });
       });
   }
  
   searchFromFsBugInfo(opts){
       return this.getEsClient().search({
           index: 'fs-bug-info',
           body: opts.body
       });
   }

}
module.exports = EsBaseService;

2. 조회 작업 정의
async getAggr1(opts = {}) {
       let name = '      bug ';
       let dateRange = opts.dateRange;
       let body = {
           "aggs": {
               "2": {
                   "terms": {
                       "field": "team",
                       "size": 100,
                       "order": {
                           "_count": "desc"
                       }
                   }
               }
           },
           "size": 0,
           "_source": {
               "excludes": []
           },
           "stored_fields": ["*"],
           "script_fields": {},
           "query": {
               "bool": {
                   "must": [{
                       "bool": {
                           "must": [{
                               "terms": {
                                   "resolution": ["", "fixed", "hold"]
                               }
                           }],
                           "must_not": [{
                               "terms": {
                                   "status": ["rejected"]
                               }
                           }, {
                               "term": {
                                   "versionReport": {
                                       "value": "  "
                                   }
                               }
                           }, {
                               "term": {
                                   "problemType": {
                                       "value": "  bug"
                                   }
                               }
                           }]
                       }
                   }, {
                       "range": {
                           "created": {
                               "format": "strict_date_optional_time",
                               "gte": dateRange[0],
                               "lte": dateRange[1]
                           }
                       }
                   }, {
                       "bool": {
                           "must": [{
                               "terms": {
                                   "resolution": ["", "fixed", "hold"]
                               }
                           }],
                           "must_not": [{
                               "terms": {
                                   "status": ["rejected"]
                               }
                           }, {
                               "term": {
                                   "versionReport": {
                                       "value": "  "
                                   }
                               }
                           }, {
                               "term": {
                                   "problemType": {
                                       "value": "  bug"
                                   }
                               }
                           }]
                       }
                   }],
                   "filter": [{
                       "match_all": {}
                   }, {
                       "match_all": {}
                   }],
                   "should": [],
                   "must_not": []
               }
           },
           "timeout": "30000ms"
       };

       return this.ctx.service.es.base.searchBucketsAndCountFromFsBugInfo({
           body,
           name
       });
   }

let data1 = await this.getAggr1(opts);


참조 링크:
https://www.npmjs.com/package/elasticsearch
https://is-log-kibana.corp.kuaishou.com/app/kibana#/dev_tools/console

좋은 웹페이지 즐겨찾기