Skip to content

Commit 7ec994c

Browse files
smessieAlexZeitler
authored andcommitted
feat: Add service stats support
1 parent 1eb97f5 commit 7ec994c

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This page demonstrates the usage of `docker-compose` for Node.js.
4343
* `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
4444
* `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
4545
* `version(options)` - Show `docker-compose` version strings
46+
* `stats(service)` - Show service container stats
4647

4748
All commands return a `Promise({object})` with stdout and stderr strings and an exit code:
4849

src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ export type DockerComposeVersionResult = {
2828
version: string
2929
}
3030

31+
export type DockerComposeStatsResult = {
32+
BlockIO: string
33+
CPUPerc: string
34+
Container: string
35+
ID: string
36+
MemPerc: string
37+
MemUsage: string
38+
Name: string
39+
NetIO: string
40+
PIDs: string
41+
}
42+
3143
export type DockerComposeConfigResult = {
3244
config: {
3345
version: Record<string, string>
@@ -694,6 +706,21 @@ export const version = async function (
694706
}
695707
}
696708

709+
export const stats = async function (
710+
service: string
711+
): Promise<DockerComposeStatsResult> {
712+
const args = ['--no-stream', '--format', '"{{ json . }}"', service]
713+
714+
try {
715+
const result = await execCompose('stats', args)
716+
// Remove first and last quote from output, as well as newline.
717+
const output = result.out.replace('\n', '').trim().slice(1, -1)
718+
return JSON.parse(output)
719+
} catch (error) {
720+
return Promise.reject(error)
721+
}
722+
}
723+
697724
export default {
698725
upAll,
699726
upMany,
@@ -727,5 +754,6 @@ export default {
727754
restartOne,
728755
logs,
729756
port,
730-
version
757+
version,
758+
stats
731759
}

0 commit comments

Comments
 (0)