From 06ea523f2529c1a75ed530b9dedee60c8cd3499f Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 3 Oct 2025 09:46:46 +0000 Subject: [PATCH] graph: Allow specifying the output dimensions Signed-off-by: Michael Tremer --- src/client/main.c | 43 ++++++++++++++++++++++++++++++++++++++++++ src/daemon/graph-bus.c | 14 +++++++++++++- src/daemon/graph.c | 15 +++++++++++++++ src/daemon/graph.h | 7 +++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/client/main.c b/src/client/main.c index 1de55dd..304abdc 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -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) diff --git a/src/daemon/graph-bus.c b/src/daemon/graph-bus.c index ddae4e1..3029a1c 100644 --- a/src/daemon/graph-bus.c +++ b/src/daemon/graph-bus.c @@ -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 diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 4a3bb8e..d1c275a 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -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) diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 043ad66..90e2be2 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -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, -- 2.47.3