前端日志监控 - Nodejs 通过 ElasticSearch 查询数据流程

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

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张驰Terry

知识源于创作热情,感谢你的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值