From 5e0e9f9ba07b65560ba117c4da8d46a37662b1e8 Mon Sep 17 00:00:00 2001 From: Joshua Date: Wed, 27 Oct 2021 15:54:10 -0700 Subject: [PATCH] Use advanced settings for date format in csv reports (#186) --- .../components/context_menu/context_menu.js | 3 ++- public/components/main/main_utils.tsx | 11 +++++++-- public/components/utils/settings_service.ts | 23 +++++++++++++++++++ public/plugin.ts | 2 ++ server/routes/lib/createReport.ts | 4 ++++ server/routes/report.ts | 3 +++ server/routes/utils/constants.ts | 2 +- server/routes/utils/dataReportHelpers.ts | 6 ++--- .../routes/utils/savedSearchReportHelper.ts | 7 ++++-- 9 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 public/components/utils/settings_service.ts diff --git a/public/components/context_menu/context_menu.js b/public/components/context_menu/context_menu.js index 109a3d17..6cf87e5d 100644 --- a/public/components/context_menu/context_menu.js +++ b/public/components/context_menu/context_menu.js @@ -45,6 +45,7 @@ import { import { timeRangeMatcher } from '../utils/utils'; import { parse } from 'url'; import { unhashUrl } from '../../../../../src/plugins/opensearch_dashboards_utils/public'; +import { uiSettingsService } from '../utils/settings_service'; const generateInContextReport = async ( timeRanges, @@ -106,7 +107,7 @@ const generateInContextReport = async ( fetch( `../api/reporting/generateReport?timezone=${ Intl.DateTimeFormat().resolvedOptions().timeZone - }`, + }&dateFormat=${uiSettingsService.get('dateFormat')}`, { headers: { 'Content-Type': 'application/json', diff --git a/public/components/main/main_utils.tsx b/public/components/main/main_utils.tsx index 726e5aca..8ab80da3 100644 --- a/public/components/main/main_utils.tsx +++ b/public/components/main/main_utils.tsx @@ -27,6 +27,7 @@ import 'babel-polyfill'; import { i18n } from '@osd/i18n'; import { HttpFetchOptions, HttpSetup } from '../../../../../src/core/public'; +import { uiSettingsService } from '../utils/settings_service'; export const displayDeliveryChannels = (configIds: Array, channels: Array<{label: string, id: string}>) => { let displayChannels = []; @@ -193,7 +194,10 @@ export const generateReportFromDefinitionId = async ( headers: { 'Content-Type': 'application/json', }, - query: { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone }, + query: { + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, + dateFormat: uiSettingsService.get('dateFormat'), + }, }) .then(async (response: any) => { // for emailing a report, this API response doesn't have response body @@ -226,7 +230,10 @@ export const generateReportById = async ( ) => { await httpClient .get(`../api/reporting/generateReport/${reportId}`, { - query: { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone }, + query: { + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, + dateFormat: uiSettingsService.get('dateFormat'), + }, }) .then(async (response) => { //TODO: duplicate code, extract to be a function that can reuse. e.g. handleResponse(response) diff --git a/public/components/utils/settings_service.ts b/public/components/utils/settings_service.ts new file mode 100644 index 00000000..f1baf906 --- /dev/null +++ b/public/components/utils/settings_service.ts @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import { IUiSettingsClient } from '../../../../../src/core/public'; + +let uiSettings: IUiSettingsClient; + +export const uiSettingsService = { + init: (client: IUiSettingsClient) => { + uiSettings = client; + }, + get: (key: string, defaultOverride?: any) => { + return uiSettings?.get(key, defaultOverride) || ''; + }, +}; diff --git a/public/plugin.ts b/public/plugin.ts index 98d8bd71..c4bbb23d 100644 --- a/public/plugin.ts +++ b/public/plugin.ts @@ -38,11 +38,13 @@ import { import { i18n } from '@osd/i18n'; import './components/context_menu/context_menu'; import { PLUGIN_ID, PLUGIN_NAME } from '../common'; +import { uiSettingsService } from './components/utils/settings_service'; export class ReportsDashboardsPlugin implements Plugin { public setup(core: CoreSetup): ReportsDashboardsPluginSetup { + uiSettingsService.init(core.uiSettings); // Register an application into the side navigation menu core.application.register({ id: PLUGIN_ID, diff --git a/server/routes/lib/createReport.ts b/server/routes/lib/createReport.ts index e5d550fc..36709343 100644 --- a/server/routes/lib/createReport.ts +++ b/server/routes/lib/createReport.ts @@ -28,6 +28,7 @@ import { REPORT_TYPE, REPORT_STATE, DELIVERY_TYPE, + DATA_REPORT_CONFIG, EXTRA_HEADERS, } from '../utils/constants'; @@ -67,6 +68,8 @@ export const createReport = async ( const opensearchClient = context.core.opensearch.legacy.client; // @ts-ignore const timezone = request.query.timezone; + // @ts-ignore + const dateFormat = request.query.dateFormat || DATA_REPORT_CONFIG.excelDateFormat; const { basePath, serverInfo: { protocol, port, hostname }, @@ -93,6 +96,7 @@ export const createReport = async ( createReportResult = await createSavedSearchReport( report, opensearchClient, + dateFormat, isScheduledTask ); } else { diff --git a/server/routes/report.ts b/server/routes/report.ts index f4d406ca..be13d7ee 100644 --- a/server/routes/report.ts +++ b/server/routes/report.ts @@ -57,6 +57,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) { body: schema.any(), query: schema.object({ timezone: schema.maybe(schema.string()), + dateFormat: schema.maybe(schema.string()), }), }, }, @@ -121,6 +122,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) { }), query: schema.object({ timezone: schema.string(), + dateFormat: schema.maybe(schema.string()), }), }, }, @@ -186,6 +188,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) { }), query: schema.object({ timezone: schema.string(), + dateFormat: schema.maybe(schema.string()), }), }, }, diff --git a/server/routes/utils/constants.ts b/server/routes/utils/constants.ts index e311c394..465256d5 100644 --- a/server/routes/utils/constants.ts +++ b/server/routes/utils/constants.ts @@ -64,7 +64,7 @@ export enum REPORT_TYPE { } export enum DATA_REPORT_CONFIG { - excelDateFormat = 'MM/DD/YYYY h:mm:ss a', + excelDateFormat = 'MM/DD/YYYY h:mm:ss.SSS a', } export enum TRIGGER_TYPE { diff --git a/server/routes/utils/dataReportHelpers.ts b/server/routes/utils/dataReportHelpers.ts index 23605ae6..7a3b6d19 100644 --- a/server/routes/utils/dataReportHelpers.ts +++ b/server/routes/utils/dataReportHelpers.ts @@ -178,7 +178,7 @@ export const buildQuery = (report, is_count) => { }; // Fetch the data from OpenSearch -export const getOpenSearchData = (arrayHits, report, params) => { +export const getOpenSearchData = (arrayHits, report, params, dateFormat: string) => { let hits: any = []; for (let valueRes of arrayHits) { for (let data of valueRes.hits) { @@ -186,9 +186,7 @@ export const getOpenSearchData = (arrayHits, report, params) => { // get all the fields of type date and format them to excel format for (let dateType of report._source.dateFields) { if (data._source[dateType]) { - data._source[dateType] = moment(fields[dateType][0]).format( - DATA_REPORT_CONFIG.excelDateFormat - ); + data._source[dateType] = moment(fields[dateType][0]).format(dateFormat); } } delete data['fields']; diff --git a/server/routes/utils/savedSearchReportHelper.ts b/server/routes/utils/savedSearchReportHelper.ts index ac2802c0..76a5a9cf 100644 --- a/server/routes/utils/savedSearchReportHelper.ts +++ b/server/routes/utils/savedSearchReportHelper.ts @@ -48,6 +48,7 @@ const scrollTimeout = '1m'; export async function createSavedSearchReport( report: any, client: ILegacyClusterClient | ILegacyScopedClusterClient, + dateFormat: string, isScheduledTask: boolean = true ): Promise { const params = report.report_definition.report_params; @@ -58,6 +59,7 @@ export async function createSavedSearchReport( const data = await generateReportData( client, params.core_params, + dateFormat, isScheduledTask ); @@ -141,6 +143,7 @@ async function populateMetaData( async function generateReportData( client: ILegacyClusterClient | ILegacyScopedClusterClient, params: any, + dateFormat: string, isScheduledTask: boolean ) { let opensearchData: any = {}; @@ -267,7 +270,7 @@ async function generateReportData( for (const dateType of report._source.dateFields) { docvalues.push({ field: dateType, - format: 'date_hour_minute', + format: 'date_hour_minute_second_fraction', }); } @@ -282,7 +285,7 @@ async function generateReportData( // Parse OpenSearch data and convert to CSV async function convertOpenSearchDataToCsv() { const dataset: any = []; - dataset.push(getOpenSearchData(arrayHits, report, params)); + dataset.push(getOpenSearchData(arrayHits, report, params, dateFormat)); return await convertToCSV(dataset); } }