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

Move to modified version of original status count for speed #41210

Merged
merged 8 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -109,7 +109,7 @@ export const createMonitorsResolvers: CreateUMGraphQLResolvers = (
{ dateRangeStart, dateRangeEnd, filters },
{ req }
): Promise<Snapshot> {
const counts = await libs.monitorStates.getSummaryCount(
const counts = await libs.monitors.getSnapshotCount(
req,
dateRangeStart,
dateRangeEnd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { MonitorSummary, SnapshotCount, StatesIndexStatus } from '../../../../common/graphql/types';
import { MonitorSummary, StatesIndexStatus } from '../../../../common/graphql/types';

export interface UMMonitorStatesAdapter {
getMonitorStates(
Expand All @@ -20,11 +20,5 @@ export interface UMMonitorStatesAdapter {
dateRangeEnd: string,
filters?: string | null
): Promise<MonitorSummary[]>;
getSummaryCount(
request: any,
dateRangeStart: string,
dateRangeEnd: string,
filters?: string | null
): Promise<SnapshotCount>;
statesIndexExists(request: any): Promise<StatesIndexStatus>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
MonitorSummary,
SummaryHistogram,
Check,
SnapshotCount,
StatesIndexStatus,
} from '../../../../common/graphql/types';
import { INDEX_NAMES, LEGACY_STATES_QUERY_SIZE } from '../../../../common/constants';
Expand Down Expand Up @@ -581,49 +580,6 @@ export class ElasticsearchMonitorStatesAdapter implements UMMonitorStatesAdapter
}, {});
}

public async getSummaryCount(
request: any,
dateRangeStart: string,
dateRangeEnd: string,
filters?: string | null
): Promise<SnapshotCount> {
// TODO: adapt this to the states index in future release
// const { count } = await this.database.count(request, { index: 'heartbeat-states-8.0.0' });
// return { count };

const count: SnapshotCount = {
up: 0,
down: 0,
mixed: 0,
total: 0,
};

let searchAfter: any | null = null;
do {
const { afterKey, result, statusFilter } = await this.runLegacyMonitorStatesQuery(
request,
dateRangeStart,
dateRangeEnd,
filters,
searchAfter
);
searchAfter = afterKey;
this.getMonitorBuckets(result, statusFilter).reduce((acc: SnapshotCount, monitor: any) => {
const status = get<string | undefined>(monitor, 'state.value.monitor.status', undefined);
if (status === 'up') {
acc.up++;
} else if (status === 'down') {
acc.down++;
} else if (status === 'mixed') {
acc.mixed++;
}
acc.total++;
return acc;
}, count);
} while (searchAfter !== null);
return count;
}

public async statesIndexExists(request: any): Promise<StatesIndexStatus> {
// TODO: adapt this to the states index in future release
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { UMMonitorStatesAdapter } from './adapter_types';
import { MonitorSummary, SnapshotCount, StatesIndexStatus } from '../../../../common/graphql/types';
import { MonitorSummary, StatesIndexStatus } from '../../../../common/graphql/types';

/**
* This class will be implemented for server-side tests.
Expand All @@ -28,14 +28,6 @@ export class UMMemoryMonitorStatesAdapter implements UMMonitorStatesAdapter {
): Promise<MonitorSummary[]> {
throw new Error('Method not implemented.');
}
public async getSummaryCount(
request: any,
dateRangeStart: string,
dateRangeEnd: string,
filters?: string | null | undefined
): Promise<SnapshotCount> {
throw new Error('Method not implemented.');
}
public async statesIndexExists(request: any): Promise<StatesIndexStatus> {
throw new Error('Method not implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,10 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
aggs: {
latest: {
top_hits: {
sort: [
{
'@timestamp': { order: 'desc' },
},
],
sort: [{ '@timestamp': { order: 'desc' } }],
_source: {
includes: ['summary.*', 'monitor.id', '@timestamp', 'observer.geo.name'],
},
size: 1,
},
},
Expand All @@ -211,10 +210,16 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
},
};

let up: number = 0;
let down: number = 0;
let searchAfter: any = null;

const summaryByIdLocation: {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably extract a type for this and put it in the adjacent adapter_types file. A separate type for the location value would probably be cleaner too.

// ID
[key: string]: {
// Location
[key: string]: { up: number; down: number; timestamp: number };
};
} = {};

do {
if (searchAfter) {
set(params, 'body.aggs.ids.composite.after', searchAfter);
Expand All @@ -225,20 +230,67 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {

idBuckets.forEach(bucket => {
// We only get the latest doc
const status = get(bucket, 'latest.hits.hits[0]._source.monitor.status', null);
if (!statusFilter || (statusFilter && statusFilter === status)) {
if (status === 'up') {
up++;
} else {
down++;
}
const source: any = get(bucket, 'latest.hits.hits[0]._source');
const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these keys guaranteed to be defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, aggregations always return a value.

summary: { up, down },
monitor: { id },
} = source;
const timestamp = source['@timestamp'];
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
const location = get(source, 'observer.geo.name', '');

let idSummary = summaryByIdLocation[id];
if (!idSummary) {
idSummary = {};
summaryByIdLocation[id] = idSummary;
}
const locationSummary = idSummary[location];
if (!locationSummary || locationSummary.timestamp < timestamp) {
idSummary[location] = { timestamp, up, down };
}
});

searchAfter = get(queryResult, 'aggregations.ids.after_key');
} while (searchAfter);

return { up, down, total: up + down };
let up: number = 0;
let mixed: number = 0;
let down: number = 0;

for (const id in summaryByIdLocation) {
if (!summaryByIdLocation.hasOwnProperty(id)) {
continue;
}
const locationInfo = summaryByIdLocation[id];
let locationUp = 0;
let locationDown = 0;
for (const locationName in locationInfo) {
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
if (!locationInfo.hasOwnProperty(locationName)) {
continue;
}
const locationData = locationInfo[locationName];
locationUp += locationData.up;
locationDown += locationData.down;
}

if (locationDown === 0) {
up++;
} else if (locationUp > 0) {
mixed++;
} else {
down++;
}
}

const result: any = { up, down, mixed, total: up + down + mixed };
if (statusFilter) {
for (const status in result) {
if (status !== 'total' && status !== statusFilter) {
result[status] = 0;
}
}
}

return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a unit test for this function? It may be good to create a mock JSON result (from real-world query results) and a subsequent test snapshot to help ensure that future revisions don't break this function, since we're planning to make it GA and we've developed it so late.

If you agree then I'm happy to add this, or do it in a follow-up PR. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this specific case I don't see a ton of value in the unit test. Our functional tests check this against real query results from an ES cluster. That said it'd be nice to have the unit test to make future development against this code easier, since running functional tests is a pain.

Copy link
Contributor

Choose a reason for hiding this comment

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

I might make more of a meta-style issue to try to address this. Once we've stabilized these backend functions it'll be important to ensure that future changes don't erode the functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++ to have a style discussion elsewhere. When/where to unit test is a complex question with no one single answer, and no shortage of different philosophies.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { UMMonitorStatesAdapter } from '../adapters/monitor_states';
import { MonitorSummary, SnapshotCount, StatesIndexStatus } from '../../../common/graphql/types';
import { MonitorSummary, StatesIndexStatus } from '../../../common/graphql/types';

export class UMMonitorStatesDomain {
constructor(private readonly adapter: UMMonitorStatesAdapter, libs: {}) {
Expand All @@ -22,15 +22,6 @@ export class UMMonitorStatesDomain {
return this.adapter.getMonitorStates(request, pageIndex, pageSize, sortField, sortDirection);
}

public async getSummaryCount(
request: any,
dateRangeStart: string,
dateRangeEnd: string,
filters?: string | null
): Promise<SnapshotCount> {
return this.adapter.getSummaryCount(request, dateRangeStart, dateRangeEnd, filters);
}

public async statesIndexExists(request: any): Promise<StatesIndexStatus> {
return this.adapter.statesIndexExists(request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"snapshot": {
"counts": { "down": 2, "mixed": 0, "up": 0, "total": 2 }
"counts": { "down": 2, "mixed": 0, "up": 0, "total": 10 }
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that we're doing this now. I think. 😺

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would categorize the previous behavior as a bug IMHO.

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"snapshot": {
"counts": { "down": 0, "mixed": 0, "up": 8, "total": 8 }
"counts": { "down": 0, "mixed": 0, "up": 8, "total": 10 }
}
}