Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

Commit a8fdc66

Browse files
author
Leonardo Chaia
committed
fix: fixed docker data usage not being calculated correctly
1 parent b9c9a13 commit a8fdc66

File tree

3 files changed

+48
-56
lines changed

3 files changed

+48
-56
lines changed

src/app/daemon-tools/docker-system.service.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { DockerService } from './docker.service';
33
import { SystemInfo, SystemDfResponse } from 'dockerode';
4+
import { map } from 'rxjs/operators';
45

56
@Injectable()
67
export class DockerSystemService {
@@ -14,4 +15,39 @@ export class DockerSystemService {
1415
public df() {
1516
return this.daemon.docker(d => d.df() as Promise<SystemDfResponse>);
1617
}
18+
19+
public diskUsageResume() {
20+
return this.df()
21+
.pipe(map(dataUsage => new DockerDiskUsageResume(dataUsage)));
22+
}
23+
}
24+
25+
export interface DockerDiskUsageListResume {
26+
size: number;
27+
count: number;
28+
}
29+
30+
// https://github.com/docker/cli/blob/a900ba8aeff764a5fb5bb7e18eee25d75a220732/cli/command/formatter/disk_usage.go
31+
export class DockerDiskUsageResume {
32+
33+
public readonly images: DockerDiskUsageListResume;
34+
public readonly containers: DockerDiskUsageListResume;
35+
public readonly volumes: DockerDiskUsageListResume;
36+
37+
constructor(dataUsage: SystemDfResponse) {
38+
this.images = {
39+
size: dataUsage.LayersSize,
40+
count: dataUsage.Images.length
41+
};
42+
43+
this.containers = {
44+
size: dataUsage.Containers.map(i => i.SizeRw || 0).reduce((p, c) => p + c, 0),
45+
count: dataUsage.Containers.length,
46+
};
47+
48+
this.volumes = {
49+
size: dataUsage.Volumes.map(i => i.UsageData.Size >= 0 ? i.UsageData.Size : 0).reduce((p, c) => p + c, 0),
50+
count: dataUsage.Volumes.length,
51+
};
52+
}
1753
}

src/app/daemon-tools/system-container/system-container.component.html

+7-39
Original file line numberDiff line numberDiff line change
@@ -89,57 +89,25 @@ <h2 style="margin:0">
8989
<div fxLayout="row"
9090
fxLayoutGap="16px"
9191
*ngIf="dataUsage">
92-
<mat-card fxLayout="row"
93-
fxLayoutAlign="start center"
94-
fxLayoutGap="16px">
95-
<div>
96-
<h1 class="mat-display-1 tim-text-primary"
97-
style="margin:0; font-weight: bold;">
98-
{{dataUsage.Images.length}}
99-
</h1>
100-
</div>
101-
<div>
102-
<h2 style="margin:0">Images</h2>
103-
<h3 style="margin:0">
104-
{{imageSize | bytesToHuman}}
105-
</h3>
106-
</div>
107-
</mat-card>
10892

10993
<mat-card fxLayout="row"
110-
fxLayoutGap="16px">
94+
fxLayoutAlign="start center"
95+
fxLayoutGap="16px"
96+
*ngFor="let key of ['images', 'containers', 'volumes']">
11197
<div>
11298
<h1 class="mat-display-1 tim-text-primary"
11399
style="margin:0; font-weight: bold;">
114-
{{dataUsage.Containers.length}}
100+
{{dataUsage[key].count}}
115101
</h1>
116102
</div>
117-
<div fxFlexAlign="center">
118-
<h2 style="margin:0">Containers</h2>
119-
<h3 style="margin:0">
120-
{{containerSize | bytesToHuman}}
121-
</h3>
122-
</div>
123-
</mat-card>
124-
125-
126-
<mat-card fxLayout="row"
127-
fxLayoutGap="16px">
128103
<div>
129-
<h1 class="mat-display-1 tim-text-primary"
130-
style="margin:0; font-weight: bold;">
131-
{{dataUsage.Volumes.length}}
132-
</h1>
133-
</div>
134-
<div fxFlexAlign="center">
135-
<h2 style="margin:0">Volumes</h2>
104+
<h2 style="margin:0">{{key | titlecase}}</h2>
136105
<h3 style="margin:0">
137-
{{volumeSize | bytesToHuman}}
106+
{{dataUsage[key].size | bytesToHuman}}
138107
</h3>
139108
</div>
140109
</mat-card>
141110

142111
</div>
143112
</ng-container>
144-
</div>
145-
<pre *ngIf="dataUsage">{{dataUsage|json}}</pre>
113+
</div>

src/app/daemon-tools/system-container/system-container.component.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { SystemDfResponse, SystemInfo } from 'dockerode';
3-
import { DockerSystemService } from '../docker-system.service';
2+
import { SystemInfo } from 'dockerode';
3+
import { DockerSystemService, DockerDiskUsageResume } from '../docker-system.service';
44
import { map, take } from 'rxjs/operators';
55
import { forkJoin } from 'rxjs';
66

@@ -11,15 +11,11 @@ import { forkJoin } from 'rxjs';
1111
})
1212
export class SystemContainerComponent implements OnInit {
1313

14-
public dataUsage: SystemDfResponse;
14+
public dataUsage: DockerDiskUsageResume;
1515
public systemInfo: SystemInfo;
1616

1717
public loading: boolean;
1818

19-
public imageSize: number;
20-
public containerSize: number;
21-
public volumeSize: number;
22-
2319
constructor(private systemService: DockerSystemService) { }
2420

2521
public ngOnInit() {
@@ -30,17 +26,9 @@ export class SystemContainerComponent implements OnInit {
3026
map(info => this.systemInfo = info),
3127
take(1)
3228
),
33-
this.systemService.df()
29+
this.systemService.diskUsageResume()
3430
.pipe(
35-
map(dataUsage => {
36-
this.dataUsage = dataUsage;
37-
38-
this.imageSize = dataUsage.Images.map(i => i.Size).reduce((p, c) => p + c, 0);
39-
this.imageSize -= dataUsage.Images.map(i => i.SharedSize).reduce((p, c) => p + c, 0);
40-
this.containerSize = dataUsage.Containers.map(i => i.SizeRootFs).reduce((p, c) => p + c, 0);
41-
this.volumeSize = dataUsage.Volumes.map(i => i.UsageData.Size).reduce((p, c) => p + c, 0);
42-
return this.dataUsage;
43-
}),
31+
map(dataUsage => this.dataUsage = dataUsage),
4432
take(1)
4533
)
4634
])

0 commit comments

Comments
 (0)