Compare commits

...

1 Commits

Author SHA1 Message Date
Kerkesni 1bb8e86679
ARSN-299 add location pause status model 2023-02-14 11:33:56 +01:00
3 changed files with 275 additions and 0 deletions

View File

@ -0,0 +1,166 @@
type status = {
paused: boolean,
scheduledResume: string|null,
};
type serviceStatus = {
crr: status|null,
ingestion: status|null,
lifecycle: status|null,
};
/**
* Class to manage a location's pause/resume state on its
* different services.
* Current supported services are : crr, ingestion, lifecycle
*/
export default class LocationStatus {
_data: serviceStatus;
/**
* @constructor
* @param services services to init
* @param locationStatus initial location status values
*/
constructor(services: string[], locationStatus?: {
crr: serviceStatus|null,
ingestion: serviceStatus|null,
lifecycle: serviceStatus|null,
} | serviceStatus) {
this._data = this._initStatus(services);
if (locationStatus) {
const data = locationStatus instanceof LocationStatus ?
locationStatus._data : locationStatus;
Object.keys(this._data).forEach(svc => {
this._data[svc] = {
paused: data[svc]?.paused || false,
scheduledResume: data[svc]?.scheduledResume || null
}
});
}
}
/**
* Initializes the status of all services
* The default status of a service is unpaused
* @returns {LocationServiceStatus}
*/
_initStatus(servicesToInit: string[]): serviceStatus {
const initStatus = {
paused: false,
scheduledResume: null,
};
return {
crr: servicesToInit.includes('crr') ? initStatus : null,
ingestion: servicesToInit.includes('ingestion') ? initStatus : null,
lifecycle: servicesToInit.includes('lifecycle') ? initStatus : null,
};
}
/**
* initializes a service status
* @param service service name
*/
_initService(service: string) {
this._data[service] = {
paused: false,
scheduledResume: null,
};
}
/**
* @param service service name
* @param paused true if paused
*/
setServicePauseStatus(service: string, paused: boolean) {
if (Object.keys(this._data).includes(service)) {
if (!this._data[service]) {
this._initService(service);
}
this._data[service].paused = paused;
}
}
/**
* @param service service name
* @returns true if paused
*/
getServicePauseStatus(service: string) : boolean | null {
if (!this._data[service]) {
return null;
}
return this._data[service].paused;
}
/**
* @param service service name
*/
setServiceResumeSchedule(service: string, date: Date | null) {
if (this._data[service]) {
if (date !== null) {
this._data[service].scheduledResume = date.toString();
} else {
this._data[service].scheduledResume = null;
}
}
}
/**
* @param service service name
* @returns scheduled resume date
*/
getServiceResumeSchedule(service: string) : Date | null {
const schedule = this._data[service].scheduledResume;
if (!schedule) {
return null;
}
return new Date(schedule);
}
/**
* @param service service(s) name
*/
pauseLocation(service: string | string[]) {
const servicesList = Array.isArray(service) ?
service : [service];
servicesList.forEach(svc => {
if (!this.getServicePauseStatus(svc)) {
this.setServicePauseStatus(svc, true);
}
});
}
/**
* @param service service(s) name
* @param schedule date to resume service(s)
*/
resumeLocation(service: string | string[], schedule?: Date) {
const servicesList = Array.isArray(service) ?
service : [service];
servicesList.forEach(svc => {
if (!this.getServicePauseStatus(svc)) {
return;
}
let shouldPause = false;
if (schedule) {
shouldPause = true
}
this.setServicePauseStatus(svc, shouldPause);
this.setServiceResumeSchedule(svc, schedule||null);
});
}
/**
* @return location status object
*/
getValue() : serviceStatus{
return this._data;
}
/**
* @returns serialized location status data
*/
getSerialized() : string {
return JSON.stringify(this.getValue());
}
}

View File

@ -5,6 +5,7 @@ export { default as BucketInfo } from './BucketInfo';
export { default as BucketPolicy } from './BucketPolicy';
export { default as LifecycleConfiguration } from './LifecycleConfiguration';
export { default as LifecycleRule } from './LifecycleRule';
export { default as LocationStatus } from './LocationStatus';
export { default as NotificationConfiguration } from './NotificationConfiguration';
export { default as ObjectLockConfiguration } from './ObjectLockConfiguration';
export { default as ObjectMD } from './ObjectMD';

