Skip to content

Commit 5cda5ca

Browse files
committed
Added deviation attribute to chain.device
1 parent 2ff494c commit 5cda5ca

File tree

4 files changed

+150
-45
lines changed

4 files changed

+150
-45
lines changed

Diff for: chain.device/chain.device.c

+37-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
#include "chainlib.h"
1616
#include "chainevent.h"
1717
#include "chainworker.h"
18+
#include "chainmath.h"
19+
#include "chainquery.h"
1820

1921
typedef struct chain_device
2022
{
2123
t_chain_worker s_worker;
2224
t_symbol *s_device_name;
2325
long s_autoupdate;
26+
long s_deviation;
2427
void *s_outlet;
2528
void *s_outlet2;
2629
} t_chain_device;
@@ -76,6 +79,7 @@ int C74_EXPORT main(void)
7679
CLASS_ATTR_ACCESSORS(c, "device_name", NULL, (method)chain_device_set_device_name);
7780

7881
CLASS_ATTR_LONG(c, "autoupdate", 0, t_chain_device, s_autoupdate);
82+
CLASS_ATTR_LONG(c, "deviation", 0, t_chain_device, s_deviation);
7983

8084
class_register(CLASS_BOX, c);
8185
s_chain_device_class = c;
@@ -97,6 +101,7 @@ void *chain_device_new(t_symbol *s, long argc, t_atom *argv)
97101
x->s_outlet2 = outlet_new(x, NULL);
98102
x->s_outlet = outlet_new(x, NULL);
99103
x->s_autoupdate = 1;
104+
x->s_deviation = 0;
100105

101106
attr_args_process(x, argc, argv);
102107

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

162+
double chain_device_compute_deviation(t_chain_device *x, t_symbol *metric,
163+
double value)
164+
{
165+
t_db_result *db_result = NULL;
166+
query_data_by_metric_name(x->s_worker.s_db, metric->s_name, &db_result);
167+
168+
long numrecords = db_result_numrecords(db_result);
169+
double values[numrecords];
170+
for (long i=0; i<numrecords; i++){
171+
values[i] = db_result_float(db_result, i, 0);
172+
}
173+
double mean = chain_mean(values, numrecords);
174+
double std = chain_std(values, numrecords);
175+
176+
return (value - mean) / std;
177+
}
178+
157179
void chain_device_send_metric(t_chain_device *x, t_symbol *metric){
158180
if(!x->s_worker.s_db){
159181
chain_error("No DB!");
@@ -177,6 +199,10 @@ void chain_device_send_metric(t_chain_device *x, t_symbol *metric){
177199
double value = db_result_float(db_result, 0, 0);
178200
const char *timestamp = db_result_string(db_result, 0, 1);
179201

202+
if (x->s_deviation){
203+
value = chain_device_compute_deviation(x, metric, value);
204+
}
205+
180206
t_atom av[2];
181207
short ac = 2;
182208
atom_setsym(av, metric);
@@ -208,10 +234,15 @@ void chain_device_send_all(t_chain_device *x){
208234
timestamp = db_result_string(db_result, i, 1);
209235
value = db_result_float(db_result, i, 0);
210236
metric_name = db_result_string(db_result, i, 2);
237+
t_symbol *metric = gensym(metric_name);
238+
239+
if (x->s_deviation){
240+
value = chain_device_compute_deviation(x, metric, value);
241+
}
211242

212243
t_atom av[2];
213244
short ac = 2;
214-
atom_setsym(av, gensym(metric_name));
245+
atom_setsym(av, metric);
215246
atom_setfloat(av+1, value);
216247

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

329360
t_atom av[num_events];
330361
for (int i=0; i<num_events; i++){
331-
atom_setfloat(av+i, (events +i)->s_value);
362+
double value = (events +i)->s_value;
363+
if (x->s_deviation){
364+
value = chain_device_compute_deviation(x, metric, value);
365+
}
366+
atom_setfloat(av+i, value);
332367
}
333368
outlet_anything(x->s_outlet, metric, num_events, av);
334369
free(events);

Diff for: chain.device/chain.device.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
917AE2D21B27492B0007B529 /* messages.c in Sources */ = {isa = PBXBuildFile; fileRef = 917AE2D11B27492B0007B529 /* messages.c */; };
2121
917AE2D41B2762240007B529 /* queries.c in Sources */ = {isa = PBXBuildFile; fileRef = 917AE2D31B2762240007B529 /* queries.c */; };
2222
918FD3C91B4DE0DD00B07589 /* pseudoclock.c in Sources */ = {isa = PBXBuildFile; fileRef = 918FD3C81B4DE0DD00B07589 /* pseudoclock.c */; };
23+
91CA22E91B6175C70030B3F0 /* chainmath.c in Sources */ = {isa = PBXBuildFile; fileRef = 91CA22E81B6175C70030B3F0 /* chainmath.c */; };
2324
/* End PBXBuildFile section */
2425

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

4244
/* Begin PBXFrameworksBuildPhase section */
@@ -61,6 +63,7 @@
6163
917AE2D31B2762240007B529 /* queries.c */,
6264
91674A3B1B55B0D600029E21 /* chainevent.c */,
6365
9112F5701B39BB2600FBDFCB /* chainlib.c */,
66+
91CA22E81B6175C70030B3F0 /* chainmath.c */,
6467
913342EB1B4C7A78000CB78A /* requests.c */,
6568
91674A121B537C2400029E21 /* chainworker.c */,
6669
918FD3C81B4DE0DD00B07589 /* pseudoclock.c */,
@@ -155,6 +158,7 @@
155158
917AE2D01B2748F90007B529 /* commonsyms.c in Sources */,
156159
91674A3C1B55B0D600029E21 /* chainevent.c in Sources */,
157160
917AE2D21B27492B0007B529 /* messages.c in Sources */,
161+
91CA22E91B6175C70030B3F0 /* chainmath.c in Sources */,
158162
917AE2CE1B264A2D0007B529 /* chain.device.c in Sources */,
159163
913342EC1B4C7A78000CB78A /* requests.c in Sources */,
160164
);

Diff for: common/chainworker.c

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ void chain_worker_new(t_chain_worker *x, t_symbol *s, long argc, t_atom *argv){
4646
}
4747

4848
if (!site_name){
49-
chain_info("No sitename set");
5049
site_name = gensym("default_site");
5150
}
5251

0 commit comments

Comments
 (0)