]> git.ipfire.org Git - collecty.git/commitdiff
graph: Allow specifying the output dimensions
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 09:46:46 +0000 (09:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 09:47:24 +0000 (09:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client/main.c
src/daemon/graph-bus.c
src/daemon/graph.c
src/daemon/graph.h

index 1de55dd5f2e4197ecbe730df6016d5dbb54f4a4f..304abdc6d281c09e6c7a71a1a104d88e25c3b354 100644 (file)
@@ -46,25 +46,54 @@ typedef struct collecty_client_ctx {
 
        // Output
        FILE* f;
+
+       // Dimensions
+       struct {
+               uint64_t h;
+               uint64_t w;
+       } dimensions;
 } collecty_client_ctx;
 
 enum {
        OPT_FORMAT  = 1,
+       OPT_HEIGHT  = 2,
+       OPT_WIDTH   = 3,
 };
 
 static struct argp_option options[] = {
        { "format", OPT_FORMAT, "FORMAT", 0, "Output Format", 0 },
+       { "height", OPT_HEIGHT, "PIXELS", 0, "Height of the output image", 1 },
+       { "width",  OPT_WIDTH,  "PIXELS", 0, "Width of the output image",  1 },
        { NULL },
 };
 
 static error_t parse(int key, char* arg, struct argp_state* state) {
        collecty_client_ctx* ctx = state->input;
+       char* p = NULL;
 
        switch (key) {
                case OPT_FORMAT:
                        ctx->format = arg;
                        break;
 
+               case OPT_HEIGHT:
+                       ctx->dimensions.h = strtoul(arg, &p, 10);
+
+                       // Fail if we could not parse the height
+                       if (ctx->dimensions.h == ULONG_MAX || *p)
+                               argp_error(state, "Invalid value for --height: %s", arg);
+
+                       break;
+
+               case OPT_WIDTH:
+                       ctx->dimensions.w = strtoul(arg, &p, 10);
+
+                       // Fail if we could not parse the height
+                       if (ctx->dimensions.w == ULONG_MAX || *p)
+                               argp_error(state, "Invalid value for --width: %s", arg);
+
+                       break;
+
                // Called for each argument
                case ARGP_KEY_ARG:
                        // Take the graph name as first argument
@@ -144,6 +173,20 @@ static int render(collecty_client_ctx* ctx) {
                        goto ERROR;
        }
 
+       // Send the desired height
+       if (ctx->dimensions.h) {
+               r = sd_bus_message_append(m, "{sv}", "height", "t", ctx->dimensions.h);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // Send the desired width
+       if (ctx->dimensions.w) {
+               r = sd_bus_message_append(m, "{sv}", "width", "t", ctx->dimensions.w);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Close the array
        r = sd_bus_message_close_container(m);
        if (r < 0)
index ddae4e1bac18f7467e8e3e351a9ecfba2a495c32..3029a1ca33815fef33768a1b51a5130fd577a1d9 100644 (file)
@@ -133,11 +133,23 @@ static int collecty_graph_bus_render(sd_bus_message* m, void* data, sd_bus_error
                if (r < 0)
                        goto ERROR;
 
-               // Parse the values
+               // Parse "format"
                if (strcmp(key, "format") == 0) {
                        r = sd_bus_message_read(m, "v", "s", &options.format);
                        if (r < 0)
                                goto ERROR;
+
+               // Parse "height"
+               } else if (strcmp(key, "height") == 0) {
+                       r = sd_bus_message_read(m, "v", "t", &options.dimensions.h);
+                       if (r < 0)
+                               goto ERROR;
+
+               // Parse "width"
+               } else if (strcmp(key, "width") == 0) {
+                       r = sd_bus_message_read(m, "v", "t", &options.dimensions.w);
+                       if (r < 0)
+                               goto ERROR;
                }
 
                // Leave the container
index 4a3bb8e772189748c10de2f80a18d2a05ea85684..d1c275a6016c20a3796f1d311200feba7d33cda2 100644 (file)
@@ -63,6 +63,9 @@ const char* DEFAULT_RENDER_ARGS[] = {
        NULL,
 };
 
+const unsigned int DEFAULT_HEIGHT = 480;
+const unsigned int DEFAULT_WIDTH  = 960;
+
 static void collecty_graph_free(collecty_graph* self) {
        if (self->daemon)
                collecty_daemon_unref(self->daemon);
@@ -210,6 +213,18 @@ int collecty_graph_render(collecty_graph* self, const char* object,
                        goto ERROR;
        }
 
+       // Configure the desired output height
+       r = collecty_args_push(args, "--height=%u",
+                       (options->dimensions.h) ? options->dimensions.h : DEFAULT_HEIGHT);
+       if (r < 0)
+               goto ERROR;
+
+       // Configure the desired output width
+       r = collecty_args_push(args, "--width=%u",
+                       (options->dimensions.w) ? options->dimensions.w : DEFAULT_WIDTH);
+       if (r < 0)
+               goto ERROR;
+
        // Write the graph to the output stream
        r = collecty_args_push(args, "-");
        if (r < 0)
index 043ad66600fd2537c42ee2f0495e1f1a63032a10..90e2be2f6d14c994ff0c1532c8d65abdbad0488d 100644 (file)
@@ -52,7 +52,14 @@ int collecty_graph_require_module(collecty_graph* self,
        collecty_args* args, const char* name, const char* object);
 
 typedef struct collecty_graph_render_options {
+       // Output Format
        const char* format;
+
+       // Dimensions
+       struct {
+               unsigned int h;
+               unsigned int w;
+       } dimensions;
 } collecty_graph_render_options;
 
 int collecty_graph_render(collecty_graph* self, const char* object,