@@ -133,15 +133,49 @@ defmodule NervesHubWeb.Live.Devices.DeviceHealth do
133133 ) do
134134 latest_metrics = Metrics . get_latest_metric_set_for_device ( device . id )
135135
136- # Create graphs for metric types and assign to socket
137- Metrics . metric_types ( )
136+ # Create graphs for default metric types and assign to socket
137+ Metrics . default_metric_types ( )
138138 |> Enum . reduce ( socket , fn type , socket ->
139139 graph =
140140 create_graph_for_type ( device . id , type , chart_type , time_frame , latest_metrics . size_mb )
141141
142142 socket |> assign ( type , graph )
143143 end )
144144 |> assign ( :latest_metrics , latest_metrics )
145+ |> assign_custom_metrics ( )
146+ end
147+
148+ def assign_custom_metrics (
149+ % {
150+ assigns: % {
151+ device: device ,
152+ chart_type: chart_type ,
153+ time_frame: time_frame
154+ }
155+ } =
156+ socket
157+ ) do
158+ custom_metrics =
159+ device . id
160+ |> Metrics . get_custom_metrics_for_device ( time_frame )
161+ |> Enum . group_by ( & & 1 . key )
162+ |> Enum . map ( fn { key , metrics } ->
163+ title =
164+ key
165+ |> String . replace ( "_" , " " )
166+ |> String . capitalize ( )
167+
168+ max_size = get_custom_max_value ( metrics )
169+
170+ graph =
171+ metrics
172+ |> organize_metrics_for_contex ( )
173+ |> create_chart ( chart_type , max_size , time_frame )
174+
175+ % { title: title , graph: graph }
176+ end )
177+
178+ socket |> assign ( :custom_metrics , custom_metrics )
145179 end
146180
147181 def create_graph_for_type ( device_id , metric_type , chart_type , time_frame , memory_size ) do
@@ -182,6 +216,12 @@ defmodule NervesHubWeb.Live.Devices.DeviceHealth do
182216 |> max ( 1 )
183217 end
184218
219+ defp get_custom_max_value ( data ) do
220+ data
221+ |> Enum . max_by ( & & 1 . value )
222+ |> Map . get ( :value )
223+ end
224+
185225 defp create_chart ( data , _chart_type , _max_value , _time_unit )
186226 when data == [ ] ,
187227 do: raw ( "<p class=\" metrics-text\" >No data for selected period</p>" )
0 commit comments