| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Alert} from 'isl/src/types'; |
| 9 | |
| 10 | /** Given raw json output from `sl config`, parse Alerts */ |
| 11 | export function parseAlerts(rawConfigs: Array<{name: string; value: unknown}>): Array<Alert> { |
| 12 | // we get back configs with their keys as prefixes |
| 13 | // [ {name: "alerts.S11111.title", value: "alert 1"}, {name: "alerts.S22222.title", value: "alert 2"}, ...] |
| 14 | const alertMap = new Map<string, Partial<Alert>>(); |
| 15 | for (const entry of Object.values(rawConfigs)) { |
| 16 | const {name, value} = entry; |
| 17 | const [, key, suffix] = name.split('.'); |
| 18 | if (!suffix) { |
| 19 | continue; |
| 20 | } |
| 21 | const existing = alertMap.get(key) ?? {key}; |
| 22 | (existing as {[key: string]: unknown})[suffix] = |
| 23 | suffix === 'show-in-isl' ? value == 'true' : value; |
| 24 | alertMap.set(key, existing); |
| 25 | } |
| 26 | |
| 27 | return [...alertMap.values()].filter( |
| 28 | (entry): entry is Alert => |
| 29 | !!entry.title && |
| 30 | !!entry.description && |
| 31 | !!entry.severity && |
| 32 | !!entry.url && |
| 33 | entry['show-in-isl'] === true, |
| 34 | ); |
| 35 | } |
| 36 | |