From 6c6b3549da967178096218b9c2ead8406e206c39 Mon Sep 17 00:00:00 2001 From: smessie Date: Mon, 30 Sep 2024 13:40:37 +0200 Subject: [PATCH] feat: Add service `stats` support --- docs/api.md | 1 + src/index.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 0656bf3f..297492e2 100644 --- a/docs/api.md +++ b/docs/api.md @@ -43,6 +43,7 @@ This page demonstrates the usage of `docker-compose` for Node.js. * `upMany(services, options)` - Builds, (re)creates, starts, and attaches to containers for the services specified in `services` - always uses the `-d` flag due to non interactive mode * `upOne(service, options)` - Builds, (re)creates, starts, and attaches to containers for a service specified in `service` - always uses the `-d` flag due to non interactive mode * `version(options)` - Show `docker-compose` version strings +* `stats(service)` - Show service container stats All commands return a `Promise({object})` with stdout and stderr strings and an exit code: diff --git a/src/index.ts b/src/index.ts index 9058629c..b340d8e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,18 @@ export type DockerComposeVersionResult = { version: string } +export type DockerComposeStatsResult = { + BlockIO: string + CPUPerc: string + Container: string + ID: string + MemPerc: string + MemUsage: string + Name: string + NetIO: string + PIDs: string +} + export type DockerComposeConfigResult = { config: { version: Record @@ -694,6 +706,21 @@ export const version = async function ( } } +export const stats = async function ( + service: string +): Promise { + const args = ['--no-stream', '--format', '"{{ json . }}"', service] + + try { + const result = await execCompose('stats', args) + // Remove first and last quote from output, as well as newline. + const output = result.out.replace('\n', '').trim().slice(1, -1) + return JSON.parse(output) + } catch (error) { + return Promise.reject(error) + } +} + export default { upAll, upMany, @@ -727,5 +754,6 @@ export default { restartOne, logs, port, - version + version, + stats }