Code

store: Calculate and store each objects update interval.
authorSebastian Harl <sh@tokkee.org>
Wed, 2 Apr 2014 10:37:10 +0000 (12:37 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 2 Apr 2014 10:37:10 +0000 (12:37 +0200)
The interval is calculated as the moving average of the incoming update
intervals. This should also work fine for out-of-order updates or updates from
multiple sources.

src/core/store-private.h
src/core/store.c

index abd94c22f1d40dba2e097870abfb5d47b343e9f8..a5bf0267b7e41bd598135ee28048f697ec3d93f7 100644 (file)
@@ -46,6 +46,7 @@ struct sdb_store_base {
 
        /* common meta information */
        sdb_time_t last_update;
+       sdb_time_t interval; /* moving average */
        sdb_store_base_t *parent;
 };
 #define STORE_BASE(obj) ((sdb_store_base_t *)(obj))
@@ -81,6 +82,7 @@ enum {
 /* shortcuts for accessing the sdb_store_obj_t attributes
  * of inheriting objects */
 #define _last_update super.last_update
+#define _interval super.interval
 
 #ifdef __cplusplus
 } /* extern "C" */
index b2fb0f602837f62128e09c849b00dde84f1389c0..6420190a319b532ed0db1b2248e4aafd6ee77784 100644 (file)
@@ -67,6 +67,7 @@ store_base_init(sdb_object_t *obj, va_list ap)
        sobj->type = va_arg(ap, int);
 
        sobj->last_update = va_arg(ap, sdb_time_t);
+       sobj->interval = 0;
        sobj->parent = NULL;
        return 0;
 } /* store_base_init */
@@ -292,7 +293,13 @@ store_obj(int parent_type, const char *parent_name,
                        status = 1;
                }
                else {
+                       sdb_time_t interval = last_update - old->last_update;
                        old->last_update = last_update;
+                       if (old->interval)
+                               old->interval = (sdb_time_t)((0.9 * (double)old->interval)
+                                               + (0.1 * interval));
+                       else
+                               old->interval = interval;
                }
 
                if (updated_obj)