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(alerts): update to display all alerts #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
35 changes: 23 additions & 12 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ export const opsgenieApiRef = createApiRef<Opsgenie>({
id: 'plugin.opsgenie.service',
});

type AlertsFetchOpts = {
limit?: number
query?: string
}

type IncidentsFetchOpts = {
type fetchOpts = {
limit?: number
query?: string;
sort?: string;
order?: string;
}

export interface Opsgenie {
getAlerts(opts?: AlertsFetchOpts): Promise<Alert[]>;
getIncidents(opts?: IncidentsFetchOpts): Promise<Incident[]>;
getAlerts(opts?: fetchOpts): Promise<Alert[]>;
getIncidents(opts?: fetchOpts): Promise<Incident[]>;

getAlertDetailsURL(alert: Alert): string;

Expand All @@ -39,6 +34,11 @@ export interface Opsgenie {

interface AlertsResponse {
data: Alert[];
paging: {
first: string;
next?: string;
last: string;
};
}

interface IncidentsResponse {
Expand Down Expand Up @@ -118,15 +118,26 @@ export class OpsgenieApi implements Opsgenie {
if (!resp.ok) throw new Error(`Request failed with ${resp.status}: ${resp.statusText}`);
}

async getAlerts(opts?: AlertsFetchOpts): Promise<Alert[]> {
async getAlerts(opts?: fetchOpts): Promise<Alert[]> {
const limit = opts?.limit || 50;
const sort = opts?.sort || 'createdAt';
const order = opts?.order || 'desc';
const query = opts?.query ? `&query=${opts?.query}` : '';
const response = await this.fetch<AlertsResponse>(`/v2/alerts?limit=${limit}${query}`);

return response.data;
let response = await this.fetch<AlertsResponse>(`/v2/alerts?limit=${limit}&sort=${sort}&order=${order}${query}`);
let alerts = response.data;

while (response.paging.next) {
Copy link
Owner

Choose a reason for hiding this comment

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

This will change the behavior of the EntityOpsgenieAlertsCard component.
It currently displays only a maximum of 3 alerts for a given entity, but this change will make it display every alerts related to that entity.

This is because AFAIK, the limit parameter accepted by Opsgenie applies to a result page, not the overall search.

const parsedUrl = new URL(response.paging.next);
response = await this.fetch(parsedUrl.pathname + parsedUrl.search);

alerts = alerts.concat(response.data);
}

return alerts;
}

async getIncidents(opts?: IncidentsFetchOpts): Promise<Incident[]> {
async getIncidents(opts?: fetchOpts): Promise<Incident[]> {
const limit = opts?.limit || 50;
const sort = opts?.sort || 'createdAt';
const order = opts?.order || 'desc';
Expand Down