Authored by 李奇

grafana上报优化

@@ -6,7 +6,7 @@ import {API_HOST, MINI_APP_TYPE} from './libs/config'; @@ -6,7 +6,7 @@ import {API_HOST, MINI_APP_TYPE} from './libs/config';
6 import mta from './vendors/mta_analysis'; 6 import mta from './vendors/mta_analysis';
7 import {Event} from '/login/utils/index.js'; 7 import {Event} from '/login/utils/index.js';
8 import {wxLogin} from '/login/utils/login/login'; 8 import {wxLogin} from '/login/utils/login/login';
9 -import {appReport} from './libs/appReport.js'; 9 +import appReport from './libs/appReport';
10 import {isStringEmpty, getYHStorageSync} from './utils/util'; 10 import {isStringEmpty, getYHStorageSync} from './utils/util';
11 import regeneratorRuntime from '/login/libs/regenerator-runtime/index'; 11 import regeneratorRuntime from '/login/libs/regenerator-runtime/index';
12 12
@@ -144,11 +144,21 @@ App({ @@ -144,11 +144,21 @@ App({
144 } 144 }
145 145
146 if (options && options.scene){ 146 if (options && options.scene){
147 - appReport('start', "LIFECYCLE", "RESUME", {  
148 - 'activeWay': options.scene + ""  
149 - }, this) 147 + appReport({
  148 + reportType: 'start',
  149 + pt: 'LIFECYCLE',
  150 + pn: 'RESUME',
  151 + param: {
  152 + 'activeWay': options.scene + ''
  153 + }
  154 + }, this);
150 }else{ 155 }else{
151 - appReport('start', "LIFECYCLE", "RESUME", {}, this) 156 + appReport({
  157 + reportType: 'start',
  158 + pt: 'LIFECYCLE',
  159 + pn: 'RESUME',
  160 + param: {}
  161 + }, this);
152 } 162 }
153 163
154 let scene = options ? options.scene : ''; 164 let scene = options ? options.scene : '';
@@ -157,9 +167,14 @@ App({ @@ -157,9 +167,14 @@ App({
157 167
158 //当应用程序进入后台状态时触发 168 //当应用程序进入后台状态时触发
159 onHide () { 169 onHide () {
160 - let params = {};  
161 - yasReport(YB_ENTER_BACKGROUND,params);  
162 - appReport('start', "LIFECYCLE", "STOP", {},this) 170 + let params = {};
  171 + yasReport(YB_ENTER_BACKGROUND, params);
  172 + appReport({
  173 + reportType: 'start',
  174 + pt: 'LIFECYCLE',
  175 + pn: 'STOP',
  176 + param: {}
  177 + }, this);
163 }, 178 },
164 179
165 getUserInfo: function () { 180 getUserInfo: function () {
@@ -468,10 +483,14 @@ App({ @@ -468,10 +483,14 @@ App({
468 } else { 483 } else {
469 net = '0'; 484 net = '0';
470 } 485 }
471 - // console.log("network:",net)  
472 }, 486 },
473 complete:function(res){ 487 complete:function(res){
474 - appReport('start', "LIFECYCLE", "START", {}, that) 488 + appReport({
  489 + reportType: 'start',
  490 + pt: 'LIFECYCLE',
  491 + pn: 'START',
  492 + param: {}
  493 + }, that);
475 } 494 }
476 }) 495 })
477 that.globalData.networkType = net; 496 that.globalData.networkType = net;
1 -'use strict'  
2 -import { APP_REPORT_HOST, APP_VERSION, APP_BUILD} from '../libs/config';  
3 -import { APP_REPORT } from '../libs/request';  
4 import md5 from '../vendors/md5'; 1 import md5 from '../vendors/md5';
  2 +import Promise from '../vendors/es6-promise';
  3 +import { APP_REPORT_HOST, APP_VERSION, APP_BUILD } from '../libs/config';
5 4
6 let getDeviceInfo = function (app) { 5 let getDeviceInfo = function (app) {
7 - let ak = 'yoholuck_mp';// 6 + let ak = 'yoholuck_mp'; // 小程序标识
8 7
9 let systemInfo = app.globalData.systemInfo 8 let systemInfo = app.globalData.systemInfo
10 let device = {} 9 let device = {}
@@ -12,12 +11,12 @@ let getDeviceInfo = function (app) { @@ -12,12 +11,12 @@ let getDeviceInfo = function (app) {
12 let union_type = app.getUnion_type(); 11 let union_type = app.getUnion_type();
13 device.ak = ak 12 device.ak = ak
14 device.udid = app.globalData.udid ? app.globalData.udid : ''; 13 device.udid = app.globalData.udid ? app.globalData.udid : '';
15 - device.ch = union_type ? union_type: app.globalData.ch; 14 + device.ch = union_type ? union_type : app.globalData.ch;
16 device.os = "weixinapp" 15 device.os = "weixinapp"
17 - device.osv = systemInfo && systemInfo.version ? systemInfo.version:''  
18 - device.dm = systemInfo && systemInfo.model ? systemInfo.model.replace(' ', '_'):'' 16 + device.osv = systemInfo && systemInfo.version ? systemInfo.version : ''
  17 + device.dm = systemInfo && systemInfo.model ? systemInfo.model.replace(' ', '_') : ''
19 18
20 - if (systemInfo.platform == 'ios'){ 19 + if (systemInfo.platform == 'ios') {
21 //针对ios设备,特殊处理dm,转为iphone_7这种格式 20 //针对ios设备,特殊处理dm,转为iphone_7这种格式
22 var start_num = device.dm.indexOf('<') 21 var start_num = device.dm.indexOf('<')
23 var end_num = device.dm.indexOf('>') 22 var end_num = device.dm.indexOf('>')
@@ -34,57 +33,68 @@ let getDeviceInfo = function (app) { @@ -34,57 +33,68 @@ let getDeviceInfo = function (app) {
34 return device; 33 return device;
35 } 34 }
36 35
37 -let appReport = function(reportType, pt, pn, param,appObject) {  
38 - let app = appObject; 36 +let appReport = function ({ reportType = 'hb', pt, pn, param = {} } = {}, _this) {
  37 + let app = _this || getApp();
39 38
40 let deviceInfo = getDeviceInfo(app) 39 let deviceInfo = getDeviceInfo(app)
41 if (!pt || !pn || !deviceInfo || !deviceInfo.dm) return; 40 if (!pt || !pn || !deviceInfo || !deviceInfo.dm) return;
42 - if(!param)param = {};  
43 - if(!reportType)reportType='hb'; 41 + if (!reportType) reportType = 'hb';
44 42
45 let ts = new Date().getTime() + '000000'; 43 let ts = new Date().getTime() + '000000';
46 let uid = app && app.globalData && app.globalData.userInfo && app.globalData.userInfo.uid ? md5(app.globalData.userInfo.uid) : ''; 44 let uid = app && app.globalData && app.globalData.userInfo && app.globalData.userInfo.uid ? md5(app.globalData.userInfo.uid) : '';
47 45
48 - if(!uid){ 46 + if (!uid) {
49 uid = md5(app.getUid()); 47 uid = md5(app.getUid());
50 } 48 }
51 49
  50 + let event = {};
52 let sid = app && app.globalData && app.globalData.sid ? app.globalData.sid : ''; 51 let sid = app && app.globalData && app.globalData.sid ? app.globalData.sid : '';
53 -  
54 let networkType = app && app.globalData && app.globalData.networkType ? app.globalData.networkType : '0'; 52 let networkType = app && app.globalData && app.globalData.networkType ? app.globalData.networkType : '0';
55 53
56 - let event = {}  
57 - event.ps = '0',  
58 - event.av = APP_VERSION,  
59 - event.ab = APP_BUILD,  
60 - event.ca = '0',  
61 - event.net = networkType,  
62 - event.sid = sid,  
63 - event.uid = uid,  
64 - event.ts = ts,  
65 - event.pt = pt,  
66 - event.pn = pn,  
67 - event.param = param 54 + event.ps = '0';
  55 + event.av = APP_VERSION;
  56 + event.ab = APP_BUILD;
  57 + event.ca = '0';
  58 + event.net = networkType;
  59 + event.sid = sid;
  60 + event.uid = uid;
  61 + event.ts = ts;
  62 + event.pt = pt;
  63 + event.pn = pn;
  64 + event.param = param;
68 65
69 let events = []; 66 let events = [];
70 events.push(event) 67 events.push(event)
71 68
72 //组装最终要上报的数据 69 //组装最终要上报的数据
73 let parameters = {}; 70 let parameters = {};
74 - parameters.type = reportType,  
75 - parameters.device = deviceInfo,  
76 - parameters.events = events,  
77 -  
78 -  
79 - APP_REPORT(APP_REPORT_HOST, parameters) 71 + parameters.type = reportType;
  72 + parameters.device = deviceInfo;
  73 + parameters.events = events;
  74 +
  75 + let _appReport = function (url, params = {}) {
  76 + return new Promise(function (resolve, reject) {
  77 + let app = getApp();
  78 + let data = JSON.stringify(params)
  79 + let sid = app && app.globalData && app.globalData.sid ? app.globalData.sid : '';
  80 + let header = {
  81 + 'content-type': "application/json",
  82 + 'x-yoho-sid': md5(sid),
  83 + }
  84 + wx.request({
  85 + url,
  86 + data,
  87 + header,
  88 + method: 'POST'
  89 + });
  90 + })
  91 + };
  92 + _appReport(APP_REPORT_HOST, parameters)
80 .then(function (data) { 93 .then(function (data) {
81 - // console.log(data)  
82 }) 94 })
83 .catch(function (error) { 95 .catch(function (error) {
84 }); 96 });
85 97
86 } 98 }
87 99
88 -module.exports = {  
89 - appReport,  
90 -}  
  100 +export default appReport;
1 { 1 {
2 "description": "项目配置文件。", 2 "description": "项目配置文件。",
3 "setting": { 3 "setting": {
4 - "urlCheck": true, 4 + "urlCheck": false,
5 "es6": true, 5 "es6": true,
6 "postcss": true, 6 "postcss": true,
7 "minified": true, 7 "minified": true,