Skip to content

Commit

Permalink
[BugFix] AlertSummary still displays when agents required are not con…
Browse files Browse the repository at this point in the history
…figured (#1142)

* bug fix for agent configuration

Signed-off-by: Sihan He <[email protected]>

* remove redundant codes

Signed-off-by: Sihan He <[email protected]>

* remove logs

Signed-off-by: Sihan He <[email protected]>

* handle exceptions

Signed-off-by: Sihan He <[email protected]>

* set default agentClient

Signed-off-by: Sihan He <[email protected]>

* update test

Signed-off-by: Sihan He <[email protected]>

* add error message

Signed-off-by: Sihan He <[email protected]>

---------

Signed-off-by: Sihan He <[email protected]>
  • Loading branch information
000FLMS authored Jan 21, 2025
1 parent 47800b7 commit 9a90b8f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
14 changes: 14 additions & 0 deletions public/pages/Dashboard/containers/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
getDataSourceId,
appendCommentsAction,
getIsCommentsEnabled,
getIsAgentConfigured
} from '../../utils/helpers';
import { getUseUpdatedUx } from '../../../services';

Expand Down Expand Up @@ -78,6 +79,7 @@ export default class Dashboard extends Component {
totalTriggers: 0,
chainedAlert: undefined,
commentsEnabled: false,
isAgentConfigured: false,
};
}

Expand All @@ -102,20 +104,30 @@ export default class Dashboard extends Component {
getIsCommentsEnabled(this.props.httpClient).then((commentsEnabled) => {
this.setState({ commentsEnabled });
});
this.getUpdatedAgentConfig();
}

componentDidUpdate(prevProps, prevState) {
const prevQuery = getQueryObjectFromState(prevState);
const currQuery = getQueryObjectFromState(this.state);
if (!_.isEqual(prevQuery, currQuery)) {
this.getUpdatedAgentConfig();
this.getUpdatedAlerts();
}
if (isDataSourceChanged(prevProps, this.props)) {
this.dataSourceQuery = getDataSourceQueryObj();
this.getUpdatedAgentConfig();
this.getUpdatedAlerts();
}
}

getUpdatedAgentConfig() {
const dataSourceId = getDataSourceId();
getIsAgentConfigured(dataSourceId).then((isAgentConfigured) => {
this.setState({ isAgentConfigured });
});
}

