[READ-ONLY] Mirror of https://github.com/FoxxMD/komodo-utilities. Small utilities to enhance Komodo
gotify komodo
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: Enable user-controlled severity level prefix inclusion

FoxxMD (Nov 26, 2024, 4:57 PM UTC) b7b1ad83 f12a5f13

+73 -25
+13
common/utils.ts
··· 8 8 minimumFractionDigits: min, 9 9 }).format(num); 10 10 }; 11 + 12 + export const valToBoolean = (val: string | undefined): boolean | undefined => { 13 + if (val === undefined || val.trim() === "") { 14 + return undefined; 15 + } 16 + const clean = val.trim().toLocaleLowerCase(); 17 + if (clean === "1" || clean === "true") { 18 + return true; 19 + } else if (clean === "0" || clean === "false") { 20 + return false; 21 + } 22 + throw new Error(`Expected truthy value but got '${val}'`); 23 + };
+10 -1
notifiers/gotify/main.ts
··· 2 2 import { gotify } from "npm:gotify@1.1.0"; 3 3 import { CommonAlert, parseAlert } from "../common/alertParser.ts"; 4 4 import { titleAndSubtitle } from "../common/notifierBuilder.ts"; 5 + import { valToBoolean } from "../../common/utils.ts"; 5 6 6 7 const GOTIFY_URL: string = Deno.env.get("GOTIFY_URL") as string; 7 8 if (GOTIFY_URL === undefined || GOTIFY_URL.trim() === "") { ··· 43 44 ); 44 45 } 45 46 47 + let levelInTitle: boolean | undefined; 48 + try { 49 + levelInTitle = valToBoolean(Deno.env.get('LEVEL_IN_TITLE')); 50 + } catch (e) { 51 + console.warn('Could not parse LEVEL_IN_TITLE to a truthy value, will use notifier default', {cause: e}); 52 + } 53 + 54 + 46 55 const pushAlert = async (title: string, message: string, priority: number): Promise<any> => { 47 56 try { 48 57 await gotify({ ··· 66 75 let data: CommonAlert; 67 76 68 77 try { 69 - data = parseAlert(alert); 78 + data = parseAlert(alert, {levelInTitle}); 70 79 } catch (e) { 71 80 console.error(e); 72 81 return new Response();
+9 -1
notifiers/ntfy/main.ts
··· 2 2 import { Config, publish } from "npm:ntfy@1.7.0"; 3 3 import { CommonAlert, parseAlert } from "../common/alertParser.ts"; 4 4 import { titleAndSubtitle } from "../common/notifierBuilder.ts"; 5 + import { valToBoolean } from "../../common/utils.ts"; 5 6 6 7 7 8 const NTFY_URL: string = Deno.env.get("NTFY_URL") as string; ··· 67 68 ); 68 69 } 69 70 71 + let levelInTitle: boolean | undefined; 72 + try { 73 + levelInTitle = valToBoolean(Deno.env.get('LEVEL_IN_TITLE')); 74 + } catch (e) { 75 + console.warn('Could not parse LEVEL_IN_TITLE to a truthy value, will use notifier default', {cause: e}); 76 + } 77 + 70 78 const pushAlert = async (title: string, message: string, priority: number): Promise<any> => { 71 79 try { 72 80 const req: Config = { ··· 99 107 let data: CommonAlert; 100 108 101 109 try { 102 - data = parseAlert(alert); 110 + data = parseAlert(alert, {levelInTitle}); 103 111 } catch (e) { 104 112 console.error(e); 105 113 return new Response();
+41 -23
notifiers/tests/alertParser.test.ts
··· 4 4 import { formatNumber } from "../../common/utils.ts"; 5 5 6 6 Deno.test({ 7 - name: "Parses server cpu without decimals", 8 - fn() { 9 - const data = parseAlert(ServerCPU); 10 - expect(data.message).toContain(formatNumber(ServerCPU.data.data.percentage, {max: 0})); 11 - }, 12 - }); 13 - 7 + name: "Parses server cpu without decimals", 8 + fn() { 9 + const data = parseAlert(ServerCPU); 10 + expect(data.message).toContain( 11 + formatNumber(ServerCPU.data.data.percentage, { max: 0 }), 12 + ); 13 + }, 14 + }); 14 15 15 - Deno.test({ 16 - name: "Parses disk GB with 2 decimals", 17 - fn() { 18 - const data = parseAlert(ServerDisk); 19 - expect(data.message).toContain(formatNumber(ServerDisk.data.data.used_gb, {max: 2})); 20 - expect(data.message).toContain(formatNumber(ServerDisk.data.data.total_gb, {max: 2})); 21 - }, 22 - }); 16 + Deno.test({ 17 + name: "Parses disk GB with 2 decimals", 18 + fn() { 19 + const data = parseAlert(ServerDisk); 20 + expect(data.message).toContain( 21 + formatNumber(ServerDisk.data.data.used_gb, { max: 2 }), 22 + ); 23 + expect(data.message).toContain( 24 + formatNumber(ServerDisk.data.data.total_gb, { max: 2 }), 25 + ); 26 + }, 27 + }); 23 28 24 - Deno.test({ 25 - name: "Parses memory GB with 2 decimals", 26 - fn() { 27 - const data = parseAlert(ServerMem); 28 - expect(data.message).toContain(formatNumber(ServerMem.data.data.used_gb, {max: 2})); 29 - expect(data.message).toContain(formatNumber(ServerMem.data.data.total_gb, {max: 2})); 30 - }, 31 - }); 29 + Deno.test({ 30 + name: "Parses memory GB with 2 decimals", 31 + fn() { 32 + const data = parseAlert(ServerMem); 33 + expect(data.message).toContain( 34 + formatNumber(ServerMem.data.data.used_gb, { max: 2 }), 35 + ); 36 + expect(data.message).toContain( 37 + formatNumber(ServerMem.data.data.total_gb, { max: 2 }), 38 + ); 39 + }, 40 + }); 41 + 42 + 43 + Deno.test({ 44 + name: "Parses level in title based on options", 45 + fn() { 46 + expect(parseAlert(ServerCPU, {levelInTitle: false}).title).not.toContain(`[${ServerCPU.level}]`); 47 + expect(parseAlert(ServerCPU).title).toContain(`[${ServerCPU.level}]`); 48 + }, 49 + });