| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- import { describe, expect, it } from 'vitest'
- import {
- parseConnectionsData,
- parseInfrastructureMetrics,
- parseNumericValue,
- } from './DatabaseInfrastructureSection.utils'
- import type {
- InfraMonitoringMultiResponse,
- InfraMonitoringSingleResponse,
- } from '@/data/analytics/infra-monitoring-query'
- describe('parseNumericValue', () => {
- it('returns number value as-is', () => {
- expect(parseNumericValue(42)).toBe(42)
- expect(parseNumericValue(0)).toBe(0)
- expect(parseNumericValue(3.14)).toBe(3.14)
- })
- it('parses valid string numbers', () => {
- expect(parseNumericValue('42')).toBe(42)
- expect(parseNumericValue('3.14')).toBe(3.14)
- expect(parseNumericValue('0')).toBe(0)
- })
- it('returns 0 for invalid string values', () => {
- expect(parseNumericValue('invalid')).toBe(0)
- expect(parseNumericValue('')).toBe(0)
- expect(parseNumericValue('NaN')).toBe(0)
- })
- it('returns 0 for undefined', () => {
- expect(parseNumericValue(undefined)).toBe(0)
- })
- })
- describe('parseInfrastructureMetrics', () => {
- it('returns null for undefined data', () => {
- expect(parseInfrastructureMetrics(undefined)).toBe(null)
- })
- it('parses valid metrics from response', () => {
- const mockResponse: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- avg_cpu_usage: { format: 'percent', total: 150, totalAverage: 50, yAxisLimit: 100 },
- ram_usage: { format: 'bytes', total: 180, totalAverage: 60, yAxisLimit: 100 },
- disk_fs_used_system: {
- format: 'bytes',
- total: 20,
- totalAverage: 20,
- yAxisLimit: 100,
- },
- disk_fs_used_wal: {
- format: 'bytes',
- total: 10,
- totalAverage: 10,
- yAxisLimit: 100,
- },
- pg_database_size: {
- format: 'bytes',
- total: 30,
- totalAverage: 30,
- yAxisLimit: 100,
- },
- disk_fs_size: {
- format: 'bytes',
- total: 200,
- totalAverage: 200,
- yAxisLimit: 100,
- },
- disk_io_consumption: {
- format: 'percent',
- total: 210,
- totalAverage: 70,
- yAxisLimit: 100,
- },
- },
- }
- const result = parseInfrastructureMetrics(mockResponse)
- expect(result).toEqual({
- cpu: { current: 50, max: 100 },
- ram: { current: 60, max: 100 },
- disk: { current: 30, max: 100 },
- diskIo: { current: 70, max: 100 },
- })
- })
- it('handles string values in metrics', () => {
- const mockResponse: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- avg_cpu_usage: { format: 'percent', total: 150, totalAverage: '50.5', yAxisLimit: 100 },
- ram_usage: { format: 'bytes', total: 180, totalAverage: '60.8', yAxisLimit: 100 },
- disk_fs_used_system: {
- format: 'bytes',
- total: 20,
- totalAverage: '20.4',
- yAxisLimit: 100,
- },
- disk_fs_used_wal: {
- format: 'bytes',
- total: 10,
- totalAverage: '10.2',
- yAxisLimit: 100,
- },
- pg_database_size: {
- format: 'bytes',
- total: 30,
- totalAverage: '30.5',
- yAxisLimit: 100,
- },
- disk_fs_size: {
- format: 'bytes',
- total: 200,
- totalAverage: '200.0',
- yAxisLimit: 100,
- },
- disk_io_consumption: {
- format: 'percent',
- total: 210,
- totalAverage: '70.2',
- yAxisLimit: 100,
- },
- },
- }
- const result = parseInfrastructureMetrics(mockResponse)
- expect(result).toEqual({
- cpu: { current: 50.5, max: 100 },
- ram: { current: 60.8, max: 100 },
- disk: { current: 30.55, max: 100 },
- diskIo: { current: 70.2, max: 100 },
- })
- })
- it('returns 0 for missing metrics', () => {
- const mockResponse: InfraMonitoringMultiResponse = {
- data: [],
- series: {},
- }
- const result = parseInfrastructureMetrics(mockResponse)
- expect(result).toEqual({
- cpu: { current: 0, max: 100 },
- ram: { current: 0, max: 100 },
- disk: { current: 0, max: 100 },
- diskIo: { current: 0, max: 100 },
- })
- })
- it('handles partial metrics data', () => {
- const mockResponse: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- avg_cpu_usage: { format: 'percent', total: 150, totalAverage: 50, yAxisLimit: 100 },
- },
- }
- const result = parseInfrastructureMetrics(mockResponse)
- expect(result).toEqual({
- cpu: { current: 50, max: 100 },
- ram: { current: 0, max: 100 },
- disk: { current: 0, max: 100 },
- diskIo: { current: 0, max: 100 },
- })
- })
- it('handles single-response format (legacy)', () => {
- const mockResponse: InfraMonitoringSingleResponse = {
- data: [],
- format: 'percent',
- total: 150,
- totalAverage: 50,
- yAxisLimit: 100,
- }
- const result = parseInfrastructureMetrics(mockResponse)
- expect(result).toEqual({
- cpu: { current: 0, max: 100 },
- ram: { current: 0, max: 100 },
- disk: { current: 0, max: 100 },
- diskIo: { current: 0, max: 100 },
- })
- })
- })
- describe('parseConnectionsData', () => {
- it('returns zeros when data is undefined', () => {
- expect(parseConnectionsData(undefined, undefined)).toEqual({ current: 0, max: 0 })
- expect(parseConnectionsData(undefined, { maxConnections: 100 })).toEqual({
- current: 0,
- max: 0,
- })
- })
- it('parses connections data correctly', () => {
- const mockInfraData: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- pg_stat_database_num_backends: {
- format: 'number',
- total: 150,
- totalAverage: 25.5,
- yAxisLimit: 100,
- },
- },
- }
- const mockMaxData = { maxConnections: 100 }
- const result = parseConnectionsData(mockInfraData, mockMaxData)
- expect(result).toEqual({ current: 26, max: 100 }) // 25.5 rounded to 26
- })
- it('handles string values for connections', () => {
- const mockInfraData: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- pg_stat_database_num_backends: {
- format: 'number',
- total: 150,
- totalAverage: '30.7',
- yAxisLimit: 100,
- },
- },
- }
- const mockMaxData = { maxConnections: 100 }
- const result = parseConnectionsData(mockInfraData, mockMaxData)
- expect(result).toEqual({ current: 31, max: 100 }) // 30.7 rounded to 31
- })
- it('returns current connections even when maxConnectionsData is undefined', () => {
- const mockInfraData: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- pg_stat_database_num_backends: {
- format: 'number',
- total: 150,
- totalAverage: 25,
- yAxisLimit: 100,
- },
- },
- }
- const result = parseConnectionsData(mockInfraData, undefined)
- expect(result).toEqual({ current: 25, max: 0 })
- })
- it('returns 0 max when maxConnections is missing from data object', () => {
- const mockInfraData: InfraMonitoringMultiResponse = {
- data: [],
- series: {
- pg_stat_database_num_backends: {
- format: 'number',
- total: 150,
- totalAverage: 25,
- yAxisLimit: 100,
- },
- },
- }
- const mockMaxData = {}
- const result = parseConnectionsData(mockInfraData, mockMaxData)
- expect(result).toEqual({ current: 25, max: 0 })
- })
- it('returns 0 current when connections metric is missing', () => {
- const mockInfraData: InfraMonitoringMultiResponse = {
- data: [],
- series: {},
- }
- const mockMaxData = { maxConnections: 100 }
- const result = parseConnectionsData(mockInfraData, mockMaxData)
- expect(result).toEqual({ current: 0, max: 100 })
- })
- it('handles single-response format (legacy)', () => {
- const mockInfraData: InfraMonitoringSingleResponse = {
- data: [],
- format: 'number',
- total: 150,
- totalAverage: 25,
- yAxisLimit: 100,
- }
- const mockMaxData = { maxConnections: 100 }
- const result = parseConnectionsData(mockInfraData, mockMaxData)
- expect(result).toEqual({ current: 0, max: 100 })
- })
- })
|