// Timestamp
struct timeval t;
- // Value
- char* value;
+ // Samples
+ char** samples;
+ unsigned int num_samples;
};
struct collecty_queue {
}
static void collecty_queue_free_object(struct collecty_queue_object* o) {
+ if (o->samples) {
+ for (unsigned int i = 0; i < o->num_samples; i++)
+ free(o->samples[i]);
+ free(o->samples);
+ }
+
if (o->module)
collecty_module_unref(o->module);
if (o->object)
free(o->object);
- if (o->value)
- free(o->value);
free(o);
}
return NULL;
}
+static struct collecty_queue_object* collecty_queue_find_object(
+ collecty_queue* self, collecty_module* module, const char* object) {
+ struct collecty_queue_object* o = NULL;
+
+ STAILQ_FOREACH(o, &self->queue, nodes) {
+ // The module must match
+ if (o->module != module)
+ continue;
+
+ // If both objects are NULL we have a match
+ if (!o->object && !object)
+ return o;
+
+ // If both have objects, we need to compare
+ else if (o->object && object)
+ if (strcmp(o->object, object) == 0)
+ return o;
+ }
+
+ // No match found
+ return NULL;
+}
+
+static int collecty_queue_object_append_sample(collecty_queue* self, collecty_module* module,
+ const char* object, struct collecty_queue_object* o, const char* sample) {
+ char** samples = NULL;
+ char* s = NULL;
+
+ // Increase the size of the array
+ samples = reallocarray(o->samples, o->num_samples + 1, sizeof(*o->samples));
+ if (!samples)
+ return -errno;
+
+ // Copy the sample to the heap
+ s = strdup(sample);
+ if (!s)
+ return -errno;
+
+ // Assign the sample
+ samples[o->num_samples++] = s;
+
+ // Replace the array
+ o->samples = samples;
+
+ return 0;
+}
+
/*
Submits a new reading into the queue
*/
int collecty_queue_submit(collecty_queue* self,
- collecty_module* module, const char* object, const char* value) {
+ collecty_module* module, const char* object, const char* sample) {
struct collecty_queue_object* o = NULL;
int r;
// Check inputs
- if (!value)
+ if (!sample)
return -EINVAL;
+ // Check if we can append the sample
+ o = collecty_queue_find_object(self, module, object);
+ if (o)
+ return collecty_queue_object_append_sample(self, module, object, o, sample);
+
// Allocate some memory
o = calloc(1, sizeof(*self));
if (!o)
}
}
- // Store the value
- o->value = strdup(value);
- if (!o->value) {
- r = -errno;
+ // Store the sample
+ r = collecty_queue_object_append_sample(self, module, object, o, sample);
+ if (r < 0)
goto ERROR;
- }
// Append the object to the queue
STAILQ_INSERT_TAIL(&self->queue, o, nodes);
- // Log action
- DEBUG(self->ctx, "%s(%s) submitted: %s\n",
- collecty_module_name(module), (o->object) ? o->object : "", o->value);
-
return 0;
ERROR: