kibana/x-pack/plugins/lens/public/utils.ts
Alexey Antonov e2c6a039f6
lens should register expression functions in setup contract (#110639)
* lens should register expression functions in setup contract

Closes: #106510

* fix CI

* build optimization

* build optimizations - step 3

* fix CI

* try to optimize bundle

* Update x-pack/plugins/lens/common/expressions/time_scale/types.ts

Co-authored-by: Marta Bondyra <marta.bondyra@gmail.com>

* Update types.ts

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Marta Bondyra <marta.bondyra@gmail.com>
2021-09-07 10:32:30 +03:00

96 lines
3.3 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { uniq } from 'lodash';
import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import type {
IndexPattern,
IndexPatternsContract,
TimefilterContract,
} from 'src/plugins/data/public';
import type { IUiSettingsClient } from 'kibana/public';
import type { SavedObjectReference } from 'kibana/public';
import type { Document } from './persistence/saved_object_store';
import type { Datasource, DatasourceMap } from './types';
import type { DatasourceStates } from './state_management';
export function getVisualizeGeoFieldMessage(fieldType: string) {
return i18n.translate('xpack.lens.visualizeGeoFieldMessage', {
defaultMessage: `Lens cannot visualize {fieldType} fields`,
values: { fieldType },
});
}
export const getResolvedDateRange = function (timefilter: TimefilterContract) {
const { from, to } = timefilter.getTime();
const { min, max } = timefilter.calculateBounds({
from,
to,
});
return { fromDate: min?.toISOString() || from, toDate: max?.toISOString() || to };
};
export function containsDynamicMath(dateMathString: string) {
return dateMathString.includes('now');
}
export function getTimeZone(uiSettings: IUiSettingsClient) {
const configuredTimeZone = uiSettings.get('dateFormat:tz');
if (configuredTimeZone === 'Browser') {
return moment.tz.guess();
}
return configuredTimeZone;
}
export function getActiveDatasourceIdFromDoc(doc?: Document) {
if (!doc) {
return null;
}
const [firstDatasourceFromDoc] = Object.keys(doc.state.datasourceStates);
return firstDatasourceFromDoc || null;
}
export const getInitialDatasourceId = (datasourceMap: DatasourceMap, doc?: Document) => {
return (doc && getActiveDatasourceIdFromDoc(doc)) || Object.keys(datasourceMap)[0] || null;
};
export function getIndexPatternsIds({
activeDatasources,
datasourceStates,
}: {
activeDatasources: Record<string, Datasource>;
datasourceStates: DatasourceStates;
}): string[] {
const references: SavedObjectReference[] = [];
Object.entries(activeDatasources).forEach(([id, datasource]) => {
const { savedObjectReferences } = datasource.getPersistableState(datasourceStates[id].state);
references.push(...savedObjectReferences);
});
const uniqueFilterableIndexPatternIds = uniq(
references.filter(({ type }) => type === 'index-pattern').map(({ id }) => id)
);
return uniqueFilterableIndexPatternIds;
}
export async function getIndexPatternsObjects(
ids: string[],
indexPatternsService: IndexPatternsContract
): Promise<{ indexPatterns: IndexPattern[]; rejectedIds: string[] }> {
const responses = await Promise.allSettled(ids.map((id) => indexPatternsService.get(id)));
const fullfilled = responses.filter(
(response): response is PromiseFulfilledResult<IndexPattern> => response.status === 'fulfilled'
);
const rejectedIds = responses
.map((_response, i) => ids[i])
.filter((id, i) => responses[i].status === 'rejected');
// return also the rejected ids in case we want to show something later on
return { indexPatterns: fullfilled.map((response) => response.value), rejectedIds };
}