Skip to content

Commit

Permalink
add reporting osd server configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongnan Su <szhongna@amazon.com>
  • Loading branch information
zhongnansu committed Nov 8, 2021
1 parent b941c82 commit 822bba9
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 63 deletions.
3 changes: 2 additions & 1 deletion dashboards-reports/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"requiredPlugins": ["navigation", "data", "opensearchDashboardsUtils"],
"optionalPlugins": ["share"],
"server": true,
"ui": true
"ui": true,
"configPath": ["reporting"]
}
9 changes: 7 additions & 2 deletions dashboards-reports/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ import {
HttpServerInfo,
PluginInitializerContext,
} from '../../../src/core/server';
import { ReportingConfigType } from './config';
import { ReportsDashboardsPlugin } from './plugin';

export type AccessInfoType = {
basePath: string;
serverInfo: HttpServerInfo;
};
export { config } from './config';
export { ReportingConfig } from './config/config';
export { ReportsDashboardsPlugin as Plugin };

// This exports static code and TypeScript types,
// as well as, OpenSearch Dashboards Platform `plugin()` initializer.

export function plugin(initializerContext: PluginInitializerContext) {
export function plugin(
initializerContext: PluginInitializerContext<ReportingConfigType>
) {
return new ReportsDashboardsPlugin(initializerContext);
}

Expand Down
45 changes: 31 additions & 14 deletions dashboards-reports/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import {
import registerRoutes from './routes';
import { pollAndExecuteJob } from './executor/executor';
import { POLL_INTERVAL } from './utils/constants';
import { AccessInfoType } from 'server';
import { NotificationsPlugin } from './clusters/notificationsPlugin';
import { buildConfig, ReportingConfigType } from './config';
import { ReportingConfig } from './config/config';

export interface ReportsPluginRequestContext {
logger: Logger;
Expand All @@ -61,24 +62,40 @@ export class ReportsDashboardsPlugin
Plugin<ReportsDashboardsPluginSetup, ReportsDashboardsPluginStart> {
private readonly logger: Logger;
private readonly semaphore: SemaphoreInterface;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();

private readonly initializerContext: PluginInitializerContext<
ReportingConfigType
>;
private reportingConfig?: ReportingConfig;

constructor(context: PluginInitializerContext<ReportingConfigType>) {
this.logger = context.logger.get();
this.initializerContext = context;
const timeoutError = new Error('Server busy');
timeoutError.statusCode = 503;
this.semaphore = withTimeout(new Semaphore(1), 180000, timeoutError);
}

public setup(core: CoreSetup) {
public async setup(core: CoreSetup) {
this.logger.debug('reports-dashboards: Setup');

const config = core.http.getServerInfo();
const serverBasePath = core.http.basePath.serverBasePath;
const accessInfo: AccessInfoType = {
basePath: serverBasePath,
serverInfo: config,
};
try {
const config = await buildConfig(
this.initializerContext,
core,
this.logger
);
this.reportingConfig = config;
this.logger.debug('Setup complete');
} catch (error) {
this.logger.error(
`Error in Reporting setup, reporting may not function properly`
);
this.logger.error(error);
}

if (!this.reportingConfig) {
throw new Error('Reporting Config is not initialized');
}

const router = core.http.createRouter();
// Deprecated API. Switch to the new opensearch client as soon as https://github.com/elastic/kibana/issues/35508 done.
Expand All @@ -97,7 +114,7 @@ export class ReportsDashboardsPlugin
);

// Register server side APIs
registerRoutes(router, accessInfo);
registerRoutes(router, this.reportingConfig);

// put logger into route handler context, so that we don't need to pass through parameters
core.http.registerRouteHandlerContext(
Expand All @@ -108,7 +125,7 @@ export class ReportsDashboardsPlugin
logger: this.logger,
semaphore: this.semaphore,
opensearchReportsClient,
notificationsClient
notificationsClient,
};
}
);
Expand Down
8 changes: 4 additions & 4 deletions dashboards-reports/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import registerReportSourceRoute from './reportSource';
import registerMetricRoute from './metric';
import registerNotificationRoute from './notifications';
import { IRouter } from '../../../../src/core/server';
import { AccessInfoType } from 'server';
import { ReportingConfig } from 'server/config/config';

export default function (router: IRouter, accessInfo: AccessInfoType) {
registerReportRoute(router, accessInfo);
registerReportDefinitionRoute(router, accessInfo);
export default function (router: IRouter, config: ReportingConfig) {
registerReportRoute(router, config);
registerReportDefinitionRoute(router, config);
registerReportSourceRoute(router);
registerMetricRoute(router);
registerNotificationRoute(router);
Expand Down
12 changes: 6 additions & 6 deletions dashboards-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ import { SetCookie, Headers } from 'puppeteer-core';
import { updateReportState } from './updateReportState';
import { saveReport } from './saveReport';
import { SemaphoreInterface } from 'async-mutex';
import { AccessInfoType } from 'server';
import { ReportingConfig } from 'server';
import _ from 'lodash';

export const createReport = async (
request: OpenSearchDashboardsRequest,
context: RequestHandlerContext,
report: ReportSchemaType,
accessInfo: AccessInfoType,
config: ReportingConfig,
savedReportId?: string
): Promise<CreateReportResultType> => {
const isScheduledTask = false;
Expand All @@ -73,10 +73,10 @@ export const createReport = async (
request.query.dateFormat || DATA_REPORT_CONFIG.excelDateFormat;
// @ts-ignore
const csvSeparator = request.query.csvSeparator || ',';
const {
basePath,
serverInfo: { protocol, port, hostname },
} = accessInfo;
const protocol = config.get('osdServer', 'protocol');
const hostname = config.get('osdServer', 'hostname');
const port = config.get('osdServer', 'port');
const basePath = config.osdConfig.get('server', 'basePath');

let createReportResult: CreateReportResultType;
let reportId;
Expand Down
25 changes: 9 additions & 16 deletions dashboards-reports/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ import {
} from './utils/converters/backendToUi';
import { addToMetric } from './utils/metricHelper';
import { validateReport } from '../../server/utils/validationHelper';
import { AccessInfoType } from 'server';
import { ReportingConfig } from 'server';

export default function (router: IRouter, accessInfo: AccessInfoType) {
const {
basePath,
serverInfo: { protocol, port, hostname },
} = accessInfo;
export default function (router: IRouter, config: ReportingConfig) {
const protocol = config.get('osdServer', 'protocol');
const hostname = config.get('osdServer', 'hostname');
const port = config.get('osdServer', 'port');
const basePath = config.osdConfig.get('server', 'basePath');
// generate report (with provided metadata)
router.post(
{
Expand All @@ -71,7 +71,6 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
let report = request.body;

// input validation
try {
report.report_definition.report_params.core_params.origin = `${protocol}://${hostname}:${port}${basePath}`;
Expand All @@ -87,12 +86,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}

try {
const reportData = await createReport(
request,
context,
report,
accessInfo
);
const reportData = await createReport(request, context, report, config);

// if not deliver to user himself , no need to send actual file data to client
const delivery = report.report_definition.delivery;
Expand Down Expand Up @@ -136,7 +130,6 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
addToMetric('report', 'download', 'count');
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
let report: any;
try {
const savedReportId = request.params.reportId;
// @ts-ignore
Expand All @@ -160,7 +153,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
request,
context,
report,
accessInfo,
config,
savedReportId
);
addToMetric('report', 'download', 'count', report);
Expand Down Expand Up @@ -231,7 +224,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
request,
context,
report,
accessInfo,
config,
reportId
);
addToMetric('report', 'create_from_definition', 'count', report);
Expand Down
13 changes: 7 additions & 6 deletions dashboards-reports/server/routes/reportDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ import { updateReportDefinition } from './lib/updateReportDefinition';
import { DEFAULT_MAX_SIZE } from './utils/constants';
import { addToMetric } from './utils/metricHelper';
import { validateReportDefinition } from '../../server/utils/validationHelper';
import { AccessInfoType } from 'server';
import { ReportingConfig } from 'server';

export default function (router: IRouter, config: ReportingConfig) {
const protocol = config.get('osdServer', 'protocol');
const hostname = config.get('osdServer', 'hostname');
const port = config.get('osdServer', 'port');
const basePath = config.osdConfig.get('server', 'basePath');

export default function (router: IRouter, accessInfo: AccessInfoType) {
const {
basePath,
serverInfo: { protocol, port, hostname },
} = accessInfo;
// Create report Definition
router.post(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* permissions and limitations under the License.
*/

import { AccessInfoType } from 'server';
import { ReportingConfig } from 'server/config/config';
import {
BackendReportInstanceType,
BACKEND_DELIVERY_FORMAT,
Expand Down Expand Up @@ -83,19 +83,10 @@ const input: BackendReportInstanceType = {
status: BACKEND_REPORT_STATE.success,
};

const testAccessInfo: AccessInfoType = {
basePath: '',
serverInfo: {
name: '',
hostname: 'localhost',
port: 5601,
protocol: 'http',
},
};
const sampleServerBasePath = '/test';

const output = {
query_url:
"/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f?_g=(time:(from:'2020-11-11T00:32:00.000Z',to:'2020-11-11T01:02:00.000Z'))",
query_url: `${sampleServerBasePath}/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f?_g=(time:(from:'2020-11-11T00:32:00.000Z',to:'2020-11-11T01:02:00.000Z'))`,
time_from: 1605054720000,
time_to: 1605056520000,
last_updated: 1605056644321,
Expand All @@ -107,7 +98,7 @@ const output = {
report_source: 'Dashboard',
description: 'some random',
core_params: {
base_url: '/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f',
base_url: `${sampleServerBasePath}/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f`,
report_format: 'pdf',
header: '<p>test header</p>',
footer: '<p>fake footer</p>',
Expand Down Expand Up @@ -141,7 +132,7 @@ const output = {

describe('test backend to ui model conversion', () => {
test('convert backend to ui report', async () => {
const res = backendToUiReport(input, testAccessInfo.basePath);
const res = backendToUiReport(input, sampleServerBasePath);
expect(res).toEqual(output);
}, 20000);
});

0 comments on commit 822bba9

Please sign in to comment.