import { Service , Context } from 'egg' ; const moment = require ( 'moment' ) ; const UUid = require ( 'uuid' ) ; class ReportService extends Service { constructor ( ctx ) { super ( ctx ) ; } param payload param event param id param timestamp param eventLog public async saveDataToEs ( payload ) { return await this . app . curl ( this . app . config . kafkaSubmit , { dataType : 'json' , method : 'POST' , contentType : 'json' , data : { event : 'web-report-collect' , id : UUid . v1 ( ) , timestamp : new Date ( ) . getTime ( ) , eventLog : payload , } , } ) ; } param ip public async getLocation ( ip ) { const isExist = await this . app . elasticsearch . indices . exists ( { index : 'frontend-event-log-web-report-location' , } ) ; if ( isExist ) { return this . getLocationToEs ( ip ) ; } else { return { location : { lng : null , lat : null , } , ad_info : { nation : null , province : null , city : null , adcode : 0 , } , } ; } } param ip public async getLocationToEs ( ip ) { const ipl = this . ctx . helper . subIp ( ip ) ; const res = await this . app . elasticsearch . search ( { index : 'frontend-event-log-web-report-location' , type : '_doc' , body : { query : { match : { ip : ipl , } , } , } , } ) ; if ( res . hits . hits . length > 0 ) { const { location , ad_info } = res . hits . hits [ 0 ] . _source ; if ( ad_info . nation === '中国' && ( ad_info . province === '香港特别行政区' || ad_info . province === '台湾省' || ad_info . province === '澳门特别行政区' ) ) { ad_info . nation = `${ ad_info . nation }${ ad_info . province }` ; } return { location , ad_info } ; } else { return { location : { lng : null , lat : null , } , ad_info : { nation : null , province : null , city : null , adcode : 0 , } , } ; } } param payload async save ( payload ) { return await this . app . elasticsearch . index ( { index : `frontend-event-log-web-report-collect-${ moment ( ) . format ( 'YYYY-MM' ) }` , type : '_doc' , body : payload , } ) ; } } module . exports = ReportService ;