Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service stats support #285

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -256,8 +268,8 @@
* Executes docker compose command with common options
*/
export const execCompose = (
command,

Check warning on line 271 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'command' should be typed
args,

Check warning on line 272 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'args' should be typed
options: IDockerComposeOptions = {}
): Promise<IDockerComposeResult> =>
new Promise((resolve, reject): void => {
Expand Down Expand Up @@ -694,6 +706,21 @@
}
}

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 @@
restartOne,
logs,
port,
version
version,
stats
}
Loading