Skip to content

Commit

Permalink
feat: Add service stats support
Browse files Browse the repository at this point in the history
  • Loading branch information
smessie authored and AlexZeitler committed Sep 30, 2024
1 parent 1eb97f5 commit 7ec994c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
30 changes: 29 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
Expand Down Expand Up @@ -694,6 +706,21 @@ export const version = async function (
}
}

export const stats = async function (
service: string
): Promise<DockerComposeStatsResult> {
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,
Expand Down Expand Up @@ -727,5 +754,6 @@ export default {
restartOne,
logs,
port,
version
version,
stats
}

0 comments on commit 7ec994c

Please sign in to comment.