Skip to content

Commit

Permalink
Add updated_at column to objects' tables (#1218) (#2392)
Browse files Browse the repository at this point in the history
* Add updated_at columnb to objects' tables

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Grammer and iso usage

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Set updated at field from advanced settings

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Use dateFormat instead of  dateFormat:scaled

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* snapshot

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Add updated_at to additional comment

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Add unit-tests for updated_at as null undefined or unknown

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Simplify test file

Signed-off-by: sitbubu <royi.sitbon@logz.io>

* Simplified header and import from src

Signed-off-by: sitbubu <royi.sitbon@logz.io>

Signed-off-by: sitbubu <royi.sitbon@logz.io>
(cherry picked from commit 8874afd)

Co-authored-by: Royi Sitbon <royi.sitbon@logz.io>
  • Loading branch information
opensearch-trigger-bot[bot] and RoyiSitbon authored Sep 22, 2022
1 parent 3aacdc3 commit 9f8dd0c
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 14 deletions.
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,
}: 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 @@
/*
* 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

0 comments on commit 9f8dd0c

Please sign in to comment.