Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alert groups automatic refresh #4463

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface RemoteFiltersProps extends WithStoreProps {
grafanaTeamStore: GrafanaTeamStore;
skipFilterOptionFn?: (filterOption: FilterOption) => boolean;
}
interface RemoteFiltersState {
export interface RemoteFiltersState {
Copy link
Member Author

@teodosii teodosii Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting these to have the typing needed in Incidents for now. This might need some additional work as we're missing a lot of typescript on the filters, but it's out of scope for this PR.

filterOptions?: FilterOption[];
filters: FilterOption[];
values: Record<string, any>;
Expand Down
22 changes: 22 additions & 0 deletions grafana-plugin/src/models/alertgroup/alertgroup.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ActionKey } from 'models/loader/action-keys';
import { makeRequest } from 'network/network';
import { ApiSchemas } from 'network/oncall-api/api.types';
import { onCallApi } from 'network/oncall-api/http-client';
import { AutoLoadingState } from 'utils/decorators';

import { AlertGroupStore } from './alertgroup';

Expand Down Expand Up @@ -33,6 +35,26 @@ export class AlertGroupHelper {
return (await onCallApi().POST('/alertgroups/bulk_action/', { params: {}, body: data })).data;
}

@AutoLoadingState(ActionKey.INCIDENTS_BULK_UPDATE)
static async updateBulkActionAndRefresh(
data: ApiSchemas['AlertGroupBulkActionRequest'],
alertGroupStore: AlertGroupStore,
onFinally?: () => void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made more sense to be here rather than in the component, it also accepts AutoLoadingState

) {
try {
alertGroupStore.setLiveUpdatesPaused(true);

await AlertGroupHelper.bulkAction(data);

// pull new data
await alertGroupStore.fetchAlertGroups();
} finally {
alertGroupStore.setLiveUpdatesPaused(false);

onFinally?.();
}
brojd marked this conversation as resolved.
Show resolved Hide resolved
}

static async renderPreview(id: ApiSchemas['AlertGroup']['pk'], template_name: string, template_body: string) {
return (
await onCallApi().POST('/alertgroups/{id}/preview_template/', {
Expand Down
25 changes: 10 additions & 15 deletions grafana-plugin/src/models/alertgroup/alertgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class AlertGroupStore {
const newAlerts = new Map(
results.map((alert: ApiSchemas['AlertGroup']) => {
const oldAlert = this.alerts.get(alert.pk) || {};
const mergedAlertData = { ...oldAlert, ...alert, undoAction: alert.undoAction };
const mergedAlertData = { ...oldAlert, ...alert };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the undoAction, it didn't seem to help in any way 🤔

return [alert.pk, mergedAlertData];
})
);
Expand Down Expand Up @@ -114,7 +114,8 @@ export class AlertGroupStore {
this.rootStore.setPageTitle(`#${alertGroup.inside_organization_number} ${alertGroup.render_for_web.title}`);
}

async fetchIncidentsAndStats(isPollingJob = false) {
@AutoLoadingState(ActionKey.FETCH_INCIDENTS_AND_STATS)
async fetchIncidentsAndStats(isPollingJob = false): Promise<void> {
await Promise.all([
this.fetchStats(IncidentStatus.Firing),
this.fetchStats(IncidentStatus.Acknowledged),
Expand Down Expand Up @@ -218,13 +219,11 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async resolve(id: ApiSchemas['AlertGroup']['pk']) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi({ skipErrorHandling: true }).POST('/alertgroups/{id}/resolve/', {
params: { path: { id } },
});
this.updateAlert(id, {
...data,
undoAction: AlertAction.Resolve,
});
}

Expand All @@ -233,11 +232,9 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async unresolve(id: ApiSchemas['AlertGroup']['pk']) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi().POST('/alertgroups/{id}/unresolve/', { params: { path: { id } } });
this.updateAlert(id, {
...data,
undoAction: AlertAction.unResolve,
});
}

Expand All @@ -246,11 +243,9 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async acknowledge(id: ApiSchemas['AlertGroup']['pk']) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi().POST('/alertgroups/{id}/acknowledge/', { params: { path: { id } } });
this.updateAlert(id, {
...data,
undoAction: AlertAction.Acknowledge,
});
}

Expand All @@ -259,11 +254,9 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async unacknowledge(id: ApiSchemas['AlertGroup']['pk']) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi().POST('/alertgroups/{id}/unacknowledge/', { params: { path: { id } } });
this.updateAlert(id, {
...data,
undoAction: AlertAction.unAcknowledge,
});
}

Expand All @@ -272,14 +265,12 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async silence(id: ApiSchemas['AlertGroup']['pk'], delay: number) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi().POST('/alertgroups/{id}/silence/', {
params: { path: { id } },
body: { delay },
});
this.updateAlert(id, {
...data,
undoAction: AlertAction.Silence,
});
}

Expand All @@ -288,11 +279,9 @@ export class AlertGroupStore {
composeFailureMessageFn,
})
async unsilence(id: ApiSchemas['AlertGroup']['pk']) {
this.setLiveUpdatesPaused(true);
const { data } = await onCallApi().POST('/alertgroups/{id}/unsilence/', { params: { path: { id } } });
this.updateAlert(id, {
...data,
undoAction: AlertAction.unSilence,
});
}

Expand All @@ -305,8 +294,14 @@ export class AlertGroupStore {
[AlertAction.Resolve]: this.resolve,
[AlertAction.unResolve]: this.unresolve,
};

if (actionToMethodMap[action]) {
await actionToMethodMap[action](id, delay);
try {
this.setLiveUpdatesPaused(true);
await actionToMethodMap[action](id, delay);
} finally {
this.setLiveUpdatesPaused(false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Centralizing these 2 calls here rather than in each method

}
}
}

Expand Down
2 changes: 2 additions & 0 deletions grafana-plugin/src/models/loader/action-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export enum ActionKey {
FETCH_INCIDENTS = 'FETCH_INCIDENTS',
FETCH_INCIDENTS_POLLING = 'FETCH_INCIDENTS_POLLING',
FETCH_INCIDENTS_AND_STATS = 'FETCH_INCIDENTS_AND_STATS',
INCIDENTS_BULK_UPDATE = 'INCIDENTS_BULK_UPDATE',

UPDATE_FILTERS_AND_FETCH_INCIDENTS = 'UPDATE_FILTERS_AND_FETCH_INCIDENTS',
UPDATE_SERVICENOW_TOKEN = 'UPDATE_SERVICENOW_TOKEN',
FETCH_INTEGRATIONS = 'FETCH_INTEGRATIONS',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AlertAction } from 'models/alertgroup/alertgroup.types';

// Custom properties not exposed by OpenAPI schema should be defined here
export type CustomApiSchemas = {
Webhook: {
Expand All @@ -15,7 +13,6 @@ export type CustomApiSchemas = {
};
};
AlertGroup: {
undoAction: AlertAction;
loading?: boolean;
created_at?: string;
};
Expand Down
Loading
Loading