Skip to content

Commit

Permalink
make dateFieldsFormatter to formatter so more general
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Feb 19, 2025
1 parent 74c4f18 commit fc4044e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 35 deletions.
17 changes: 11 additions & 6 deletions src/plugins/data/common/data_frames/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import moment from 'moment';
import { ISearchOptions, SearchSourceFields } from '../search';
import { IIndexPatternFieldList, IndexPattern, IndexPatternField } from '../index_patterns';
import { OSD_FIELD_TYPES } from '../types';

describe('formatTimePickerDate', () => {
const mockDateFormat = 'YYYY-MM-DD HH:mm:ss';
Expand Down Expand Up @@ -95,12 +96,14 @@ describe('convertResult', () => {
};

// Custom date formatter
const customFormatter = (dateStr: string) => {
return moment.utc(dateStr).format('YYYY-MM-DDTHH:mm:ssZ');
const customFormatter = (dateStr: string, type: OSD_FIELD_TYPES) => {
if (type === OSD_FIELD_TYPES.DATE) {
return moment.utc(dateStr).format('YYYY-MM-DDTHH:mm:ssZ');
}
};

const options: ISearchOptions = {
dateFieldsFormatter: customFormatter,
formatter: customFormatter,
};

const result = convertResult({ response, options });
Expand Down Expand Up @@ -188,12 +191,14 @@ describe('convertResult', () => {
};

// Custom date formatter
const customFormatter = (dateStr: string) => {
return moment.utc(dateStr).format('YYYY-MM-DDTHH:mm:ssZ');
const customFormatter = (dateStr: string, type: OSD_FIELD_TYPES) => {
if (type === OSD_FIELD_TYPES.DATE) {
return moment.utc(dateStr).format('YYYY-MM-DDTHH:mm:ssZ');
}
};

const options: ISearchOptions = {
dateFieldsFormatter: customFormatter,
formatter: customFormatter,
};

const result = convertResult({ response, fields, options });
Expand Down
43 changes: 24 additions & 19 deletions src/plugins/data/common/data_frames/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './types';
import { IFieldType } from './fields';
import { IndexPatternFieldMap, IndexPatternSpec } from '../index_patterns';
import { TimeRange } from '../types';
import { OSD_FIELD_TYPES, TimeRange } from '../types';
import { IDataFrame } from './types';
import { ISearchOptions, SearchSourceFields } from '../search';

Expand Down Expand Up @@ -62,35 +62,40 @@ export const convertResult = ({
for (let index = 0; index < data.size; index++) {
const hit: { [key: string]: any } = {};

const processNestedField = (field: any, value: any, formatter: any): any => {
const nestedHit: { [key: string]: any } = value;
Object.entries(value).forEach(([nestedField, nestedValue]) => {
// Need to get the flattened field name for nested fields ex.products.created_on
const flattenedFieldName = `${field.name}.${nestedField}`;

// Go through search source fields to find the field type of the nested field
fields?.index?.fields.forEach((searchSourceField) => {
if (
searchSourceField.displayName === flattenedFieldName &&
searchSourceField.type === 'date'
) {
nestedHit[nestedField] = formatter(nestedValue as string, OSD_FIELD_TYPES.DATE);
}
});
});
return nestedHit;
};

const processField = (field: any, value: any): any => {
if (options && options.dateFieldsFormatter) {
const formatter = options.dateFieldsFormatter;
if (options && options.formatter) {
// Handle date fields
if (field.type === 'date') {
return formatter(value);
return options.formatter(value, OSD_FIELD_TYPES.DATE);
}
// Handle nested objects with potential date fields
else if (field.type === 'object') {
const nestedHit: { [key: string]: any } = value;
Object.entries(value).forEach(([nestedField, nestedValue]) => {
// Need to get the flattened field name
const flattenedFieldName = `${field.name}.${nestedField}`;
fields?.index?.fields.forEach((indexPatternField) => {
if (
indexPatternField.displayName === flattenedFieldName &&
indexPatternField.type === 'date'
) {
nestedHit[nestedField] = formatter(nestedValue as string);
}
});
});
return nestedHit;
return processNestedField(field, value, options.formatter);
} else {
// Default case when the field is either a date type or object type
return value;
}
} else {
// Default case when we don't have a dateFieldsFormatter
// Default case when we don't have a formatter
return value;
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/data/common/search/opensearch_search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import { SearchResponse } from 'elasticsearch';
import { Search } from '@opensearch-project/opensearch/api/requestParams';
import { IOpenSearchDashboardsSearchRequest, IOpenSearchDashboardsSearchResponse } from '../types';
import { OSD_FIELD_TYPES } from '../../types';

export const OPENSEARCH_SEARCH_STRATEGY = 'opensearch';
export const OPENSEARCH_SEARCH_WITH_LONG_NUMERALS_STRATEGY = 'opensearch-with-long-numerals';
Expand All @@ -49,9 +50,9 @@ export interface ISearchOptions {
*/
withLongNumeralsSupport?: boolean;
/**
* Use this option to format the date fields in the search response.
* Use this option to format the fields in the search response.
*/
dateFieldsFormatter?: (value: string) => string;
formatter?: (value: any, type: OSD_FIELD_TYPES) => any;
}

export type ISearchRequestParams<T = Record<string, any>> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ export const indexTypeConfig: DatasetTypeConfig = {
pattern: dataset.title,
dataSourceId: dataset.dataSource?.id,
});
// TODO: map fields to the OSD Field type
// Similar to the S3 where it casteS3FieldTypeToOsdFieldType
// if field is object then transform
return fields.map((field: any) => ({
name: field.name,
type: field.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { ISearchInterceptor } from '../../../search';
import {
OSD_FIELD_TYPES,
Query,
QueryEditorExtensionConfig,
QueryStringContract,
Expand Down Expand Up @@ -54,7 +55,7 @@ export interface LanguageConfig {
sortable?: boolean;
filterable?: boolean;
visualizable?: boolean;
dateFieldsFormatter?: (value: string) => string;
formatter?: (value: any, type: OSD_FIELD_TYPES) => any;
};
showDocLinks?: boolean;
docLink?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ export const useSearch = (services: DiscoverViewServices) => {
abortSignal: fetchStateRef.current.abortController.signal,
withLongNumeralsSupport: await services.uiSettings.get(UI_SETTINGS.DATA_WITH_LONG_NUMERALS),
...(languageConfig &&
languageConfig.fields?.dateFieldsFormatter && {
dateFieldsFormatter: languageConfig.fields.dateFieldsFormatter,
languageConfig.fields?.formatter && {
formatter: languageConfig.fields.formatter,
}),
});

Expand Down
12 changes: 10 additions & 2 deletions src/plugins/query_enhancements/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { i18n } from '@osd/i18n';
import { BehaviorSubject } from 'rxjs';
import moment from 'moment';
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '../../../core/public';
import { DataStorage } from '../../data/common';
import { DataStorage, OSD_FIELD_TYPES } from '../../data/common';
import {
createEditor,
DefaultInput,
Expand Down Expand Up @@ -71,7 +71,15 @@ export class QueryEnhancementsPlugin
sortable: false,
filterable: false,
visualizable: false,
dateFieldsFormatter: (value: string) => moment.utc(value).format('YYYY-MM-DDTHH:mm:ssZ'), // PPL date fields need special formatting in order for discover table formatter to render in the correct time zone
formatter: (value: string, type: OSD_FIELD_TYPES) => {
switch (type) {
case OSD_FIELD_TYPES.DATE:
return moment.utc(value).format('YYYY-MM-DDTHH:mm:ssZ'); // PPL date fields need special formatting in order for discover table formatter to render in the correct time zone

default:
return value;
}
},
},
docLink: {
title: i18n.translate('queryEnhancements.pplLanguage.docLink', {
Expand Down

0 comments on commit fc4044e

Please sign in to comment.