Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisronline committed Jan 18, 2021
1 parent 8d969c8 commit e6ba004
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 33 deletions.
8 changes: 8 additions & 0 deletions x-pack/plugins/monitoring/common/types/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ export interface ElasticsearchLegacySource {
};
ccr_stats?: {
leader_index?: string;
follower_index?: string;
shard_id?: number;
read_exceptions?: Array<{
exception?: {
type?: string;
};
}>;
time_since_last_read_millis?: number;
};
index_recovery?: {
shards?: ElasticsearchIndexRecoveryShard[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ export async function getTimeOfLastEvent({
};

const response = await callWithRequest(req, 'search', params);
return response.hits?.hits.length ? response.hits?.hits[0]._source.timestamp : undefined;
return response.hits?.hits.length ? response.hits?.hits[0]?._source.timestamp : undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function findSupportedBasicLicenseCluster(
},
},
})) as ElasticsearchResponse;
const supportedClusterUuid = kibanaDataResult.hits?.hits[0]._source.cluster_uuid ?? undefined;
const supportedClusterUuid = kibanaDataResult.hits?.hits[0]?._source.cluster_uuid ?? undefined;
for (const cluster of clusters) {
if (cluster.cluster_uuid === supportedClusterUuid) {
cluster.isSupported = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export function getClusterLicense(req: LegacyRequest, esIndexPattern: string, cl

const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring');
return callWithRequest(req, 'search', params).then((response: ElasticsearchResponse) => {
return response.hits?.hits[0]._source.license;
return response.hits?.hits[0]?._source.license;
});
}
5 changes: 3 additions & 2 deletions x-pack/plugins/monitoring/server/lib/elasticsearch/ccr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { ElasticsearchResponse } from '../../../common/types/es';
import { LegacyRequest } from '../../types';

export function handleResponse(response: ElasticsearchResponse) {
const isEnabled = response.hits?.hits[0]._source.stack_stats?.xpack?.ccr?.enabled ?? false;
const isAvailable = response.hits?.hits[0]._source.stack_stats?.xpack?.ccr?.available ?? false;
const isEnabled = response.hits?.hits[0]?._source.stack_stats?.xpack?.ccr?.enabled ?? undefined;
const isAvailable =
response.hits?.hits[0]?._source.stack_stats?.xpack?.ccr?.available ?? undefined;
return isEnabled && isAvailable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function filterOldShardActivity(startMs: number) {
*/
export function handleLastRecoveries(resp: ElasticsearchResponse, start: number) {
if (resp.hits?.hits.length === 1) {
const data = (resp.hits?.hits[0]._source.index_recovery?.shards ?? []).filter(
const data = (resp.hits?.hits[0]?._source.index_recovery?.shards ?? []).filter(
filterOldShardActivity(moment.utc(start).valueOf())
);
data.sort((a, b) => b.start_time_in_millis - a.start_time_in_millis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { LegacyRequest } from '../../../types';

export function handleResponse(shardStats: any, indexUuid: string) {
return (response: ElasticsearchResponse) => {
const indexStats = response.hits?.hits[0]._source.index_stats;
const indexStats = response.hits?.hits[0]?._source.index_stats;
const primaries = indexStats?.primaries;
const total = indexStats?.total;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export function handleResponse(
const hits = resp.hits?.hits ?? [];
return hits.map((hit) => {
const stats = hit._source.index_stats;
const earliestStats = hit.inner_hits?.earliest?.hits?.hits[0]._source.index_stats;
const earliestStats = hit.inner_hits?.earliest?.hits?.hits[0]?._source.index_stats;

const rateOptions = {
hitTimestamp: hit._source.timestamp,
earliestHitTimestamp: hit.inner_hits?.earliest?.hits?.hits[0]._source.timestamp,
earliestHitTimestamp: hit.inner_hits?.earliest?.hits?.hits[0]?._source.timestamp,
timeWindowMin: min,
timeWindowMax: max,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ export function handleResponse(
> = nodeStatsHits.map((hit) => hit._source.elasticsearch?.node || hit._source.source_node); // using [0] value because query results are sorted desc per timestamp
const node = nodes[0] || getDefaultNodeFromId(nodeUuid);
const sourceStats =
response.hits?.hits[0]._source.elasticsearch?.node?.stats ||
response.hits?.hits[0]._source.node_stats;
response.hits?.hits[0]?._source.elasticsearch?.node?.stats ||
response.hits?.hits[0]?._source.node_stats;
const clusterNode =
clusterState && clusterState.nodes ? clusterState.nodes[nodeUuid] : undefined;
const stats = {
resolver: nodeUuid,
node_ids: nodes.map((_node) => node.id || node.uuid),
attributes: node.attributes,
transport_address: response.hits?.hits[0]._source.service?.address || node.transport_address,
transport_address: response.hits?.hits[0]?._source.service?.address || node.transport_address,
name: node.name,
type: node.type,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function mapNodesInfo(
isOnline,
nodeTypeLabel,
nodeTypeClass,
shardCount: nodesShardCount.nodes[uuid].shardCount ?? 0,
shardCount: nodesShardCount.nodes[uuid]?.shardCount ?? 0,
},
};
}, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { LegacyRequest } from '../../types';
import { ElasticsearchResponse } from '../../../common/types/es';

export function handleResponse(resp: ElasticsearchResponse) {
const source = resp.hits?.hits[0]._source.kibana_stats;
const source = resp.hits?.hits[0]?._source.kibana_stats;
const kibana = source?.kibana;
return merge(kibana, {
availability: calculateAvailability(source?.timestamp),
Expand Down
38 changes: 22 additions & 16 deletions x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { handleError } from '../../../../lib/errors/handle_error';
// @ts-ignore
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { INDEX_PATTERN_ELASTICSEARCH } from '../../../../../common/constants';
import { ElasticsearchResponse } from '../../../../../common/types/es';
import { ElasticsearchResponse, ElasticsearchSource } from '../../../../../common/types/es';
import { LegacyRequest } from '../../../../types';

function getBucketScript(max: string, min: string) {
Expand Down Expand Up @@ -218,19 +218,25 @@ export function ccrRoute(server: {
return { data: [] };
}

const fullStats = response.hits?.hits.reduce((accum, hit) => {
const innerHits = hit.inner_hits?.by_shard.hits?.hits ?? [];
const innerHitsSource = innerHits.map((innerHit) => get(innerHit, '_source.ccr_stats'));
const grouped = groupBy(
innerHitsSource,
(stat) => `${stat.follower_index}:${stat.shard_id}`
);
const fullStats: {
[key: string]: Array<NonNullable<ElasticsearchSource['ccr_stats']>>;
} =
response.hits?.hits.reduce((accum, hit) => {
const innerHits = hit.inner_hits?.by_shard.hits?.hits ?? [];
const innerHitsSource = innerHits.map(
(innerHit) =>
innerHit._source.ccr_stats as NonNullable<ElasticsearchSource['ccr_stats']>
);
const grouped = groupBy(
innerHitsSource,
(stat) => `${stat.follower_index}:${stat.shard_id}`
);

return {
...accum,
...grouped,
};
}, {});
return {
...accum,
...grouped,
};
}, {}) ?? {};

const buckets = response.aggregations.by_follower_index.buckets;
const data = buckets.reduce((accum: any, bucket: any) => {
Expand Down Expand Up @@ -261,11 +267,11 @@ export function ccrRoute(server: {

stat.shards = get(bucket, 'by_shard_id.buckets').reduce(
(accum2: any, shardBucket: any) => {
const fullStat = get(fullStats, [`${bucket.key}:${shardBucket.key}`, '[0]'], {});
const fullStat = fullStats[`${bucket.key}:${shardBucket.key}`][0] ?? {};
const shardStat = {
shardId: shardBucket.key,
error: fullStat.read_exceptions.length
? fullStat.read_exceptions[0].exception.type
error: fullStat.read_exceptions?.length
? fullStat.read_exceptions[0].exception?.type
: null,
opsSynced: get(shardBucket, 'ops_synced.value'),
syncLagTime: fullStat.time_since_last_read_millis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ export function ccrShardRoute(server: { route: (p: any) => void; config: () => {
getCcrStat(req, esIndexPattern, filters),
]);

const stat = ccrResponse.hits?.hits[0]._source.ccr_stats ?? {};
const stat = ccrResponse.hits?.hits[0]?._source.ccr_stats ?? {};
const oldestStat =
ccrResponse.hits?.hits[0].inner_hits?.oldest.hits?.hits[0]._source.ccr_stats ?? {};
ccrResponse.hits?.hits[0].inner_hits?.oldest.hits?.hits[0]?._source.ccr_stats ?? {};

return {
metrics,
stat,
formattedLeader: getFormattedLeaderIndex(stat.leader_index ?? ''),
timestamp: ccrResponse.hits?.hits[0]._source.timestamp,
timestamp: ccrResponse.hits?.hits[0]?._source.timestamp,
oldestStat,
};
} catch (err) {
Expand Down

0 comments on commit e6ba004

Please sign in to comment.