Class: Mu::Xtractr::Views

Inherits:
Object
  • Object
show all
Defined in:
lib/mu/xtractr/views.rb,
lib/mu/xtractr/test/tc_views.rb

Overview

tutorial on how Map/Reduce works.

Defined Under Namespace

Classes: Count, Sum

Class Method Summary collapse

Class Method Details

.count(xtractr, field, url, opts = {}) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mu/xtractr/views.rb', line 140

def self.count xtractr, field, url, opts={} # :nodoc:
    field = Field.new(xtractr, field) if field.is_a? String
    name = field.name.gsub /^(pkt|flow)\./, ''
    _opts = opts.dup
    _opts[:r] = <<-EOS
        ({
            map: function(_pf) {
                _pf.values("#{name}", function(_value) {
                    if (_value) {
                        if (typeof(_value) === 'string') {
                            if (_value.length > 1024) {
                                _value = _value.slice(0,1024);
                            }
                        }
                        emit(_value, 1);
                    }
                });
            },
            reduce: function(_key, _values) {
                return sum(_values);
            }
        })
    EOS
    result = xtractr.json url, _opts
    result['rows'].map do |row| 
        Views::Count.new(xtractr, field, row['key'], row['value'])
    end.sort { |a, b| b.count <=> a.count }
end

.sum(xtractr, kfield, vfield, url, opts = {}) ⇒ Object

:nodoc:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mu/xtractr/views.rb', line 169

def self.sum xtractr, kfield, vfield, url, opts={} # :nodoc:
    kfield = Field.new(xtractr, kfield) if kfield.is_a? String
    vfield = Field.new(xtractr, vfield) if vfield.is_a? String
    kname = kfield.name.gsub /^(pkt|flow)\./, ''
    vname = vfield.name.gsub /^(pkt|flow)\./, ''
    _opts = opts.dup
    _opts[:r] = <<-EOS
        ({
            map: function(_pf) {
                var _key = _pf["#{kname}"];
                if (_key) {
                    if (typeof(_key) === 'string') {
                        if (_key.length > 1024) {
                            _key = _key.slice(0,1024);
                        }
                    }
                    _pf.values("#{vname}", function(_val) {
                        if (typeof(_val) === 'number') {
                            emit(_key, _val);
                        }
                    });
                }
            },
            reduce: function(_key, _values) {
                return sum(_values);
            }
        })
    EOS
    result = xtractr.json url, _opts
    result['rows'].map do |row| 
        Views::Sum.new(xtractr, kfield, row['key'], row['value'])
    end.sort { |a, b| b.sum <=> a.sum }
end