Skip to content

Commit

Permalink
🐛 fixed bug with time for resolved alert
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohland committed Nov 27, 2023
1 parent bb5b7d4 commit 8d31f76
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "barky",
"version": "1.1.4",
"version": "1.1.5",
"description": "A simple cloud services watchdog with digest notification support & no external dependencies",
"homepage": "https://github.com/Rohland/barky#readme",
"main": "dist/cli.js",
Expand Down
30 changes: 30 additions & 0 deletions src/models/alerts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { AlertState } from "./alerts";
import { toLocalTimeString } from "../lib/utility";
import { Snapshot } from "./snapshot";
import { getTestSnapshot } from "./snapshot.spec";

describe("alert", () => {
describe("constructor", () => {
describe("when initialised", () => {
it("should bind all fields", async () => {
// arrange
const snapshot = getTestSnapshot();
const lastFailure = {
date: snapshot.date,
result: snapshot.last_result,
alert: snapshot.alert,
resolvedDate: new Date()
};

// act
const alert = new AlertState({
channel: "console",
start_date: new Date(new Date().getTime() - 1000 * 60 * 2),
last_alert_date: new Date(new Date().getTime() - 1000 * 60 * 2),
affected: JSON.stringify([["web|health|www.codeo.co.za", lastFailure]]),
});

// assert
const affected = alert.affected.get("web|health|www.codeo.co.za");
expect(affected.date).toEqual(lastFailure.date);
expect(affected.result).toEqual(lastFailure.result);
expect(affected.alert.getConfig()).toEqual(lastFailure.alert);
expect(affected.resolvedDate).toEqual(lastFailure.resolvedDate);
});
});
});
describe("endTime", () => {
describe("when called and not resolved", () => {
it("should return null", async () => {
Expand Down
25 changes: 17 additions & 8 deletions src/models/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { humanizeDuration } from "../lib/time";
import { AlertConfiguration } from "./alert_configuration";

export interface ILastFailureSnapshot {
resolvedDate?: Date;
date: Date;
result: string;
alert: AlertConfiguration;
Expand All @@ -22,14 +23,15 @@ export class AlertState {
private _wasMuted: boolean;

constructor(data: any) {
for(const key in data) {
if (key.indexOf("date")> -1){
for (const key in data) {
if (key.indexOf("date") > -1) {
this[key] = new Date(data[key]);
} else if (key === "affected") {
const json = data[key];
const map = json ? new Map<string, any>(JSON.parse(json)) : new Map<string, any>();
for (const [key, value] of map) {
this.affected.set(key, {
resolvedDate: value.resolvedDate ? new Date(value.resolvedDate) : value.resolvedDate,
date: new Date(value.date),
result: value.result,
alert: value.alert ? new AlertConfiguration(value.alert) : null
Expand Down Expand Up @@ -60,7 +62,7 @@ export class AlertState {
}

public get durationMinutes() {
return (new Date().valueOf() - this.start_date.valueOf()) / 1000 / 60;
return (new Date().valueOf() - this.start_date.valueOf()) / 1000 / 60;
}

public get durationHuman() {
Expand Down Expand Up @@ -98,14 +100,21 @@ export class AlertState {
})
}

public getResolvedOrMutedSnapshotList(currentIssueIds: string[]): { key: IUniqueKey, lastSnapshot: ILastFailureSnapshot}[] {
public getResolvedOrMutedSnapshotList(currentIssueIds: string[]): {
key: IUniqueKey,
lastSnapshot: ILastFailureSnapshot
}[] {
const all = this.affectedKeys;
const keys = all.filter(x => !currentIssueIds.includes(x));
return keys.map(x => {
return {
key: explodeUniqueKey(x),
lastSnapshot: this.affected.get(x)
}
const lastSnapshot = this.affected.get(x);
if (lastSnapshot) {
lastSnapshot.resolvedDate ||= new Date();
}
return {
key: explodeUniqueKey(x),
lastSnapshot: lastSnapshot
}
});
}

Expand Down
5 changes: 3 additions & 2 deletions src/models/channels/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ export class SlackChannelConfig extends ChannelConfig {
if (resolvedOrMuted.length > 0) {
parts.push(`*☑️ ${ resolvedOrMuted.length } resolved/muted ${ pluraliseWithS("check", resolvedOrMuted.length) }:*`);
resolvedOrMuted.forEach(x => {
const time = toLocalTimeString(x.lastSnapshot.date, { noSeconds: true });
const lastResult = x.lastSnapshot ? `(last failure at ${ time }: _${ x.lastSnapshot.result }_)` : "";
const time = x.lastSnapshot?.resolvedDate ? toLocalTimeString(x.lastSnapshot.resolvedDate, { noSeconds: true }) : null;
const timeString = time ? `resolved at ${ time }, ` : "";
const lastResult = x.lastSnapshot ? `(${ timeString } last failure: _${ x.lastSnapshot.result }_)` : "";
parts.push(` • ${ x.key.type }:${ x.key.label } → *${ x.key.identifier }* ${ lastResult } ${ this.generateLinks(x.lastSnapshot) }`);
});
parts.push("");
Expand Down

0 comments on commit 8d31f76

Please sign in to comment.