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
|
import vue from '@/main.js'
// import Moment from "moment/moment";
// import {transformToUTCTime} from './TimeZone.js'
// 默认Schema 延时15s ,时间粒度 1s
const defaultSchema = {
doc: {
measurements: {
granularity: 1,
ingestion_delay: 15
}
}
}
let schemaDict = null
function getSchemaFromLocal (schemaType) {
let schemaData = null
// if(schemaDict?.[schemaType]){
// schemaData=schemaDict
// }else{
schemaData = localStorage.getItem('TSGSchema')
// }
if (!schemaData) {
return null
}
const storeData = JSON.parse(schemaData)[schemaType]
// 如果根据key没有找到数据,直接返回空
if (!storeData) {
return null
}
const parsedData = storeData
const currentTimestamp = new Date().getTime()
// 将当前的时间戳和保存在storage中的timestamp进行比较
// 如果时间差小于等于过期时间说明没有过期,直接返回数据
// 否则,说明数据已经过期,将storage中的key清除
if (currentTimestamp - parsedData.timestamp <= parsedData.expire) {
return parsedData.value ? JSON.parse(parsedData.value) : parsedData.value
} else {
setDashboardSchema(schemaType, '')
}
return null
}
/**
* 向localStorage中添加字段
* @param {*} schemaType 保存数据的key
* @param {*} value 保存的数据
* @param {*} expire 过期时间,默认为1分钟
*/
function setDashboardSchema (schemaType, value, expire = 60000) {
const schemaData = localStorage.getItem('TSGSchema')
if (!schemaData) {
localStorage.setItem('TSGSchema', '{}')
}
const TSGSchema = JSON.parse(localStorage.getItem('TSGSchema'))
TSGSchema[schemaType] = {
value: value,
expire: expire,
timestamp: new Date().getTime()
}
const stringfiedData = JSON.stringify(TSGSchema)
localStorage.setItem('TSGSchema', stringfiedData)
schemaDict = TSGSchema
}
// 获取过期时间毫秒数
function getExpireTime (data) {
const expireDate = data.expireDate
if (!expireDate) {
return 24 * 60 * 60 * 1000
}
const expireTime = new Date().getTime() - expireDate
return expireTime
}
function getSchemaFromRemote (schemaType) {
return vue.$get(`/interface/gateway/api/galaxy/v1/metadata/schema/${schemaType}`).then(res => {
if (res.status === 200) {
const expireTime = getExpireTime(res.data)
setDashboardSchema(schemaType, JSON.stringify(res.data), expireTime)
return res.data
}
return defaultSchema
}).catch(err => {
console.error(err)
return defaultSchema
})
}
// async 延时时间
// 转换时间 start end schmaType 相对时间 绝对时间
// 时间粒度
// 获取Schema数据,存在于local 直接取,不存在 直接请求,存本地
async function getSchemaInfo (schemaType = '') {
let schemaInfo = getSchemaFromLocal(schemaType)
if (!schemaInfo) {
schemaInfo = await getSchemaFromRemote(schemaType)
}
return schemaInfo
}
/*
* 这里对 时间的延时
* 时间范围做处理
* 默认 不做 UTC 转换处理
*
* granularity 单位必须是S 秒
* */
async function getTimeQueryParams ({ start, end, schemaType = '', granularity = 1, toUtc = false, delay = false }) {
const schemaData = await getSchemaInfo(schemaType)
const schema_ingestion_delay = schemaData?.doc?.measurements?.ingestion_delay || 0
const schema_granularity = schemaData?.doc?.measurements?.granularity || 1
// 这里需要考虑 传入的是UTC 时间,Moment 可以正常处理吗
const delayTime = delay ? schema_ingestion_delay : 0
// let startDate = Moment(start).subtract(delayTime, 'seconds')
// let endDate = Moment(end).subtract(delayTime, 'seconds')
const startDate = '1'
const endDate = '2'
let startStr = startDate.format('YYYY-MM-DD HH:mm:ss')
let endStr = endDate.format('YYYY-MM-DD HH:mm:ss')
// 前端计算时间粒度,不能比Schema 支持的最小时间粒度 小
const alignmentPeriod = schema_granularity > granularity ? schema_granularity : granularity
// UTC 转换
if (toUtc) {
// startStr = transformToUTCTime(startStr)
// endStr = transformToUTCTime(endStr)
startStr = 'transformToUTCTime(startStr)'
endStr = 'transformToUTCTime(endStr)'
}
return {
start: startStr,
end: endStr,
granularity: 'PT' + schema_granularity + 'S',
alignmentPeriod: 'PT' + alignmentPeriod + 'S'
}
}
export default {
getTimeQueryParams,
getSchemaInfo
}
export { getSchemaFromLocal, getSchemaInfo, setDashboardSchema, getSchemaFromRemote, getTimeQueryParams }
|