-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEN-1843]: restructure webapp/functions/* dir (#1902)
- Loading branch information
1 parent
f554451
commit aacf22e
Showing
26 changed files
with
262 additions
and
289 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
frontend/webapp/utils/functions/formatters/clean-object-empty-strings-values/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
} |
7 changes: 7 additions & 0 deletions
7
frontend/webapp/utils/functions/formatters/extract-monitors/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
9 changes: 9 additions & 0 deletions
9
frontend/webapp/utils/functions/formatters/format-bytes/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}`; | ||
}; |
23 changes: 23 additions & 0 deletions
23
frontend/webapp/utils/functions/formatters/get-id-from-sse-target/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
20 changes: 20 additions & 0 deletions
20
frontend/webapp/utils/functions/formatters/get-sse-target-from-id/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
33 changes: 33 additions & 0 deletions
33
frontend/webapp/utils/functions/formatters/parse-json-string-to-pretty-string/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
10 changes: 10 additions & 0 deletions
10
frontend/webapp/utils/functions/formatters/safe-json-parse/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
frontend/webapp/utils/functions/formatters/stringify-non-string-values/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
} |
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
frontend/webapp/utils/functions/icons/get-action-icon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
}; |
9 changes: 9 additions & 0 deletions
9
frontend/webapp/utils/functions/icons/get-entity-icon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
}; |
26 changes: 26 additions & 0 deletions
26
frontend/webapp/utils/functions/icons/get-programming-language-icon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
frontend/webapp/utils/functions/icons/get-rule-icon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
frontend/webapp/utils/functions/icons/get-status-icon/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.