Skip to content

Commit

Permalink
Use upserts instead of appending SAP instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Aug 1, 2023
1 parent e530e90 commit 475fe2f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 38 deletions.
10 changes: 3 additions & 7 deletions assets/js/state/databases.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { filterByInstances, maybeUpdateInstanceHealth } from './instances';
import { upsertInstances, maybeUpdateInstanceHealth } from './instances';

const initialState = {
loading: false,
Expand Down Expand Up @@ -27,14 +27,11 @@ export const databasesListSlice = createSlice({
appendDatabase: (state, action) => {
state.databases = [...state.databases, action.payload];
},
appendDatabaseInstance: (state, action) => {
state.databaseInstances = [...state.databaseInstances, action.payload];
},
upsertDatabaseInstances: (state, action) => {
state.databaseInstances = filterByInstances(
state.databaseInstances = upsertInstances(
state.databaseInstances,
action.payload
).concat(action.payload);
);
},
removeDatabase: (state, { payload: { id } }) => {
state.databases = state.databases.filter(
Expand Down Expand Up @@ -123,7 +120,6 @@ export const {
appendDatabase,
removeDatabase,
removeDatabaseInstance,
appendDatabaseInstance,
upsertDatabaseInstances,
updateDatabaseHealth,
updateDatabaseInstanceHealth,
Expand Down
5 changes: 4 additions & 1 deletion assets/js/state/instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ const payloadMatchesInstance = (payload, instance) =>
payload.host_id === instance.host_id &&
payload.instance_number === instance.instance_number;

export const filterByInstances = (currentInstances, newInstances) =>
const filterByInstances = (currentInstances, newInstances) =>
currentInstances.filter((currentInstance) =>
newInstances.every(
(newInstance) => !payloadMatchesInstance(newInstance, currentInstance)
)
);

export const upsertInstances = (currentInstances, newInstances) =>
filterByInstances(currentInstances, newInstances).concat(newInstances);

export const maybeUpdateInstanceHealth = (payload, instance) => {
if (payloadMatchesInstance(payload, instance)) {
instance.health = payload.health;
Expand Down
7 changes: 3 additions & 4 deletions assets/js/state/sagas/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DATABASE_INSTANCE_HEALTH_CHANGED,
DATABASE_INSTANCE_SYSTEM_REPLICATION_CHANGED,
appendDatabase,
appendDatabaseInstance,
upsertDatabaseInstances,
updateDatabaseHealth,
updateDatabaseInstanceHealth,
Expand All @@ -19,7 +18,7 @@ import {
} from '@state/databases';

import {
appendDatabaseInstanceToSapSystem,
upsertDatabaseInstancesToSapSystem,
removeDatabaseInstanceFromSapSystem,
updateSAPSystemDatabaseInstanceHealth,
updateSAPSystemDatabaseInstanceSystemReplication,
Expand Down Expand Up @@ -52,8 +51,8 @@ function* databaseHealthChanged({ payload }) {
}

function* databaseInstanceRegistered({ payload }) {
yield put(appendDatabaseInstance(payload));
yield put(appendDatabaseInstanceToSapSystem(payload));
yield put(upsertDatabaseInstances([payload]));
yield put(upsertDatabaseInstancesToSapSystem([payload]));
yield put(
notify({
text: `A new Database instance, ${payload.sid}, has been discovered.`,
Expand Down
7 changes: 3 additions & 4 deletions assets/js/state/sagas/sapSystems.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import {
SAP_SYSTEM_UPDATED,
appendSapsystem,
updateSapSystemHealth,
upsertDatabaseInstances,
upsertDatabaseInstancesToSapSystem,
upsertApplicationInstances,
appendApplicationInstance,
removeApplicationInstance,
updateApplicationInstanceHost,
updateApplicationInstanceHealth,
Expand Down Expand Up @@ -47,7 +46,7 @@ function* sapSystemHealthChanged({ payload }) {
}

function* applicationInstanceRegistered({ payload }) {
yield put(appendApplicationInstance(payload));
yield put(upsertApplicationInstances([payload]));
}

export function* applicationInstanceMoved({ payload }) {
Expand Down Expand Up @@ -93,7 +92,7 @@ export function* sapSystemRestored({ payload }) {
application_instances: applicationInstances,
} = payload;

yield put(upsertDatabaseInstances(databaseInstances));
yield put(upsertDatabaseInstancesToSapSystem(databaseInstances));
yield put(upsertApplicationInstances(applicationInstances));

yield put(
Expand Down
4 changes: 2 additions & 2 deletions assets/js/state/sagas/sapSystems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {
appendSapsystem,
removeSAPSystem,
upsertDatabaseInstances,
upsertDatabaseInstancesToSapSystem,
updateApplicationInstanceHost,
upsertApplicationInstances,
removeApplicationInstance,
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('SAP Systems sagas', () => {

expect(dispatched).toEqual([
appendSapsystem(sapSystem),
upsertDatabaseInstances(sapSystem.database_instances),
upsertDatabaseInstancesToSapSystem(sapSystem.database_instances),
upsertApplicationInstances(sapSystem.application_instances),
notify({
text: `SAP System, ${sapSystem.sid}, has been restored.`,
Expand Down
25 changes: 7 additions & 18 deletions assets/js/state/sapSystems.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { filterByInstances, maybeUpdateInstanceHealth } from './instances';
import { upsertInstances, maybeUpdateInstanceHealth } from './instances';

const initialState = {
loading: false,
Expand Down Expand Up @@ -39,17 +39,11 @@ export const sapSystemsListSlice = createSlice({
},
// When a new ApplicationInstanceRegistered comes in,
// it need to be appended to the list of the application instances of the relative sap system
appendApplicationInstance: (state, action) => {
state.applicationInstances = [
...state.applicationInstances,
action.payload,
];
},
upsertApplicationInstances: (state, action) => {
state.applicationInstances = filterByInstances(
state.applicationInstances = upsertInstances(
state.applicationInstances,
action.payload
).concat(action.payload);
);
},
removeApplicationInstance: (
state,
Expand All @@ -66,14 +60,11 @@ export const sapSystemsListSlice = createSlice({
},
// When a new DatabaseInstanceRegistered comes in,
// it need to be appended to the list of the database instances of the relative sap system
appendDatabaseInstanceToSapSystem: (state, action) => {
state.databaseInstances = [...state.databaseInstances, action.payload];
},
upsertDatabaseInstances: (state, action) => {
state.databaseInstances = filterByInstances(
upsertDatabaseInstancesToSapSystem: (state, action) => {
state.databaseInstances = upsertInstances(
state.databaseInstances,
action.payload
).concat(action.payload);
);
},
removeDatabaseInstanceFromSapSystem: (
state,
Expand Down Expand Up @@ -195,11 +186,9 @@ export const {
stopSapSystemsLoading,
setSapSystems,
appendSapsystem,
appendApplicationInstance,
upsertApplicationInstances,
removeApplicationInstance,
appendDatabaseInstanceToSapSystem,
upsertDatabaseInstances,
upsertDatabaseInstancesToSapSystem,
removeDatabaseInstanceFromSapSystem,
updateSapSystemHealth,
updateApplicationInstanceHost,
Expand Down
4 changes: 2 additions & 2 deletions assets/js/state/sapSystems.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sapSystemsReducer, {
removeSAPSystem,
upsertDatabaseInstances,
upsertDatabaseInstancesToSapSystem,
upsertApplicationInstances,
updateApplicationInstanceHost,
removeApplicationInstance,
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('SAP Systems reducer', () => {
const newInstance = databaseInstanceFactory.build();
const newInstances = [updatedInstance, newInstance];

const action = upsertDatabaseInstances(newInstances);
const action = upsertDatabaseInstancesToSapSystem(newInstances);

const expectedState = {
databaseInstances: [initialInstances[1], ...newInstances],
Expand Down

0 comments on commit 475fe2f

Please sign in to comment.