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