Skip to content
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

Use advanced settings for date format in csv reports #186

Merged
merged 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 9 additions & 2 deletions dashboards-reports/public/components/main/main_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, channels: Array<{label: string, id: string}>) => {
let displayChannels = [];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions dashboards-reports/public/components/utils/settings_service.ts
Original file line number Diff line number Diff line change
@@ -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) || '';
},
};
2 changes: 2 additions & 0 deletions dashboards-reports/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReportsDashboardsPluginSetup, ReportsDashboardsPluginStart>
{
public setup(core: CoreSetup): ReportsDashboardsPluginSetup {
uiSettingsService.init(core.uiSettings);
// Register an application into the side navigation menu
core.application.register({
id: PLUGIN_ID,
Expand Down
4 changes: 4 additions & 0 deletions dashboards-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
REPORT_TYPE,
REPORT_STATE,
DELIVERY_TYPE,
DATA_REPORT_CONFIG,
EXTRA_HEADERS,
} from '../utils/constants';

Expand Down Expand Up @@ -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 },
Expand All @@ -93,6 +96,7 @@ export const createReport = async (
createReportResult = await createSavedSearchReport(
report,
opensearchClient,
dateFormat,
isScheduledTask
);
} else {
Expand Down
3 changes: 3 additions & 0 deletions dashboards-reports/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}),
},
},
Expand Down Expand Up @@ -121,6 +122,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}),
query: schema.object({
timezone: schema.string(),
dateFormat: schema.maybe(schema.string()),
}),
},
},
Expand Down Expand Up @@ -186,6 +188,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}),
query: schema.object({
timezone: schema.string(),
dateFormat: schema.maybe(schema.string()),
}),
},
},
Expand Down
2 changes: 1 addition & 1 deletion dashboards-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions dashboards-reports/server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,15 @@ 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) {
const fields = data.fields;
// 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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const scrollTimeout = '1m';
export async function createSavedSearchReport(
report: any,
client: ILegacyClusterClient | ILegacyScopedClusterClient,
dateFormat: string,
isScheduledTask: boolean = true
): Promise<CreateReportResultType> {
const params = report.report_definition.report_params;
Expand All @@ -58,6 +59,7 @@ export async function createSavedSearchReport(
const data = await generateReportData(
client,
params.core_params,
dateFormat,
isScheduledTask
);

Expand Down Expand Up @@ -141,6 +143,7 @@ async function populateMetaData(
async function generateReportData(
client: ILegacyClusterClient | ILegacyScopedClusterClient,
params: any,
dateFormat: string,
isScheduledTask: boolean
) {
let opensearchData: any = {};
Expand Down Expand Up @@ -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',
});
}

Expand All @@ -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);
}
}