Skip to content

Commit

Permalink
Added deviation attribute to chain.device
Browse files Browse the repository at this point in the history
  • Loading branch information
eflynch committed Jul 23, 2015
1 parent 2ff494c commit 5cda5ca
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 45 deletions.
39 changes: 37 additions & 2 deletions chain.device/chain.device.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
#include "chainlib.h"
#include "chainevent.h"
#include "chainworker.h"
#include "chainmath.h"
#include "chainquery.h"

typedef struct chain_device
{
t_chain_worker s_worker;
t_symbol *s_device_name;
long s_autoupdate;
long s_deviation;
void *s_outlet;
void *s_outlet2;
} t_chain_device;
Expand Down Expand Up @@ -76,6 +79,7 @@ int C74_EXPORT main(void)
CLASS_ATTR_ACCESSORS(c, "device_name", NULL, (method)chain_device_set_device_name);

CLASS_ATTR_LONG(c, "autoupdate", 0, t_chain_device, s_autoupdate);
CLASS_ATTR_LONG(c, "deviation", 0, t_chain_device, s_deviation);

class_register(CLASS_BOX, c);
s_chain_device_class = c;
Expand All @@ -97,6 +101,7 @@ void *chain_device_new(t_symbol *s, long argc, t_atom *argv)
x->s_outlet2 = outlet_new(x, NULL);
x->s_outlet = outlet_new(x, NULL);
x->s_autoupdate = 1;
x->s_deviation = 0;

attr_args_process(x, argc, argv);

Expand Down Expand Up @@ -154,6 +159,23 @@ void chain_device_send_sensor(t_chain_device *x, const char *href){
outlet_list(x->s_outlet, 0L, ac, av);
}

double chain_device_compute_deviation(t_chain_device *x, t_symbol *metric,
double value)
{
t_db_result *db_result = NULL;
query_data_by_metric_name(x->s_worker.s_db, metric->s_name, &db_result);

long numrecords = db_result_numrecords(db_result);
double values[numrecords];
for (long i=0; i<numrecords; i++){
values[i] = db_result_float(db_result, i, 0);
}
double mean = chain_mean(values, numrecords);
double std = chain_std(values, numrecords);

return (value - mean) / std;
}

void chain_device_send_metric(t_chain_device *x, t_symbol *metric){
if(!x->s_worker.s_db){
chain_error("No DB!");
Expand All @@ -177,6 +199,10 @@ void chain_device_send_metric(t_chain_device *x, t_symbol *metric){
double value = db_result_float(db_result, 0, 0);
const char *timestamp = db_result_string(db_result, 0, 1);

if (x->s_deviation){
value = chain_device_compute_deviation(x, metric, value);
}

t_atom av[2];
short ac = 2;
atom_setsym(av, metric);
Expand Down Expand Up @@ -208,10 +234,15 @@ void chain_device_send_all(t_chain_device *x){
timestamp = db_result_string(db_result, i, 1);
value = db_result_float(db_result, i, 0);
metric_name = db_result_string(db_result, i, 2);
t_symbol *metric = gensym(metric_name);

if (x->s_deviation){
value = chain_device_compute_deviation(x, metric, value);
}

t_atom av[2];
short ac = 2;
atom_setsym(av, gensym(metric_name));
atom_setsym(av, metric);
atom_setfloat(av+1, value);

outlet_list(x->s_outlet, 0L, ac, av);
Expand Down Expand Up @@ -328,7 +359,11 @@ void chain_device_data(t_chain_device *x, t_symbol *metric, long start, long end

t_atom av[num_events];
for (int i=0; i<num_events; i++){
atom_setfloat(av+i, (events +i)->s_value);
double value = (events +i)->s_value;
if (x->s_deviation){
value = chain_device_compute_deviation(x, metric, value);
}
atom_setfloat(av+i, value);
}
outlet_anything(x->s_outlet, metric, num_events, av);
free(events);
Expand Down
4 changes: 4 additions & 0 deletions chain.device/chain.device.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
917AE2D21B27492B0007B529 /* messages.c in Sources */ = {isa = PBXBuildFile; fileRef = 917AE2D11B27492B0007B529 /* messages.c */; };
917AE2D41B2762240007B529 /* queries.c in Sources */ = {isa = PBXBuildFile; fileRef = 917AE2D31B2762240007B529 /* queries.c */; };
918FD3C91B4DE0DD00B07589 /* pseudoclock.c in Sources */ = {isa = PBXBuildFile; fileRef = 918FD3C81B4DE0DD00B07589 /* pseudoclock.c */; };
91CA22E91B6175C70030B3F0 /* chainmath.c in Sources */ = {isa = PBXBuildFile; fileRef = 91CA22E81B6175C70030B3F0 /* chainmath.c */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -37,6 +38,7 @@
917AE2D11B27492B0007B529 /* messages.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = messages.c; path = ../common/messages.c; sourceTree = "<group>"; };
917AE2D31B2762240007B529 /* queries.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = queries.c; path = ../common/queries.c; sourceTree = "<group>"; };
918FD3C81B4DE0DD00B07589 /* pseudoclock.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pseudoclock.c; path = ../common/pseudoclock.c; sourceTree = "<group>"; };
91CA22E81B6175C70030B3F0 /* chainmath.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = chainmath.c; path = ../common/chainmath.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -61,6 +63,7 @@
917AE2D31B2762240007B529 /* queries.c */,
91674A3B1B55B0D600029E21 /* chainevent.c */,
9112F5701B39BB2600FBDFCB /* chainlib.c */,
91CA22E81B6175C70030B3F0 /* chainmath.c */,
913342EB1B4C7A78000CB78A /* requests.c */,
91674A121B537C2400029E21 /* chainworker.c */,
918FD3C81B4DE0DD00B07589 /* pseudoclock.c */,
Expand Down Expand Up @@ -155,6 +158,7 @@
917AE2D01B2748F90007B529 /* commonsyms.c in Sources */,
91674A3C1B55B0D600029E21 /* chainevent.c in Sources */,
917AE2D21B27492B0007B529 /* messages.c in Sources */,
91CA22E91B6175C70030B3F0 /* chainmath.c in Sources */,
917AE2CE1B264A2D0007B529 /* chain.device.c in Sources */,
913342EC1B4C7A78000CB78A /* requests.c in Sources */,
);
Expand Down
1 change: 0 additions & 1 deletion common/chainworker.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void chain_worker_new(t_chain_worker *x, t_symbol *s, long argc, t_atom *argv){
}

if (!site_name){
chain_info("No sitename set");
site_name = gensym("default_site");
}

Expand Down
Loading

0 comments on commit 5cda5ca

Please sign in to comment.