Skip to content

Commit e4b2952

Browse files
committed
Add option to set history size for line graphs
1 parent 1950410 commit e4b2952

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

library/stats.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from library.display import display
3636
from library.log import logger
3737

38+
DEFAULT_HISTORY_SIZE = 10
39+
3840
ETH_CARD = config.CONFIG_DATA["config"]["ETH"]
3941
WLO_CARD = config.CONFIG_DATA["config"]["WLO"]
4042
HW_SENSORS = config.CONFIG_DATA["config"]["HW_SENSORS"]
@@ -220,10 +222,13 @@ def display_themed_line_graph(theme_data, values):
220222
)
221223

222224

223-
def save_last_value(value, last_values: List[float]):
224-
# Store the value to the history list that can be used for line graph
225+
def save_last_value(value: float, last_values: List[float], history_size: int):
226+
# Initialize last values list the first time with given size
227+
if len(last_values) != history_size:
228+
last_values[:] = last_values_list(size=history_size)
229+
# Store the value to the list that can then be used for line graph
225230
last_values.append(value)
226-
# Also remove the oldest value from history list
231+
# Also remove the oldest value from list
227232
last_values.pop(0)
228233

229234

@@ -232,17 +237,18 @@ def last_values_list(size: int) -> List[float]:
232237

233238

234239
class CPU:
235-
last_values_cpu_percentage = last_values_list(size=10)
236-
last_values_cpu_temperature = last_values_list(size=10)
237-
last_values_cpu_fan_speed = last_values_list(size=10)
240+
last_values_cpu_percentage = []
241+
last_values_cpu_temperature = []
242+
last_values_cpu_fan_speed = []
238243

