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

Notify user when cluster health changes #1908

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions assets/js/state/sagas/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CLUSTER_RESTORED,
appendCluster,
removeCluster,
updateClusterHealth,
} from '@state/clusters';

export function* clusterDeregistered({ payload: { name, id } }) {
Expand All @@ -28,6 +29,22 @@ export function* clusterRestored({ payload }) {
);
}

export function* clusterHealthChanged({
payload: { cluster_id, name, health },
}) {
yield put(updateClusterHealth({ cluster_id, name, health }));
yield put(
notify({
text: `Cluster ${name} health changed to ${health}.`,
icon: 'ℹ️',
})
);
}

export function* watchClusterHealthChanged() {
yield takeEvery('CLUSTER_HEALTH_CHANGED', clusterHealthChanged);
nelsonkopliku marked this conversation as resolved.
Show resolved Hide resolved
}

export function* watchClusterDeregistered() {
yield takeEvery(CLUSTER_DEREGISTERED, clusterDeregistered);
}
Expand Down
28 changes: 26 additions & 2 deletions assets/js/state/sagas/clusters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { recordSaga } from '@lib/test-utils';

import { clusterFactory } from '@lib/test-utils/factories';

import { clusterDeregistered, clusterRestored } from '@state/sagas/clusters';
import { removeCluster, appendCluster } from '@state/clusters';
import {
clusterDeregistered,
clusterRestored,
clusterHealthChanged,
} from '@state/sagas/clusters';
import {
removeCluster,
appendCluster,
updateClusterHealth,
} from '@state/clusters';
import { notify } from '@state/actions/notifications';

describe('Clusters sagas', () => {
Expand All @@ -30,4 +38,20 @@ describe('Clusters sagas', () => {
}),
]);
});

it('should update health status of a cluster', async () => {
const { id: cluster_id, name, health } = clusterFactory.build();

const dispatched = await recordSaga(clusterHealthChanged, {
payload: { cluster_id, name, health },
});

expect(dispatched).toEqual([
updateClusterHealth({ cluster_id, name, health }),
notify({
text: `Cluster ${name} health changed to ${health}.`,
icon: 'ℹ️',
}),
]);
});
});
10 changes: 1 addition & 9 deletions assets/js/state/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
updateCluster,
updateCibLastWritten,
updateChecksResults,
updateClusterHealth,
startClustersLoading,
stopClustersLoading,
} from '@state/clusters';
Expand Down Expand Up @@ -84,6 +83,7 @@ import {
import {
watchClusterDeregistered,
watchClusterRestored,
watchClusterHealthChanged,
} from '@state/sagas/clusters';
import {
watchUpdateLastExecution,
Expand Down Expand Up @@ -287,14 +287,6 @@ function* watchChecksResultsUpdated() {
yield takeEvery('CHECKS_RESULTS_UPDATED', checksResultsUpdated);
}

function* clusterHealthChanged({ payload }) {
yield put(updateClusterHealth(payload));
}

function* watchClusterHealthChanged() {
yield takeEvery('CLUSTER_HEALTH_CHANGED', clusterHealthChanged);
}

function* refreshHealthSummaryOnComponentsHealthChange() {
const debounceDuration = 5000;

Expand Down
9 changes: 4 additions & 5 deletions lib/trento/application/projectors/cluster_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ defmodule Trento.ClusterProjector do
})
end

def after_update(%ClusterHealthChanged{cluster_id: cluster_id, health: health}, _, _) do
TrentoWeb.Endpoint.broadcast("monitoring:clusters", "cluster_health_changed", %{
cluster_id: cluster_id,
health: health
})
def after_update(%ClusterHealthChanged{}, _, %{cluster: %ClusterReadModel{} = cluster}) do
message = ClusterView.render("cluster_health_changed.json", %{cluster: cluster})

TrentoWeb.Endpoint.broadcast("monitoring:clusters", "cluster_health_changed", message)
end

@impl true
Expand Down
4 changes: 4 additions & 0 deletions lib/trento_web/views/v2/cluster_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ defmodule TrentoWeb.V2.ClusterView do
|> Map.delete(:cluster_id)
|> Map.put(:id, data.cluster_id)
end

def render("cluster_health_changed.json", %{cluster: %{id: id, name: name, health: health}}) do
%{cluster_id: id, name: name, health: health}
end
end
9 changes: 3 additions & 6 deletions test/trento/application/projectors/cluster_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,9 @@ defmodule Trento.ClusterProjectorTest do
end

test "should broadcast cluster_health_changed after the ClusterHealthChanged event" do
insert(:cluster, id: cluster_id = Faker.UUID.v4())
%{id: cluster_id, name: name, health: health} = insert(:cluster)

event = %ClusterHealthChanged{
cluster_id: cluster_id,
health: :passing
}
event = %ClusterHealthChanged{cluster_id: cluster_id, health: health}

ProjectorTestHelper.project(
ClusterProjector,
Expand All @@ -255,7 +252,7 @@ defmodule Trento.ClusterProjectorTest do
)

assert_broadcast "cluster_health_changed",
%{cluster_id: ^cluster_id, health: :passing},
%{cluster_id: ^cluster_id, name: ^name, health: ^health},
1000
end
end
17 changes: 17 additions & 0 deletions test/trento_web/views/v2/cluster_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule TrentoWeb.V2.ClusterViewTest do
use TrentoWeb.ConnCase, async: true

import Phoenix.View
import Trento.Factory

alias TrentoWeb.V2.ClusterView

alias Trento.ClusterReadModel

test "should render health changed relevant information" do
%ClusterReadModel{id: id, name: name, health: health} = cluster = build(:cluster)

assert %{cluster_id: id, name: name, health: health} ==
render(ClusterView, "cluster_health_changed.json", %{cluster: cluster})
end
end