SiteAnalysis.vue
17.7 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<template>
<Card
:tab-list="tabListTitle"
v-bind="$attrs"
:active-tab-key="activeKey"
@tabChange="onTabChange"
v-if="!isAdmin(role)"
>
<template #alarm>
<div>告警数统计</div>
</template>
<template #tabBarExtraContent>
<div class="extra-date">
<Tooltip overlay-class-name="!max-w-80 w-80">
<template #title>
<div> 30天: 查询最近30天的数据,间隔时间为1天.</div>
<div>7天: 查询最近7天的数据,间隔时间为2小时.</div>
<div>1天: 查询最近1天的数据,间隔时间为30分钟.</div>
<div>1小时: 查询最近1小时的数据,间隔时间为5分钟.</div>
<div>
间隔时间:
<span class="font-bold underline">以当前时间作为结束时间</span>
,往前推移对应天数或小时的时间作为开始时间,然后在此时间区间内进行分组聚合查询.
</div>
</template>
<QuestionCircleOutlined class="!mr-1" />
</Tooltip>
<template v-for="(item, index) in dateList" :key="item.value">
<span
@click="
quickQueryDate(index, item.value, role === RoleEnum.CUSTOMER_USER, item.interval)
"
:class="{ active: index === activeIndex }"
>{{ item.label }}</span
>
</template>
<DatePicker
@change="(_, DateString) => onDateChange(_, DateString, role === RoleEnum.CUSTOMER_USER)"
v-model:value="dateValue"
>
<Button
type="link"
class="!px-0"
:style="dateValue ? { color: '#0960bd', fontWeight: 500 } : { color: '#000000d9' }"
>
{{ dateValue ? (dateValue as moment.Moment).format('YYYY-MM-DD') : '自定义' }}
</Button>
</DatePicker>
</div>
</template>
<div v-if="activeKey === '1'">
<!-- 折线图 -->
<CustomerAlarmMessage
v-if="role === 'CUSTOMER_USER'"
type="CUSTOMER_ALARM_STATISTICAL"
:customerList="customerAlarmList"
/>
<VisitAnalysis v-else :alarmList="state.alarmList" />
</div>
<div v-if="activeKey === '2'">
<!-- 柱形图 -->
<CustomerAlarmMessage
v-if="role === 'CUSTOMER_USER'"
type="CUSTOMER_MESSAGE_STATISTICAL"
:customerList="customerMessageList"
/>
<VisitAnalysisBar
v-else
:dataPointList="state.dataPointList"
:messageList="state.messageList"
/>
</div>
</Card>
<div v-if="isAdmin(role)">
<!-- <Card
v-bind="$attrs"
title="租户趋势"
style="height: 26.75rem"
:bodyStyle="{ padding: '0 24px' }"
>
<template #extra>
<div class="extra-date">
<template v-for="(item, index) in TenantOrCustomerDateList" :key="item.value">
<span
@click="quickQueryTenantOrCustomerTime(index, item.value, 'tenant')"
:class="{ active: index === activeTenantIndex }"
>{{ item.label }}</span
>
</template>
<DatePicker.RangePicker
@change="onDateTenantChange"
size="small"
v-model:value="tenantDateValue"
/>
</div>
</template>
<TenantTrend :tenantTrendList="tenantTrendList" />
</Card> -->
<Card
v-bind="$attrs"
title="客户趋势"
style="height: 26.75rem"
:bodyStyle="{ padding: '0 24px' }"
>
<template #extra>
<div class="extra-date">
<Tooltip :overlayStyle="{ maxWidth: '340px' }">
<template #title>
<section>
<div>最近一个月: 查询最近一个月的数据,间隔时间为1天.</div>
<div>最近三个月: 查询最近三个月的数据,间隔时间为1天.</div>
<div>最近一年: 查询最近一年的数据,间隔时间为1月.</div>
<div>
间隔时间:
<span class="font-bold underline"> 以当天的(最后时间)作为结束时间 </span>
,往前推移对应天数或月的时间作为开始时间,然后在此时间区间内进行分组聚合查询.
</div>
</section>
</template>
<QuestionCircleOutlined class="!mr-2 cursor-pointer" />
</Tooltip>
<template v-for="(item, index) in TenantOrCustomerDateList" :key="item.value">
<span
@click="quickQueryTenantOrCustomerTime(index, item.value)"
:class="{ active: index === activeCustomerIndex }"
>{{ item.label }}</span
>
</template>
<DatePicker.RangePicker
@change="onDateCustomerChange"
:disabledDate="handleDisableDate"
@calendarChange="handleCalendarChange"
size="small"
v-model:value="customerDateValue"
:showTime="{
defaultValue: [dateUtil('00:00:00', 'HH:mm:ss'), dateUtil('23:59:59', 'HH:mm:ss')],
}"
>
<Button
type="link"
class="!px-0"
:style="
customerDateValue && customerDateValue.length
? { color: '#0960bd', fontWeight: 500 }
: { color: '#000000d9' }
"
>{{
customerDateValue && customerDateValue.length
? `${(customerDateValue?.[0] as moment.Moment)?.format('YYYY-MM-DD')}
~
${(customerDateValue?.[1] as moment.Moment)?.format('YYYY-MM-DD')}`
: '自定义'
}}</Button
>
</DatePicker.RangePicker>
</div>
</template>
<CustomerTrend :customerTrendList="customerTrendList" />
</Card>
</div>
</template>
<script lang="ts" setup>
import { ref, reactive, unref } from 'vue';
import { Card, DatePicker, Tooltip, Button } from 'ant-design-vue';
import VisitAnalysis from './VisitAnalysis.vue';
import VisitAnalysisBar from './VisitAnalysisBar.vue';
import { RoleEnum, isAdmin } from '/@/enums/roleEnum';
import { useWebSocket } from '@vueuse/core';
import { getAuthCache } from '/@/utils/auth';
import { JWT_TOKEN_KEY } from '/@/enums/cacheEnum';
import { dateUtil, formatToDateTime } from '/@/utils/dateUtil';
import CustomerTrend from './CustomerTrend.vue';
// import TenantTrend from './TenantTrend.vue';
import CustomerAlarmMessage from './CustomerAlarmMessage.vue';
import { useDate } from '../hooks/useDate';
import { getTrendData } from '/@/api/dashboard';
import { useGlobSetting } from '/@/hooks/setting';
import { QuestionCircleOutlined } from '@ant-design/icons-vue';
import { RangePickerValue } from 'ant-design-vue/lib/date-picker/interface';
defineExpose({
isAdmin,
});
const props = defineProps<{
role: string;
}>();
const activeKey = ref('1');
let entityId = null;
// 图表tab切换选项卡 1965 1280 1861 836
const tabListTitle = [
{
key: '1',
slots: { tab: 'alarm' },
},
// {
// key: '2',
// tab: '消息量统计',
// },
];
// 快速选择日期
const activeIndex = ref(3);
const dateValue = ref();
const dateList = ref([
{ label: '1小时', value: 1000 * 60 * 60, interval: 1000 * 60 * 5 },
{ label: '1天', value: 1000 * 60 * 60 * 24, interval: 1000 * 60 * 30 },
{ label: '7天', value: 1000 * 60 * 60 * 24 * 7, interval: 1000 * 60 * 60 * 2 },
{ label: '30天', value: 1000 * 60 * 60 * 24 * 30, interval: 1000 * 60 * 60 * 24 },
]);
// web Socket
const token: string = getAuthCache(JWT_TOKEN_KEY);
const { socketUrl } = useGlobSetting();
const state = reactive({
server: `${socketUrl}${token}`,
alarmList: new Array<[number, string]>(),
alarmItem: new Array<[number, string]>(),
dataPointList: new Array<[number, string]>(),
messageList: new Array<[number, string]>(),
dataPoint: new Array<[number, string]>(),
MsgCount: new Array<[number, string]>(),
});
const { send, close } = useWebSocket(state.server, {
async onConnected() {
if (isAdmin(props.role)) return;
let content =
JSON.parse(window.localStorage.getItem('entityId')) ||
JSON.parse(window.sessionStorage.getItem('entityId'));
entityId = content;
const sendValue = JSON.stringify({
entityDataCmds: [
{
query: {
entityFilter: {
type: 'singleEntity',
singleEntity: entityId,
},
pageLink: {
pageSize: 1024,
page: 0,
sortOrder: {
key: {
type: 'ENTITY_FIELD',
key: 'createdTime',
},
direction: 'DESC',
},
},
entityFields: [
{
type: 'ENTITY_FIELD',
key: 'name',
},
{
type: 'ENTITY_FIELD',
key: 'label',
},
{
type: 'ENTITY_FIELD',
key: 'additionalInfo',
},
],
latestValues: [
{
type: 'TIME_SERIES',
key: 'createdAlarmsCountHourly',
},
],
},
cmdId: activeKey.value,
},
],
});
const sendValue1 = JSON.stringify({
entityDataCmds: [
{
cmdId: activeKey.value,
historyCmd: {
keys: ['createdAlarmsCountHourly'],
startTs: Date.now() - 2592000000,
endTs: Date.now(),
interval: 86400000,
agg: 'SUM',
},
},
],
});
send(sendValue);
send(sendValue1);
},
onMessage(_, e) {
const { data, update } = JSON.parse(e.data);
if (activeKey.value === '1') {
if (data) {
const { createdAlarmsCountHourly } = data.data[0].latest.TIME_SERIES;
state.alarmItem = [createdAlarmsCountHourly.ts, createdAlarmsCountHourly.value];
state.alarmList.push([createdAlarmsCountHourly.ts, createdAlarmsCountHourly.value]);
}
if (update) {
const { createdAlarmsCountHourly } = update[0].timeseries;
const newArray: any = [];
for (const item of createdAlarmsCountHourly) {
newArray.push([item.ts, item.value]);
}
state.alarmList = newArray;
}
} else {
if (data) {
const { transportDataPointsCountHourly, transportMsgCountHourly } =
data.data[0].latest.TIME_SERIES;
state.dataPoint = [
transportDataPointsCountHourly.ts,
transportDataPointsCountHourly.value,
];
state.MsgCount = [
transportDataPointsCountHourly.ts,
transportDataPointsCountHourly.value,
];
state.dataPointList.push([
transportDataPointsCountHourly.ts,
transportDataPointsCountHourly.value,
]);
state.messageList.push([transportMsgCountHourly.ts, transportMsgCountHourly.value]);
}
if (update) {
const { transportDataPointsCountHourly, transportMsgCountHourly } = update[0].timeseries;
const newArray: any[] = [];
const newArray1: any[] = [];
for (const item of transportDataPointsCountHourly) {
newArray.push([item.ts, item.value]);
}
for (const item of transportMsgCountHourly) {
newArray1.push([item.ts, item.value]);
}
state.dataPointList = newArray;
state.messageList = newArray1;
}
}
},
onDisconnected() {
close();
},
});
// 切换tab页
function onTabChange(key: string) {
activeKey.value = key;
activeIndex.value = 3;
dateValue.value = '';
const sendValue = JSON.stringify({
entityDataCmds: [
{
cmdId: activeKey.value,
historyCmd: {
keys:
activeKey.value === '1'
? ['createdAlarmsCountHourly']
: ['transportMsgCountHourly', 'transportDataPointsCountHourly'],
startTs: Date.now() - 2592000000,
endTs: Date.now(),
interval: 86400000,
agg: 'SUM',
},
},
],
});
if (key === '2') {
const sendMessageValue = JSON.stringify({
entityDataCmds: [
{
query: {
entityFilter: {
type: 'singleEntity',
singleEntity: entityId,
},
pageLink: {
pageSize: 1024,
page: 0,
sortOrder: {
key: {
type: 'ENTITY_FIELD',
key: 'createdTime',
},
direction: 'DESC',
},
},
entityFields: [
{
type: 'ENTITY_FIELD',
key: 'name',
},
{
type: 'ENTITY_FIELD',
key: 'label',
},
{
type: 'ENTITY_FIELD',
key: 'additionalInfo',
},
],
latestValues: [
{
type: 'TIME_SERIES',
key: 'transportMsgCountHourly',
},
{
type: 'TIME_SERIES',
key: 'transportDataPointsCountHourly',
},
],
},
cmdId: activeKey.value,
},
],
});
send(sendMessageValue);
}
send(sendValue);
}
// 选择日期
async function onDateChange(_, dateString, isCustomer: boolean) {
activeIndex.value = -1;
const dateTime = Number(formatToDateTime(dateString, 'x'));
if (!dateString) return;
if (isCustomer) {
if (activeKey.value === '1') {
const data = await getTrendData({
startTs: dateTime,
endTs: dateTime + 86400000,
interval: 1000 * 60 * 30,
trend: 'CUSTOMER_ALARM_STATISTICAL',
});
customerAlarmList.value = data;
} else if (activeKey.value === '2') {
const data = await getTrendData({
startTs: dateTime,
endTs: dateTime + 86400000,
interval: 1000 * 60 * 30,
trend: 'CUSTOMER_MESSAGE_STATISTICAL',
});
customerMessageList.value = data;
}
} else {
// 动态发送ws数据
const sendValue = JSON.stringify({
entityDataCmds: [
{
cmdId: activeKey.value,
historyCmd: {
keys:
activeKey.value === '1'
? ['createdAlarmsCountHourly']
: ['transportMsgCountHourly', 'transportDataPointsCountHourly'],
startTs: dateTime,
endTs: dateTime + 86400000,
interval: 1000 * 60 * 30,
limit: 12,
agg: 'SUM',
},
},
],
});
send(sendValue);
}
}
const customerAlarmList = ref([]);
const customerMessageList = ref([]);
// 快速选择时间
async function quickQueryDate(
index: number,
value: number,
isCustomer: boolean,
interval: number
) {
if (activeIndex.value === index) return;
activeIndex.value = index;
dateValue.value = '';
if (isCustomer) {
if (activeKey.value === '1') {
const data = await getTrendData({
startTs: Date.now() - value,
endTs: Date.now(),
interval,
trend: 'CUSTOMER_ALARM_STATISTICAL',
});
customerAlarmList.value = data;
} else if (activeKey.value === '2') {
const data = await getTrendData({
startTs: Date.now() - value,
endTs: Date.now(),
interval,
trend: 'CUSTOMER_MESSAGE_STATISTICAL',
});
customerMessageList.value = data;
}
} else {
// 动态发送ws数据
const sendValue = JSON.stringify({
entityDataCmds: [
{
cmdId: activeKey.value,
historyCmd: {
keys:
activeKey.value === '1'
? ['createdAlarmsCountHourly']
: ['transportMsgCountHourly', 'transportDataPointsCountHourly'],
startTs: Date.now() - value,
endTs: Date.now(),
interval,
agg: 'SUM',
},
},
],
});
send(sendValue);
}
}
const {
// tenantDateValue,
customerDateValue,
// tenantTrendList,
customerTrendList,
// activeTenantIndex,
activeCustomerIndex,
TenantOrCustomerDateList,
quickQueryTenantOrCustomerTime,
// onDateTenantChange,
onDateCustomerChange,
} = useDate();
const handleCalendarChange = (val: RangePickerValue) => {
customerDateValue.value = val as any;
};
const handleDisableDate = (current: moment.Moment) => {
if (!current) return true;
if (!unref(customerDateValue) || unref(customerDateValue).length === 0) {
return false;
}
const diffDate = current.diff(unref(customerDateValue)[0], 'days');
return Math.abs(diffDate) > 30;
};
</script>
<style lang="less">
.center {
display: flex;
justify-content: center;
font-size: 16px;
}
.active {
color: #0960bd;
font-weight: 500;
}
.extra-date {
display: flex;
align-items: center;
justify-content: space-between;
span {
margin-right: 20px;
cursor: pointer;
}
}
</style>