]> git.ipfire.org Git - telemetry.git/commitdiff
graphs: memory: Refactor the entire legend
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Apr 2026 14:15:39 +0000 (14:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Apr 2026 14:15:39 +0000 (14:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graphs/graph.h
src/daemon/graphs/memory.c

index 09c3a8edda1923e907cee7d6a42953dd2d3dd91b..25aee73b59f9322db39c00efbcc573da2adb52cc 100644 (file)
@@ -191,7 +191,6 @@ static inline int __DRAW(td_args* args, const char* what, const char* field,
 
 // Add something to the legend of the graph
 #define PRINT_EMPTY_LINE(args)                                                 SCRIPT(args, "COMMENT: \\n")
-#define PRINT_HEADLINE(args, headline)                                 SCRIPT(args, "COMMENT:<b>%s</b>\\n", headline)
 #define PRINT_HEADER(args, label)                                              SCRIPT(args, "COMMENT:" COLUMN, label)
 #define PRINT_LABEL(args, label)                                               SCRIPT(args, "COMMENT: %-31s", label)
 #define PRINT_EMPTY_LABEL(args)                                                        SCRIPT(args, "COMMENT:                                ")
@@ -200,6 +199,25 @@ static inline int __DRAW(td_args* args, const char* what, const char* field,
        SCRIPT(args, "GPRINT:" FIELD ":" format, FIELD_AND_OBJECT(field, object), ##__VA_ARGS__)
 #define PRINT_EOL(args)                                                                        SCRIPT(args, "COMMENT:" EOL)
 
+// Headlines
+#define __PRINT_HEADLINE(args, headline)                               SCRIPT(args, "COMMENT:<b>%-32s</b>", headline)
+
+#define PRINT_HEADLINE(args, headline) \
+       { \
+               __PRINT_HEADLINE(args, headline); \
+               PRINT_EOL(args); \
+       } while (0)
+
+#define PRINT_HEADLINE_WITH_CURRENT(args, headline, field, object, format, ...) \
+       { \
+               __PRINT_HEADLINE(args, headline); \
+               PRINT_CURRENT(args, field, object, format, ##__VA_ARGS__); \
+               PRINT_NOTHING(args); \
+               PRINT_NOTHING(args); \
+               PRINT_NOTHING(args); \
+               PRINT_EOL(args); \
+       } while (0)
+
 /*
        Prints CURRENT, AVERAGE, MINIMUM and MAXIMUM
 */
index 13d08ab872758ea0db5d0184063f8806c288b3d1..dbe2112d644450f3fa98435126be61daf2f70e01 100644 (file)
@@ -54,44 +54,45 @@ static int memory_render(td_ctx* ctx, td_graph* graph,
        COMPUTE_DIFFERENCE(args, "swap_used", NULL, "swap_total", NULL, "swap_free", NULL);
        COMPUTE_PERCENTAGE(args, "swap_used", NULL, "swap_total", NULL);
 
+       // Headline
+       PRINT_HEADLINE_WITH_CURRENT(args, _("Memory"), "mem_total", NULL, BYTES, BYTES_UNIT);
+
+       // Draw the total memory as a line
+       DRAW_LINE(args, 1, "mem_total", NULL, COLOR_MEM_TOTAL, SKIPSCALE);
+
        // Draw the stacked background first
        DRAW_AREA_BACKGROUND(args, "mem_used", NULL, COLOR_MEM_USED, 0);
        DRAW_AREA_BACKGROUND(args, "buffers", NULL, COLOR_BUFFERS, STACKED);
        DRAW_AREA_BACKGROUND(args, "cached", NULL, COLOR_CACHED, STACKED);
 
        // Draw the area outlines afterwards
-       DRAW_AREA_OUTLINE_WITH_LABEL(args, "mem_used", NULL, COLOR_MEM_USED, 0, _("Used Memory"));
-       PRINT_CAMM(args, "mem_used", NULL, LARGE_FLOAT);
+       DRAW_AREA_OUTLINE_WITH_LABEL(args, "mem_used", NULL, COLOR_MEM_USED, 0, _("Used"));
+       PRINT_CAMM(args, "mem_used", NULL, BYTES, BYTES_UNIT);
 
        PRINT_EMPTY_LABEL(args);
        PRINT_CAMM(args, FIELD_PERCENT("mem_used"), NULL, PERCENTAGE);
 
        DRAW_AREA_OUTLINE_WITH_LABEL(args, "buffers", NULL, COLOR_BUFFERS, STACKED, _("Buffers"));
-       PRINT_CAMM(args, "buffers", NULL, LARGE_FLOAT);
+       PRINT_CAMM(args, "buffers", NULL, BYTES, BYTES_UNIT);
 
        DRAW_AREA_OUTLINE_WITH_LABEL(args, "cached", NULL, COLOR_CACHED, STACKED, _("Cached"));
-       PRINT_CAMM(args, "cached", NULL, LARGE_FLOAT);
-
-       // Draw the total memory as a line
-       DRAW_LINE(args, 1, "mem_total", NULL, COLOR_MEM_TOTAL, SKIPSCALE);
+       PRINT_CAMM(args, "cached", NULL, BYTES, BYTES_UNIT);
 
        // Make some space for swap
        PRINT_EMPTY_LINE(args);
 
-       DRAW_LINE_WITH_LABEL(args, 1, "swap_used", NULL, COLOR_SWAP_USED, DASHED, _("Used Swap"));
-       PRINT_CAMM(args, "swap_used", NULL, LARGE_FLOAT);
+       // Headline
+       PRINT_HEADLINE_WITH_CURRENT(args, _("Swap"), "swap_total", NULL, BYTES, BYTES_UNIT);
+
+       // Draw the total swap as a line
+       DRAW_LINE(args, 1, "swap_total", NULL, COLOR_SWAP_TOTAL, DASHED | SKIPSCALE);
+
+       DRAW_LINE_WITH_LABEL(args, 1, "swap_used", NULL, COLOR_SWAP_USED, DASHED, _("Used"));
+       PRINT_CAMM(args, "swap_used", NULL, BYTES, BYTES_UNIT);
 
        PRINT_EMPTY_LABEL(args);
        PRINT_CAMM(args, FIELD_PERCENT("swap_used"), NULL, PERCENTAGE);
 
-       DRAW_LINE_WITH_LABEL(args, 1, "swap_total", NULL,
-                       COLOR_SWAP_TOTAL, DASHED | SKIPSCALE, _("Total Swap"));
-       PRINT(args, FIELD_CURRENT("swap_used"), NULL, LARGE_FLOAT);
-       PRINT_NOTHING(args);
-       PRINT_NOTHING(args);
-       PRINT_NOTHING(args);
-       PRINT_EOL(args);
-
        return 0;
 }