-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
moving visualize/utils to new platform #56650
Changes from all commits
f94e251
7c021de
47e228b
c2c7dfc
81f102b
ee2c696
044950a
c8496ab
19da49a
cb0985f
98b203c
3d3cbfa
f0648cb
5f5091d
4554631
e1cf453
2c5581e
2fffeb6
d86ee0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export interface DateRangeKey { | ||
from: number; | ||
to: number; | ||
} | ||
|
||
export const convertDateRangeToString = ( | ||
{ from, to }: DateRangeKey, | ||
format: (val: any) => string | ||
) => { | ||
if (!from) { | ||
return 'Before ' + format(to); | ||
} else if (!to) { | ||
return 'After ' + format(from); | ||
} else { | ||
return format(from) + ' to ' + format(to); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export type IpRangeKey = | ||
| { type: 'mask'; mask: string } | ||
| { type: 'range'; from: string; to: string }; | ||
|
||
export const convertIPRangeToString = (range: IpRangeKey, format: (val: any) => string) => { | ||
if (range.type === 'mask') { | ||
return format(range.mask); | ||
} | ||
const from = range.from ? format(range.from) : '-Infinity'; | ||
const to = range.to ? format(range.to) : 'Infinity'; | ||
|
||
return `${from} to ${to}`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,24 @@ import { compact, uniq, map } from 'lodash'; | |
|
||
import { i18n } from '@kbn/i18n'; | ||
import { EuiPopoverProps, EuiIcon, keyCodes, htmlIdGenerator } from '@elastic/eui'; | ||
import { IAggConfig } from '../../../../../data/public'; | ||
|
||
// @ts-ignore | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { createFiltersFromEvent } from '../../../../../data/public/actions/filters/create_filters_from_event'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we re-export this from the top level of the data plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would consider waiting for #57177 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
import { CUSTOM_LEGEND_VIS_TYPES, LegendItem } from './models'; | ||
import { VisLegendItem } from './legend_item'; | ||
import { getPieNames } from './pie_utils'; | ||
import { getTableAggs } from '../../../legacy_imports'; | ||
|
||
import { Vis } from '../../../../../visualizations/public'; | ||
import { tabifyGetColumns } from '../../../legacy_imports'; | ||
|
||
const getTableAggs = (vis: Vis): IAggConfig[] => { | ||
if (!vis.aggs || !vis.aggs.getResponseAggs) { | ||
return []; | ||
} | ||
const columns = tabifyGetColumns(vis.aggs.getResponseAggs(), !vis.isHierarchical()); | ||
return columns.map(c => c.aggConfig); | ||
}; | ||
|
||
export interface VisLegendProps { | ||
vis: any; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,150 +17,15 @@ | |||||||||
* under the License. | ||||||||||
*/ | ||||||||||
|
||||||||||
import { i18n } from '@kbn/i18n'; | ||||||||||
import { identity } from 'lodash'; | ||||||||||
import { IAggConfig } from 'ui/agg_types'; | ||||||||||
import { npStart } from 'ui/new_platform'; | ||||||||||
import { SerializedFieldFormat } from 'src/plugins/expressions/public'; | ||||||||||
import { | ||||||||||
fieldFormats, | ||||||||||
IFieldFormat, | ||||||||||
FieldFormatId, | ||||||||||
FieldFormatsContentType, | ||||||||||
} from '../../../../../../plugins/data/public'; | ||||||||||
import { Vis } from '../../../../../core_plugins/visualizations/public'; | ||||||||||
import { fieldFormats, IFieldFormat } from '../../../../../../plugins/data/public'; | ||||||||||
import { SerializedFieldFormat } from '../../../../../../plugins/expressions/common/types'; | ||||||||||
|
||||||||||
import { tabifyGetColumns } from '../../../agg_response/tabify/_get_columns'; | ||||||||||
import { DateRangeKey, convertDateRangeToString } from '../../../agg_types'; | ||||||||||
import { IpRangeKey, convertIPRangeToString } from '../../../agg_types'; | ||||||||||
type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; | ||||||||||
|
||||||||||
interface TermsFieldFormatParams { | ||||||||||
otherBucketLabel: string; | ||||||||||
missingBucketLabel: string; | ||||||||||
id: string; | ||||||||||
} | ||||||||||
|
||||||||||
function isTermsFieldFormat( | ||||||||||
serializedFieldFormat: SerializedFieldFormat | ||||||||||
): serializedFieldFormat is SerializedFieldFormat<TermsFieldFormatParams> { | ||||||||||
return serializedFieldFormat.id === 'terms'; | ||||||||||
} | ||||||||||
|
||||||||||
const getConfig = (key: string, defaultOverride?: any): any => | ||||||||||
npStart.core.uiSettings.get(key, defaultOverride); | ||||||||||
const DefaultFieldFormat = fieldFormats.FieldFormat.from(identity); | ||||||||||
|
||||||||||
const getFieldFormat = (id?: FieldFormatId, params: object = {}): IFieldFormat => { | ||||||||||
const fieldFormatsService = npStart.plugins.data.fieldFormats; | ||||||||||
|
||||||||||
if (id) { | ||||||||||
const Format = fieldFormatsService.getType(id); | ||||||||||
|
||||||||||
if (Format) { | ||||||||||
return new Format(params, getConfig); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return new DefaultFieldFormat(); | ||||||||||
}; | ||||||||||
|
||||||||||
export const createFormat = (agg: IAggConfig): SerializedFieldFormat => { | ||||||||||
const format: SerializedFieldFormat = agg.params.field ? agg.params.field.format.toJSON() : {}; | ||||||||||
const formats: Record<string, () => SerializedFieldFormat> = { | ||||||||||
date_range: () => ({ id: 'date_range', params: format }), | ||||||||||
ip_range: () => ({ id: 'ip_range', params: format }), | ||||||||||
percentile_ranks: () => ({ id: 'percent' }), | ||||||||||
count: () => ({ id: 'number' }), | ||||||||||
cardinality: () => ({ id: 'number' }), | ||||||||||
date_histogram: () => ({ | ||||||||||
id: 'date', | ||||||||||
params: { | ||||||||||
pattern: (agg as any).buckets.getScaledDateFormat(), | ||||||||||
}, | ||||||||||
}), | ||||||||||
terms: () => ({ | ||||||||||
id: 'terms', | ||||||||||
params: { | ||||||||||
id: format.id, | ||||||||||
otherBucketLabel: agg.params.otherBucketLabel, | ||||||||||
missingBucketLabel: agg.params.missingBucketLabel, | ||||||||||
...format.params, | ||||||||||
}, | ||||||||||
}), | ||||||||||
range: () => ({ | ||||||||||
id: 'range', | ||||||||||
params: { id: format.id, ...format.params }, | ||||||||||
}), | ||||||||||
}; | ||||||||||
|
||||||||||
return formats[agg.type.name] ? formats[agg.type.name]() : format; | ||||||||||
}; | ||||||||||
|
||||||||||
export type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; | ||||||||||
|
||||||||||
export const getFormat: FormatFactory = mapping => { | ||||||||||
if (!mapping) { | ||||||||||
return new DefaultFieldFormat(); | ||||||||||
} | ||||||||||
const { id } = mapping; | ||||||||||
if (id === 'range') { | ||||||||||
const RangeFormat = fieldFormats.FieldFormat.from((range: any) => { | ||||||||||
const format = getFieldFormat(id, mapping.params); | ||||||||||
const gte = '\u2265'; | ||||||||||
const lt = '\u003c'; | ||||||||||
return i18n.translate('common.ui.aggTypes.rangesFormatMessage', { | ||||||||||
defaultMessage: '{gte} {from} and {lt} {to}', | ||||||||||
values: { | ||||||||||
gte, | ||||||||||
from: format.convert(range.gte), | ||||||||||
lt, | ||||||||||
to: format.convert(range.lt), | ||||||||||
}, | ||||||||||
}); | ||||||||||
}); | ||||||||||
return new RangeFormat(); | ||||||||||
} else if (id === 'date_range') { | ||||||||||
const nestedFormatter = mapping.params as SerializedFieldFormat; | ||||||||||
const DateRangeFormat = fieldFormats.FieldFormat.from((range: DateRangeKey) => { | ||||||||||
const format = getFieldFormat(nestedFormatter.id, nestedFormatter.params); | ||||||||||
return convertDateRangeToString(range, format.convert.bind(format)); | ||||||||||
}); | ||||||||||
return new DateRangeFormat(); | ||||||||||
} else if (id === 'ip_range') { | ||||||||||
const nestedFormatter = mapping.params as SerializedFieldFormat; | ||||||||||
const IpRangeFormat = fieldFormats.FieldFormat.from((range: IpRangeKey) => { | ||||||||||
const format = getFieldFormat(nestedFormatter.id, nestedFormatter.params); | ||||||||||
return convertIPRangeToString(range, format.convert.bind(format)); | ||||||||||
}); | ||||||||||
return new IpRangeFormat(); | ||||||||||
} else if (isTermsFieldFormat(mapping) && mapping.params) { | ||||||||||
const { params } = mapping; | ||||||||||
const convert = (val: string, type: FieldFormatsContentType) => { | ||||||||||
const format = getFieldFormat(params.id, mapping.params); | ||||||||||
|
||||||||||
if (val === '__other__') { | ||||||||||
return params.otherBucketLabel; | ||||||||||
} | ||||||||||
if (val === '__missing__') { | ||||||||||
return params.missingBucketLabel; | ||||||||||
} | ||||||||||
|
||||||||||
return format.convert(val, type); | ||||||||||
}; | ||||||||||
|
||||||||||
return { | ||||||||||
convert, | ||||||||||
getConverterFor: (type: FieldFormatsContentType) => (val: string) => convert(val, type), | ||||||||||
} as IFieldFormat; | ||||||||||
} else { | ||||||||||
return getFieldFormat(id, mapping.params); | ||||||||||
} | ||||||||||
const createFormat = fieldFormats.serialize; | ||||||||||
const getFormat: FormatFactory = (mapping?) => { | ||||||||||
return npStart.plugins.data.fieldFormats.deserialize(mapping as any); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may be able to get rid of
Suggested change
...or:
Suggested change
|
||||||||||
}; | ||||||||||
|
||||||||||
export const getTableAggs = (vis: Vis): IAggConfig[] => { | ||||||||||
if (!vis.aggs || !vis.aggs.getResponseAggs) { | ||||||||||
return []; | ||||||||||
} | ||||||||||
const columns = tabifyGetColumns(vis.aggs.getResponseAggs(), !vis.isHierarchical()); | ||||||||||
return columns.map(c => c.aggConfig); | ||||||||||
}; | ||||||||||
export { getFormat, createFormat, FormatFactory }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same as the
TimeRange
type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And
convertDateRangeToString
looks like a function that could be exported as a utility fromdata/public/index
, taking in aTimeRange
and returning a string