239244
@classmethod
240245
def percentage(cls):
241246
theme_data = config.THEME_DATA['STATS']['CPU']['PERCENTAGE']
242247
cpu_percentage = sensors.Cpu.percentage(
243248
interval=theme_data.get("INTERVAL", None)
244249
)
245-
save_last_value(cpu_percentage, cls.last_values_cpu_percentage)
250+
save_last_value(cpu_percentage, cls.last_values_cpu_percentage,
251+
theme_data['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
246252
# logger.debug(f"CPU Percentage: {cpu_percentage}")
247253

248254
display_themed_progress_bar(theme_data['GRAPH'], cpu_percentage)
@@ -272,7 +278,9 @@ def load(cls):
272278
@classmethod
273279
def temperature(cls):
274280
temperature = sensors.Cpu.temperature()
275-
save_last_value(temperature, cls.last_values_cpu_temperature)
281+
save_last_value(temperature, cls.last_values_cpu_temperature,
282+
config.THEME_DATA['STATS']['CPU']['TEMPERATURE']['LINE_GRAPH'].get("HISTORY_SIZE",
283+
DEFAULT_HISTORY_SIZE))
276284

277285
cpu_temp_text_data = config.THEME_DATA['STATS']['CPU']['TEMPERATURE']['TEXT']
278286
cpu_temp_radial_data = config.THEME_DATA['STATS']['CPU']['TEMPERATURE']['RADIAL']
@@ -297,7 +305,9 @@ def temperature(cls):
297305
@classmethod
298306
def fan_speed(cls):
299307
fan_percent = sensors.Cpu.fan_percent()
300-
save_last_value(fan_percent, cls.last_values_cpu_fan_speed)
308+
save_last_value(fan_percent, cls.last_values_cpu_fan_speed,
309+
config.THEME_DATA['STATS']['CPU']['FAN_SPEED']['LINE_GRAPH'].get("HISTORY_SIZE",
310+
DEFAULT_HISTORY_SIZE))
301311

302312
cpu_fan_text_data = config.THEME_DATA['STATS']['CPU']['FAN_SPEED']['TEXT']
303313
cpu_fan_radial_data = config.THEME_DATA['STATS']['CPU']['FAN_SPEED']['RADIAL']
@@ -321,26 +331,31 @@ def fan_speed(cls):
321331

322332

323333
class Gpu:
324-
last_values_gpu_percentage = last_values_list(size=10)
325-
last_values_gpu_mem_percentage = last_values_list(size=10)
326-
last_values_gpu_temperature = last_values_list(size=10)
327-
last_values_gpu_fps = last_values_list(size=10)
328-
last_values_gpu_fan_speed = last_values_list(size=10)
334+
last_values_gpu_percentage = []
335+
last_values_gpu_mem_percentage = []
336+
last_values_gpu_temperature = []
337+
last_values_gpu_fps = []
338+
last_values_gpu_fan_speed = []
329339

330340
@classmethod
331341
def stats(cls):
332342
load, memory_percentage, memory_used_mb, temperature = sensors.Gpu.stats()
333343
fps = sensors.Gpu.fps()
334344
fan_percent = sensors.Gpu.fan_percent()
335345

336-
save_last_value(load, cls.last_values_gpu_percentage)
337-
save_last_value(memory_percentage, cls.last_values_gpu_mem_percentage)
338-
save_last_value(temperature, cls.last_values_gpu_temperature)
339-
save_last_value(fps, cls.last_values_gpu_fps)
340-
save_last_value(fan_percent, cls.last_values_gpu_fan_speed)
341-
342346
theme_gpu_data = config.THEME_DATA['STATS']['GPU']
343347

348+
save_last_value(load, cls.last_values_gpu_percentage,
349+
theme_gpu_data['PERCENTAGE']['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
350+
save_last_value(memory_percentage, cls.last_values_gpu_mem_percentage,
351+
theme_gpu_data['MEMORY_PERCENT']['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
352+
save_last_value(temperature, cls.last_values_gpu_temperature,
353+
theme_gpu_data['TEMPERATURE']['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
354+
save_last_value(fps, cls.last_values_gpu_fps,
355+
theme_gpu_data['FPS']['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
356+
save_last_value(fan_percent, cls.last_values_gpu_fan_speed,
357+
theme_gpu_data['FAN_SPEED']['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
358+
344359
################################ for backward compatibility only
345360
gpu_mem_graph_data = theme_gpu_data['MEMORY']['GRAPH']
346361
gpu_mem_radial_data = theme_gpu_data['MEMORY']['RADIAL']
@@ -518,19 +533,19 @@ def stats():
518533

519534
display_themed_value(
520535
theme_data=memory_stats_theme_data['VIRTUAL']['USED'],
521-
value=int(sensors.Memory.virtual_used() / 1024**2),
536+
value=int(sensors.Memory.virtual_used() / 1024 ** 2),
522537
min_size=5,
523538
unit=" M"
524539
)
525540
display_themed_value(
526541
theme_data=memory_stats_theme_data['VIRTUAL']['FREE'],
527-
value=int(sensors.Memory.virtual_free() / 1024**2),
542+
value=int(sensors.Memory.virtual_free() / 1024 ** 2),
528543
min_size=5,
529544
unit=" M"
530545
)
531546
display_themed_value(
532547
theme_data=memory_stats_theme_data['VIRTUAL']['TOTAL'],
533-
value=int((sensors.Memory.virtual_free() + sensors.Memory.virtual_used()) / 1024**2),
548+
value=int((sensors.Memory.virtual_free() + sensors.Memory.virtual_used()) / 1024 ** 2),
534549
min_size=5,
535550
unit=" M"
536551
)

res/themes/Cyberdeck/theme.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ STATS:
201201
HEIGHT: 82
202202
MIN_VALUE: 0
203203
MAX_VALUE: 144
204+
HISTORY_SIZE: 100
204205
AUTOSCALE: True
205206
LINE_COLOR: 255, 255, 255
206207
AXIS: True

res/themes/theme_example.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ STATS:
117117
HEIGHT: 70
118118
MIN_VALUE: 0
119119
MAX_VALUE: 100
120+
HISTORY_SIZE: 10
120121
AUTOSCALE: False
121122
LINE_COLOR: 61, 184, 225
122123
AXIS: True
@@ -229,6 +230,7 @@ STATS:
229230
HEIGHT: 70
230231
MIN_VALUE: 30
231232
MAX_VALUE: 120
233+
HISTORY_SIZE: 10
232234
AUTOSCALE: True
233235
LINE_COLOR: 61, 184, 225
234236
AXIS: True
@@ -288,6 +290,7 @@ STATS:
288290
HEIGHT: 70
289291
MIN_VALUE: 0
290292
MAX_VALUE: 3000
293+
HISTORY_SIZE: 10
291294
AUTOSCALE: True
292295
LINE_COLOR: 61, 184, 225
293296
AXIS: True
@@ -351,6 +354,7 @@ STATS:
351354
HEIGHT: 70
352355
MIN_VALUE: 0
353356
MAX_VALUE: 100
357+
HISTORY_SIZE: 10
354358
AUTOSCALE: False
355359
LINE_COLOR: 61, 184, 225
356360
AXIS: True
@@ -409,6 +413,7 @@ STATS:
409413
HEIGHT: 70
410414
MIN_VALUE: 0
411415
MAX_VALUE: 100
416+
HISTORY_SIZE: 10
412417
AUTOSCALE: False
413418
LINE_COLOR: 61, 184, 225
414419
AXIS: True
@@ -478,6 +483,7 @@ STATS:
478483
HEIGHT: 70
479484
MIN_VALUE: 30
480485
MAX_VALUE: 120
486+
HISTORY_SIZE: 10
481487
AUTOSCALE: True
482488
LINE_COLOR: 61, 184, 225
483489
AXIS: True
@@ -536,6 +542,7 @@ STATS:
536542
HEIGHT: 70
537543
MIN_VALUE: 0
538544
MAX_VALUE: 300
545+
HISTORY_SIZE: 10
539546
AUTOSCALE: True
540547
LINE_COLOR: 61, 184, 225
541548
AXIS: True
@@ -594,6 +601,7 @@ STATS:
594601
HEIGHT: 70
595602
MIN_VALUE: 0
596603
MAX_VALUE: 3000
604+
HISTORY_SIZE: 10
597605
AUTOSCALE: True
598606
LINE_COLOR: 61, 184, 225
599607
AXIS: True

0 commit comments

Comments
 (0)