Skip to content

Commit 39603de

Browse files
authored
Improve disk usage table (#1090)
Can provide freeTarget instead of usageTarget as an option.
1 parent 197b22d commit 39603de

File tree

1 file changed

+204
-90
lines changed

1 file changed

+204
-90
lines changed

common-lib/common/panels/disk/table/usage.libsonnet

Lines changed: 204 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ local fieldOverride = g.panel.table.fieldOverride;
55
local custom = table.fieldConfig.defaults.custom;
66
local defaults = table.fieldConfig.defaults;
77
local options = table.options;
8+
89
base {
910
new(
1011
title='Disk space usage',
1112
totalTarget,
12-
usageTarget,
13+
usageTarget=null,
14+
freeTarget=null,
1315
groupLabel,
1416
description=|||
1517
This table provides information about total disk space, used space, available space, and usage percentages for each mounted file system on the system.
@@ -19,22 +21,210 @@ base {
1921
std.prune(
2022
{
2123
checks: [
24+
if (usageTarget == null && freeTarget == null) then error 'Must provide at leason one of "usageTarget" or "freeTraget"',
2225
if !(std.objectHas(totalTarget, 'format') && std.assertEqual(totalTarget.format, 'table')) then error 'totalTarget format must be "table"',
23-
if !(std.objectHas(totalTarget, 'instant') && std.assertEqual(totalTarget.instant, true)) then error 'totalTarget must be type instant',
24-
if !(std.objectHas(usageTarget, 'format') && std.assertEqual(usageTarget.format, 'table')) then error 'usageTarget format must be "table"',
25-
if !(std.objectHas(usageTarget, 'instant') && std.assertEqual(usageTarget.instant, true)) then error 'usageTarget must be type instant',
26+
if !(std.objectHas(totalTarget, 'instant') && std.assertEqual(totalTarget.instant, true)) then error 'totalTarget must have type "instant"',
27+
if usageTarget != null && !(std.objectHas(usageTarget, 'format') && std.assertEqual(usageTarget.format, 'table')) then error 'usageTarget format must be "table"',
28+
if usageTarget != null && !(std.objectHas(usageTarget, 'instant') && std.assertEqual(usageTarget.instant, true)) then error 'usageTarget must have type "instant"',
29+
if freeTarget != null && !(std.objectHas(freeTarget, 'format') && std.assertEqual(freeTarget.format, 'table')) then error 'freeTarget format must be "table"',
30+
if freeTarget != null && !(std.objectHas(freeTarget, 'instant') && std.assertEqual(freeTarget.instant, true)) then error 'freeTarget must have type "instant"',
2631
],
2732
}
2833
) +
29-
super.new(
30-
title=title,
31-
targets=[
32-
totalTarget { refId: 'TOTAL' },
33-
usageTarget { refId: 'USAGE' },
34-
],
35-
description=description,
36-
)
37-
+ table.standardOptions.thresholds.withSteps(
34+
if usageTarget != null
35+
then
36+
(
37+
super.new(
38+
title=title,
39+
targets=[
40+
totalTarget { refId: 'TOTAL' },
41+
usageTarget { refId: 'USAGE' },
42+
],
43+
description=description,
44+
)
45+
+ $.withUsageTableCommonMixin()
46+
+ table.queryOptions.withTransformationsMixin(
47+
[
48+
{
49+
id: 'groupBy',
50+
options: {
51+
fields: {
52+
'Value #TOTAL': {
53+
aggregations: [
54+
'lastNotNull',
55+
],
56+
operation: 'aggregate',
57+
},
58+
'Value #USAGE': {
59+
aggregations: [
60+
'lastNotNull',
61+
],
62+
operation: 'aggregate',
63+
},
64+
[groupLabel]: {
65+
aggregations: [],
66+
operation: 'groupby',
67+
},
68+
},
69+
},
70+
},
71+
{
72+
id: 'merge',
73+
options: {},
74+
},
75+
{
76+
id: 'calculateField',
77+
options: {
78+
alias: 'Available',
79+
binary: {
80+
left: 'Value #TOTAL (lastNotNull)',
81+
operator: '-',
82+
reducer: 'sum',
83+
right: 'Value #USAGE (lastNotNull)',
84+
},
85+
mode: 'binary',
86+
reduce: {
87+
reducer: 'sum',
88+
},
89+
},
90+
},
91+
{
92+
id: 'calculateField',
93+
options: {
94+
alias: 'Used, %',
95+
binary: {
96+
left: 'Value #USAGE (lastNotNull)',
97+
operator: '/',
98+
reducer: 'sum',
99+
right: 'Value #TOTAL (lastNotNull)',
100+
},
101+
mode: 'binary',
102+
reduce: {
103+
reducer: 'sum',
104+
},
105+
},
106+
},
107+
{
108+
id: 'organize',
109+
options: {
110+
excludeByName: {},
111+
indexByName: {
112+
[groupLabel]: 0,
113+
'Value #TOTAL (lastNotNull)': 1,
114+
Available: 2,
115+
'Value #USAGE (lastNotNull)': 3,
116+
'Used, %': 4,
117+
},
118+
renameByName: {
119+
'Value #TOTAL (lastNotNull)': 'Size',
120+
'Value #USAGE (lastNotNull)': 'Used',
121+
[groupLabel]: 'Mounted on',
122+
},
123+
},
124+
},
125+
self.transformations.sortBy('Mounted on'),
126+
]
127+
)
128+
)
129+
else if freeTarget != null && usageTarget == null
130+
then
131+
(
132+
super.new(
133+
title=title,
134+
targets=[
135+
totalTarget { refId: 'TOTAL' },
136+
freeTarget { refId: 'FREE' },
137+
],
138+
description=description,
139+
)
140+
+ $.withUsageTableCommonMixin()
141+
+ table.queryOptions.withTransformationsMixin(
142+
[
143+
{
144+
id: 'groupBy',
145+
options: {
146+
fields: {
147+
'Value #TOTAL': {
148+
aggregations: [
149+
'lastNotNull',
150+
],
151+
operation: 'aggregate',
152+
},
153+
'Value #FREE': {
154+
aggregations: [
155+
'lastNotNull',
156+
],
157+
operation: 'aggregate',
158+
},
159+
[groupLabel]: {
160+
aggregations: [],
161+
operation: 'groupby',
162+
},
163+
},
164+
},
165+
},
166+
{
167+
id: 'merge',
168+
options: {},
169+
},
170+
{
171+
id: 'calculateField',
172+
options: {
173+
alias: 'Used',
174+
binary: {
175+
left: 'Value #TOTAL (lastNotNull)',
176+
operator: '-',
177+
reducer: 'sum',
178+
right: 'Value #FREE (lastNotNull)',
179+
},
180+
mode: 'binary',
181+
reduce: {
182+
reducer: 'sum',
183+
},
184+
},
185+
},
186+
{
187+
id: 'calculateField',
188+
options: {
189+
alias: 'Used, %',
190+
binary: {
191+
left: 'Used',
192+
operator: '/',
193+
reducer: 'sum',
194+
right: 'Value #TOTAL (lastNotNull)',
195+
},
196+
mode: 'binary',
197+
reduce: {
198+
reducer: 'sum',
199+
},
200+
},
201+
},
202+
{
203+
id: 'organize',
204+
options: {
205+
excludeByName: {},
206+
indexByName: {
207+
[groupLabel]: 0,
208+
'Value #TOTAL (lastNotNull)': 1,
209+
'Value #FREE (lastNotNull)': 2,
210+
Used: 3,
211+
'Used, %': 4,
212+
},
213+
renameByName: {
214+
'Value #TOTAL (lastNotNull)': 'Size',
215+
'Value #FREE (lastNotNull)': 'Available',
216+
[groupLabel]: 'Mounted on',
217+
},
218+
},
219+
},
220+
self.transformations.sortBy('Mounted on'),
221+
]
222+
)
223+
)
224+
else {},
225+
226+
withUsageTableCommonMixin():
227+
table.standardOptions.thresholds.withSteps(
38228
[
39229
table.thresholdStep.withColor('light-blue')
40230
+ table.thresholdStep.withValue(null),
@@ -62,81 +252,5 @@ base {
62252
+ table.standardOptions.withUnit('percentunit')
63253
),
64254
])
65-
+ table.standardOptions.withUnit('bytes')
66-
+ table.queryOptions.withTransformationsMixin(
67-
[
68-
{
69-
id: 'groupBy',
70-
options: {
71-
fields: {
72-
'Value #TOTAL': {
73-
aggregations: [
74-
'lastNotNull',
75-
],
76-
operation: 'aggregate',
77-
},
78-
'Value #USAGE': {
79-
aggregations: [
80-
'lastNotNull',
81-
],
82-
operation: 'aggregate',
83-
},
84-
[groupLabel]: {
85-
aggregations: [],
86-
operation: 'groupby',
87-
},
88-
},
89-
},
90-
},
91-
{
92-
id: 'merge',
93-
options: {},
94-
},
95-
{
96-
id: 'calculateField',
97-
options: {
98-
alias: 'Free',
99-
binary: {
100-
left: 'Value #TOTAL (lastNotNull)',
101-
operator: '-',
102-
reducer: 'sum',
103-
right: 'Value #USAGE (lastNotNull)',
104-
},
105-
mode: 'binary',
106-
reduce: {
107-
reducer: 'sum',
108-
},
109-
},
110-
},
111-
{
112-
id: 'calculateField',
113-
options: {
114-
alias: 'Used, %',
115-
binary: {
116-
left: 'Value #USAGE (lastNotNull)',
117-
operator: '/',
118-
reducer: 'sum',
119-
right: 'Value #TOTAL (lastNotNull)',
120-
},
121-
mode: 'binary',
122-
reduce: {
123-
reducer: 'sum',
124-
},
125-
},
126-
},
127-
{
128-
id: 'organize',
129-
options: {
130-
excludeByName: {},
131-
indexByName: {},
132-
renameByName: {
133-
'Value #TOTAL (lastNotNull)': 'Size',
134-
'Value #USAGE (lastNotNull)': 'Available',
135-
[groupLabel]: 'Mounted on',
136-
},
137-
},
138-
},
139-
self.transformations.sortBy('Mounted on'),
140-
]
141-
),
255+
+ table.standardOptions.withUnit('bytes'),
142256
}

0 commit comments

Comments
 (0)