Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React, { useState, useEffect } from 'react';
import { EuiFlexItem, EuiFlexGroup, EuiTitle } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import type { LensAttributes } from '@kbn/lens-embeddable-utils';
import type { ReactElement } from 'react';
import { isLeft } from 'fp-ts/Either';
import { createKeyInsightsPanelLensAttributes } from './lens_attributes';
Expand All @@ -24,7 +25,8 @@ const LENS_VISUALIZATION_HEIGHT = 140;
interface KeyInsightsTileProps {
title: string;
label: string;
getEsqlQuery: (namespace: string) => EsqlQueryOrInvalidFields;
getEsqlQuery?: (namespace: string) => EsqlQueryOrInvalidFields;
getLensAttributes?: (namespace: string) => LensAttributes;
id: string;
inspectTitle: ReactElement;
spaceId?: string;
Expand All @@ -34,6 +36,7 @@ export const KeyInsightsTile: React.FC<KeyInsightsTileProps> = ({
title,
label,
getEsqlQuery,
getLensAttributes,
id,
inspectTitle,
spaceId: propSpaceId,
Expand Down Expand Up @@ -63,14 +66,16 @@ export const KeyInsightsTile: React.FC<KeyInsightsTileProps> = ({
setHasStartedLoading(false);
}, [timerange.from, timerange.to, filterQuery, effectiveSpaceId]);

const esqlQuery = getEsqlQuery(effectiveSpaceId);
const esqlQuery = getEsqlQuery ? getEsqlQuery(effectiveSpaceId) : null;

// Only show error state if:
// 1. Loading has started at least once (hasStartedLoading)
// 2. Loading is now complete (loading === false)
// 3. We have no tables (indicating an error)
const hasInvalidEsqlQuery = esqlQuery ? isLeft(esqlQuery) : false;

if (
isLeft(esqlQuery) ||
hasInvalidEsqlQuery ||
(hasStartedLoading &&
visualizationResponse &&
visualizationResponse.loading === false &&
Expand Down Expand Up @@ -108,13 +113,17 @@ export const KeyInsightsTile: React.FC<KeyInsightsTileProps> = ({
);
}

const lensAttributes = createKeyInsightsPanelLensAttributes({
title,
label,
esqlQuery: esqlQuery.right,
dataViewId: 'default-dataview',
filterQuery,
});
const esqlQueryString = esqlQuery && !isLeft(esqlQuery) ? esqlQuery.right : '';

const lensAttributes = getLensAttributes
? getLensAttributes(effectiveSpaceId)
: createKeyInsightsPanelLensAttributes({
title,
label,
esqlQuery: esqlQueryString,
dataViewId: 'default-dataview',
filterQuery,
});

// If we reach here, either still loading or we have a valid response, so show the embeddable
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import type { Filter } from '@kbn/es-query';
import type { LensAttributes } from '@kbn/lens-embeddable-utils';
import type { ESBoolQuery } from '../../../../../../../common/typed_json';

Expand All @@ -16,6 +17,14 @@ interface KeyInsightsPanelParams {
filterQuery: ESBoolQuery | undefined;
}

interface KeyInsightsPanelFormBasedParams {
title: string;
label: string;
dataViewId: string;
dataViewTitle: string;
filters: Filter[];
}

export const createKeyInsightsPanelLensAttributes = ({
title,
label,
Expand Down Expand Up @@ -75,3 +84,77 @@ export const createKeyInsightsPanelLensAttributes = ({
references: [],
};
};

export const createKeyInsightsPanelFormBasedLensAttributes = ({
title,
label,
dataViewId,
dataViewTitle,
filters,
}: KeyInsightsPanelFormBasedParams): LensAttributes => {
const layerId = 'key-insights-privileged-users-layer';
const countColumnId = 'key-insights-privileged-users-count';

return {
title,
description: '',
visualizationType: 'lnsMetric',
state: {
visualization: {
layerId,
layerType: 'data',
metricAccessor: countColumnId,
},
query: {
query: '',
language: 'kuery',
},
filters,
datasourceStates: {
formBased: {
layers: {
[layerId]: {
columns: {
[countColumnId]: {
label,
dataType: 'number',
isBucketed: false,
operationType: 'unique_count',
scale: 'ratio',
sourceField: 'user.name',
customLabel: true,
},
},
columnOrder: [countColumnId],
incompleteColumns: {},
},
},
},
textBased: {
layers: {},
},
},
internalReferences: [
{
type: 'index-pattern',
id: dataViewId,
name: `indexpattern-datasource-layer-${layerId}`,
},
],
adHocDataViews: {
[dataViewId]: {
id: dataViewId,
title: dataViewTitle,
timeFieldName: '@timestamp',
sourceFilters: [],
fieldFormats: {},
runtimeFieldMap: {},
fieldAttrs: {},
allowNoIndex: false,
name: dataViewTitle,
},
},
},
references: [],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const getPrivilegedUsersEsqlCount = (
namespace: string
) => `FROM ${getPrivilegedMonitorUsersIndex(namespace)}
| WHERE user.is_privileged == true
| STATS count = COUNT_DISTINCT(user.name)`;
| STATS BY user.name
Comment thread
abhishekbhatia1710 marked this conversation as resolved.
Outdated
| STATS count = COUNT(*)`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This query change made the count appear correct for the viz. I tested with 1k,4k and 10k users. Counts for all appeared correct on the viz

Image Image Image