]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Send information that does not change only once
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 25 Jan 2025 17:53:23 +0000 (17:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 25 Jan 2025 17:53:23 +0000 (17:53 +0000)
We used to send a lot of unnecessary stuff when we submitted stats. The
CPU model usually does not change, and so we will only send it once when
we connect.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/daemon.c

index fe8a61a53cfedaad9fb453dd183fcb0670ef4503..c4a35a1cb707def234d8e6293ae0490703162dfb 100644 (file)
@@ -130,14 +130,11 @@ static int pakfire_daemon_terminate(sd_event_source* source,
 
 static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* p) {
        struct pakfire_daemon* daemon = p;
-       struct pakfire_sysinfo sysinfo = {};
-       struct pakfire_cpuinfo cpuinfo = {};
        struct pakfire_cpustat cpustat = {};
        struct pakfire_loadavg loadavg = {};
        struct pakfire_meminfo meminfo = {};
        struct json_object* stats = NULL;
        struct json_object* data = NULL;
-       const char* arch = NULL;
        size_t length = 0;
        int r;
 
@@ -160,23 +157,6 @@ static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void*
 
        DEBUG(daemon->ctx, "Submitting stats...\n");
 
-       // Fetch the distro
-       const struct pakfire_distro* distro = pakfire_ctx_get_distro(daemon->ctx);
-
-       // Fetch system information
-       r = pakfire_sysinfo(&sysinfo);
-       if (r < 0) {
-               ERROR(daemon->ctx, "Failed to fetch system info: %s\n", strerror(-r));
-               goto ERROR;
-       }
-
-       // Fetch CPU info
-       r = pakfire_cpuinfo(&cpuinfo);
-       if (r) {
-               ERROR(daemon->ctx, "Failed to fetch CPU info: %s\n", strerror(-r));
-               goto ERROR;
-       }
-
        // Fetch CPU stats
        r = pakfire_cpustat(&cpustat);
        if (r) {
@@ -198,58 +178,11 @@ static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void*
                goto ERROR;
        }
 
-       // Fetch native arch
-       arch = pakfire_arch_native();
-       if (!arch) {
-               ERROR(daemon->ctx, "Failed to fetch native arch: %s\n", strerror(errno));
-               r = -errno;
-               goto ERROR;
-       }
-
        // Create new data object
        data = json_object_new_object();
        if (!data)
                goto ERROR;
 
-       // System Vendor
-       if (*sysinfo.vendor) {
-               r = pakfire_json_add_string(data, "sys_vendor", sysinfo.vendor);
-               if (r)
-                       goto ERROR;
-       }
-
-       // System Model
-       if (*sysinfo.name) {
-               r = pakfire_json_add_string(data, "sys_name", sysinfo.name);
-               if (r)
-                       goto ERROR;
-       }
-
-       // CPU Model
-       r = pakfire_json_add_string(data, "cpu_model", cpuinfo.model);
-       if (r)
-               goto ERROR;
-
-       // CPU Count
-       r = pakfire_json_add_uint64(data, "cpu_count", cpuinfo.count);
-       if (r)
-               goto ERROR;
-
-       // CPU Arch
-       r = pakfire_json_add_string(data, "cpu_arch", arch);
-       if (r)
-               goto ERROR;
-
-       // Pakfire Version
-       r = pakfire_json_add_string(data, "pakfire_version", PACKAGE_VERSION);
-       if (r)
-               goto ERROR;
-
-       // OS
-       r = pakfire_json_add_string(data, "os_name", distro->pretty_name);
-       if (r)
-               goto ERROR;
-
        // CPU Stats: User
        r = pakfire_json_add_double(data, "cpu_user", cpustat.user);
        if (r)
@@ -607,10 +540,38 @@ static int pakfire_daemon_connected(struct pakfire_xfer* xfer, void* data) {
 
 static int pakfire_daemon_connect(sd_event_source* s, uint64_t usec, void* data) {
        struct pakfire_daemon* daemon = data;
+       struct pakfire_sysinfo sysinfo = {};
+       struct pakfire_cpuinfo cpuinfo = {};
        struct pakfire_xfer* xfer = NULL;
+       const char* arch = NULL;
        int r;
 
-       INFO(daemon->ctx, "Connecting...\n");
+       DEBUG(daemon->ctx, "Connecting...\n");
+
+       // Fetch the distro
+       const struct pakfire_distro* distro = pakfire_ctx_get_distro(daemon->ctx);
+
+       // Fetch system information
+       r = pakfire_sysinfo(&sysinfo);
+       if (r < 0) {
+               ERROR(daemon->ctx, "Failed to fetch system info: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+       // Fetch native arch
+       arch = pakfire_arch_native();
+       if (!arch) {
+               ERROR(daemon->ctx, "Failed to fetch native arch: %s\n", strerror(errno));
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Fetch CPU info
+       r = pakfire_cpuinfo(&cpuinfo);
+       if (r) {
+               ERROR(daemon->ctx, "Failed to fetch CPU info: %s\n", strerror(-r));
+               goto ERROR;
+       }
 
        // Create a new xfer
        r = pakfire_daemon_xfer_create(&xfer, daemon, "/api/v1/builders/control");
@@ -622,6 +583,47 @@ static int pakfire_daemon_connect(sd_event_source* s, uint64_t usec, void* data)
        if (r)
                goto ERROR;
 
+       // Send our version
+       r = pakfire_xfer_add_query(xfer, "version", "%s", PACKAGE_VERSION);
+       if (r < 0)
+               goto ERROR;
+
+       // System Vendor
+       if (*sysinfo.vendor) {
+               r = pakfire_xfer_add_query(xfer, "sys_vendor", "%s", sysinfo.vendor);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // System Model
+       if (*sysinfo.name) {
+               r = pakfire_xfer_add_query(xfer, "sys_name", "%s", sysinfo.name);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // CPU Model
+       if (*cpuinfo.model) {
+               r = pakfire_xfer_add_query(xfer, "cpu_model", "%s", cpuinfo.model);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // CPU Count
+       r = pakfire_xfer_add_query(xfer, "cpu_count", "%u", cpuinfo.count);
+       if (r < 0)
+               goto ERROR;
+
+       // Arch
+       r = pakfire_xfer_add_query(xfer, "arch", "%s", arch);
+       if (r < 0)
+               goto ERROR;
+
+       // OS
+       r = pakfire_xfer_add_query(xfer, "os_name", "%s", distro->pretty_name);
+       if (r < 0)
+               goto ERROR;
+
        // Make this a WebSocket connection
        r = pakfire_xfer_socket(xfer, pakfire_daemon_connected,
                pakfire_daemon_recv, NULL, pakfire_daemon_close, daemon);