Skip to content

Commit 1118ecc

Browse files
authored
A bundle of health chart improvements (#2389)
Add min and max value calculations for health charts Chart.js failed to handle some big (scientific notation) numbers. This also adds 1 to the max and subtracts 1 on the min for values that we don't know the range of. This makes values that do not change much look more reasonable.
1 parent 63c9e86 commit 1118ecc

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

assets/ui-rework/hooks/chart.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default {
1212
let type = JSON.parse(this.el.dataset.type)
1313
let max = JSON.parse(this.el.dataset.max)
1414
let min = JSON.parse(this.el.dataset.min)
15+
let maxTime = JSON.parse(this.el.dataset.maxtime)
16+
let minTime = JSON.parse(this.el.dataset.mintime)
1517
let title = JSON.parse(this.el.dataset.title)
1618

1719
const ctx = this.el
@@ -73,16 +75,18 @@ export default {
7375
ticks: {
7476
display: true,
7577
autoSkip: false
76-
}
78+
},
79+
min: minTime,
80+
max: maxTime
7781
},
7882
y: {
7983
offset: true,
8084
grid: {
8185
color: "rgba(63 63 70)"
8286
},
8387
type: "linear",
84-
min: 0
85-
// max: max
88+
min: min,
89+
max: max
8690
}
8791
},
8892
responsive: true,

lib/nerves_hub_web/components/device_page/health_tab.ex

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ defmodule NervesHubWeb.Components.DevicePage.HealthTab do
192192
data-unit={Jason.encode!(chart.unit)}
193193
data-max={Jason.encode!(chart.max)}
194194
data-min={Jason.encode!(chart.min)}
195+
data-maxtime={Jason.encode!(chart.max_time)}
196+
data-mintime={Jason.encode!(chart.min_time)}
195197
data-metrics={Jason.encode!(chart.data)}
196198
data-title={Jason.encode!(chart_title(chart))}
197199
>
@@ -271,8 +273,21 @@ defmodule NervesHubWeb.Components.DevicePage.HealthTab do
271273
end
272274

273275
defp create_chart_data(device_id, {unit, _} = time_frame, memory_size) do
274-
device_id
275-
|> Metrics.get_device_metrics(time_frame)
276+
metrics =
277+
device_id
278+
|> Metrics.get_device_metrics(time_frame)
279+
280+
%{inserted_at: max_time} =
281+
Enum.max_by(metrics, & &1.inserted_at, DateTime, fn ->
282+
%{inserted_at: DateTime.from_unix!(0)}
283+
end)
284+
285+
%{inserted_at: min_time} =
286+
Enum.min_by(metrics, & &1.inserted_at, DateTime, fn ->
287+
%{inserted_at: DateTime.from_unix!(1)}
288+
end)
289+
290+
metrics
276291
|> Enum.group_by(& &1.key)
277292
|> filter_and_sort()
278293
|> Enum.map(fn {type, metrics} ->
@@ -284,7 +299,9 @@ defmodule NervesHubWeb.Components.DevicePage.HealthTab do
284299
title: title(type),
285300
data: data,
286301
max: get_max_value(type, data, memory_size),
287-
min: get_min_value(data),
302+
min: get_min_value(type, data),
303+
min_time: min_time,
304+
max_time: max_time,
288305
unit: get_time_unit(time_frame)
289306
}
290307
end)
@@ -359,6 +376,8 @@ defmodule NervesHubWeb.Components.DevicePage.HealthTab do
359376
data
360377
|> Enum.max_by(& &1.y)
361378
|> Map.get(:y)
379+
# Space it out a little
380+
|> Kernel.+(1.0)
362381
end
363382
end
364383

@@ -370,10 +389,28 @@ defmodule NervesHubWeb.Components.DevicePage.HealthTab do
370389
|> max(1)
371390
end
372391

373-
defp get_min_value(data) do
374-
data
375-
|> Enum.min_by(& &1.y)
376-
|> Map.get(:y)
392+
defp get_min_value(type, data) do
393+
case type do
394+
"load_" <> _ ->
395+
0
396+
397+
type
398+
when type in [
399+
"mem_used_mb",
400+
"mem_used_percent",
401+
"cpu_temp",
402+
"cpu_usage_percent",
403+
"disk_used_percentage"
404+
] ->
405+
0
406+
407+
_ ->
408+
data
409+
|> Enum.min_by(& &1.y)
410+
|> Map.get(:y)
411+
# Space it out a little
412+
|> Kernel.-(1.0)
413+
end
377414
end
378415

379416
defp custom_metrics(metrics) do

0 commit comments

Comments
 (0)