Skip to content

Commit

Permalink
[Uptime] Fix filter status bar location selection (elastic#41382) (el…
Browse files Browse the repository at this point in the history
…astic#41591)

The way this query worked was strange, this replaces it with a standard terms query and gets rid of the weird MonitorKey stuff.
  • Loading branch information
andrewvc committed Jul 19, 2019
1 parent ab41ceb commit 9a9cfc5
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 105 deletions.
18 changes: 17 additions & 1 deletion x-pack/legacy/plugins/uptime/common/graphql/introspection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": { "kind": "OBJECT", "name": "MonitorKey", "ofType": null }
"ofType": { "kind": "SCALAR", "name": "String", "ofType": null }
}
},
"isDeprecated": false,
Expand Down Expand Up @@ -2512,6 +2512,22 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "urls",
"description": "The list of URLs",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": { "kind": "SCALAR", "name": "String", "ofType": null }
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/uptime/common/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export interface StatusData {
/** The data used to enrich the filter bar. */
export interface FilterBar {
/** A series of monitor IDs in the heartbeat indices. */
ids?: MonitorKey[] | null;
ids?: string[] | null;
/** The location values users have configured for the agents. */
locations?: string[] | null;
/** The names users have configured for the monitors. */
Expand All @@ -472,6 +472,8 @@ export interface FilterBar {
schemes?: string[] | null;
/** The possible status values contained in the indices. */
statuses?: string[] | null;
/** The list of URLs */
urls?: string[] | null;
}
/** A representation of an error state for a monitor. */
export interface ErrorListItem {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@ import { shallowWithIntl } from 'test_utils/enzyme_helpers';
import { FilterBarComponent as FilterBar } from '../filter_bar';

describe('FilterBar component', () => {
const data = {
const data: any = {
filterBar: {
ports: [9200, 12349],
ids: [
{ key: 'auto-tcp-0X81440A68E839814C', url: 'tcp://localhost:9200' },
{ key: 'auto-http-0X3675F89EF0612091', url: 'http://localhost:12349/' },
{ key: 'auto-http-0X970CBD2F2102BFA8', url: 'http://www.google.com/' },
{ key: 'auto-http-0X131221E73F825974', url: 'https://www.google.com/' },
{ key: 'auto-http-0X9CB71300ABD5A2A8', url: 'https://www.github.com/' },
{ key: 'auto-http-0XD9AE729FC1C1E04A', url: 'http://www.reddit.com/' },
{ key: 'auto-http-0XDD2D4E60FD4A61C3', url: 'https://www.elastic.co' },
{ key: 'auto-http-0XA8096548ECEB85B7', url: 'http://www.example.com/' },
{ key: 'auto-http-0XC9CDA429418EDC2B', url: 'https://www.wikipedia.org/' },
{ key: 'auto-http-0XE3B163481423197D', url: 'https://news.google.com/' },
'auto-tcp-0X81440A68E839814C',
'auto-http-0X3675F89EF0612091',
'auto-http-0X970CBD2F2102BFA8',
'auto-http-0X131221E73F825974',
'auto-http-0X9CB71300ABD5A2A8',
'auto-http-0XD9AE729FC1C1E04A',
'auto-http-0XDD2D4E60FD4A61C3',
'auto-http-0XA8096548ECEB85B7',
'auto-http-0XC9CDA429418EDC2B',
'auto-http-0XE3B163481423197D',
],
names: [],
schemes: ['tcp', 'http'],
urls: [
'tcp://localhost:9200',
'http://localhost:12349/',
'http://www.google.com/',
'https://www.google.com/',
'https://www.github.com/',
'http://www.reddit.com/',
'https://www.elastic.co',
'http://www.example.com/',
'https://www.wikipedia.org/',
],
},
};
let currentQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import React from 'react';
import { FilterBar as FilterBarType, MonitorKey } from '../../../common/graphql/types';
import { FilterBar as FilterBarType } from '../../../common/graphql/types';
import { UptimeSearchBarQueryChangeHandler } from '../../pages/overview';
import { UptimeGraphQLQueryProps, withUptimeGraphQL } from '../higher_order';
import { filterBarQuery } from '../../queries';
Expand All @@ -42,7 +42,7 @@ export const FilterBarComponent = ({ currentQuery, data, error, updateQuery }: P
return <FilterBarLoading />;
}
const {
filterBar: { ids, locations, names, ports, schemes },
filterBar: { ids, locations, names, ports, schemes, urls },
} = data;
// TODO: add a factory function + type for these filter options
const filters = [
Expand Down Expand Up @@ -82,9 +82,9 @@ export const FilterBarComponent = ({ currentQuery, data, error, updateQuery }: P
}),
multiSelect: false,
options: ids
? ids.map(({ key }: MonitorKey) => ({
value: key,
view: key,
? ids.map((id: string) => ({
value: id,
view: id,
}))
: [],
searchThreshold: SEARCH_THRESHOLD,
Expand All @@ -108,7 +108,7 @@ export const FilterBarComponent = ({ currentQuery, data, error, updateQuery }: P
defaultMessage: 'URL',
}),
multiSelect: false,
options: ids ? ids.map(({ url }: MonitorKey) => ({ value: url, view: url })) : [],
options: urls ? urls.map((url: string) => ({ value: url, view: url })) : [],
searchThreshold: SEARCH_THRESHOLD,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import gql from 'graphql-tag';
export const filterBarQueryString = `
query FilterBar($dateRangeStart: String!, $dateRangeEnd: String!) {
filterBar: getFilterBar(dateRangeStart: $dateRangeStart, dateRangeEnd: $dateRangeEnd) {
ids {
key
url
}
ids
locations
names
ports
schemes
urls
}
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const monitorsSchema = gql`
"The data used to enrich the filter bar."
type FilterBar {
"A series of monitor IDs in the heartbeat indices."
ids: [MonitorKey!]
ids: [String!]
"The location values users have configured for the agents."
locations: [String!]
"The names users have configured for the monitors."
Expand All @@ -21,6 +21,8 @@ export const monitorsSchema = gql`
schemes: [String!]
"The possible status values contained in the indices."
statuses: [String!]
"The list of URLs"
urls: [String!]
}
type HistogramDataPoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FilterBar,
LatestMonitor,
MonitorChart,
MonitorKey,
MonitorPageTitle,
MonitorSeriesPoint,
Ping,
Expand Down Expand Up @@ -436,18 +435,17 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
dateRangeStart: string,
dateRangeEnd: string
): Promise<FilterBar> {
const fields: { [key: string]: string } = {
ids: 'monitor.id',
schemes: 'monitor.type',
urls: 'url.full',
ports: 'url.port',
locations: 'observer.geo.name',
};
const params = {
index: INDEX_NAMES.HEARTBEAT,
body: {
_source: [
'monitor.id',
'monitor.type',
'url.full',
'url.port',
'monitor.name',
'observer.geo.name',
],
size: 1000,
size: 0,
query: {
range: {
'@timestamp': {
Expand All @@ -456,55 +454,19 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
},
},
},
collapse: {
field: 'monitor.id',
},
sort: {
'@timestamp': 'desc',
},
aggs: Object.values(fields).reduce((acc: { [key: string]: any }, field) => {
acc[field] = { terms: { field, size: 20 } };
return acc;
}, {}),
},
};
const result = await this.database.search(request, params);
const ids: MonitorKey[] = [];
const ports = new Set<number>();
const types = new Set<string>();
const names = new Set<string>();
const locations = new Set<string>();

const hits = get(result, 'hits.hits', []);
hits.forEach((hit: any) => {
const key: string = get(hit, '_source.monitor.id');
const url: string | null = get(hit, '_source.url.full', null);
const port: number | undefined = get(hit, '_source.url.port', undefined);
const type: string | undefined = get(hit, '_source.monitor.type', undefined);
const name: string | null = get(hit, '_source.monitor.name', null);
const location: string | null = get(hit, '_source.observer.geo.name', null);

if (key) {
ids.push({ key, url });
}
if (port) {
ports.add(port);
}
if (type) {
types.add(type);
}
if (name) {
names.add(name);
}
if (location) {
locations.add(location);
}
});
const { aggregations } = await this.database.search(request, params);

return {
ids,
locations: Array.from(locations),
names: Array.from(names),
ports: Array.from(ports),
schemes: Array.from(types),
statuses: ['up', 'down'],
};
return Object.keys(fields).reduce((acc: { [key: string]: any[] }, field) => {
const bucketName = fields[field];
acc[field] = aggregations[bucketName].buckets.map((b: { key: string | number }) => b.key);
return acc;
}, {});
}

/**
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/uptime/graphql/filter_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { expectFixtureEql } from './expect_fixture_eql';
import { filterBarQueryString } from '../../../../../legacy/plugins/uptime/public/queries';
import filterList from './fixtures/filter_list';

export default function ({ getService }) {
describe('filterBar query', () => {
Expand All @@ -27,7 +26,7 @@ export default function ({ getService }) {
.post('/api/uptime/graphql')
.set('kbn-xsrf', 'foo')
.send({ ...getFilterBarQuery });
expect(data).to.eql(filterList);
expectFixtureEql(data, 'filter_list');
});
});
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
{
"filterBar": {
"ids": [
{ "key": "auto-tcp-0X81440A68E839814C", "url": "tcp://localhost:9200" },
{ "key": "auto-http-0X3675F89EF0612091", "url": "http://localhost:12349/" },
{ "key": "auto-http-0X970CBD2F2102BFA8", "url": "http://www.google.com/" },
{ "key": "auto-http-0X131221E73F825974", "url": "https://www.google.com/" },
{ "key": "auto-http-0X9CB71300ABD5A2A8", "url": "https://www.github.com/" },
{ "key": "auto-http-0XD9AE729FC1C1E04A", "url": "http://www.reddit.com/" },
{ "key": "auto-http-0XDD2D4E60FD4A61C3", "url": "https://www.elastic.co" },
{ "key": "auto-http-0XA8096548ECEB85B7", "url": "http://www.example.com/" },
{ "key": "auto-http-0XC9CDA429418EDC2B", "url": "https://www.wikipedia.org/" },
{ "key": "auto-http-0XE3B163481423197D", "url": "https://news.google.com/" }
"auto-tcp-0X81440A68E839814C",
"auto-http-0XD9AE729FC1C1E04A",
"auto-http-0XDD2D4E60FD4A61C3",
"auto-http-0X3675F89EF0612091",
"auto-http-0X131221E73F825974",
"auto-http-0X9CB71300ABD5A2A8",
"auto-http-0X970CBD2F2102BFA8",
"auto-http-0XA8096548ECEB85B7",
"auto-http-0XC9CDA429418EDC2B",
"auto-http-0XE3B163481423197D"
],
"locations": [],
"names": [],
"ports": [9200, 12349],
"schemes": ["tcp", "http"]
"names": null,
"ports": [
9200,
12349
],
"schemes": [
"http",
"tcp"
],
"urls": [
"tcp://localhost:9200",
"http://www.reddit.com/",
"https://www.elastic.co",
"http://localhost:12349/",
"https://www.google.com/",
"https://www.github.com/",
"http://www.google.com/",
"http://www.example.com/",
"https://news.google.com/",
"https://www.wikipedia.org/"
]
}
}
}

0 comments on commit 9a9cfc5

Please sign in to comment.