getUpdatedAlerts() {
const { page, size, search, sortField, sortDirection, severityLevel, alertState, monitorIds } =
this.state;
Expand Down Expand Up @@ -361,6 +373,7 @@ export default class Dashboard extends Component {
totalAlerts,
totalTriggers,
commentsEnabled,
isAgentConfigured,
} = this.state;
const {
monitorIds,
Expand Down Expand Up @@ -444,6 +457,7 @@ export default class Dashboard extends Component {
location,
monitors,
notifications,
isAgentConfigured,
setFlyout,
this.openFlyout,
this.closeFlyout,
Expand Down
4 changes: 2 additions & 2 deletions public/pages/Dashboard/containers/Dashboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mount } from 'enzyme';
import Dashboard from './Dashboard';
import { historyMock, httpClientMock } from '../../../../test/mocks';
import { setupCoreStart } from '../../../../test/utils/helpers';
import { setAssistantDashboards } from '../../../services';
import { setAssistantDashboards, setAssistantClient } from '../../../services';

const location = {
hash: '',
Expand Down Expand Up @@ -64,7 +64,7 @@ beforeAll(() => {

describe('Dashboard', () => {
setAssistantDashboards({ getFeatureStatus: () => ({ chat: false, alertInsight: false }) });

setAssistantClient({agentConfigExists: (agentConfigName, options) => {return Promise.resolve({ exists: false });}})
beforeEach(() => {
jest.clearAllMocks();
});
Expand Down
5 changes: 3 additions & 2 deletions public/pages/Dashboard/utils/tableUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const alertColumns = (
location,
monitors,
notifications,
isAgentConfigured,
setFlyout,
openFlyout,
closeFlyout,
Expand Down Expand Up @@ -167,7 +168,7 @@ export const alertColumns = (
}}
data-test-subj={`euiLink_${alert.trigger_name}`}
>
{`${total} alerts`}
{total > 1 ? `${total} alerts`:`${total} alert`}
</EuiLink>
);
const contextProvider = async () => {
Expand Down Expand Up @@ -291,7 +292,7 @@ export const alertColumns = (

const assistantEnabled = getApplication().capabilities?.assistant?.enabled === true;
const assistantFeatureStatus = getAssistantDashboards().getFeatureStatus();
if (assistantFeatureStatus.alertInsight && assistantEnabled) {
if (assistantFeatureStatus.alertInsight && assistantEnabled && isAgentConfigured) {
getAssistantDashboards().registerIncontextInsight([
{
key: alertId,
Expand Down
4 changes: 3 additions & 1 deletion public/pages/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ const LocalCluster: DataSourceOption = {
};

// We should use empty object for default value as local cluster may be disabled
export const dataSourceObservable = new BehaviorSubject<DataSourceOption>({});
export const dataSourceObservable = new BehaviorSubject<DataSourceOption>({});
export const SUMMARY_AGENT_CONFIG_ID = 'os_summary';
export const LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID = 'os_summary_with_log_pattern';
26 changes: 24 additions & 2 deletions public/pages/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
*/

import React from 'react';
import { getDataSourceEnabled, getDataSource } from '../../services/services';
import { getDataSourceEnabled, getDataSource, getAssistantClient } from '../../services/services';
import _ from 'lodash';
import { ShowAlertComments } from '../../components/Comments/ShowAlertComments';
import { COMMENTS_ENABLED_SETTING } from './constants';
import {
COMMENTS_ENABLED_SETTING,
SUMMARY_AGENT_CONFIG_ID,
LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID
} from './constants';

export function dataSourceEnabled() {
return getDataSourceEnabled()?.enabled;
Expand Down Expand Up @@ -68,6 +72,24 @@ export const appendCommentsAction = (columns, httpClient) => {
return columns;
};

export async function getIsAgentConfigured(dataSourceId){
const assistantClient = getAssistantClient();
try{
const res = await assistantClient.agentConfigExists(
[
SUMMARY_AGENT_CONFIG_ID,
LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID,
],
{ dataSourceId: dataSourceId }
);
return res.exists;
}
catch(e){
console.error('Error while checking if agent is configured:', e);
return false;
}
}

export async function getIsCommentsEnabled(httpClient) {
let commentsEnabled = await getClusterSetting(httpClient, COMMENTS_ENABLED_SETTING, false);

Expand Down
9 changes: 5 additions & 4 deletions public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import { alertingTriggerAd } from './utils/contextMenu/triggers';
import { ExpressionsSetup } from '../../../src/plugins/expressions/public';
import { UiActionsSetup } from '../../../src/plugins/ui_actions/public';
import { overlayAlertsFunction } from './expressions/overlay_alerts';
import { setClient, setEmbeddable, setNotifications, setOverlays, setSavedAugmentVisLoader, setUISettings, setQueryService, setSavedObjectsClient, setDataSourceEnabled, setDataSourceManagementPlugin, setNavigationUI, setApplication, setContentManagementStart, setAssistantDashboards } from './services';
import { setClient, setEmbeddable, setNotifications, setOverlays, setSavedAugmentVisLoader, setUISettings, setQueryService, setSavedObjectsClient, setDataSourceEnabled, setDataSourceManagementPlugin, setNavigationUI, setApplication, setContentManagementStart, setAssistantDashboards, setAssistantClient } from './services';
import { VisAugmenterStart } from '../../../src/plugins/vis_augmenter/public';
import { DataPublicPluginStart } from '../../../src/plugins/data/public';
import { AssistantSetup } from './types';
import { AssistantSetup, AssistantPublicPluginStart } from './types';
import { DataSourceManagementPluginSetup } from '../../../src/plugins/data_source_management/public';
import { DataSourcePluginSetup } from '../../../src/plugins/data_source/public';
import { NavigationPublicPluginStart } from '../../../src/plugins/navigation/public';
Expand Down Expand Up @@ -57,6 +57,7 @@ export interface AlertingStartDeps {
data: DataPublicPluginStart;
navigation: NavigationPublicPluginStart;
contentManagement: ContentManagementPluginStart;
assistantDashboards?: AssistantPublicPluginStart;
}

export class AlertingPlugin implements Plugin<void, AlertingStart, AlertingSetupDeps, AlertingStartDeps> {
Expand Down Expand Up @@ -200,7 +201,6 @@ export class AlertingPlugin implements Plugin<void, AlertingStart, AlertingSetup
}

setAssistantDashboards(assistantDashboards || { getFeatureStatus: () => ({ chat: false, alertInsight: false }) });

setUISettings(core.uiSettings);

// Set the HTTP client so it can be pulled into expression fns to make
Expand Down Expand Up @@ -230,7 +230,7 @@ export class AlertingPlugin implements Plugin<void, AlertingStart, AlertingSetup
uiActions.addTriggerAction(alertingTriggerAd.id, adAction);
}

public start(core: CoreStart, { visAugmenter, embeddable, data, navigation, contentManagement }: AlertingStartDeps): AlertingStart {
public start(core: CoreStart, { visAugmenter, embeddable, data, navigation, contentManagement, assistantDashboards }: AlertingStartDeps): AlertingStart {
setEmbeddable(embeddable);
setOverlays(core.overlays);
setQueryService(data.query);
Expand All @@ -241,6 +241,7 @@ export class AlertingPlugin implements Plugin<void, AlertingStart, AlertingSetup
setApplication(core.application);
setContentManagementStart(contentManagement);
registerAlertsCard();
setAssistantClient(assistantDashboards?.assistantClient || {agentConfigExists: (agentConfigName: string | string[], options?: string) => {return Promise.resolve({ exists: false });}})
return {};
}
}
5 changes: 4 additions & 1 deletion public/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import { NavigationPublicPluginStart } from '../../../../src/plugins/navigation/public';
import { ContentManagementPluginStart } from '../../../../src/plugins/content_management/public';
import { createNullableGetterSetter } from './utils/helper';
import { AssistantSetup } from '../types';
import { AssistantSetup, AssistantPublicPluginStart } from '../types';

const ServicesContext = createContext<BrowserServices | null>(null);

Expand All @@ -35,6 +35,9 @@ export const [getAssistantDashboards, setAssistantDashboards] = createGetterSett
AssistantSetup | {}
>('assistantDashboards');

export const [getAssistantClient, setAssistantClient] =
createGetterSetter<AssistantPublicPluginStart['assistantClient'] | {}>('AssistantClient');

export const [getEmbeddable, setEmbeddable] = createGetterSetter<EmbeddableStart>('embeddable');

export const [getOverlays, setOverlays] =
Expand Down
1 change: 1 addition & 0 deletions public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
*/
// @ts-ignore
export type { AssistantSetup } from '../../dashboards-assistant/public';
export type { AssistantPublicPluginStart } from '../../dashboards-assistant/public/';

0 comments on commit 9a90b8f

Please sign in to comment.