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

Add updated_at column to objects' tables #1218

Merged
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
6 changes: 4 additions & 2 deletions src/core/public/saved_objects/saved_objects_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import { SimpleSavedObject } from './simple_saved_object';
import { httpServiceMock } from '../http/http_service.mock';

describe('SavedObjectsClient', () => {
const updatedAt = new Date().toISOString();
const doc = {
id: 'AVwSwFxtcMV38qjDZoQg',
type: 'config',
attributes: { title: 'Example title' },
version: 'foo',
updated_at: updatedAt,
};

const http = httpServiceMock.createStartContract();
Expand Down Expand Up @@ -356,7 +358,7 @@ describe('SavedObjectsClient', () => {
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\"}]",
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": false,
Expand All @@ -374,7 +376,7 @@ describe('SavedObjectsClient', () => {
Array [
"/api/saved_objects/_bulk_create",
Object {
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\"}]",
"body": "[{\\"id\\":\\"AVwSwFxtcMV38qjDZoQg\\",\\"type\\":\\"config\\",\\"attributes\\":{\\"title\\":\\"Example title\\"},\\"version\\":\\"foo\\",\\"updated_at\\":\\"${updatedAt}\\"}]",
"method": "POST",
"query": Object {
"overwrite": true,
Expand Down
7 changes: 7 additions & 0 deletions src/core/public/saved_objects/simple_saved_object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ describe('SimpleSavedObject', () => {
const savedObject = new SimpleSavedObject(client, { version } as SavedObject);
expect(savedObject._version).toEqual(version);
});

it('persists updated_at', () => {
const updatedAt = new Date().toString();

const savedObject = new SimpleSavedObject(client, { updated_at: updatedAt } as SavedObject);
expect(savedObject.updated_at).toEqual(updatedAt);
});
});
14 changes: 13 additions & 1 deletion src/core/public/saved_objects/simple_saved_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,29 @@ export class SimpleSavedObject<T = unknown> {
public migrationVersion: SavedObjectType<T>['migrationVersion'];
public error: SavedObjectType<T>['error'];
public references: SavedObjectType<T>['references'];
public updated_at: SavedObjectType<T>['updated_at'];

constructor(
private client: SavedObjectsClientContract,
{ id, type, version, attributes, error, references, migrationVersion }: SavedObjectType<T>
{
id,
type,
version,
attributes,
error,
references,
migrationVersion,
// eslint-disable-next-line @typescript-eslint/naming-convention
updated_at,
Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

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

Why does it need to be updated_at instead of updatedAt?

Copy link
Member

Choose a reason for hiding this comment

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

Thats the OpenSearch response. Don't think we can change that.

}: SavedObjectType<T>
) {
this.id = id;
this.type = type;
this.attributes = attributes || ({} as T);
this.references = references || [];
this._version = version;
this.migrationVersion = migrationVersion;
this.updated_at = updated_at;
if (error) {
this.error = error;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';

import { FormattedMessage, I18nProvider } from '@osd/i18n/react';
import { i18n } from '@osd/i18n';
Expand Down Expand Up @@ -161,6 +162,7 @@ export class DashboardListing extends React.Component {
}

getTableColumns() {
const dateFormat = this.props.core.uiSettings.get('dateFormat');
const tableColumns = [
{
field: 'title',
Expand All @@ -185,6 +187,19 @@ export class DashboardListing extends React.Component {
dataType: 'string',
sortable: true,
},
{
field: `updated_at`,
name: i18n.translate('dashboard.listing.table.columnUpdatedAtName', {
defaultMessage: 'Last updated',
}),
dataType: 'date',
sortable: true,
description: i18n.translate('dashboard.listing.table.columnUpdatedAtDescription', {
defaultMessage: 'Last update of the saved object',
}),
['data-test-subj']: 'updated-at',
render: (updatedAt) => updatedAt && moment(updatedAt).format(dateFormat),
},
];
return tableColumns;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
tmarkley marked this conversation as resolved.
Show resolved Hide resolved
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { SavedObjectsClientContract } from 'opensearch-dashboards/public';
import { SavedObjectLoader } from './saved_object_loader';

describe('SimpleSavedObjectLoader', () => {
const createLoader = (updatedAt?: any) => {
const id = 'logstash-*';
const type = 'index-pattern';

const savedObject = {
attributes: {},
id,
type,
updated_at: updatedAt as any,
};

client = {
...client,
find: jest.fn(() =>
Promise.resolve({
total: 1,
savedObjects: [savedObject],
})
),
} as any;

return new SavedObjectLoader(savedObject, client);
};

let client: SavedObjectsClientContract;
let loader: SavedObjectLoader;
beforeEach(() => {
client = {
update: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
} as any;
});

afterEach(async () => {
const savedObjects = await loader.findAll();

expect(savedObjects.hits[0].updated_at).toEqual(undefined);
});

it('set updated_at as undefined if undefined', async () => {
loader = createLoader(undefined);
});

it("set updated_at as undefined if doesn't exist", async () => {
loader = createLoader();
});

it('set updated_at as undefined if null', async () => {
loader = createLoader(null);
});
});
Loading