View File

@ -0,0 +1,108 @@
const assert = require('assert');
const LocationStatus = require('../../../lib/models/LocationStatus').default;
const locationStatusObj = {
crr: {
paused: true,
scheduledResume: null,
},
ingestion: {
paused: false,
scheduledResume: null,
},
lifecycle: {
paused: true,
scheduledResume: 'Fri Jan 27 2023 13:56:12 GMT+0100',
},
};
describe('LocationStatus', () => {
let locationStatus = null;
beforeEach(() => {
locationStatus = new LocationStatus(['crr', 'ingestion', 'lifecycle'], locationStatusObj);
});
it('should init location service status as unpaused', () => {
const ls = new LocationStatus(['crr', 'ingestion', 'lifecycle']);
Object.keys(ls._data).forEach(service => {
assert.strictEqual(ls._data[service].paused, false);
assert.strictEqual(ls._data[service].scheduledResume, null);
});
});
it('should copy data from LocationStatus', () => {
const lsToCopy = new LocationStatus(['crr', 'ingestion', 'lifecycle'], locationStatusObj);
const ls = new LocationStatus(['crr', 'ingestion', 'lifecycle'], lsToCopy);
Object.keys(locationStatusObj).forEach(service => {
assert.strictEqual(ls._data[service].paused, lsToCopy._data[service].paused);
assert.strictEqual(ls._data[service].scheduledResume, lsToCopy._data[service].scheduledResume);
});
});
it('should copy data from object', () => {
const ls = new LocationStatus(['crr', 'ingestion', 'lifecycle'], locationStatusObj);
Object.keys(locationStatusObj).forEach(service => {
assert.strictEqual(ls._data[service].paused, locationStatusObj[service].paused);
assert.strictEqual(ls._data[service].scheduledResume, locationStatusObj[service].scheduledResume);
});
});
it('should initialize non specified services', () => {
const tmpLocStatus = Object.assign({}, locationStatusObj);
delete tmpLocStatus.ingestion;
const ls = new LocationStatus(['crr', 'ingestion', 'lifecycle'], tmpLocStatus);
assert.strictEqual(ls._data.ingestion.paused, false);
assert.strictEqual(ls._data.ingestion.scheduledResume, null);
});
it('should set service status', () => {
locationStatus.setServicePauseStatus('ingestion', true);
assert.strictEqual(locationStatus._data.ingestion.paused, true);
});
it('should get service status', () => {
const isPaused = locationStatus.getServicePauseStatus('lifecycle');
assert.strictEqual(isPaused, locationStatusObj.lifecycle.paused);
});
it('should set service resume schedule', () => {
const date = new Date();
locationStatus.setServiceResumeSchedule('crr', date);
assert.strictEqual(locationStatus._data.crr.scheduledResume, date.toString());
});
it('should get service resume schedule', () => {
const schedule = locationStatus.getServiceResumeSchedule('lifecycle');
assert.deepEqual(schedule, new Date(locationStatusObj.lifecycle.scheduledResume));
});
it('should get null service resume schedule', () => {
const schedule = locationStatus.getServiceResumeSchedule('crr');
assert.strictEqual(schedule, null);
});
it('should pause services', () => {
locationStatus.pauseLocation('ingestion');
assert.strictEqual(locationStatus._data.ingestion.paused, true);
});
it('should resume services', () => {
locationStatus.resumeLocation('lifecycle');
assert.strictEqual(locationStatus._data.lifecycle.paused, false);
assert.strictEqual(locationStatus._data.lifecycle.scheduledResume, null);
});
it('should set resume shedule', () => {
const date = new Date();
locationStatus.resumeLocation('crr', date);
assert.strictEqual(locationStatus._data.crr.paused, true);
assert.strictEqual(locationStatus._data.crr.scheduledResume, date.toString());
});
it('should return serialized location status data', () => {
const data = locationStatus.getValue();
assert.deepEqual(data, locationStatusObj);
});
});