Skip to content

Commit

Permalink
Merge pull request #348 from prymitive/fix-silence
Browse files Browse the repository at this point in the history
fix(ui): use new style alertmanager input values
  • Loading branch information
prymitive authored Jan 1, 2019
2 parents 14b2d1d + abd14bb commit ef5af9a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
12 changes: 5 additions & 7 deletions ui/src/Components/SilenceModal/AlertManagerInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import { observer } from "mobx-react";
import ReactSelect from "react-select";

import { AlertStore } from "Stores/AlertStore";
import { SilenceFormStore } from "Stores/SilenceFormStore";
import {
SilenceFormStore,
AlertmanagerClustersToOption
} from "Stores/SilenceFormStore";
import { MultiSelect, ReactSelectStyles } from "Components/MultiSelect";
import { ValidationError } from "Components/MultiSelect/ValidationError";

const AlertmanagerClustersToOption = clusterDict =>
Object.entries(clusterDict).map(([clusterID, clusterMembers]) => ({
label: clusterMembers.join(" | "),
value: clusterMembers
}));

const AlertManagerInput = observer(
class AlertManagerInput extends MultiSelect {
static propTypes = {
Expand Down Expand Up @@ -77,6 +74,7 @@ const AlertManagerInput = observer(
options={AlertmanagerClustersToOption(
alertStore.data.upstreams.clusters
)}
getOptionValue={JSON.stringify}
placeholder={
silenceFormStore.data.wasValidated ? (
<ValidationError />
Expand Down
4 changes: 1 addition & 3 deletions ui/src/Components/SilenceModal/SilenceForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ describe("<SilenceForm />", () => {
matcher.name = "job";
matcher.values = [{ label: "node_exporter", value: "node_exporter" }];
silenceFormStore.data.matchers = [matcher];
silenceFormStore.data.alertmanagers = [
{ label: "am1", value: "http://example.com" }
];
silenceFormStore.data.alertmanagers = [{ label: "am1", value: ["am1"] }];
silenceFormStore.data.author = "[email protected]";
silenceFormStore.data.comment = "fake silence";
const tree = ShallowSilenceForm();
Expand Down
28 changes: 28 additions & 0 deletions ui/src/Components/SilenceModal/SilencePreview/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ describe("<SilencePreview />", () => {
expect(fetch).toHaveBeenCalled();
});

it("fetch uses correct filters with single Alertmanager instance", async () => {
fetch.mockResponse(JSON.stringify(MockAPIResponse()));
silenceFormStore.data.alertmanagers = [
{ label: "amName", value: ["amValue"] }
];

const tree = MountedSilencePreview();
await expect(tree.instance().matchedAlerts.fetch).resolves.toBeUndefined();
expect(fetch).toHaveBeenCalledWith(
"./alerts.json?q=foo%3Dbar&q=%40alertmanager%3D~%5E%28amValue%29%24",
{ credentials: "include" }
);
});

it("fetch uses correct filters with multiple Alertmanager instances", async () => {
fetch.mockResponse(JSON.stringify(MockAPIResponse()));
silenceFormStore.data.alertmanagers = [
{ label: "cluster", value: ["am1", "am2"] }
];

const tree = MountedSilencePreview();
await expect(tree.instance().matchedAlerts.fetch).resolves.toBeUndefined();
expect(fetch).toHaveBeenCalledWith(
"./alerts.json?q=foo%3Dbar&q=%40alertmanager%3D~%5E%28am1%7Cam2%29%24",
{ credentials: "include" }
);
});

it("matches snapshot", async () => {
fetch.mockResponse(JSON.stringify(MockAPIResponse()));

Expand Down
23 changes: 14 additions & 9 deletions ui/src/Stores/SilenceFormStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const NewEmptyMatcher = () => {

const MatcherValueToObject = value => ({ label: value, value: value });

const AlertmanagerClustersToOption = clusterDict =>
Object.entries(clusterDict).map(([clusterID, clusterMembers]) => ({
label: clusterMembers.join(" | "),
value: clusterMembers
}));

const SilenceFormStage = Object.freeze({
UserInput: "form",
Preview: "preview",
Expand Down Expand Up @@ -155,12 +161,10 @@ class SilenceFormStore {

fillFormFromSilence(alertmanager, silence) {
this.silenceID = silence.id;
this.alertmanagers = [
{
label: alertmanager.name,
value: alertmanager.publicURI
}
];

this.alertmanagers = AlertmanagerClustersToOption({
[alertmanager.cluster]: alertmanager.clusterMembers
});

const matchers = [];
for (const m of silence.matchers) {
Expand Down Expand Up @@ -214,8 +218,8 @@ class SilenceFormStore {
m.values.length > 1
? `(${m.values.map(v => v.value).join("|")})`
: m.values.length === 1
? m.values[0].value
: "",
? m.values[0].value
: "",
isRegex: m.isRegex
})),
startsAt: this.startsAt
Expand Down Expand Up @@ -270,5 +274,6 @@ export {
SilenceFormStore,
SilenceFormStage,
NewEmptyMatcher,
MatcherValueToObject
MatcherValueToObject,
AlertmanagerClustersToOption
};
4 changes: 2 additions & 2 deletions ui/src/Stores/SilenceFormStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const MockGroup = () => {

const MockAlertmanagerOption = () => ({
label: "default",
value: "http://localhost"
value: ["default"]
});

const MockMatcher = (name, values) => {
Expand Down Expand Up @@ -209,7 +209,7 @@ describe("SilenceFormStore.data", () => {
expect(store.data.alertmanagers).toHaveLength(1);
expect(store.data.alertmanagers[0]).toMatchObject({
label: alertmanager.name,
value: alertmanager.publicURI
value: [alertmanager.name]
});

expect(store.data.matchers).toHaveLength(2);
Expand Down

0 comments on commit ef5af9a

Please sign in to comment.