Merge pull request #828 from balena-io/add-status-endpoint

Add application status endpoint
This commit is contained in:
CameronDiver 2019-02-05 18:21:46 +00:00 committed by GitHub
commit b606332312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import ServiceManager from './compose/service-manager';
import DB from './db'; import DB from './db';
import { Service } from './compose/service'; import { Service } from './compose/service';
import Config from './config';
declare interface Options { declare interface Options {
force?: boolean; force?: boolean;
@ -38,6 +39,7 @@ export class ApplicationManager extends EventEmitter {
public eventTracker: EventTracker; public eventTracker: EventTracker;
public services: ServiceManager; public services: ServiceManager;
public config: Config;
public db: DB; public db: DB;
public images: Images; public images: Images;

View File

@ -667,3 +667,5 @@ export class ServiceManager extends (EventEmitter as {
}); });
} }
} }
export default ServiceManager;

View File

@ -206,7 +206,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
if (svc == null) { if (svc == null) {
status = img.status; status = img.status;
} else { } else {
status = svc.status; status = svc.status || img.status;
} }
response[appName].services[img.serviceName] = { response[appName].services[img.serviceName] = {
status, status,
@ -382,4 +382,58 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
}); });
} }
}); });
router.get('/v2/state/status', async (_req, res) => {
const localMode = await applications.config.get('localMode');
const currentRelease = await applications.config.get('currentCommit');
const pending = applications.deviceState.applyInProgress;
const containerStates = (await applications.services.getAll()).map(svc =>
_.pick(
svc,
'status',
'serviceName',
'appId',
'imageId',
'serviceId',
'containerId',
'createdAt',
),
);
let downloadProgressTotal = 0;
let downloads = 0;
const imagesStates = (await applications.images.getStatus(localMode)).map(
img => {
if (img.downloadProgress != null) {
downloadProgressTotal += img.downloadProgress;
downloads += 1;
}
return _.pick(
img,
'name',
'appId',
'serviceName',
'imageId',
'dockerImageId',
'status',
'downloadProgress',
);
},
);
let overallDownloadProgress = null;
if (downloads > 0) {
overallDownloadProgress = downloadProgressTotal / downloads;
}
return res.status(200).send({
status: 'success',
appState: pending ? 'applying' : 'applied',
overallDownloadProgress,
containers: containerStates,
images: imagesStates,
release: currentRelease,
});
});
} }