Skip to content

Commit

Permalink
[GEN-1843]: restructure webapp/functions/* dir (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenElferink authored Dec 4, 2024
1 parent f554451 commit aacf22e
Show file tree
Hide file tree
Showing 26 changed files with 262 additions and 289 deletions.
58 changes: 0 additions & 58 deletions frontend/webapp/utils/functions/formatters.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export function cleanObjectEmptyStringsValues(obj: Record<string, any>): Record<string, any> {
const cleanArray = (arr: any[]): any[] =>
arr.filter((item) => {
if (typeof item === 'object' && item !== null) {
return item.key !== '' && item.value !== '';
}
return item !== '';
});

const cleanObject = (o: Record<string, any>): Record<string, any> =>
Object.fromEntries(
Object.entries(o)
.filter(([key, value]) => key !== '' && value !== '')
.map(([key, value]) => {
if (Array.isArray(value)) return [key, cleanArray(value)];
else if (typeof value === 'object' && value !== null) return [key, cleanObject(value)];
return [key, value];
}),
);

return Object.entries(obj).reduce((acc, [key, value]) => {
try {
const parsed = JSON.parse(value);
if (Array.isArray(parsed)) {
acc[key] = JSON.stringify(cleanArray(parsed));
} else if (typeof parsed === 'object' && parsed !== null) {
acc[key] = JSON.stringify(cleanObject(parsed));
} else {
acc[key] = value;
}
} catch (error) {
// Handle non-stringified objects or arrays directly
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) acc[key] = JSON.stringify(cleanArray(value));
else acc[key] = JSON.stringify(cleanObject(value));
} else {
// In case JSON.parse fails, assume value is a plain string or non-object/array
acc[key] = value;
}
}
return acc;
}, {} as Record<string, any>);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ExportedSignals } from '@/types';

export const extractMonitors = (exportedSignals: ExportedSignals) => {
const filtered = Object.keys(exportedSignals).filter((signal) => exportedSignals[signal] === true);

return filtered;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const formatBytes = (bytes?: number) => {
if (!bytes) return '0 KB/s';

const sizes = ['Bytes/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const value = bytes / Math.pow(1024, i);

return `${value.toFixed(i === 0 ? 0 : 1)} ${sizes[i]}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OVERVIEW_ENTITY_TYPES, type WorkloadId } from '@/types';

export const getIdFromSseTarget = (target: string, type: OVERVIEW_ENTITY_TYPES) => {
switch (type) {
case OVERVIEW_ENTITY_TYPES.SOURCE: {
const id: WorkloadId = {
namespace: '',
name: '',
kind: '',
};

target.split('&').forEach((str) => {
const [key, value] = str.split('=');
id[key] = value;
});

return id;
}

default:
return target as string;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { OVERVIEW_ENTITY_TYPES, type WorkloadId } from '@/types';

export const getSseTargetFromId = (id: string | WorkloadId, type: OVERVIEW_ENTITY_TYPES) => {
switch (type) {
case OVERVIEW_ENTITY_TYPES.SOURCE: {
let target = '';

Object.entries(id as WorkloadId).forEach(([key, value]) => {
target += `${key}=${value}&`;
});

target.slice(0, -1);

return target;
}

default:
return id as string;
}
};
8 changes: 8 additions & 0 deletions frontend/webapp/utils/functions/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './clean-object-empty-strings-values';
export * from './extract-monitors';
export * from './format-bytes';
export * from './get-id-from-sse-target';
export * from './get-sse-target-from-id';
export * from './parse-json-string-to-pretty-string';
export * from './safe-json-parse';
export * from './stringify-non-string-values';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const parseJsonStringToPrettyString = (value: string) => {
let str = '';

try {
const parsed = JSON.parse(value);

// Handle arrays
if (Array.isArray(parsed)) {
str = parsed
.map((item) => {
if (typeof item === 'object' && item !== null) return `${item.key}: ${item.value}`;
else return item;
})
.join(', ');
}

// Handle objects (non-array JSON objects)
else if (typeof parsed === 'object' && parsed !== null) {
str = Object.entries(parsed)
.map(([key, val]) => `${key}: ${val}`)
.join(', ');
}

// Should never reach this if it's a string (it will throw)
else {
str = value;
}
} catch (error) {
str = value;
}

return str;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function safeJsonParse<T>(str: string | undefined, fallback: T): T {
if (!str) return fallback;
try {
const parsed = JSON.parse(str) as T;
return parsed;
} catch (e) {
console.error('Error parsing JSON string:', e);
return fallback;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function stringifyNonStringValues(obj: Record<string, any>): Record<string, string> {
return Object.entries(obj).reduce((acc, [key, value]) => {
// Check if the value is already a string
if (typeof value === 'string') {
acc[key] = value;
} else {
// If not, stringify the value
acc[key] = JSON.stringify(value);
}
return acc;
}, {} as Record<string, string>);
}
69 changes: 0 additions & 69 deletions frontend/webapp/utils/functions/icons.ts

This file was deleted.

15 changes: 15 additions & 0 deletions frontend/webapp/utils/functions/icons/get-action-icon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type ActionsType } from '@/types';

const BRAND_ICON = '/brand/odigos-icon.svg';

export const getActionIcon = (type?: ActionsType | 'sampler' | 'attributes') => {
if (!type) return BRAND_ICON;

const typeLowerCased = type.toLowerCase();
const isSampler = typeLowerCased.includes('sampler');
const isAttributes = typeLowerCased === 'attributes';

const iconName = isSampler ? 'sampler' : isAttributes ? 'piimasking' : typeLowerCased;

return `/icons/actions/${iconName}.svg`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OVERVIEW_ENTITY_TYPES } from '@/types';

const BRAND_ICON = '/brand/odigos-icon.svg';

export const getEntityIcon = (type?: OVERVIEW_ENTITY_TYPES) => {
if (!type) return BRAND_ICON;

return `/icons/overview/${type}s.svg`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { WORKLOAD_PROGRAMMING_LANGUAGES } from '@/utils';
import { type SourceContainer } from '@/types';

const BRAND_ICON = '/brand/odigos-icon.svg';

export const getProgrammingLanguageIcon = (language?: SourceContainer['language']) => {
if (!language) return BRAND_ICON;

const BASE_URL = 'https://d1n7d4xz7fr8b4.cloudfront.net/';
const LANGUAGES_LOGOS: Record<WORKLOAD_PROGRAMMING_LANGUAGES, string> = {
[WORKLOAD_PROGRAMMING_LANGUAGES.JAVA]: `${BASE_URL}java.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.GO]: `${BASE_URL}go.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.JAVASCRIPT]: `${BASE_URL}nodejs.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.PYTHON]: `${BASE_URL}python.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.DOTNET]: `${BASE_URL}dotnet.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.MYSQL]: `${BASE_URL}mysql.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.NGINX]: `${BASE_URL}nginx.svg`,
[WORKLOAD_PROGRAMMING_LANGUAGES.IGNORED]: BRAND_ICON, // TODO: good icon
[WORKLOAD_PROGRAMMING_LANGUAGES.UNKNOWN]: BRAND_ICON, // TODO: good icon
[WORKLOAD_PROGRAMMING_LANGUAGES.PROCESSING]: BRAND_ICON, // TODO: good icon
[WORKLOAD_PROGRAMMING_LANGUAGES.NO_CONTAINERS]: BRAND_ICON, // TODO: good icon
[WORKLOAD_PROGRAMMING_LANGUAGES.NO_RUNNING_PODS]: BRAND_ICON, // TODO: good icon
};

return LANGUAGES_LOGOS[language] || BRAND_ICON;
};
11 changes: 11 additions & 0 deletions frontend/webapp/utils/functions/icons/get-rule-icon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type InstrumentationRuleType } from '@/types';

const BRAND_ICON = '/brand/odigos-icon.svg';

export const getRuleIcon = (type?: InstrumentationRuleType) => {
if (!type) return BRAND_ICON;

const typeLowerCased = type.replaceAll('-', '').toLowerCase();

return `/icons/rules/${typeLowerCased}.svg`;
};
20 changes: 20 additions & 0 deletions frontend/webapp/utils/functions/icons/get-status-icon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type NotificationType } from '@/types';

const BRAND_ICON = '/brand/odigos-icon.svg';

export const getStatusIcon = (status?: NotificationType) => {
if (!status) return BRAND_ICON;

switch (status) {
case 'success':
return '/icons/notification/success-icon.svg';
case 'error':
return '/icons/notification/error-icon2.svg';
case 'warning':
return '/icons/notification/warning-icon2.svg';
case 'info':
return '/icons/common/info.svg';
default:
return BRAND_ICON;
}
};
5 changes: 5 additions & 0 deletions frontend/webapp/utils/functions/icons/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './get-action-icon';
export * from './get-entity-icon';
export * from './get-programming-language-icon';
export * from './get-rule-icon';
export * from './get-status-icon';
Loading

0 comments on commit aacf22e

Please sign in to comment.