github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/apps/sys.monitor/site.main.src/src/utils/Units.js (about) 1 /* 2 * Copyright (c) 2022-present unTill Pro, Ltd. 3 */ 4 import {filesize} from "filesize"; 5 import { AUTH_CHECK } from "react-admin"; 6 7 export const PERCENT = "percent" 8 export const BPS = "bps" 9 export const DATASIZE = "datasize" 10 export const COUNT = "count" 11 export const DURATION = "duration" // from Nanoseconds 12 13 export function Rnd(min, max) { 14 return min + Math.floor(Math.random() * (max - min)); 15 } 16 17 const EMULATE_SECONDS = 3600 18 const EMULATE_STEP_SEC = 15 19 export const NANOS_IN_SECOND = 1000000000 20 21 /* 22 in : [ 23 { 24 key: string 25 start: int 26 offs: 10 27 }, 28 ... 29 ], 30 31 out: [ 32 { 33 x: Date, 34 key1: value1_1, 35 key2: value1_2, 36 ... 37 }, 38 ... 39 ] 40 41 */ 42 43 export function EmuData(meta) { 44 const now = new Date() 45 const before = new Date(now.getTime() - EMULATE_SECONDS * 1000) 46 var t = new Date(before.getTime()) 47 var result = [] 48 var last = null 49 while (t < now) { 50 var cur = {} 51 meta.map((e) => { 52 cur.x = new Date(t.getTime()) 53 if (e.calc) { 54 cur[e.key] = e.calc(cur) 55 } else { 56 cur[e.key] = last ? Math.max(0, last[e.key] + Rnd(-e.offs, e.offs)) : e.start 57 } 58 }) 59 result.push(cur) 60 last = cur 61 t.setSeconds(t.getSeconds() + EMULATE_STEP_SEC) 62 } 63 return result 64 } 65 66 export function EmuSerie(id, name, start, offs) { 67 const now = new Date() 68 const before = new Date(now.getTime() - EMULATE_SECONDS * 1000) 69 var t = new Date(before.getTime()) 70 var serie = { 71 id: id, 72 name: name, 73 data: [] 74 } 75 var last = null 76 while (t < now) { 77 var cur = { 78 x: t.getTime(), 79 value: last ? Math.max(0, last.value + Rnd(-offs, offs)) : start 80 } 81 serie.data.push(cur) 82 last = cur 83 t.setSeconds(t.getSeconds() + EMULATE_STEP_SEC) 84 } 85 return serie 86 } 87 88 export function EmulateData(callback) { 89 const now = new Date() 90 const before = new Date(now.getTime() - EMULATE_SECONDS * 1000) 91 var t = new Date(before.getTime()) 92 var result = [] 93 var last = null 94 while (t < now) { 95 var cur = callback(new Date(t.getTime()), last) 96 result.push(cur) 97 last = cur 98 t.setSeconds(t.getSeconds() + EMULATE_STEP_SEC) 99 } 100 return result 101 } 102 103 export function FormatValue(units, value) { 104 if (units === PERCENT) { 105 return Math.floor(value) + "%" 106 } 107 if (units === BPS) { 108 return filesize(value) + "s" 109 } 110 if (units === DATASIZE) { 111 return filesize(value) 112 } 113 if (units === COUNT) { 114 var hrnumbers = require('human-readable-numbers'); 115 return hrnumbers.toHumanString(value) 116 } 117 if (units === DURATION) { 118 return nanosecondsToStr(value) 119 } 120 return value 121 } 122 123 function nanosecondsToStr(nanos) { 124 if (nanos >= 1000) { 125 return microsecondsToStr(nanos / 1000) 126 } else { 127 return Math.round(nanos*10)/10 + ' ns' 128 } 129 } 130 131 function microsecondsToStr(micros) { 132 if (micros >= 1000) { 133 return millisecondsToStr(micros / 1000) 134 } else { 135 return Math.round(micros*10)/10 + ' us' 136 } 137 } 138 139 function millisecondsToStr(millis) { 140 if (millis >= 1000) { 141 return secondsToStr(millis / 1000) 142 } else { 143 return Math.round(millis*10)/10 + ' ms' 144 } 145 } 146 147 function secondsToStr(seconds) { 148 149 var years = Math.floor(seconds / 31536000); 150 if (years) { 151 return years + ' yr' 152 } 153 var days = Math.floor((seconds %= 31536000) / 86400); 154 if (days) { 155 return days + ' d'; 156 } 157 var hours = Math.floor((seconds %= 86400) / 3600); 158 if (hours) { 159 return hours + ' h'; 160 } 161 var minutes = Math.floor((seconds %= 3600) / 60); 162 if (minutes) { 163 return minutes + ' m'; 164 } 165 166 return Math.round(seconds*10)/10 + ' s'; 167 }