Skip to content

Commit

Permalink
resolving conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Bharathwaj G <bharath78910@gmail.com>
  • Loading branch information
bharath-techie committed May 19, 2023
1 parent 8e3ba7a commit 71a90dd
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
PointInTime,
createSavedObject,
getSavedPits,
deletePointInTimeById,
updatePointInTimeSavedObject,
} from '../utils';
import { EmptyState, NoDataSourceState } from './empty_state';
// import { PageHeader } from './page_header';
Expand All @@ -58,6 +60,7 @@ export interface DashboardPitItem {
name: string;
creation_time: number;
keep_alive: number;
delete_on_expiry: boolean;
}

export interface PitItem {
Expand Down Expand Up @@ -129,6 +132,11 @@ const PITTable = ({ history }: RouteComponentProps) => {
});
};

const navigateEdit = (pit) => {
console.log(pit);
history.push(`${pit.id}`);
};

const getPits = (dataSourceId?: string) => {
// setMessage(<>Loading PITs...</>);
setLoading(true);
Expand Down Expand Up @@ -168,46 +176,85 @@ const PITTable = ({ history }: RouteComponentProps) => {
// if (fetchedDataSources?.length) {
// setDashboardPits(fetchedDataSources);
// }

console.log('dashboard pits', fetchedDashboardPits);
setLoading(false);
if (fetchedPits?.resp?.pits) {
let expiredPits: DashboardPitItem[] = [];
const expiredPits: DashboardPitItem[] = [];
// if (dataSourceId === undefined) {
// expiredPits = fetchedDashboardPits.filter(
// (x) => !fetchedPits?.resp?.pits.some((x2) => x.attributes.id === x2.pit_id)
// );
// }
// console.log('expired', expiredPits);
// expiredPits.filter(x=>x.attributes.delete_on_expiry).forEach(x=> {
// console.log('deleting ', x)
// deletePointInTimeById(savedObjects.client, x.id);
// })

if (dataSourceId === undefined) {
expiredPits = fetchedDashboardPits.filter(
(x) => !fetchedPits?.resp?.pits.some((x2) => x.attributes.id === x2.pit_id)
);
fetchedDashboardPits.forEach((x) => {
if (!fetchedPits?.resp?.pits.some((x2) => x.attributes.pit_id === x2.pit_id)) {
if (x.attributes.delete_on_expiry) {
console.log('deleting ', x);
deletePointInTimeById(savedObjects.client, x.id);
} else {
expiredPits.push(x);
}
}
});
}
console.log('expired', expiredPits);

setPits(
fetchedPits?.resp?.pits
.map((val) => {
const date = moment(val.creation_time);
let formattedDate = date.format('MMM D @ HH:mm:ss');
const formattedDate = date.format('MMM D @ HH:mm:ss');
const expiry = val.creation_time + val.keep_alive;
const dashboardPit = fetchedDashboardPits.filter(
(x) => x.attributes.id === val.pit_id
(x) => x.attributes.pit_id === val.pit_id
);
console.log(dashboardPit);
if (dashboardPit.length > 0) {
formattedDate = dashboardPit[0].attributes.name;
console.log(dashboardPit);
dashboardPit[0].attributes.keepAlive = val.keep_alive;
console.log('updating', dashboardPit);
updatePointInTimeSavedObject(
savedObjects.client,
dashboardPit[0].id,
dashboardPit[0].attributes,
dashboardPit[0].references
);
return {
pit_id: val.pit_id,
id: dashboardPit[0].id,
name: dashboardPit[0].attributes.name,
creation_time: val.creation_time,
keep_alive: val.keep_alive,
dataSource: dataSourceName,
isSavedObject: true,
expiry,
};
}

return {
pit_id: val.pit_id,
id: val.id,
name: formattedDate,
creation_time: val.creation_time,
keep_alive: val.keep_alive,
dataSource: dataSourceName,
isSavedObject: false,
expiry,
};
})
.concat(
expiredPits.map((x) => ({
pit_id: x.attributes.id,
pit_id: x.attributes.pit_id,
name: x.attributes.name,
id: x.id,
creation_time: x.attributes.creation_time,
keep_alive: x.attributes.keepAlive,
dataSource: dataSourceName,
isSavedObject: true,
expiry: x.attributes.creation_time + x.attributes.keepAlive,
}))
)
Expand Down Expand Up @@ -238,17 +285,18 @@ const PITTable = ({ history }: RouteComponentProps) => {
const createPointInTime = () => {
// setIsFlyoutVisible(false);
const pit: PointInTime = {
id:
'o463QQEKbXktaW5kZXgtMRZtN2RWMHdaRlNILThIMUVWWDJJMVBRABZxMlNNZVdPZVRGbVR6MUxPc1RZYkx3AAAAAAAAAAAjFmhZdDNoTk9hUlBlVng2RVNIMUNhelEBFm03ZFYwd1pGU0gtOEgxRVZYMkkxUFEAAA==',
keepAlive: 600000,
creation_time: 1681386155468,
name: 'PIT-my-index-2', // Todo create pit and fill the pit id
pit_id:
'o463QQEKbXktaW5kZXgtMRZqUlZFU2lSaFE1eUx0cHdiSjNLWjRRABZtWjNCdk1ZdFRZbS1JbnltaVlBTWdBAAAAAAAAAACGFlJXLVVNYXVQVFctQVIxVmh1OUJuSlEBFmpSVkVTaVJoUTV5THRwd2JKM0taNFEAAA==',
keepAlive: 30000000,
creation_time: 1684418768188,
name: 'PIT-my-index-1223', // Todo create pit and fill the pit id
delete_on_expiry: false,
};

const reference: SavedObjectReference = {
id: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
id: '5586e600-f57b-11ed-90e3-a75eeb2a18a5',
type: 'index-pattern',
name: 'opensearch_dashboards_sample_data_ecommerce',
name: 'my*',
};
createSavedObject(pit, savedObjects.client, reference);
};
Expand Down Expand Up @@ -366,7 +414,7 @@ const PITTable = ({ history }: RouteComponentProps) => {
description: 'Configure PIT',
icon: 'pencil',
type: 'icon',
onClick: fetchDataSources,
onClick: navigateEdit, // route it to edit page --> create a route page
},
{
name: 'Delete',
Expand Down Expand Up @@ -664,4 +712,4 @@ const PITTable = ({ history }: RouteComponentProps) => {
);
};

export const PITTableWithRouter = withRouter(PITTable);
export const PITTableWithRouter = withRouter(PITTable);
74 changes: 51 additions & 23 deletions src/plugins/point_in_time_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpStart, SavedObjectsClientContract } from 'src/core/public';
//import { DuplicateIndexPatternError, IndexPattern } from 'src/plugins/data/common';


import { HttpStart, SavedObjectReference, SavedObjectsClientContract } from 'src/core/public';
import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources';
import { IndexPatternCreationConfig } from 'src/plugins/index_pattern_management/public';
import { responseToItemArray } from 'src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices';
import { PointInTimeAttributes } from '../types';
import { ResolveIndexResponse } from 'src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/types';

export async function getDataSources(savedObjectsClient: SavedObjectsClientContract) {
Expand Down Expand Up @@ -64,16 +64,17 @@ export async function getDataSources(savedObjectsClient: SavedObjectsClientContr
export interface PointInTime {
name: string;
keepAlive: number;
id: string;
pit_id: string;
creation_time: number;
id?: string;
delete_on_expiry: boolean;
}

export interface SavedObjectReference {
name?: string;
id: string;
type: string;
}

// export interface SavedObjectReference {
// name?: string;
// id: string;
// type: string;
// }

export async function getIndexPatterns(savedObjectsClient: SavedObjectsClientContract) {
return (
savedObjectsClient
Expand Down Expand Up @@ -114,7 +115,7 @@ export async function getSavedPits(client: SavedObjectsClientContract) {
const savedObjects = await client.find({
type: 'point-in-time',
perPage: 1000,
fields: ['id', 'creation_time', 'keepAlive', 'name'],
fields: ['id', 'creation_time', 'keepAlive', 'name', 'pit_id', 'delete_on_expiry'],
});

return savedObjects.savedObjects;
Expand All @@ -126,26 +127,52 @@ export async function findById(client: SavedObjectsClientContract, id: string) {
const savedObjects = await client.find({
type: 'point-in-time',
perPage: 1000,
// search: `${id}`,
// searchFields: ['id'],
fields: ['id'],
fields: [],
});
console.log(savedObjects.savedObjects);
return savedObjects.savedObjects.find(
(obj) => obj.attributes.id.toLowerCase() === id.toLowerCase()
);
return savedObjects.savedObjects.find((obj) => obj.id === id);
}
}

export async function updatePointInTimeById(
savedObjectsClient: SavedObjectsClientContract,
id: string,
attributes: PointInTimeAttributes
) {
return savedObjectsClient.update('point-in-time', id, attributes);
}

export async function updatePointInTimeSavedObject(
savedObjectsClient: SavedObjectsClientContract,
id: string,
attributes: PointInTimeAttributes,
reference: SavedObjectReference[]
) {
return savedObjectsClient.update('point-in-time', id, attributes, { references: reference });
}

export async function deletePointInTimeById(
savedObjectsClient: SavedObjectsClientContract,
id: string
) {
return savedObjectsClient.delete('point-in-time', id);
}

export async function updatePointInTimeKeepAlive(
savedObjectsClient: SavedObjectsClientContract,
id: string,
addTime: number
) {}

export async function createSavedObject(
pointintime: PointInTime,
client: SavedObjectsClientContract,
reference: SavedObjectReference
) {
const dupe = await findById(client, pointintime.id);
const dupe = await findById(client, pointintime.pit_id);
console.log('This is dupe output');
console.log(dupe);
if (dupe) {
throw new Error(`Duplicate Point in time: ${pointintime.id}`);
throw new Error(`Duplicate Point in time: ${pointintime.pit_id}`);
}
// if (dupe) {
// if (override) {
Expand All @@ -159,11 +186,12 @@ export async function createSavedObject(
const references = [{ ...reference }];
const savedObjectType = 'point-in-time';
const response = await client.create(savedObjectType, body, {
id: pointintime.id,
references,
});
console.log('This is the response');
console.log(response);
pointintime.id = response.id;
console.log(pointintime);
return pointintime;
}
export async function getIndicesViaResolve (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { StartServicesAccessor } from '../../../../core/public';
import { PointInTimeManagementContext } from '../types';
import { OpenSearchDashboardsContextProvider } from '../../../opensearch_dashboards_react/public';
import { PITTableWithRouter } from '../components';
import { PointInTimeCreateForm } from '../components/create_pit';
import { PITEditWithRouter } from '../components/pit_edit';
import { CreatePitWithRouter } from '../components/create_pit/create_pit';

export async function mountManagementSection(
Expand All @@ -38,6 +38,10 @@ export async function mountManagementSection(
<Route path={['/create']}>
<CreatePitWithRouter/>
</Route>
<Route path={['/:id']}>
<PITEditWithRouter />
</Route>

{/* <Route path={['/:id']}>
<PointInTimeEditForm />
</Route> */}
Expand Down
19 changes: 19 additions & 0 deletions src/plugins/point_in_time_management/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Services {
getAllPits: (dataSourceId?: string) => Promise<undefined | HttpFetchError>;
deletePits: (pits: string[], dataSourceId?: string) => any;
createPit: (index: string, keepAlive: string, allowPartialCreation: boolean, dataSourceId?: string) => any;
addPitTime: (pit_id: string, keepAlive: string, dataSourceId?: string) => any;
}

export function getServices(http: CoreStart['http']): Services {
Expand All @@ -20,11 +21,29 @@ export function getServices(http: CoreStart['http']): Services {
dataSourceId: dataSourceId ? dataSourceId : '',
}),
});
console.log('These are backend PITs');
console.log(response);
return response;
} catch (e) {
return e;
}
},
addPitTime: async (pit_id: string, keepAlive: string, dataSourceId?: string) => {
try {
console.log(pit_id, keepAlive, dataSourceId);
const response = await http.post('/api/pit/addTime', {
body: JSON.stringify({
dataSourceId: dataSourceId ? dataSourceId : 'default',
pit_id,
keepAlive,
}),
});
console.log('Updated the PIT KeepAlive');
console.log(response);
} catch (e) {
return e;
}
},

deletePits: async (pits: string[], dataSourceId?: string) => {
try {
Expand Down
40 changes: 40 additions & 0 deletions src/plugins/point_in_time_management/server/routes/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ export function registerPitRoutes(router: IRouter) {
}
);

router.post(
{
path: '/api/pit/addTime',
validate: {
body: schema.object({
dataSourceId: schema.string(),
pit_id: schema.string(),
keepAlive: schema.string(),
}),
},
},
async (context, req, res) => {
const client: OpenSearchClient = await getClient(req, context);
console.log(req.body);
try {
const response = await client.search({
body: {
size: 0,
pit: {
id: req.body.pit_id,
keep_alive: req.body.keepAlive,
},
},
});
return res.ok({
body: {
ok: true,
resp: response.body,
},
});
} catch (err: any) {
return res.ok({
body: {
ok: false,
resp: err.message,
},
});
}
}
);
router.post(
{
path: '/api/pit/delete',
Expand Down
Loading

0 comments on commit 71a90dd

Please sign in to comment.