decoders.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. package source
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/log"
  5. "github.com/opencost/opencost/core/pkg/util"
  6. )
  7. const (
  8. ProviderLabel = "provider"
  9. AccountIDLabel = "account_id"
  10. ClusterNameLabel = "cluster_name"
  11. RegionLabel = "region"
  12. ClusterIDLabel = "cluster_id"
  13. NamespaceLabel = "namespace"
  14. NamespaceUIDLabel = "namespace_uid"
  15. NodeLabel = "node"
  16. NodeUIDLabel = "node_uid"
  17. InstanceLabel = "instance"
  18. InstanceTypeLabel = "instance_type"
  19. ContainerLabel = "container"
  20. PodLabel = "pod"
  21. PodUIDLabel = "pod_uid"
  22. PodNameLabel = "pod_name"
  23. PodVolumeNameLabel = "pod_volume_name"
  24. ProviderIDLabel = "provider_id"
  25. DeviceLabel = "device"
  26. PVCLabel = "persistentvolumeclaim"
  27. PVCUIDLabel = "persistentvolumeclaim_uid"
  28. PVLabel = "persistentvolume"
  29. CSIVolumeHandleLabel = "csi_volume_handle"
  30. StorageClassLabel = "storageclass"
  31. VolumeNameLabel = "volumename"
  32. PVUIDLabel = "persistentvolume_uid"
  33. ServiceLabel = "service"
  34. ServiceNameLabel = "service_name"
  35. ServiceTypeLabel = "service_type"
  36. IngressIPLabel = "ingress_ip"
  37. LBIngressAddress = "lb_ingress_address"
  38. ProvisionerNameLabel = "provisioner_name"
  39. UIDLabel = "uid"
  40. KubernetesNodeLabel = "kubernetes_node"
  41. ModeLabel = "mode"
  42. ModelNameLabel = "modelName"
  43. HostNameLabel = "Hostname"
  44. UUIDLabel = "UUID"
  45. ResourceLabel = "resource"
  46. DeploymentLabel = "deployment"
  47. StatefulSetLabel = "statefulSet"
  48. DaemonSetLabel = "daemonset"
  49. JobLabel = "job"
  50. CronJobLabel = "cronjob"
  51. ReplicaSetLabel = "replicaset"
  52. ResourceQuotaLabel = "resourcequota"
  53. OwnerNameLabel = "owner_name"
  54. OwnerKindLabel = "owner_kind"
  55. OwnerUIDLabel = "owner_uid"
  56. ControllerLabel = "controller"
  57. UnitLabel = "unit"
  58. InternetLabel = "internet"
  59. SameZoneLabel = "same_zone"
  60. SameRegionLabel = "same_region"
  61. NatGatewayLabel = "nat_gateway"
  62. )
  63. const (
  64. NoneLabelValue = "<none>"
  65. )
  66. type UIDValueResult struct {
  67. UID string
  68. Value float64
  69. }
  70. func DecodeUIDValueResult(result *QueryResult) *UIDValueResult {
  71. return decodeValueResult(result, UIDLabel)
  72. }
  73. type NodeUIDValueResult UIDValueResult
  74. func DecodeNodeUIDValueResult(result *QueryResult) *NodeUIDValueResult {
  75. return (*NodeUIDValueResult)(decodeValueResult(result, NodeUIDLabel))
  76. }
  77. type PVCUIDValueResult UIDValueResult
  78. func DecodePVCUIDValueResult(result *QueryResult) *PVCUIDValueResult {
  79. return (*PVCUIDValueResult)(decodeValueResult(result, PVCUIDLabel))
  80. }
  81. func decodeValueResult(result *QueryResult, uidLabel string) *UIDValueResult {
  82. uid, _ := result.GetString(uidLabel)
  83. var value float64
  84. if len(result.Values) > 0 {
  85. value = result.Values[0].Value
  86. } else {
  87. log.Warnf("Error decoding value for uid '%s': empty value returned", uid)
  88. }
  89. return &UIDValueResult{
  90. UID: uid,
  91. Value: value,
  92. }
  93. }
  94. // UptimeResult represents the first and last recorded sample timestamp within the query window
  95. type UptimeResult struct {
  96. UID string
  97. First time.Time
  98. Last time.Time
  99. }
  100. func (res *UptimeResult) GetStartEnd(windowStart, windowEnd time.Time, resolution time.Duration) (time.Time, time.Time) {
  101. return getStartEnd(res.First, res.Last, windowStart, windowEnd, resolution)
  102. }
  103. func DecodeUptimeResult(result *QueryResult) *UptimeResult {
  104. uid, _ := result.GetString(UIDLabel)
  105. first := time.Unix(int64(result.Values[0].Timestamp), 0).UTC()
  106. last := time.Unix(int64(result.Values[len(result.Values)-1].Timestamp), 0).UTC()
  107. return &UptimeResult{
  108. UID: uid,
  109. First: first,
  110. Last: last,
  111. }
  112. }
  113. // Container requires some special results because container name and pod UID is required to uniqly identify it
  114. type ContainerUptimeResult struct {
  115. UptimeResult
  116. Container string
  117. }
  118. func DecodeContainerUptimeResult(result *QueryResult) *ContainerUptimeResult {
  119. container, _ := result.GetString(ContainerLabel)
  120. ur := DecodeUptimeResult(result)
  121. return &ContainerUptimeResult{
  122. UptimeResult: *ur,
  123. Container: container,
  124. }
  125. }
  126. type ContainerResourceResult struct {
  127. ResourceResult
  128. Container string
  129. }
  130. func DecodeContainerResourceResult(result *QueryResult) *ContainerResourceResult {
  131. container, _ := result.GetString(ContainerLabel)
  132. rr := DecodeResourceResult(result)
  133. return &ContainerResourceResult{
  134. ResourceResult: *rr,
  135. Container: container,
  136. }
  137. }
  138. type LabelsResult struct {
  139. UID string
  140. Cluster string
  141. Labels map[string]string
  142. }
  143. func DecodeLabelsResult(result *QueryResult) *LabelsResult {
  144. uid, _ := result.GetString(UIDLabel)
  145. cluster, _ := result.GetCluster()
  146. labels := result.GetLabels()
  147. return &LabelsResult{
  148. UID: uid,
  149. Cluster: cluster,
  150. Labels: labels,
  151. }
  152. }
  153. type AnnotationsResult struct {
  154. UID string
  155. Cluster string
  156. Annotations map[string]string
  157. }
  158. func DecodeAnnotationsResult(result *QueryResult) *AnnotationsResult {
  159. uid, _ := result.GetString(UIDLabel)
  160. cluster, _ := result.GetCluster()
  161. annotations := result.GetAnnotations()
  162. return &AnnotationsResult{
  163. UID: uid,
  164. Cluster: cluster,
  165. Annotations: annotations,
  166. }
  167. }
  168. type PVResult struct {
  169. Cluster string
  170. PersistentVolume string
  171. }
  172. type PVUsedAvgResult struct {
  173. Cluster string
  174. Namespace string
  175. PersistentVolumeClaim string
  176. Data []*util.Vector
  177. }
  178. func DecodePVUsedAvgResult(result *QueryResult) *PVUsedAvgResult {
  179. cluster, _ := result.GetCluster()
  180. namespace, _ := result.GetNamespace()
  181. pvc, _ := result.GetString(PVCLabel)
  182. return &PVUsedAvgResult{
  183. Cluster: cluster,
  184. Namespace: namespace,
  185. PersistentVolumeClaim: pvc,
  186. Data: result.Values,
  187. }
  188. }
  189. type PVActiveMinutesResult struct {
  190. UID string
  191. Cluster string
  192. PersistentVolume string
  193. Data []*util.Vector
  194. }
  195. func DecodePVActiveMinutesResult(result *QueryResult) *PVActiveMinutesResult {
  196. uid, _ := result.GetString(UIDLabel)
  197. cluster, _ := result.GetCluster()
  198. pv, _ := result.GetString(PVLabel)
  199. return &PVActiveMinutesResult{
  200. UID: uid,
  201. Cluster: cluster,
  202. PersistentVolume: pv,
  203. Data: result.Values,
  204. }
  205. }
  206. type PVUsedMaxResult struct {
  207. Cluster string
  208. Namespace string
  209. PersistentVolumeClaim string
  210. Data []*util.Vector
  211. }
  212. func DecodePVUsedMaxResult(result *QueryResult) *PVUsedMaxResult {
  213. cluster, _ := result.GetCluster()
  214. namespace, _ := result.GetNamespace()
  215. pvc, _ := result.GetString(PVCLabel)
  216. return &PVUsedMaxResult{
  217. Cluster: cluster,
  218. Namespace: namespace,
  219. PersistentVolumeClaim: pvc,
  220. Data: result.Values,
  221. }
  222. }
  223. type LocalStorageActiveMinutesResult struct {
  224. Cluster string
  225. Node string
  226. ProviderID string
  227. Data []*util.Vector
  228. }
  229. func DecodeLocalStorageActiveMinutesResult(result *QueryResult) *LocalStorageActiveMinutesResult {
  230. cluster, _ := result.GetCluster()
  231. node, _ := result.GetNode()
  232. if node == "" {
  233. node, _ = result.GetInstance()
  234. }
  235. providerId, _ := result.GetProviderID()
  236. return &LocalStorageActiveMinutesResult{
  237. Cluster: cluster,
  238. Node: node,
  239. ProviderID: providerId,
  240. Data: result.Values,
  241. }
  242. }
  243. type LocalStorageCostResult struct {
  244. UID string
  245. Cluster string
  246. Instance string
  247. Device string
  248. Data []*util.Vector
  249. }
  250. func DecodeLocalStorageCostResult(result *QueryResult) *LocalStorageCostResult {
  251. uid, _ := result.GetString(UIDLabel)
  252. cluster, _ := result.GetCluster()
  253. instance, _ := result.GetInstance()
  254. device, _ := result.GetDevice()
  255. return &LocalStorageCostResult{
  256. UID: uid,
  257. Cluster: cluster,
  258. Instance: instance,
  259. Device: device,
  260. Data: result.Values,
  261. }
  262. }
  263. type LocalStorageUsedCostResult struct {
  264. UID string
  265. Cluster string
  266. Instance string
  267. Device string
  268. Data []*util.Vector
  269. }
  270. func DecodeLocalStorageUsedCostResult(result *QueryResult) *LocalStorageUsedCostResult {
  271. uid, _ := result.GetString(UIDLabel)
  272. cluster, _ := result.GetCluster()
  273. instance, _ := result.GetInstance()
  274. device, _ := result.GetDevice()
  275. return &LocalStorageUsedCostResult{
  276. UID: uid,
  277. Cluster: cluster,
  278. Instance: instance,
  279. Device: device,
  280. Data: result.Values,
  281. }
  282. }
  283. type LocalStorageUsedAvgResult struct {
  284. UID string
  285. Cluster string
  286. Instance string
  287. Device string
  288. Data []*util.Vector
  289. }
  290. func DecodeLocalStorageUsedAvgResult(result *QueryResult) *LocalStorageUsedAvgResult {
  291. uid, _ := result.GetString(UIDLabel)
  292. cluster, _ := result.GetCluster()
  293. instance, _ := result.GetInstance()
  294. device, _ := result.GetDevice()
  295. return &LocalStorageUsedAvgResult{
  296. UID: uid,
  297. Cluster: cluster,
  298. Instance: instance,
  299. Device: device,
  300. Data: result.Values,
  301. }
  302. }
  303. type LocalStorageUsedMaxResult struct {
  304. UID string
  305. Cluster string
  306. Instance string
  307. Device string
  308. Data []*util.Vector
  309. }
  310. func DecodeLocalStorageUsedMaxResult(result *QueryResult) *LocalStorageUsedMaxResult {
  311. uid, _ := result.GetString(UIDLabel)
  312. cluster, _ := result.GetCluster()
  313. instance, _ := result.GetInstance()
  314. device, _ := result.GetDevice()
  315. return &LocalStorageUsedMaxResult{
  316. UID: uid,
  317. Cluster: cluster,
  318. Instance: instance,
  319. Device: device,
  320. Data: result.Values,
  321. }
  322. }
  323. type LocalStorageBytesResult struct {
  324. UID string
  325. Cluster string
  326. Instance string
  327. Device string
  328. Data []*util.Vector
  329. }
  330. func DecodeLocalStorageBytesResult(result *QueryResult) *LocalStorageBytesResult {
  331. uid, _ := result.GetString(UIDLabel)
  332. cluster, _ := result.GetCluster()
  333. instance, _ := result.GetInstance()
  334. device, _ := result.GetDevice()
  335. return &LocalStorageBytesResult{
  336. UID: uid,
  337. Cluster: cluster,
  338. Instance: instance,
  339. Device: device,
  340. Data: result.Values,
  341. }
  342. }
  343. type NodeInfoResult struct {
  344. UID string
  345. Cluster string
  346. Node string
  347. ProviderID string
  348. InstanceType string
  349. }
  350. func DecodeNodeInfoResult(result *QueryResult) *NodeInfoResult {
  351. uid, _ := result.GetString(UIDLabel)
  352. cluster, _ := result.GetCluster()
  353. node, _ := result.GetNode()
  354. providerId, _ := result.GetProviderID()
  355. instanceType, _ := result.GetInstanceType()
  356. return &NodeInfoResult{
  357. UID: uid,
  358. Cluster: cluster,
  359. Node: node,
  360. ProviderID: providerId,
  361. InstanceType: instanceType,
  362. }
  363. }
  364. type NodeActiveMinutesResult struct {
  365. UID string
  366. Cluster string
  367. Node string
  368. ProviderID string
  369. Data []*util.Vector
  370. }
  371. func DecodeNodeActiveMinutesResult(result *QueryResult) *NodeActiveMinutesResult {
  372. uid, _ := result.GetString(UIDLabel)
  373. cluster, _ := result.GetCluster()
  374. node, _ := result.GetNode()
  375. providerId, _ := result.GetProviderID()
  376. return &NodeActiveMinutesResult{
  377. UID: uid,
  378. Cluster: cluster,
  379. Node: node,
  380. ProviderID: providerId,
  381. Data: result.Values,
  382. }
  383. }
  384. type NodeCPUCoresCapacityResult struct {
  385. UID string
  386. Cluster string
  387. Node string
  388. CPUCores float64
  389. }
  390. func DecodeNodeCPUCoresCapacityResult(result *QueryResult) *NodeCPUCoresCapacityResult {
  391. uid, _ := result.GetString(UIDLabel)
  392. cluster, _ := result.GetCluster()
  393. node, _ := result.GetNode()
  394. var value float64
  395. if len(result.Values) > 0 {
  396. value = result.Values[0].Value
  397. } else {
  398. log.Warnf("Error decoding node CPU cores capacity result for node '%s': empty value returned", uid)
  399. }
  400. return &NodeCPUCoresCapacityResult{
  401. UID: uid,
  402. Cluster: cluster,
  403. Node: node,
  404. CPUCores: value,
  405. }
  406. }
  407. type NodeCPUCoresAllocatableResult = NodeCPUCoresCapacityResult
  408. func DecodeNodeCPUCoresAllocatableResult(result *QueryResult) *NodeCPUCoresAllocatableResult {
  409. return DecodeNodeCPUCoresCapacityResult(result)
  410. }
  411. type NodeRAMBytesCapacityResult struct {
  412. UID string
  413. Cluster string
  414. Node string
  415. RAMBytes float64
  416. }
  417. func DecodeNodeRAMBytesCapacityResult(result *QueryResult) *NodeRAMBytesCapacityResult {
  418. uid, _ := result.GetString(UIDLabel)
  419. cluster, _ := result.GetCluster()
  420. node, _ := result.GetNode()
  421. var value float64
  422. if len(result.Values) > 0 {
  423. value = result.Values[0].Value
  424. } else {
  425. log.Warnf("Error decoding node RAM bytes capacity result for node '%s': empty value returned", uid)
  426. }
  427. return &NodeRAMBytesCapacityResult{
  428. UID: uid,
  429. Cluster: cluster,
  430. Node: node,
  431. RAMBytes: value,
  432. }
  433. }
  434. type NodeRAMBytesAllocatableResult = NodeRAMBytesCapacityResult
  435. func DecodeNodeRAMBytesAllocatableResult(result *QueryResult) *NodeRAMBytesAllocatableResult {
  436. return DecodeNodeRAMBytesCapacityResult(result)
  437. }
  438. type NodeGPUCountResult struct {
  439. UID string
  440. Cluster string
  441. Node string
  442. ProviderID string
  443. GPUCount float64
  444. }
  445. func DecodeNodeGPUCountResult(result *QueryResult) *NodeGPUCountResult {
  446. uid, _ := result.GetString(UIDLabel)
  447. cluster, _ := result.GetCluster()
  448. node, _ := result.GetNode()
  449. providerId, _ := result.GetProviderID()
  450. var value float64
  451. if len(result.Values) > 0 {
  452. value = result.Values[0].Value
  453. } else {
  454. log.Warnf("Error decoding node GPU count capacity result for node '%s': empty value returned", uid)
  455. }
  456. return &NodeGPUCountResult{
  457. UID: uid,
  458. Cluster: cluster,
  459. Node: node,
  460. ProviderID: providerId,
  461. GPUCount: value,
  462. }
  463. }
  464. type NodeCPUModeTotalResult struct {
  465. UID string
  466. Cluster string
  467. Node string
  468. Mode string
  469. Data []*util.Vector
  470. }
  471. func DecodeNodeCPUModeTotalResult(result *QueryResult) *NodeCPUModeTotalResult {
  472. uid, _ := result.GetString(UIDLabel)
  473. cluster, _ := result.GetCluster()
  474. node, _ := result.GetString(KubernetesNodeLabel)
  475. mode, _ := result.GetString(ModeLabel)
  476. return &NodeCPUModeTotalResult{
  477. UID: uid,
  478. Cluster: cluster,
  479. Node: node,
  480. Mode: mode,
  481. Data: result.Values,
  482. }
  483. }
  484. type NodeIsSpotResult struct {
  485. UID string
  486. Cluster string
  487. Node string
  488. ProviderID string
  489. Data []*util.Vector
  490. }
  491. func DecodeNodeIsSpotResult(result *QueryResult) *NodeIsSpotResult {
  492. uid, _ := result.GetString(UIDLabel)
  493. cluster, _ := result.GetCluster()
  494. node, _ := result.GetNode()
  495. providerId, _ := result.GetProviderID()
  496. return &NodeIsSpotResult{
  497. UID: uid,
  498. Cluster: cluster,
  499. Node: node,
  500. ProviderID: providerId,
  501. Data: result.Values,
  502. }
  503. }
  504. type NodeRAMSystemPercentResult struct {
  505. UID string
  506. Cluster string
  507. Instance string
  508. Data []*util.Vector
  509. }
  510. func DecodeNodeRAMSystemPercentResult(result *QueryResult) *NodeRAMSystemPercentResult {
  511. uid, _ := result.GetString(UIDLabel)
  512. cluster, _ := result.GetCluster()
  513. instance, _ := result.GetInstance()
  514. return &NodeRAMSystemPercentResult{
  515. UID: uid,
  516. Cluster: cluster,
  517. Instance: instance,
  518. Data: result.Values,
  519. }
  520. }
  521. type NodeRAMUserPercentResult = NodeRAMSystemPercentResult
  522. func DecodeNodeRAMUserPercentResult(result *QueryResult) *NodeRAMUserPercentResult {
  523. return DecodeNodeRAMSystemPercentResult(result)
  524. }
  525. type LBActiveMinutesResult struct {
  526. UID string
  527. Cluster string
  528. Namespace string
  529. Service string
  530. IngressIP string
  531. Data []*util.Vector
  532. }
  533. func DecodeLBActiveMinutesResult(result *QueryResult) *LBActiveMinutesResult {
  534. uid, _ := result.GetString(UIDLabel)
  535. cluster, _ := result.GetCluster()
  536. namespace, _ := result.GetNamespace()
  537. service, _ := result.GetString(ServiceNameLabel)
  538. ingressIp, _ := result.GetString(IngressIPLabel)
  539. return &LBActiveMinutesResult{
  540. UID: uid,
  541. Cluster: cluster,
  542. Namespace: namespace,
  543. Service: service,
  544. IngressIP: ingressIp,
  545. Data: result.Values,
  546. }
  547. }
  548. type LBPricePerHrResult = LBActiveMinutesResult
  549. func DecodeLBPricePerHrResult(result *QueryResult) *LBPricePerHrResult {
  550. return DecodeLBActiveMinutesResult(result)
  551. }
  552. type ClusterInfoResult struct {
  553. UID string
  554. Cluster string
  555. Provider string
  556. AccountID string
  557. Provisioner string
  558. Region string
  559. }
  560. func DecodeClusterInfoResult(result *QueryResult) *ClusterInfoResult {
  561. uid, _ := result.GetString(UIDLabel)
  562. cluster, _ := result.GetString(ClusterNameLabel)
  563. provider, _ := result.GetString(ProviderLabel)
  564. accountID, _ := result.GetString(AccountIDLabel)
  565. provisioner, _ := result.GetString(ProvisionerNameLabel)
  566. region, _ := result.GetString(RegionLabel)
  567. return &ClusterInfoResult{
  568. UID: uid,
  569. Cluster: cluster,
  570. Provider: provider,
  571. AccountID: accountID,
  572. Provisioner: provisioner,
  573. Region: region,
  574. }
  575. }
  576. type ClusterManagementDurationResult struct {
  577. UID string
  578. Cluster string
  579. Provisioner string
  580. Data []*util.Vector
  581. }
  582. func DecodeClusterManagementDurationResult(result *QueryResult) *ClusterManagementDurationResult {
  583. uid, _ := result.GetString(UIDLabel)
  584. cluster, _ := result.GetCluster()
  585. provisioner, _ := result.GetString(ProvisionerNameLabel)
  586. return &ClusterManagementDurationResult{
  587. UID: uid,
  588. Cluster: cluster,
  589. Provisioner: provisioner,
  590. Data: result.Values,
  591. }
  592. }
  593. type ClusterManagementPricePerHrResult = ClusterManagementDurationResult
  594. func DecodeClusterManagementPricePerHrResult(result *QueryResult) *ClusterManagementPricePerHrResult {
  595. return DecodeClusterManagementDurationResult(result)
  596. }
  597. type PodInfoResult struct {
  598. UID string
  599. Cluster string
  600. Pod string
  601. NamespaceUID string
  602. NodeUID string
  603. }
  604. func DecodePodInfoResult(result *QueryResult) *PodInfoResult {
  605. uid, _ := result.GetString(UIDLabel)
  606. cluster, _ := result.GetCluster()
  607. pod, _ := result.GetPod()
  608. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  609. nodeUID, _ := result.GetString(NodeUIDLabel)
  610. return &PodInfoResult{
  611. UID: uid,
  612. Cluster: cluster,
  613. Pod: pod,
  614. NamespaceUID: namespaceUID,
  615. NodeUID: nodeUID,
  616. }
  617. }
  618. type PodPVCVolumeResult struct {
  619. UID string
  620. Cluster string
  621. PVCUID string
  622. PodVolumeName string
  623. }
  624. func DecodePodPVCVolumeResult(result *QueryResult) *PodPVCVolumeResult {
  625. uid, _ := result.GetString(UIDLabel)
  626. cluster, _ := result.GetCluster()
  627. pvcUID, _ := result.GetString(PVCUIDLabel)
  628. podVolumeName, _ := result.GetString(PodVolumeNameLabel)
  629. return &PodPVCVolumeResult{
  630. UID: uid,
  631. Cluster: cluster,
  632. PVCUID: pvcUID,
  633. PodVolumeName: podVolumeName,
  634. }
  635. }
  636. type OwnerResult struct {
  637. UID string
  638. Cluster string
  639. OwnerUID string
  640. OwnerKind string
  641. Controller bool
  642. }
  643. func DecodeOwnerResult(result *QueryResult) *OwnerResult {
  644. uid, _ := result.GetString(UIDLabel)
  645. cluster, _ := result.GetCluster()
  646. ownerUID, _ := result.GetString(OwnerUIDLabel)
  647. ownerKind, _ := result.GetString(OwnerKindLabel)
  648. controller, _ := result.GetBool(ControllerLabel)
  649. return &OwnerResult{
  650. UID: uid,
  651. Cluster: cluster,
  652. OwnerUID: ownerUID,
  653. OwnerKind: ownerKind,
  654. Controller: controller,
  655. }
  656. }
  657. type PodsResult struct {
  658. UID string
  659. Cluster string
  660. Namespace string
  661. Pod string
  662. Data []*util.Vector
  663. }
  664. func DecodePodsResult(result *QueryResult) *PodsResult {
  665. uid, _ := result.GetString(UIDLabel)
  666. cluster, _ := result.GetCluster()
  667. namespace, _ := result.GetNamespace()
  668. pod, _ := result.GetPod()
  669. return &PodsResult{
  670. UID: uid,
  671. Cluster: cluster,
  672. Namespace: namespace,
  673. Pod: pod,
  674. Data: result.Values,
  675. }
  676. }
  677. type ContainerMetricResult struct {
  678. UID string
  679. Cluster string
  680. Node string
  681. Instance string
  682. Namespace string
  683. Pod string
  684. Container string
  685. Data []*util.Vector
  686. }
  687. func DecodeContainerMetricResult(result *QueryResult) *ContainerMetricResult {
  688. uid, _ := result.GetString(UIDLabel)
  689. cluster, _ := result.GetCluster()
  690. node, _ := result.GetNode()
  691. instance, _ := result.GetInstance()
  692. // NOTE: this addresses cases where the node isn't set, but the instance is,
  693. // NOTE: we just inherit the instance as the node
  694. if node == "" {
  695. node = instance
  696. }
  697. namespace, _ := result.GetNamespace()
  698. pod, _ := result.GetPod()
  699. container, _ := result.GetContainer()
  700. return &ContainerMetricResult{
  701. UID: uid,
  702. Cluster: cluster,
  703. Node: node,
  704. Instance: instance,
  705. Namespace: namespace,
  706. Pod: pod,
  707. Container: container,
  708. Data: result.Values,
  709. }
  710. }
  711. type RAMBytesAllocatedResult = ContainerMetricResult
  712. func DecodeRAMBytesAllocatedResult(result *QueryResult) *RAMBytesAllocatedResult {
  713. return DecodeContainerMetricResult(result)
  714. }
  715. type RAMRequestsResult = ContainerMetricResult
  716. func DecodeRAMRequestsResult(result *QueryResult) *RAMRequestsResult {
  717. return DecodeContainerMetricResult(result)
  718. }
  719. type RAMLimitsResult = ContainerMetricResult
  720. func DecodeRAMLimitsResult(result *QueryResult) *RAMLimitsResult {
  721. return DecodeContainerMetricResult(result)
  722. }
  723. type RAMUsageAvgResult = ContainerMetricResult
  724. func DecodeRAMUsageAvgResult(result *QueryResult) *RAMUsageAvgResult {
  725. return DecodeContainerMetricResult(result)
  726. }
  727. type RAMUsageMaxResult = ContainerMetricResult
  728. func DecodeRAMUsageMaxResult(result *QueryResult) *RAMUsageMaxResult {
  729. return DecodeContainerMetricResult(result)
  730. }
  731. type NodeRAMPricePerGiBHrResult struct {
  732. UID string
  733. Cluster string
  734. Node string
  735. InstanceType string
  736. ProviderID string
  737. Data []*util.Vector
  738. }
  739. func DecodeNodeRAMPricePerGiBHrResult(result *QueryResult) *NodeRAMPricePerGiBHrResult {
  740. uid, _ := result.GetString(UIDLabel)
  741. cluster, _ := result.GetCluster()
  742. node, _ := result.GetNode()
  743. instanceType, _ := result.GetInstanceType()
  744. providerId, _ := result.GetProviderID()
  745. return &NodeRAMPricePerGiBHrResult{
  746. UID: uid,
  747. Cluster: cluster,
  748. Node: node,
  749. InstanceType: instanceType,
  750. ProviderID: providerId,
  751. Data: result.Values,
  752. }
  753. }
  754. type CPUCoresAllocatedResult = ContainerMetricResult
  755. func DecodeCPUCoresAllocatedResult(result *QueryResult) *CPUCoresAllocatedResult {
  756. return DecodeContainerMetricResult(result)
  757. }
  758. type CPURequestsResult = ContainerMetricResult
  759. func DecodeCPURequestsResult(result *QueryResult) *CPURequestsResult {
  760. return DecodeContainerMetricResult(result)
  761. }
  762. type CPULimitsResult = ContainerMetricResult
  763. func DecodeCPULimitsResult(result *QueryResult) *CPULimitsResult {
  764. return DecodeContainerMetricResult(result)
  765. }
  766. type CPUUsageAvgResult = ContainerMetricResult
  767. func DecodeCPUUsageAvgResult(result *QueryResult) *CPUUsageAvgResult {
  768. return DecodeContainerMetricResult(result)
  769. }
  770. type CPUUsageMaxResult = ContainerMetricResult
  771. func DecodeCPUUsageMaxResult(result *QueryResult) *CPUUsageMaxResult {
  772. return DecodeContainerMetricResult(result)
  773. }
  774. type NodeCPUPricePerHrResult struct {
  775. UID string
  776. Cluster string
  777. Node string
  778. InstanceType string
  779. ProviderID string
  780. Data []*util.Vector
  781. }
  782. func DecodeNodeCPUPricePerHrResult(result *QueryResult) *NodeCPUPricePerHrResult {
  783. uid, _ := result.GetString(UIDLabel)
  784. cluster, _ := result.GetCluster()
  785. node, _ := result.GetNode()
  786. instanceType, _ := result.GetInstanceType()
  787. providerId, _ := result.GetProviderID()
  788. return &NodeCPUPricePerHrResult{
  789. UID: uid,
  790. Cluster: cluster,
  791. Node: node,
  792. InstanceType: instanceType,
  793. ProviderID: providerId,
  794. Data: result.Values,
  795. }
  796. }
  797. // type alias requested result to allocated result, as you can only request a full GPU
  798. type GPUsRequestedResult = GPUsAllocatedResult
  799. func DecodeGPUsRequestedResult(result *QueryResult) *GPUsRequestedResult {
  800. return DecodeGPUsAllocatedResult(result)
  801. }
  802. type GPUsAllocatedResult struct {
  803. UID string
  804. Cluster string
  805. Namespace string
  806. Pod string
  807. Container string
  808. Data []*util.Vector
  809. }
  810. func DecodeGPUsAllocatedResult(result *QueryResult) *GPUsAllocatedResult {
  811. uid, _ := result.GetString(UIDLabel)
  812. cluster, _ := result.GetCluster()
  813. namespace, _ := result.GetNamespace()
  814. pod, _ := result.GetPod()
  815. container, _ := result.GetContainer()
  816. return &GPUsAllocatedResult{
  817. UID: uid,
  818. Cluster: cluster,
  819. Namespace: namespace,
  820. Pod: pod,
  821. Container: container,
  822. Data: result.Values,
  823. }
  824. }
  825. type GPUsUsageAvgResult struct {
  826. UID string
  827. Cluster string
  828. Namespace string
  829. Pod string
  830. Container string
  831. Data []*util.Vector
  832. }
  833. func DecodeGPUsUsageAvgResult(result *QueryResult) *GPUsUsageAvgResult {
  834. uid, _ := result.GetString(PodUIDLabel)
  835. cluster, _ := result.GetCluster()
  836. namespace, _ := result.GetNamespace()
  837. pod, _ := result.GetPod()
  838. container, _ := result.GetContainer()
  839. return &GPUsUsageAvgResult{
  840. UID: uid,
  841. Cluster: cluster,
  842. Namespace: namespace,
  843. Pod: pod,
  844. Container: container,
  845. Data: result.Values,
  846. }
  847. }
  848. type GPUsUsageMaxResult struct {
  849. UID string
  850. Cluster string
  851. Namespace string
  852. Pod string
  853. Container string
  854. Data []*util.Vector
  855. }
  856. func DecodeGPUsUsageMaxResult(result *QueryResult) *GPUsUsageMaxResult {
  857. uid, _ := result.GetString(PodUIDLabel)
  858. cluster, _ := result.GetCluster()
  859. namespace, _ := result.GetNamespace()
  860. pod, _ := result.GetPod()
  861. container, _ := result.GetContainer()
  862. return &GPUsUsageMaxResult{
  863. UID: uid,
  864. Cluster: cluster,
  865. Namespace: namespace,
  866. Pod: pod,
  867. Container: container,
  868. Data: result.Values,
  869. }
  870. }
  871. type NodeGPUPricePerHrResult struct {
  872. UID string
  873. Cluster string
  874. Node string
  875. InstanceType string
  876. ProviderID string
  877. Data []*util.Vector
  878. }
  879. func DecodeNodeGPUPricePerHrResult(result *QueryResult) *NodeGPUPricePerHrResult {
  880. uid, _ := result.GetString(UIDLabel)
  881. cluster, _ := result.GetCluster()
  882. node, _ := result.GetNode()
  883. instanceType, _ := result.GetInstanceType()
  884. providerId, _ := result.GetProviderID()
  885. return &NodeGPUPricePerHrResult{
  886. UID: uid,
  887. Cluster: cluster,
  888. Node: node,
  889. InstanceType: instanceType,
  890. ProviderID: providerId,
  891. Data: result.Values,
  892. }
  893. }
  894. type GPUInfoResult struct {
  895. UID string
  896. Cluster string
  897. Namespace string
  898. Pod string
  899. Container string
  900. Device string
  901. ModelName string
  902. UUID string
  903. Data []*util.Vector
  904. }
  905. func DecodeGPUInfoResult(result *QueryResult) *GPUInfoResult {
  906. uid, _ := result.GetString(PodUIDLabel)
  907. cluster, _ := result.GetCluster()
  908. namespace, _ := result.GetNamespace()
  909. pod, _ := result.GetPod()
  910. container, _ := result.GetContainer()
  911. device, _ := result.GetString(DeviceLabel)
  912. modelName, _ := result.GetString(ModelNameLabel)
  913. uuid, _ := result.GetString(UUIDLabel)
  914. return &GPUInfoResult{
  915. UID: uid,
  916. Cluster: cluster,
  917. Namespace: namespace,
  918. Pod: pod,
  919. Container: container,
  920. Device: device,
  921. ModelName: modelName,
  922. UUID: uuid,
  923. Data: result.Values,
  924. }
  925. }
  926. type IsGPUSharedResult struct {
  927. UID string
  928. Cluster string
  929. Namespace string
  930. Pod string
  931. Container string
  932. Resource string
  933. Data []*util.Vector
  934. }
  935. func DecodeIsGPUSharedResult(result *QueryResult) *IsGPUSharedResult {
  936. uid, _ := result.GetString(UIDLabel)
  937. cluster, _ := result.GetCluster()
  938. namespace, _ := result.GetNamespace()
  939. pod, _ := result.GetPod()
  940. container, _ := result.GetContainer()
  941. resource, _ := result.GetString(ResourceLabel)
  942. return &IsGPUSharedResult{
  943. UID: uid,
  944. Cluster: cluster,
  945. Namespace: namespace,
  946. Pod: pod,
  947. Container: container,
  948. Resource: resource,
  949. Data: result.Values,
  950. }
  951. }
  952. type PodPVCAllocationResult struct {
  953. UID string
  954. Cluster string
  955. Namespace string
  956. Pod string
  957. PersistentVolume string
  958. PersistentVolumeClaim string
  959. Data []*util.Vector
  960. }
  961. func DecodePodPVCAllocationResult(result *QueryResult) *PodPVCAllocationResult {
  962. uid, _ := result.GetString(UIDLabel)
  963. cluster, _ := result.GetCluster()
  964. namespace, _ := result.GetNamespace()
  965. pod, _ := result.GetPod()
  966. pv, _ := result.GetString(PVLabel)
  967. pvc, _ := result.GetString(PVCLabel)
  968. return &PodPVCAllocationResult{
  969. UID: uid,
  970. Cluster: cluster,
  971. Namespace: namespace,
  972. Pod: pod,
  973. PersistentVolume: pv,
  974. PersistentVolumeClaim: pvc,
  975. Data: result.Values,
  976. }
  977. }
  978. type PVCBytesRequestedResult struct {
  979. UID string
  980. Cluster string
  981. Namespace string
  982. PersistentVolumeClaim string
  983. Data []*util.Vector
  984. }
  985. func DecodePVCBytesRequestedResult(result *QueryResult) *PVCBytesRequestedResult {
  986. uid, _ := result.GetString(UIDLabel)
  987. cluster, _ := result.GetCluster()
  988. namespace, _ := result.GetNamespace()
  989. pvc, _ := result.GetString(PVCLabel)
  990. return &PVCBytesRequestedResult{
  991. UID: uid,
  992. Cluster: cluster,
  993. Namespace: namespace,
  994. PersistentVolumeClaim: pvc,
  995. Data: result.Values,
  996. }
  997. }
  998. type PVCInfoResult struct {
  999. UID string
  1000. Cluster string
  1001. NamespaceUID string
  1002. Namespace string
  1003. VolumeName string
  1004. PVUID string
  1005. PersistentVolumeClaim string
  1006. StorageClass string
  1007. Data []*util.Vector
  1008. }
  1009. func DecodePVCInfoResult(result *QueryResult) *PVCInfoResult {
  1010. uid, _ := result.GetString(UIDLabel)
  1011. cluster, _ := result.GetCluster()
  1012. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1013. namespace, _ := result.GetNamespace()
  1014. pvUID, _ := result.GetString(PVUIDLabel)
  1015. volumeName, _ := result.GetString(VolumeNameLabel)
  1016. pvc, _ := result.GetString(PVCLabel)
  1017. storageClass, _ := result.GetString(StorageClassLabel)
  1018. return &PVCInfoResult{
  1019. UID: uid,
  1020. Cluster: cluster,
  1021. NamespaceUID: namespaceUID,
  1022. Namespace: namespace,
  1023. PVUID: pvUID,
  1024. VolumeName: volumeName,
  1025. PersistentVolumeClaim: pvc,
  1026. StorageClass: storageClass,
  1027. Data: result.Values,
  1028. }
  1029. }
  1030. type PVBytesResult struct {
  1031. UID string
  1032. Cluster string
  1033. PersistentVolume string
  1034. Value float64
  1035. }
  1036. func DecodePVBytesResult(result *QueryResult) *PVBytesResult {
  1037. uid, _ := result.GetString(UIDLabel)
  1038. cluster, _ := result.GetCluster()
  1039. pv, _ := result.GetString(PVLabel)
  1040. var value float64
  1041. if len(result.Values) > 0 {
  1042. value = result.Values[0].Value
  1043. } else {
  1044. log.Warnf("Error decoding PV bytes result for pv '%s': empty value returned", uid)
  1045. }
  1046. return &PVBytesResult{
  1047. UID: uid,
  1048. Cluster: cluster,
  1049. PersistentVolume: pv,
  1050. Value: value,
  1051. }
  1052. }
  1053. type PVPricePerGiBHourResult struct {
  1054. UID string
  1055. Cluster string
  1056. VolumeName string
  1057. PersistentVolume string
  1058. ProviderID string
  1059. Data []*util.Vector
  1060. }
  1061. func DecodePVPricePerGiBHourResult(result *QueryResult) *PVPricePerGiBHourResult {
  1062. uid, _ := result.GetString(UIDLabel)
  1063. cluster, _ := result.GetCluster()
  1064. volumeName, _ := result.GetString(VolumeNameLabel)
  1065. pv, _ := result.GetString(PVLabel)
  1066. providerId, _ := result.GetProviderID()
  1067. return &PVPricePerGiBHourResult{
  1068. UID: uid,
  1069. Cluster: cluster,
  1070. VolumeName: volumeName,
  1071. PersistentVolume: pv,
  1072. ProviderID: providerId,
  1073. Data: result.Values,
  1074. }
  1075. }
  1076. type PVInfoResult struct {
  1077. UID string
  1078. Cluster string
  1079. PersistentVolume string
  1080. StorageClass string
  1081. ProviderID string
  1082. CSIVolumeHandle string
  1083. Data []*util.Vector
  1084. }
  1085. func DecodePVInfoResult(result *QueryResult) *PVInfoResult {
  1086. uid, _ := result.GetString(UIDLabel)
  1087. cluster, _ := result.GetCluster()
  1088. storageClass, _ := result.GetString(StorageClassLabel)
  1089. providerId, _ := result.GetProviderID()
  1090. pv, _ := result.GetString(PVLabel)
  1091. csiVolumeHandle, _ := result.GetString(CSIVolumeHandleLabel)
  1092. return &PVInfoResult{
  1093. UID: uid,
  1094. Cluster: cluster,
  1095. PersistentVolume: pv,
  1096. StorageClass: storageClass,
  1097. ProviderID: providerId,
  1098. CSIVolumeHandle: csiVolumeHandle,
  1099. Data: result.Values,
  1100. }
  1101. }
  1102. type PodNetworkBytesResult struct {
  1103. UID string
  1104. Cluster string
  1105. Service string
  1106. Internet bool
  1107. SameRegion bool
  1108. SameZone bool
  1109. NatGateway bool
  1110. Value float64
  1111. }
  1112. func DecodePodNetworkBytesResult(result *QueryResult) *PodNetworkBytesResult {
  1113. uid, _ := result.GetString(UIDLabel)
  1114. cluster, _ := result.GetCluster()
  1115. service, _ := result.GetString(ServiceLabel)
  1116. internet, _ := result.GetBool(InternetLabel)
  1117. sameRegion, _ := result.GetBool(SameRegionLabel)
  1118. sameZone, _ := result.GetBool(SameZoneLabel)
  1119. natGateway, _ := result.GetBool(NatGatewayLabel)
  1120. var value float64
  1121. if len(result.Values) > 0 {
  1122. value = result.Values[0].Value
  1123. } else {
  1124. log.Warnf("Error decoding pod network bytes result for pod '%s': empty value returned", uid)
  1125. }
  1126. return &PodNetworkBytesResult{
  1127. UID: uid,
  1128. Cluster: cluster,
  1129. Service: service,
  1130. Internet: internet,
  1131. SameRegion: sameRegion,
  1132. SameZone: sameZone,
  1133. NatGateway: natGateway,
  1134. Value: value,
  1135. }
  1136. }
  1137. // Base type for network usage results
  1138. type NetworkGiBResult struct {
  1139. UID string
  1140. Cluster string
  1141. Namespace string
  1142. Pod string
  1143. Service string
  1144. Data []*util.Vector
  1145. }
  1146. func DecodeNetworkGiBResult(result *QueryResult) *NetworkGiBResult {
  1147. uid, _ := result.GetString(UIDLabel)
  1148. cluster, _ := result.GetCluster()
  1149. namespace, _ := result.GetNamespace()
  1150. pod, _ := result.GetPod()
  1151. service, _ := result.GetString(ServiceLabel)
  1152. return &NetworkGiBResult{
  1153. UID: uid,
  1154. Cluster: cluster,
  1155. Namespace: namespace,
  1156. Pod: pod,
  1157. Service: service,
  1158. Data: result.Values,
  1159. }
  1160. }
  1161. // Base type for network price results
  1162. type NetworkPricePerGiBResult struct {
  1163. UID string
  1164. Cluster string
  1165. Data []*util.Vector
  1166. }
  1167. func DecodeNetworkPricePerGiBResult(result *QueryResult) *NetworkPricePerGiBResult {
  1168. uid, _ := result.GetString(UIDLabel)
  1169. cluster, _ := result.GetCluster()
  1170. return &NetworkPricePerGiBResult{
  1171. UID: uid,
  1172. Cluster: cluster,
  1173. Data: result.Values,
  1174. }
  1175. }
  1176. // Type alias the specific network subclassification results AND price results
  1177. type NetZoneGiBResult = NetworkGiBResult
  1178. type NetZonePricePerGiBResult = NetworkPricePerGiBResult
  1179. type NetRegionGiBResult = NetworkGiBResult
  1180. type NetRegionPricePerGiBResult = NetworkPricePerGiBResult
  1181. type NetInternetGiBResult = NetworkGiBResult
  1182. type NetInternetPricePerGiBResult = NetworkPricePerGiBResult
  1183. type NetInternetServiceGiBResult = NetworkGiBResult
  1184. type NetNatGatewayPricePerGiBResult = NetworkPricePerGiBResult
  1185. type NetNatGatewayGiBResult = NetworkGiBResult
  1186. type NetZoneIngressGiBResult = NetworkGiBResult
  1187. type NetRegionIngressGiBResult = NetworkGiBResult
  1188. type NetInternetIngressGiBResult = NetworkGiBResult
  1189. type NetInternetServiceIngressGiBResult = NetworkGiBResult
  1190. type NetNatGatewayIngressGiBResult = NetworkGiBResult
  1191. func DecodeNetZoneGiBResult(result *QueryResult) *NetZoneGiBResult {
  1192. return DecodeNetworkGiBResult(result)
  1193. }
  1194. func DecodeNetZonePricePerGiBResult(result *QueryResult) *NetZonePricePerGiBResult {
  1195. return DecodeNetworkPricePerGiBResult(result)
  1196. }
  1197. func DecodeNetRegionGiBResult(result *QueryResult) *NetRegionGiBResult {
  1198. return DecodeNetworkGiBResult(result)
  1199. }
  1200. func DecodeNetRegionPricePerGiBResult(result *QueryResult) *NetRegionPricePerGiBResult {
  1201. return DecodeNetworkPricePerGiBResult(result)
  1202. }
  1203. func DecodeNetInternetGiBResult(result *QueryResult) *NetInternetGiBResult {
  1204. return DecodeNetworkGiBResult(result)
  1205. }
  1206. func DecodeNetInternetPricePerGiBResult(result *QueryResult) *NetInternetPricePerGiBResult {
  1207. return DecodeNetworkPricePerGiBResult(result)
  1208. }
  1209. func DecodeNetInternetServiceGiBResult(result *QueryResult) *NetInternetServiceGiBResult {
  1210. return DecodeNetworkGiBResult(result)
  1211. }
  1212. func DecodeNetNatGatewayPricePerGiBResult(result *QueryResult) *NetNatGatewayPricePerGiBResult {
  1213. return DecodeNetworkPricePerGiBResult(result)
  1214. }
  1215. func DecodeNetNatGatewayGiBResult(result *QueryResult) *NetNatGatewayGiBResult {
  1216. return DecodeNetworkGiBResult(result)
  1217. }
  1218. func DecodeNetZoneIngressGiBResult(result *QueryResult) *NetZoneIngressGiBResult {
  1219. return DecodeNetworkGiBResult(result)
  1220. }
  1221. func DecodeNetRegionIngressGiBResult(result *QueryResult) *NetRegionIngressGiBResult {
  1222. return DecodeNetworkGiBResult(result)
  1223. }
  1224. func DecodeNetInternetIngressGiBResult(result *QueryResult) *NetInternetIngressGiBResult {
  1225. return DecodeNetworkGiBResult(result)
  1226. }
  1227. func DecodeNetInternetServiceIngressGiBResult(result *QueryResult) *NetInternetServiceIngressGiBResult {
  1228. return DecodeNetworkGiBResult(result)
  1229. }
  1230. func DecodeNetNatGatewayIngressGiBResult(result *QueryResult) *NetNatGatewayIngressGiBResult {
  1231. return DecodeNetworkGiBResult(result)
  1232. }
  1233. type NetReceiveBytesResult struct {
  1234. UID string
  1235. Cluster string
  1236. Namespace string
  1237. Pod string
  1238. Container string
  1239. Data []*util.Vector
  1240. }
  1241. func DecodeNetReceiveBytesResult(result *QueryResult) *NetReceiveBytesResult {
  1242. uid, _ := result.GetString(UIDLabel)
  1243. cluster, _ := result.GetCluster()
  1244. namespace, _ := result.GetNamespace()
  1245. pod, _ := result.GetPod()
  1246. container, _ := result.GetContainer()
  1247. return &NetReceiveBytesResult{
  1248. UID: uid,
  1249. Cluster: cluster,
  1250. Namespace: namespace,
  1251. Pod: pod,
  1252. Container: container,
  1253. Data: result.Values,
  1254. }
  1255. }
  1256. type NetTransferBytesResult struct {
  1257. UID string
  1258. Cluster string
  1259. Namespace string
  1260. Pod string
  1261. Container string
  1262. Data []*util.Vector
  1263. }
  1264. func DecodeNetTransferBytesResult(result *QueryResult) *NetTransferBytesResult {
  1265. uid, _ := result.GetString(UIDLabel)
  1266. cluster, _ := result.GetCluster()
  1267. namespace, _ := result.GetNamespace()
  1268. pod, _ := result.GetPod()
  1269. container, _ := result.GetContainer()
  1270. return &NetTransferBytesResult{
  1271. UID: uid,
  1272. Cluster: cluster,
  1273. Namespace: namespace,
  1274. Pod: pod,
  1275. Container: container,
  1276. Data: result.Values,
  1277. }
  1278. }
  1279. type NamespaceAnnotationsResult struct {
  1280. UID string
  1281. Cluster string
  1282. Namespace string
  1283. Annotations map[string]string
  1284. Data []*util.Vector
  1285. }
  1286. func DecodeNamespaceAnnotationsResult(result *QueryResult) *NamespaceAnnotationsResult {
  1287. uid, _ := result.GetString(UIDLabel)
  1288. cluster, _ := result.GetCluster()
  1289. namespace, _ := result.GetNamespace()
  1290. annotations := result.GetAnnotations()
  1291. return &NamespaceAnnotationsResult{
  1292. UID: uid,
  1293. Cluster: cluster,
  1294. Namespace: namespace,
  1295. Annotations: annotations,
  1296. Data: result.Values,
  1297. }
  1298. }
  1299. type PodAnnotationsResult struct {
  1300. UID string
  1301. Cluster string
  1302. Namespace string
  1303. Pod string
  1304. Annotations map[string]string
  1305. Data []*util.Vector
  1306. }
  1307. func DecodePodAnnotationsResult(result *QueryResult) *PodAnnotationsResult {
  1308. uid, _ := result.GetString(UIDLabel)
  1309. cluster, _ := result.GetCluster()
  1310. namespace, _ := result.GetNamespace()
  1311. pod, _ := result.GetPod()
  1312. annotations := result.GetAnnotations()
  1313. return &PodAnnotationsResult{
  1314. UID: uid,
  1315. Cluster: cluster,
  1316. Namespace: namespace,
  1317. Pod: pod,
  1318. Annotations: annotations,
  1319. Data: result.Values,
  1320. }
  1321. }
  1322. type NodeLabelsResult struct {
  1323. UID string
  1324. Cluster string
  1325. Node string
  1326. Labels map[string]string
  1327. }
  1328. func DecodeNodeLabelsResult(result *QueryResult) *NodeLabelsResult {
  1329. uid, _ := result.GetString(UIDLabel)
  1330. cluster, _ := result.GetCluster()
  1331. node, _ := result.GetNode()
  1332. labels := result.GetLabels()
  1333. return &NodeLabelsResult{
  1334. UID: uid,
  1335. Cluster: cluster,
  1336. Node: node,
  1337. Labels: labels,
  1338. }
  1339. }
  1340. type NamespaceInfoResult struct {
  1341. UID string
  1342. Cluster string
  1343. Namespace string
  1344. }
  1345. func DecodeNamespaceInfoResult(result *QueryResult) *NamespaceInfoResult {
  1346. uid, _ := result.GetString(UIDLabel)
  1347. cluster, _ := result.GetCluster()
  1348. namespace, _ := result.GetNamespace()
  1349. return &NamespaceInfoResult{
  1350. UID: uid,
  1351. Cluster: cluster,
  1352. Namespace: namespace,
  1353. }
  1354. }
  1355. type NamespaceLabelsResult struct {
  1356. UID string
  1357. Cluster string
  1358. Namespace string
  1359. Labels map[string]string
  1360. Data []*util.Vector
  1361. }
  1362. func DecodeNamespaceLabelsResult(result *QueryResult) *NamespaceLabelsResult {
  1363. uid, _ := result.GetString(UIDLabel)
  1364. cluster, _ := result.GetCluster()
  1365. namespace, _ := result.GetNamespace()
  1366. labels := result.GetLabels()
  1367. return &NamespaceLabelsResult{
  1368. UID: uid,
  1369. Cluster: cluster,
  1370. Namespace: namespace,
  1371. Labels: labels,
  1372. Data: result.Values,
  1373. }
  1374. }
  1375. type PodLabelsResult struct {
  1376. UID string
  1377. Cluster string
  1378. Namespace string
  1379. Pod string
  1380. Labels map[string]string
  1381. Data []*util.Vector
  1382. }
  1383. func DecodePodLabelsResult(result *QueryResult) *PodLabelsResult {
  1384. uid, _ := result.GetString(UIDLabel)
  1385. cluster, _ := result.GetCluster()
  1386. namespace, _ := result.GetNamespace()
  1387. pod, _ := result.GetPod()
  1388. labels := result.GetLabels()
  1389. return &PodLabelsResult{
  1390. UID: uid,
  1391. Cluster: cluster,
  1392. Namespace: namespace,
  1393. Pod: pod,
  1394. Labels: labels,
  1395. Data: result.Values,
  1396. }
  1397. }
  1398. type ServiceLabelsResult struct {
  1399. UID string
  1400. Cluster string
  1401. Namespace string
  1402. Service string
  1403. Labels map[string]string
  1404. Data []*util.Vector
  1405. }
  1406. func DecodeServiceLabelsResult(result *QueryResult) *ServiceLabelsResult {
  1407. uid, _ := result.GetString(UIDLabel)
  1408. cluster, _ := result.GetCluster()
  1409. namespace, _ := result.GetNamespace()
  1410. service, _ := result.GetString(ServiceLabel)
  1411. labels := result.GetLabels()
  1412. return &ServiceLabelsResult{
  1413. UID: uid,
  1414. Cluster: cluster,
  1415. Namespace: namespace,
  1416. Service: service,
  1417. Labels: labels,
  1418. Data: result.Values,
  1419. }
  1420. }
  1421. type ServiceInfoResult struct {
  1422. UID string
  1423. Cluster string
  1424. NamespaceUID string
  1425. Service string
  1426. ServiceType string
  1427. LBIngressAddress string
  1428. }
  1429. func DecodeServiceInfoResult(result *QueryResult) *ServiceInfoResult {
  1430. uid, _ := result.GetString(UIDLabel)
  1431. cluster, _ := result.GetCluster()
  1432. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1433. service, _ := result.GetString(ServiceLabel)
  1434. serviceType, _ := result.GetString(ServiceTypeLabel)
  1435. lbIngressAddress, _ := result.GetString(LBIngressAddress)
  1436. return &ServiceInfoResult{
  1437. UID: uid,
  1438. Cluster: cluster,
  1439. NamespaceUID: namespaceUID,
  1440. Service: service,
  1441. ServiceType: serviceType,
  1442. LBIngressAddress: lbIngressAddress,
  1443. }
  1444. }
  1445. type DeploymentInfoResult struct {
  1446. UID string
  1447. Cluster string
  1448. NamespaceUID string
  1449. Deployment string
  1450. }
  1451. func DecodeDeploymentInfoResult(result *QueryResult) *DeploymentInfoResult {
  1452. uid, _ := result.GetString(UIDLabel)
  1453. cluster, _ := result.GetCluster()
  1454. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1455. deployment, _ := result.GetString(DeploymentLabel)
  1456. return &DeploymentInfoResult{
  1457. UID: uid,
  1458. Cluster: cluster,
  1459. NamespaceUID: namespaceUID,
  1460. Deployment: deployment,
  1461. }
  1462. }
  1463. type DeploymentLabelsResult struct {
  1464. UID string
  1465. Cluster string
  1466. Namespace string
  1467. Deployment string
  1468. Labels map[string]string
  1469. Data []*util.Vector
  1470. }
  1471. func DecodeDeploymentLabelsResult(result *QueryResult) *DeploymentLabelsResult {
  1472. uid, _ := result.GetString(UIDLabel)
  1473. cluster, _ := result.GetCluster()
  1474. namespace, _ := result.GetNamespace()
  1475. deployment, _ := result.GetString(DeploymentLabel)
  1476. labels := result.GetLabels()
  1477. return &DeploymentLabelsResult{
  1478. UID: uid,
  1479. Cluster: cluster,
  1480. Namespace: namespace,
  1481. Deployment: deployment,
  1482. Labels: labels,
  1483. Data: result.Values,
  1484. }
  1485. }
  1486. type StatefulSetInfoResult struct {
  1487. UID string
  1488. Cluster string
  1489. NamespaceUID string
  1490. StatefulSet string
  1491. }
  1492. func DecodeStatefulSetInfoResult(result *QueryResult) *StatefulSetInfoResult {
  1493. uid, _ := result.GetString(UIDLabel)
  1494. cluster, _ := result.GetCluster()
  1495. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1496. statefulSet, _ := result.GetString(StatefulSetLabel)
  1497. return &StatefulSetInfoResult{
  1498. UID: uid,
  1499. Cluster: cluster,
  1500. NamespaceUID: namespaceUID,
  1501. StatefulSet: statefulSet,
  1502. }
  1503. }
  1504. type DaemonSetInfoResult struct {
  1505. UID string
  1506. Cluster string
  1507. NamespaceUID string
  1508. DaemonSet string
  1509. }
  1510. func DecodeDaemonSetInfoResult(result *QueryResult) *DaemonSetInfoResult {
  1511. uid, _ := result.GetString(UIDLabel)
  1512. cluster, _ := result.GetCluster()
  1513. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1514. daemonSet, _ := result.GetString(DaemonSetLabel)
  1515. return &DaemonSetInfoResult{
  1516. UID: uid,
  1517. Cluster: cluster,
  1518. NamespaceUID: namespaceUID,
  1519. DaemonSet: daemonSet,
  1520. }
  1521. }
  1522. type JobInfoResult struct {
  1523. UID string
  1524. Cluster string
  1525. NamespaceUID string
  1526. Job string
  1527. }
  1528. func DecodeJobInfoResult(result *QueryResult) *JobInfoResult {
  1529. uid, _ := result.GetString(UIDLabel)
  1530. cluster, _ := result.GetCluster()
  1531. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1532. job, _ := result.GetString(JobLabel)
  1533. return &JobInfoResult{
  1534. UID: uid,
  1535. Cluster: cluster,
  1536. NamespaceUID: namespaceUID,
  1537. Job: job,
  1538. }
  1539. }
  1540. type CronJobInfoResult struct {
  1541. UID string
  1542. Cluster string
  1543. NamespaceUID string
  1544. CronJob string
  1545. }
  1546. func DecodeCronJobInfoResult(result *QueryResult) *CronJobInfoResult {
  1547. uid, _ := result.GetString(UIDLabel)
  1548. cluster, _ := result.GetCluster()
  1549. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1550. cronJob, _ := result.GetString(CronJobLabel)
  1551. return &CronJobInfoResult{
  1552. UID: uid,
  1553. Cluster: cluster,
  1554. NamespaceUID: namespaceUID,
  1555. CronJob: cronJob,
  1556. }
  1557. }
  1558. type ReplicaSetInfoResult struct {
  1559. UID string
  1560. Cluster string
  1561. NamespaceUID string
  1562. ReplicaSet string
  1563. }
  1564. func DecodeReplicaSetInfoResult(result *QueryResult) *ReplicaSetInfoResult {
  1565. uid, _ := result.GetString(UIDLabel)
  1566. cluster, _ := result.GetCluster()
  1567. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1568. replicaSet, _ := result.GetString(ReplicaSetLabel)
  1569. return &ReplicaSetInfoResult{
  1570. UID: uid,
  1571. Cluster: cluster,
  1572. NamespaceUID: namespaceUID,
  1573. ReplicaSet: replicaSet,
  1574. }
  1575. }
  1576. type StatefulSetLabelsResult struct {
  1577. UID string
  1578. Cluster string
  1579. Namespace string
  1580. StatefulSet string
  1581. Labels map[string]string
  1582. Data []*util.Vector
  1583. }
  1584. func DecodeStatefulSetLabelsResult(result *QueryResult) *StatefulSetLabelsResult {
  1585. uid, _ := result.GetString(UIDLabel)
  1586. cluster, _ := result.GetCluster()
  1587. namespace, _ := result.GetNamespace()
  1588. statefulSet, _ := result.GetString(StatefulSetLabel)
  1589. labels := result.GetLabels()
  1590. return &StatefulSetLabelsResult{
  1591. UID: uid,
  1592. Cluster: cluster,
  1593. Namespace: namespace,
  1594. StatefulSet: statefulSet,
  1595. Labels: labels,
  1596. Data: result.Values,
  1597. }
  1598. }
  1599. type PodsWithDaemonSetOwnerResult struct {
  1600. UID string
  1601. Cluster string
  1602. Namespace string
  1603. Pod string
  1604. DaemonSet string
  1605. }
  1606. func DecodePodsWithDaemonSetOwnerResult(result *QueryResult) *PodsWithDaemonSetOwnerResult {
  1607. uid, _ := result.GetString(UIDLabel)
  1608. cluster, _ := result.GetCluster()
  1609. namespace, _ := result.GetNamespace()
  1610. pod, _ := result.GetPod()
  1611. daemonSet, _ := result.GetString(OwnerNameLabel)
  1612. return &PodsWithDaemonSetOwnerResult{
  1613. UID: uid,
  1614. Cluster: cluster,
  1615. Namespace: namespace,
  1616. Pod: pod,
  1617. DaemonSet: daemonSet,
  1618. }
  1619. }
  1620. type PodsWithJobOwnerResult struct {
  1621. UID string
  1622. Cluster string
  1623. Namespace string
  1624. Pod string
  1625. Job string
  1626. }
  1627. func DecodePodsWithJobOwnerResult(result *QueryResult) *PodsWithJobOwnerResult {
  1628. uid, _ := result.GetString(UIDLabel)
  1629. cluster, _ := result.GetCluster()
  1630. namespace, _ := result.GetNamespace()
  1631. pod, _ := result.GetPod()
  1632. job, _ := result.GetString(OwnerNameLabel)
  1633. return &PodsWithJobOwnerResult{
  1634. UID: uid,
  1635. Cluster: cluster,
  1636. Namespace: namespace,
  1637. Pod: pod,
  1638. Job: job,
  1639. }
  1640. }
  1641. type PodsWithReplicaSetOwnerResult struct {
  1642. UID string
  1643. Cluster string
  1644. Namespace string
  1645. Pod string
  1646. ReplicaSet string
  1647. Data []*util.Vector
  1648. }
  1649. func DecodePodsWithReplicaSetOwnerResult(result *QueryResult) *PodsWithReplicaSetOwnerResult {
  1650. uid, _ := result.GetString(UIDLabel)
  1651. cluster, _ := result.GetCluster()
  1652. namespace, _ := result.GetNamespace()
  1653. replicaSet, _ := result.GetString(OwnerNameLabel)
  1654. pod, _ := result.GetPod()
  1655. return &PodsWithReplicaSetOwnerResult{
  1656. UID: uid,
  1657. Cluster: cluster,
  1658. Namespace: namespace,
  1659. Pod: pod,
  1660. ReplicaSet: replicaSet,
  1661. Data: result.Values,
  1662. }
  1663. }
  1664. type ReplicaSetsWithoutOwnersResult struct {
  1665. UID string
  1666. Cluster string
  1667. Namespace string
  1668. ReplicaSet string
  1669. Data []*util.Vector
  1670. }
  1671. func DecodeReplicaSetsWithoutOwnersResult(result *QueryResult) *ReplicaSetsWithoutOwnersResult {
  1672. uid, _ := result.GetString(UIDLabel)
  1673. return &ReplicaSetsWithoutOwnersResult{
  1674. UID: uid,
  1675. Data: result.Values,
  1676. }
  1677. }
  1678. type ReplicaSetsWithRolloutResult struct {
  1679. UID string
  1680. Cluster string
  1681. Namespace string
  1682. ReplicaSet string
  1683. OwnerName string
  1684. OwnerKind string
  1685. Data []*util.Vector
  1686. }
  1687. func DecodeReplicaSetsWithRolloutResult(result *QueryResult) *ReplicaSetsWithRolloutResult {
  1688. uid, _ := result.GetString(UIDLabel)
  1689. cluster, _ := result.GetCluster()
  1690. namespace, _ := result.GetNamespace()
  1691. replicaSet, _ := result.GetString(ReplicaSetLabel)
  1692. ownerName, _ := result.GetString(OwnerNameLabel)
  1693. ownerKind, _ := result.GetString(OwnerKindLabel)
  1694. return &ReplicaSetsWithRolloutResult{
  1695. UID: uid,
  1696. Cluster: cluster,
  1697. Namespace: namespace,
  1698. ReplicaSet: replicaSet,
  1699. OwnerName: ownerName,
  1700. OwnerKind: ownerKind,
  1701. Data: result.Values,
  1702. }
  1703. }
  1704. type ResourceQuotaInfoResult struct {
  1705. UID string
  1706. NamespaceUID string
  1707. ResourceQuota string
  1708. }
  1709. func DecodeResourceQuotaInfoResult(result *QueryResult) *ResourceQuotaInfoResult {
  1710. uid, _ := result.GetString(UIDLabel)
  1711. namespaceUID, _ := result.GetString(NamespaceUIDLabel)
  1712. resourceQuota, _ := result.GetString(ResourceQuotaLabel)
  1713. return &ResourceQuotaInfoResult{
  1714. UID: uid,
  1715. NamespaceUID: namespaceUID,
  1716. ResourceQuota: resourceQuota,
  1717. }
  1718. }
  1719. type ResourceResult struct {
  1720. UID string
  1721. Resource string
  1722. Unit string
  1723. Value float64
  1724. }
  1725. func DecodeResourceResult(result *QueryResult) *ResourceResult {
  1726. uid, _ := result.GetString(UIDLabel)
  1727. resource, _ := result.GetString(ResourceLabel)
  1728. unit, _ := result.GetString(UnitLabel)
  1729. var value float64
  1730. if len(result.Values) > 0 {
  1731. value = result.Values[0].Value
  1732. } else {
  1733. log.Warnf("Error decoding resource for uid '%s': empty value returned", uid)
  1734. }
  1735. return &ResourceResult{
  1736. UID: uid,
  1737. Resource: resource,
  1738. Unit: unit,
  1739. Value: value,
  1740. }
  1741. }
  1742. // DCGM needs specialized results because it uses UUID instead of the uid label that we use.
  1743. type DCGMDeviceInfoResult struct {
  1744. UUID string
  1745. Device string
  1746. ModelName string
  1747. HostName string
  1748. }
  1749. func DecodeDCGMDeviceInfoResult(result *QueryResult) *DCGMDeviceInfoResult {
  1750. uuid, _ := result.GetString(UUIDLabel)
  1751. device, _ := result.GetString(DeviceLabel)
  1752. modelName, _ := result.GetString(ModelNameLabel)
  1753. hostName, _ := result.GetString(HostNameLabel)
  1754. return &DCGMDeviceInfoResult{
  1755. UUID: uuid,
  1756. Device: device,
  1757. ModelName: modelName,
  1758. HostName: hostName,
  1759. }
  1760. }
  1761. type DCGMDeviceUptimeResult struct {
  1762. UUID string
  1763. First time.Time
  1764. Last time.Time
  1765. }
  1766. func (res *DCGMDeviceUptimeResult) GetStartEnd(windowStart, windowEnd time.Time, resolution time.Duration) (time.Time, time.Time) {
  1767. return getStartEnd(res.First, res.Last, windowStart, windowEnd, resolution)
  1768. }
  1769. func getStartEnd(first, last, windowStart, windowEnd time.Time, resolution time.Duration) (time.Time, time.Time) {
  1770. // The only corner-case here is what to do if you only get one timestamp.
  1771. // This dilemma still requires the use of the resolution, and can be
  1772. // clamped using the window. In this case, we want to honor the existence
  1773. // of the pod by giving "one resolution" worth of duration, half on each
  1774. // side of the given timestamp.
  1775. if first.Equal(last) {
  1776. first = first.Add(-1 * resolution / time.Duration(2))
  1777. last = last.Add(resolution / time.Duration(2))
  1778. }
  1779. if first.Before(windowStart) {
  1780. first = windowStart
  1781. }
  1782. if last.After(windowEnd) {
  1783. last = windowEnd
  1784. }
  1785. // prevent end times in the future
  1786. now := time.Now().UTC()
  1787. if last.After(now) {
  1788. last = now
  1789. }
  1790. return first, last
  1791. }
  1792. func DecodeDCGMDeviceUptimeResult(result *QueryResult) *DCGMDeviceUptimeResult {
  1793. uuid, _ := result.GetString(UUIDLabel)
  1794. first := time.Unix(int64(result.Values[0].Timestamp), 0).UTC()
  1795. last := time.Unix(int64(result.Values[len(result.Values)-1].Timestamp), 0).UTC()
  1796. return &DCGMDeviceUptimeResult{
  1797. UUID: uuid,
  1798. First: first,
  1799. Last: last,
  1800. }
  1801. }
  1802. type DCGMDeviceContainerUsageResult struct {
  1803. UUID string
  1804. PodUID string
  1805. Container string
  1806. Value float64
  1807. }
  1808. func DecodeDCGMDeviceContainerUsageResult(result *QueryResult) *DCGMDeviceContainerUsageResult {
  1809. uuid, _ := result.GetString(UUIDLabel)
  1810. podUID, _ := result.GetString(PodUIDLabel)
  1811. container, _ := result.GetString(ContainerLabel)
  1812. var value float64
  1813. if len(result.Values) > 0 {
  1814. value = result.Values[0].Value
  1815. } else {
  1816. log.Warnf("Error decoding DCGM Device Container Udage Result for device '%s': empty value returned", uuid)
  1817. }
  1818. return &DCGMDeviceContainerUsageResult{
  1819. UUID: uuid,
  1820. PodUID: podUID,
  1821. Container: container,
  1822. Value: value,
  1823. }
  1824. }
  1825. func DecodeAll[T any](results []*QueryResult, decode ResultDecoder[T]) []*T {
  1826. decoded := make([]*T, 0, len(results))
  1827. for _, result := range results {
  1828. decoded = append(decoded, decode(result))
  1829. }
  1830. return decoded
  1831. }