This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/utils/timeQueryApi.js

150 lines
4.4 KiB
JavaScript
Raw Normal View History

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
const defaultSchema = {
2024-06-27 10:19:28 +08:00
doc: {
measurements: {
granularity: 1,
ingestion_delay: 15
}
}
}
2024-06-27 10:19:28 +08:00
let schemaDict = null
function getSchemaFromLocal (schemaType) {
let schemaData = null
// if(schemaDict?.[schemaType]){
// schemaData=schemaDict
// }else{
schemaData = localStorage.getItem('TSGSchema')
// }
if (!schemaData) {
2024-06-27 10:19:28 +08:00
return null
}
2024-06-27 10:19:28 +08:00
const storeData = JSON.parse(schemaData)[schemaType]
// 如果根据key没有找到数据直接返回空
if (!storeData) {
2024-06-27 10:19:28 +08:00
return null
}
2024-06-27 10:19:28 +08:00
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 {
2024-06-27 10:19:28 +08:00
setDashboardSchema(schemaType, '')
}
2024-06-27 10:19:28 +08:00
return null
}
/**
* 向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')
if (!schemaData) {
localStorage.setItem('TSGSchema', '{}')
}
2024-06-27 10:19:28 +08:00
const TSGSchema = JSON.parse(localStorage.getItem('TSGSchema'))
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
}
// 获取过期时间毫秒数
2024-06-27 10:19:28 +08:00
function getExpireTime (data) {
const expireDate = data.expireDate
if (!expireDate) {
return 24 * 60 * 60 * 1000
}
2024-06-27 10:19:28 +08:00
const expireTime = new Date().getTime() - expireDate
return expireTime
}
2024-06-27 10:19:28 +08:00
function getSchemaFromRemote (schemaType) {
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)
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)
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)
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
2024-06-27 10:19:28 +08:00
// 这里需要考虑 传入的是UTC 时间,Moment 可以正常处理吗
const delayTime = delay ? schema_ingestion_delay : 0
// 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'
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')
2024-06-27 10:19:28 +08:00
// 前端计算时间粒度,不能比Schema 支持的最小时间粒度 小
const alignmentPeriod = schema_granularity > granularity ? schema_granularity : granularity
2024-06-27 10:19:28 +08:00
// UTC 转换
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'
}
}
export default {
getTimeQueryParams,
getSchemaInfo
}
2024-06-27 10:19:28 +08:00
export { getSchemaFromLocal, getSchemaInfo, setDashboardSchema, getSchemaFromRemote, getTimeQueryParams }