Class: StreamStats::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/stream_stats/stream.rb,
ext/stream_stats/stream_stats.c

Constant Summary collapse

ATTRIBUTE_LIST =
[:count, :sum, :min, :max, :mean, :stddev]

Instance Method Summary collapse

Constructor Details

#initialize(rb_eps, rb_quantiles) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/stream_stats/stream_stats.c', line 13

static VALUE strstat_timer_init(VALUE self, VALUE rb_eps, VALUE rb_quantiles) {
  timer *i_timer;
  VALUE data;
  double eps, *quantiles;
  uint32_t num_quantiles;

  i_timer = (timer *) malloc(sizeof(timer));

  eps = NUM2DBL(rb_eps);

  switch (TYPE(rb_quantiles)) {
    case T_ARRAY:
      rb_iv_set(self, "@quantiles", rb_quantiles);
      num_quantiles = RARRAY_LEN(rb_quantiles);
      if (num_quantiles < 1)
        rb_raise(rb_eRuntimeError, "no quantiles defined");
      quantiles = malloc(sizeof(double) * num_quantiles);
      for (int i = 0; i < num_quantiles; i++) {
        quantiles[i] = NUM2DBL(rb_ary_entry(rb_quantiles, i));
      }
      break;
    default:
      /* raise exception */
      rb_raise(rb_eTypeError, "not valid value");
      break;
  }

  if (init_timer(eps, quantiles, num_quantiles, i_timer) != 0)
    rb_raise(rb_eArgError, "failed to initialize");

  data = Data_Wrap_Struct(timer_class, NULL, strstat_timer_free, i_timer);
  rb_ivar_set(self, rb_intern("internal_struct"), data);

  return Qnil;
}

Instance Method Details

#<<(rb_sample) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/stream_stats/stream_stats.c', line 58

static VALUE strstat_timer_add_sample(VALUE self, VALUE rb_sample) {

  double sample;
  timer *i_timer;
  int returned;

  sample = NUM2DBL(rb_sample);
  i_timer = (timer*) strstat_get_struct(self);

  returned = timer_add_sample(i_timer, sample);
  if (returned != 0) {
    rb_raise(rb_eRuntimeError, "add sample returned %d", returned);
  }

  return Qnil;
}

#countObject



75
76
77
78
79
80
81
# File 'ext/stream_stats/stream_stats.c', line 75

static VALUE strstat_timer_count(VALUE self) {
  timer *i_timer;

  i_timer = (timer*) strstat_get_struct(self);

  return LONG2NUM(timer_count(i_timer));
}

#get_quantilesObject



6
7
8
9
10
# File 'lib/stream_stats/stream.rb', line 6

def get_quantiles
  Hash[@quantiles.map do |q|
    [q, quantile(q)]
  end]
end

#inspectObject



12
13
14
15
16
17
# File 'lib/stream_stats/stream.rb', line 12

def inspect
  attr_list = ATTRIBUTE_LIST.map do |method|
    "#{method.to_s}: #{self.send(method)}"
  end * ', '
  "#{self.to_s} {#{attr_list}, quantiles: #{get_quantiles.to_s}}"
end

#maxObject



115
116
117
# File 'ext/stream_stats/stream_stats.c', line 115

static VALUE strstat_timer_max(VALUE self) {
  return strstat_timer_commoncall(self, timer_max);
}

#meanObject



118
119
120
# File 'ext/stream_stats/stream_stats.c', line 118

static VALUE strstat_timer_mean(VALUE self) {
  return strstat_timer_commoncall(self, timer_mean);
}

#minObject



112
113
114
# File 'ext/stream_stats/stream_stats.c', line 112

static VALUE strstat_timer_min(VALUE self) {
  return strstat_timer_commoncall(self, timer_min);
}

#percentile(rb_percentile) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'ext/stream_stats/stream_stats.c', line 95

static VALUE strstat_timer_percentile(VALUE self, VALUE rb_percentile) {
  int percentile;

  percentile = NUM2INT(rb_percentile);
  if (percentile < 0 || percentile > 100)
    rb_raise(rb_eRuntimeError, "invalid percentile");

  return strstat_timer_query(self, DBL2NUM(percentile / 100.0));
}

#quantile(rb_query) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'ext/stream_stats/stream_stats.c', line 83

static VALUE strstat_timer_query(VALUE self, VALUE rb_query) {
  double query;
  timer *i_timer;

  query = NUM2DBL(rb_query);
  if (query < 0 || query > 1)
    rb_raise(rb_eRuntimeError, "invalid quantile");

  i_timer = (timer*) strstat_get_struct(self);
  return DBL2NUM(timer_query(i_timer, query));
}

#squared_sumObject



127
128
129
# File 'ext/stream_stats/stream_stats.c', line 127

static VALUE strstat_timer_squared_sum(VALUE self) {
  return strstat_timer_commoncall(self, timer_squared_sum);
}

#stddevObject



121
122
123
# File 'ext/stream_stats/stream_stats.c', line 121

static VALUE strstat_timer_stddev(VALUE self) {
  return strstat_timer_commoncall(self, timer_stddev);
}

#sumObject



124
125
126
# File 'ext/stream_stats/stream_stats.c', line 124

static VALUE strstat_timer_sum(VALUE self) {
  return strstat_timer_commoncall(self, timer_sum);
}