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

[Demo][Dashboard] Public create API #181950

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 119 additions & 0 deletions src/plugins/dashboard/common/api/2023_10_31/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, type TypeOf } from '@kbn/config-schema';

const controlGroupInputSchema = schema
.object({
panelsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
controlStyle: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
chainingSystem: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
ignoreParentSettingsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
})
.extends({}, { unknowns: 'ignore' });

const baseDashboard = schema.object({
// General
title: schema.string({ meta: { description: 'A human-readable title for the dashboard' } }),
description: schema.string({ defaultValue: '', meta: { description: 'A short description.' } }),

// Search
kibanaSavedObjectMeta: schema.object(
{
searchSourceJSON: schema.maybe(
schema.string({
meta: {
description: 'A string containing search source',
},
})
),
},
{
meta: {
description: 'A container for various metadata',
},
}
),

// Time
timeRestore: schema.maybe(
schema.boolean({ meta: { description: 'Whether to restore time upon viewing this dashboard' } })
),
timeFrom: schema.maybe(
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } })
),
timeTo: schema.maybe(
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } })
),
refreshInterval: schema.maybe(
schema.object(
{
pause: schema.boolean({
meta: {
description:
'Whether the refresh interval is set to be paused while viewing the dashboard.',
},
}),
value: schema.number({
meta: {
description:
'A numeric value indicating refresh frequency expressed as, for example, 30s.',
},
}),
display: schema.maybe(
schema.string({
meta: {
description: 'Lorem ipsum',
},
})
),
section: schema.maybe(
schema.number({
meta: {
description: 'Lorem ipsum',
},
})
),
},
{
meta: {
description: 'A container for various refresh interval settings',
},
}
)
),

// Dashboard Content
controlGroupInput: schema.maybe(controlGroupInputSchema),
panelsJSON: schema.string({ defaultValue: '[]', meta: { description: 'Lorem ipsum' } }),
optionsJSON: schema.string({ defaultValue: '{}', meta: { description: 'Lorem ipsum' } }),

// Legacy
// hits: schema.maybe(schema.number()),
// version: schema.maybe(schema.number()),
});

export const dashboardCreate = baseDashboard.extends(
{
id: schema.maybe(schema.string({ meta: { description: 'The ID of the dashboard' } })),
},
{
meta: { id: 'dashboard.create' },
}
);

export const dashboard = baseDashboard.extends(
{
id: schema.string({ meta: { description: 'The ID of the dashboard' } }),
},
{
meta: { id: 'dashboard' },
}
);

export type Dashboard = TypeOf<typeof dashboard>;
9 changes: 9 additions & 0 deletions src/plugins/dashboard/common/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * as v2023_10_31 from './2023_10_31';
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const dashboardAttributesSchema = schema.object(
hits: schema.maybe(schema.number()),
version: schema.maybe(schema.number()),
},
{ unknowns: 'forbid' }
{ unknowns: 'forbid', meta: { id: 'dashboard.publicSchema' } }
);

export const dashboardSavedObjectSchema = savedObjectSchema(dashboardAttributesSchema);
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/dashboard/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { createDashboardSavedObjectType } from './dashboard_saved_object';
import { CONTENT_ID, LATEST_VERSION } from '../common/content_management';
import { registerDashboardUsageCollector } from './usage/register_collector';
import { dashboardPersistableStateServiceFactory } from './dashboard_container/dashboard_container_embeddable_factory';
import { registerCreate } from './routes/create';

interface SetupDeps {
embeddable: EmbeddableSetup;
Expand Down Expand Up @@ -60,6 +61,8 @@ export class DashboardPlugin
})
);

registerCreate(core.http.createRouter().versioned, plugins.contentManagement);

plugins.contentManagement.register({
id: CONTENT_ID,
storage: new DashboardStorage({
Expand Down
53 changes: 53 additions & 0 deletions src/plugins/dashboard/server/routes/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { RequestHandlerContext } from '@kbn/core-http-request-handler-context-server';
import type { VersionedRouter } from '@kbn/core-http-server';
import type { ContentManagementServerSetup } from '@kbn/content-management-plugin/server';
import { CONTENT_ID } from '@kbn/visualizations-plugin/common/content_management';
import type { Dashboard } from '../../common/api/2023_10_31';
import { v2023_10_31 } from '../../common/api';

export function registerCreate(
router: VersionedRouter<RequestHandlerContext>,
cm: ContentManagementServerSetup
) {
router
.post({
path: '/api/dashboard',
access: 'public',
description: 'Create a dashboard',
})
.addVersion(
{
version: '2023-10-31',
validate: {
request: {
body: v2023_10_31.dashboardCreate,
},
response: {
200: { body: v2023_10_31.dashboard },
},
},
},
async (ctx, req, res) => {
const client = cm.contentClient
.getForRequest({ request: req, requestHandlerContext: ctx })
.for<Dashboard>(CONTENT_ID);
let result: Dashboard;
try {
const cmResult = await client.create(req.body);
result = cmResult.result as unknown as Dashboard;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there is a gap in the Dashboard type being passed through so I was getting object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be investigated, in the code it looks like we are forwarding the type in CreateItemResponse<T>

Screenshot 2024-05-07 at 11 33 17

Copy link
Contributor Author

@jloleysens jloleysens May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is data: object does not let the return type infer the type of the generic to something other than object

Copy link
Contributor

@sebelga sebelga May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But here we are talking about the response right? You might be right but it is strange the the input type affects the output type. Although I've seen weirder things with TS 😄

} catch (e) {
// do some handling;
throw e;
}
return res.ok({ body: result });
}
);
}