-
Notifications
You must be signed in to change notification settings - Fork 29
/
customerFlowInfo.js
344 lines (299 loc) · 9.33 KB
/
customerFlowInfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* Created by yyy on 2017/5/17.
* to get the data for customerFlow's charts
*/
import {message,Modal} from 'antd';
import pathToRegexp from 'path-to-regexp';
import {getProbeAll, getProbeDetail} from '../../services/probeService';
import {getCustomerFlow, getCustomerFlowDetail,getRealTimeCustomerFlow} from '../../services/customerFlowService';
import cookie from 'react-cookie';
import { routerRedux } from 'dva/router';
export default {
namespace: 'customerFlowInfo',
state: {
probeOptions:[],
hourData:[],
detailData:[],
realTimeData:[],
timer:-1000,
last_num1: 0,
last_num2: 0,
},
subscriptions: {
setup ({ dispatch, history }) {
history.listen(location => {
const matchFlow = pathToRegexp('/customerFlow').exec(location.pathname);
if(matchFlow) {
const currentDate = new Date();
const currentHour = parseInt(currentDate.valueOf()/(1000*60*60));//距离现在最近的整点对应的小时数
const beforeHour = currentHour-5*24;//获取距离最近整点前5天的数据
console.log("currentHour :"+currentHour+";beforehour:"+beforeHour);
dispatch({
type: 'getFlow',
payload: {probeId:"1s12sz",startHour:beforeHour,startRange:5,threshold:"DAY"}
});
dispatch({
type: 'getDetail',
payload: {hour:currentHour-1,probeId:"1s12sz"}
});
dispatch({
type: 'getProbeOptions',
payload: {page:0, size: 10}
});
//重新加载本页面时清空interval
dispatch({
type:"clearTimer",
});
dispatch({
type:"getRealTimeFlow",
payload:{}
});
let time = 0;
function refreshRealTimeData(){
dispatch({
type:"getRealTimeFlow",
payload:{}
});
console.log("execute:"+ (++time));
}
const tempTimer = setInterval(refreshRealTimeData,30000);
dispatch({
type:"setTimer",
payload:tempTimer
});
}
})
}
},
effects: {
*getRealTimeFlow({payload},{call,put}){
let num1 = -1;
let num2 = -1;
let time = '';
while (true){
const data = yield call(getRealTimeCustomerFlow,payload);
if(!data){
continue;
}
console.log("data is:"+JSON.stringify(data));
console.log(data.latestCommit != null);
if(data.serverName == 'server1'){
if(data.latestCommit != null) {
time = data.latestCommit.slice(11, 19);
num1 = data.connectNum;
}else{//服务器刚刚提交数据
let currentDate = new Date();
time = currentDate.toLocaleTimeString().slice(0,8);
num1 = -2; //-2表示服务器刚刚提交数据,需要上一次数据
}
console.log("server1 num:"+num1);
}
if(data.serverName == 'server2'){
if(data.latestCommit != null){
num2 = data.connectNum;
}else{//服务器刚刚提交数据
num2 = -2;
}
console.log("server2 num:"+num2);
}
if(num1 != -1 && num2 !=-1){
console.log("get both server1 and server2");
if(num1 !=-2){ //更新存储的前一次数据
yield put({
type: 'setLastNum1',
payload:{last_num1:num1}
});
}
if(num2 !=-2){ //更新存储的前一次数据
yield put({
type: 'setLastNum2',
payload:{last_num2:num2}
});
}
let newCurrentDate = new Date();
time = newCurrentDate.toTimeString().slice(0,8);
console.log("localeTimeString:"+time);
break;
}
}
yield put({
type: 'updateRealTimeData',
payload:{num1: num1,num2:num2, time: time}
});
},
*getFlow ({payload}, {call,put}) {
const data = yield call(getCustomerFlow, payload);
if(data){
console.log(JSON.stringify("data is: "+JSON.stringify(data)));
const hourVo = data.data;
const len = hourVo.length;
let realData = hourVo.slice(0,len-3);
let commonItem = hourVo[len-3];
commonItem["inNoOutWifiPre"] = commonItem.inNoOutWifi;
commonItem["inNoOutStorePre"] = commonItem.inNoOutStore;
commonItem["outNoInWifiPre"] = commonItem.outNoInWifi;
commonItem["outNoInStorePre"] = commonItem.outNoInStore;
commonItem["inAndOutWifiPre"] = commonItem.inAndOutWifi;
commonItem["intAndOutStorePre"] = commonItem.intAndOutStore;
commonItem["stayInWifiPre"] = commonItem.stayInWifi;
commonItem["stayInStorePre"] = commonItem.stayInStore;
commonItem["jumpRatePre"] = commonItem.jumpRate;
commonItem["deepVisitPre"] = commonItem.deepVisit;
commonItem["inStoreRatePre"] = commonItem.inStoreRate;
delete commonItem.inNoOutWifi;
delete commonItem.inNoOutStore;
delete commonItem.outNoInWifi;
delete commonItem.outNoInStore;
delete commonItem.inAndOutWifi;
delete commonItem.intAndOutStore;
delete commonItem.stayInWifi;
delete commonItem.stayInStore;
realData.push(commonItem);
const predictHourData = hourVo.slice(len-2);
predictHourData.forEach(function(item) {
console.log("item is :" + JSON.stringify(item));
let predictItem = {
id: item.id,
wifiProb: item.wifiProb,
hour: item.hour,
inNoOutWifiPre: item.inNoOutWifi,
inNoOutStorePre: item.inNoOutStore,
outNoInWifiPre: item.outNoInWifi,
outNoInStorePre: item.outNoInStore,
inAndOutWifiPre: item.inAndOutWifi,
intAndOutStorePre: item.intAndOutStore,
stayInWifiPre: item.stayInWifi,
stayInStorePre: item.stayInStore,
jumpRatePre: item.jumpRate,
deepVisitPre: item.deepVisit,
inStoreRatePre: item.inStoreRate
};
console.log("predict item is: "+ JSON.stringify(predictItem));
realData.push(predictItem);
}
);
console.log("final data is :"+JSON.stringify(realData));
yield put({
type: 'setHourData',
payload:realData
});
}else{
console.log("data is "+JSON.stringify(data));
Modal.error({
title: 'get data occurs error',
content: data.msg,
});
}
},
*getDetail({payload}, {call,put}) {
const data = yield call(getCustomerFlowDetail, payload);
if(data.code==1000){
const dataVo = data.data;
dataVo.splice(8,3);
yield put({
type: 'setDetailData',
payload:dataVo
})
}else{
console.log("data is "+JSON.stringify(data));
Modal.error({
title: 'get data occurs error',
content: data.msg,
});
}
},
*getProbeOptions({payload},{call,put}) {
const data = yield call(getProbeAll, payload);
if(data){
if(data.code == 1000){
let probeList = [];
const initProbeList = data.data.content;
for(let key in initProbeList){
probeList.push({value: initProbeList[key].probeId, label: initProbeList[key].probeId});
}
yield put({
type: 'setProbeOptions',
payload: probeList
})
}
}
}
},
reducers: {
setHourData(state, action) {
return {
...state,
hourData:action.payload
}
},
setDetailData(state, action) {
return {
...state,
detailData:action.payload
}
},
setProbeOptions(state, action) {
return {
...state,
probeOptions:action.payload
}
},
updateRealTimeData(state,action){
let num1 = action.payload.num1;
let num2 = action.payload.num2;
if(num1 == -2){
num1 = state.last_num1;
console.log("last num1 :"+num1);
}
if(num2 == -2){
num2 = state.last_num2;
console.log("last num2:"+num2);
}
let newItem = {
num: num1+num2,
time: action.payload.time
};
console.log("new item is:"+JSON.stringify(newItem));
let size = state.realTimeData.push(newItem);
if(size > 16){
state.realTimeData.shift();
}
state.realTimeData = [...new Set(state.realTimeData)];
return {
...state
}
},
setTimer(state,action){
return {
...state,
timer:action.payload
}
},
clearTimer(state,action){
if(state.timer != -1000) {
console.log("clear timer!");
clearInterval(state.timer);
}
return {
...state,timer:-1000
}
},
setLastNum1(state,action){
console.log("redux setLastNum1: "+action.payload.last_num1);
return {
...state,last_num1:action.payload.last_num1
}
},
setLastNum2(state, action){
console.log("redux setLastNum2:"+action.payload.last_num2);
return {
...state,last_num2:action.payload.last_num2
}
},
getState(state,action){
return {
...state
}
}
}
}