Rename aplication manager getStatus as getLegacyState

With the move to v3 target state and the move forward to remove
database ids from the supervisor, we want to ensure the ids are only
used for legacy support (such as within the API). This change renames
the method and sets it as deprecated
This commit is contained in:
Felipe Lalanne
2021-07-28 16:35:21 +00:00
parent 474fc4e890
commit ccae1f7cb8
8 changed files with 36 additions and 21 deletions

View File

@ -788,12 +788,17 @@ function reportOptionalContainers(serviceNames: string[]) {
); );
} }
// FIXME: This would be better to implement using the App class, and have each one /**
// generate its status. For now we use the original from application-manager.coffee. * This will be replaced by ApplicationManager.getState, at which
export async function getStatus() { * point the only place this will be used will be in the API endpoints
* once, the API moves to v3 or we update the endpoints to return uuids, we will
* be able to get rid of this
* @deprecated
*/
export async function getLegacyState() {
const [services, images] = await Promise.all([ const [services, images] = await Promise.all([
serviceManager.getStatus(), serviceManager.getState(),
imageManager.getStatus(), imageManager.getState(),
]); ]);
const apps: Dictionary<any> = {}; const apps: Dictionary<any> = {};
@ -822,7 +827,7 @@ export async function getStatus() {
} }
if (imageId == null) { if (imageId == null) {
throw new InternalInconsistencyError( throw new InternalInconsistencyError(
`imageId not defined in ApplicationManager.getStatus: ${service}`, `imageId not defined in ApplicationManager.getLegacyApplicationsState: ${service}`,
); );
} }
if (apps[appId].services[imageId] == null) { if (apps[appId].services[imageId] == null) {

View File

@ -404,7 +404,10 @@ export async function cleanImageData(): Promise<void> {
await db.models('image').del().whereIn('id', ids); await db.models('image').del().whereIn('id', ids);
} }
export const getStatus = async () => { /**
* Get the current state of all downloaded and downloading images on the device
*/
export const getState = async () => {
const images = (await getAvailable()).map((img) => ({ const images = (await getAvailable()).map((img) => ({
...img, ...img,
status: 'Downloaded' as Image['status'], status: 'Downloaded' as Image['status'],

View File

@ -103,14 +103,17 @@ export async function get(service: Service) {
return services[0]; return services[0];
} }
export async function getStatus() { /**
* Get the current state of all supervised services
*/
export async function getState() {
const services = await getAll(); const services = await getAll();
const status = _.clone(volatileState); const status = _.clone(volatileState);
for (const service of services) { for (const service of services) {
if (service.containerId == null) { if (service.containerId == null) {
throw new InternalInconsistencyError( throw new InternalInconsistencyError(
`containerId not defined in ServiceManager.getStatus: ${service}`, `containerId not defined in ServiceManager.getLegacyServicesState: ${service}`,
); );
} }
if (status[service.containerId] == null) { if (status[service.containerId] == null) {

View File

@ -191,8 +191,8 @@ export function createV2Api(router: Router) {
// It's kinda hacky to access the services and db via the application manager // It's kinda hacky to access the services and db via the application manager
// maybe refactor this code // maybe refactor this code
Bluebird.join( Bluebird.join(
serviceManager.getStatus(), serviceManager.getState(),
images.getStatus(), images.getState(),
db.models('app').select(['appId', 'commit', 'name']), db.models('app').select(['appId', 'commit', 'name']),
( (
services, services,
@ -284,7 +284,7 @@ export function createV2Api(router: Router) {
// Query device for all applications // Query device for all applications
let apps: any; let apps: any;
try { try {
apps = await applicationManager.getStatus(); apps = await applicationManager.getLegacyState();
} catch (e) { } catch (e) {
log.error(e.message); log.error(e.message);
return res.status(500).json({ return res.status(500).json({
@ -472,7 +472,7 @@ export function createV2Api(router: Router) {
let downloadProgressTotal = 0; let downloadProgressTotal = 0;
let downloads = 0; let downloads = 0;
const imagesStates = (await images.getStatus()) const imagesStates = (await images.getState())
.filter((img) => req.auth.isScoped({ apps: [img.appId] })) .filter((img) => req.auth.isScoped({ apps: [img.appId] }))
.map((img) => { .map((img) => {
appIds.push(img.appId); appIds.push(img.appId);

View File

@ -166,7 +166,7 @@ function createDeviceStateRouter() {
router.get('/v1/device', async (_req, res) => { router.get('/v1/device', async (_req, res) => {
try { try {
const state = await getStatus(); const state = await getCurrentForReport();
const stateToSend = _.pick(state.local, [ const stateToSend = _.pick(state.local, [
'api_port', 'api_port',
'ip_address', 'ip_address',
@ -528,8 +528,12 @@ export function getTarget({
}); });
} }
export async function getStatus(): Promise<DeviceStatus> { // This returns the current state of the device in (more or less)
const appsStatus = await applicationManager.getStatus(); // the same format as the target state. This method,
// getCurrent and getCurrentForComparison should probably get
// merged into a single method
export async function getCurrentForReport(): Promise<DeviceStatus> {
const appsStatus = await applicationManager.getLegacyState();
const theState: DeepPartial<DeviceStatus> = { const theState: DeepPartial<DeviceStatus> = {
local: {}, local: {},
dependent: {}, dependent: {},

View File

@ -197,7 +197,7 @@ function handleRetry(retryInfo: OnFailureInfo) {
async function generateStateForReport() { async function generateStateForReport() {
const { hardwareMetrics } = await config.getMany(['hardwareMetrics']); const { hardwareMetrics } = await config.getMany(['hardwareMetrics']);
const currentDeviceState = await deviceState.getStatus(); const currentDeviceState = await deviceState.getCurrentForReport();
// If hardwareMetrics is false, send null patch for system metrics to cloud API // If hardwareMetrics is false, send null patch for system metrics to cloud API
const info = { const info = {

View File

@ -49,7 +49,7 @@ describe('SupervisorAPI [V2 Endpoints]', () => {
await apiKeys.initialized; await apiKeys.initialized;
await apiKeys.generateCloudKey(); await apiKeys.generateCloudKey();
serviceManagerMock = stub(serviceManager, 'getAll').resolves([]); serviceManagerMock = stub(serviceManager, 'getAll').resolves([]);
imagesMock = stub(images, 'getStatus').resolves([]); imagesMock = stub(images, 'getState').resolves([]);
// We want to check the actual step that was triggered // We want to check the actual step that was triggered
applicationManagerSpy = spy(applicationManager, 'executeStep'); applicationManagerSpy = spy(applicationManager, 'executeStep');

View File

@ -188,7 +188,7 @@ function buildRoutes(): Router {
const originalNetGetAll = networkManager.getAllByAppId; const originalNetGetAll = networkManager.getAllByAppId;
const originalVolGetAll = volumeManager.getAllByAppId; const originalVolGetAll = volumeManager.getAllByAppId;
const originalSvcGetAppId = serviceManager.getAllByAppId; const originalSvcGetAppId = serviceManager.getAllByAppId;
const originalSvcGetStatus = serviceManager.getStatus; const originalSvcGetStatus = serviceManager.getState;
const originalReadyForUpdates = apiBinder.__get__('readyForUpdates'); const originalReadyForUpdates = apiBinder.__get__('readyForUpdates');
function setupStubs() { function setupStubs() {
@ -198,7 +198,7 @@ function setupStubs() {
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
volumeManager.getAllByAppId = async () => STUBBED_VALUES.volumes; volumeManager.getAllByAppId = async () => STUBBED_VALUES.volumes;
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
serviceManager.getStatus = async () => STUBBED_VALUES.services; serviceManager.getState = async () => STUBBED_VALUES.services;
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
serviceManager.getAllByAppId = async (appId) => serviceManager.getAllByAppId = async (appId) =>
_.filter(STUBBED_VALUES.services, (service) => service.appId === appId); _.filter(STUBBED_VALUES.services, (service) => service.appId === appId);
@ -211,7 +211,7 @@ function restoreStubs() {
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
volumeManager.getAllByAppId = originalVolGetAll; volumeManager.getAllByAppId = originalVolGetAll;
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
serviceManager.getStatus = originalSvcGetStatus; serviceManager.getState = originalSvcGetStatus;
// @ts-expect-error Assigning to a RO property // @ts-expect-error Assigning to a RO property
serviceManager.getAllByAppId = originalSvcGetAppId; serviceManager.getAllByAppId = originalSvcGetAppId;
} }