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
# 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;
  }

  init_timer(eps, quantiles, num_quantiles, i_timer);

  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



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

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



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

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



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

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

#meanObject



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

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

#minObject



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

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

#percentile(rb_percentile) ⇒ Object



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

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



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

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



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

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

#stddevObject



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

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

#sumObject



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

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