Skip to content
This repository
Browse code

+ analytics

  • Loading branch information...
commit 5768dd6b860f8356d749cd835b39fe7a21e9d340 1 parent b46efbb
Florian R. Hanke authored November 17, 2011
36  server/lib/picky/analytics.rb
... ...
@@ -0,0 +1,36 @@
  1
+module Picky
  2
+
  3
+  # This class is wrapped around indexes
  4
+  # and extracts useful information to be
  5
+  # displayed in beoootiful, live-updating
  6
+  # graphs.
  7
+  #
  8
+  class Analytics
  9
+
  10
+    attr_reader :indexes
  11
+
  12
+    def initialize *indexes
  13
+      @indexes = Indexes.new *indexes
  14
+    end
  15
+
  16
+    # Returns the number of tokens in all the inverted indexes.
  17
+    #
  18
+    def tokens
  19
+      total = 0
  20
+      indexes.each_bundle do |bundle|
  21
+        total += bundle.inverted.size
  22
+      end
  23
+      total
  24
+    end
  25
+
  26
+    def ids
  27
+      total = 0
  28
+      indexes.each_bundle do |bundle|
  29
+        total += bundle.inverted.inject(0) { |total, (_, values)| total + values.size }
  30
+      end
  31
+      total
  32
+    end
  33
+
  34
+  end
  35
+
  36
+end
3  server/lib/picky/categories.rb
@@ -11,9 +11,10 @@ class Categories
11 11
 
12 12
     each_delegate :cache,
13 13
                   :dump,
14  
-                  :each_category,
  14
+                  :each,
15 15
                   :empty,
16 16
                   :index,
  17
+                  :inject,
17 18
                   :reindex,
18 19
                   :reset_backend,
19 20
                   :to => :categories
15  server/lib/picky/categories_convenience.rb
... ...
@@ -0,0 +1,15 @@
  1
+module Picky
  2
+
  3
+  #
  4
+  #
  5
+  class Categories
  6
+
  7
+    each_delegate :each_bundle,
  8
+                  :to => :categories
  9
+
  10
+    def each_category &block
  11
+      categories.each &block
  12
+    end
  13
+  end
  14
+
  15
+end
16  server/lib/picky/category_convenience.rb
... ...
@@ -0,0 +1,16 @@
  1
+module Picky
  2
+
  3
+  class Category
  4
+
  5
+    def each_bundle &block
  6
+      if block
  7
+        yield exact
  8
+        yield partial
  9
+      else
  10
+        [exact, partial]
  11
+      end
  12
+    end
  13
+
  14
+  end
  15
+
  16
+end
3  server/lib/picky/index.rb
@@ -91,7 +91,8 @@ class Index
91 91
 
92 92
     delegate :[],
93 93
              :dump,
94  
-             :each_category,
  94
+             :each,
  95
+             :inject,
95 96
              :to => :categories
96 97
 
97 98
     # Create a new index with a given source.
13  server/lib/picky/index_convenience.rb
... ...
@@ -0,0 +1,13 @@
  1
+module Picky
  2
+
  3
+  #
  4
+  #
  5
+  class Index
  6
+
  7
+    delegate :each_bundle,
  8
+             :each_category,
  9
+             :to => :categories
  10
+
  11
+  end
  12
+
  13
+end
4  server/lib/picky/indexes.rb
@@ -16,11 +16,11 @@ class Indexes
16 16
              :to => :indexes
17 17
 
18 18
     each_delegate :reindex,
19  
-                  :each_category,
20 19
                   :to => :indexes
21 20
 
22  
-    def initialize
  21
+    def initialize *indexes
23 22
       clear_indexes
  23
+      indexes.each { |index| register index }
24 24
     end
25 25
 
26 26
     # Return the Indexes instance.
11  server/lib/picky/indexes_convenience.rb
... ...
@@ -0,0 +1,11 @@
  1
+module Picky
  2
+
  3
+  class Indexes
  4
+
  5
+    each_delegate :each_bundle,
  6
+                  :each_category,
  7
+                  :to => :indexes
  8
+
  9
+  end
  10
+
  11
+end
4  server/lib/picky/loader.rb
@@ -209,20 +209,24 @@ def self.load_user_interface
209 209
       load_relative 'category_indexed'
210 210
       load_relative 'category_indexing'
211 211
       load_relative 'category_realtime'
  212
+      load_relative 'category_convenience'
212 213
 
213 214
       load_relative 'categories'
214 215
       load_relative 'categories_indexed'
215 216
       load_relative 'categories_indexing'
216 217
       load_relative 'categories_realtime'
  218
+      load_relative 'categories_convenience'
217 219
 
218 220
       load_relative 'indexes'
219 221
       load_relative 'indexes_indexed'
220 222
       load_relative 'indexes_indexing'
  223
+      load_relative 'indexes_convenience'
221 224
 
222 225
       load_relative 'index'
223 226
       load_relative 'index_indexed'
224 227
       load_relative 'index_indexing'
225 228
       load_relative 'index_realtime'
  229
+      load_relative 'index_convenience'
226 230
 
227 231
       # Results.
228 232
       #
65  server/spec/lib/analytics_spec.rb
... ...
@@ -0,0 +1,65 @@
  1
+# encoding: utf-8
  2
+#
  3
+require 'spec_helper'
  4
+
  5
+# Not loaded by default.
  6
+#
  7
+require File.expand_path '../../../lib/picky/analytics', __FILE__
  8
+
  9
+describe Picky::Analytics do
  10
+
  11
+  attr_reader :index1, :index2
  12
+
  13
+  Item = Struct.new :id, :text
  14
+
  15
+  before(:all) do
  16
+    @index1 = Picky::Index.new :index1 do
  17
+      source [
  18
+        Item.new(1, 'test one'),
  19
+        Item.new(2, 'test two'),
  20
+      ]
  21
+      category :text
  22
+    end
  23
+    @index1.index
  24
+    @index1
  25
+
  26
+    @index2 = Picky::Index.new :index2 do
  27
+      source [
  28
+        Item.new(3, 'test three'),
  29
+        Item.new(4, 'test four'),
  30
+      ]
  31
+      category :text
  32
+    end
  33
+    @index2.index
  34
+    @index2
  35
+  end
  36
+
  37
+  let(:analytics) { described_class.new index1, index2 }
  38
+
  39
+  it 'can be initialized' do
  40
+    analytics # La-zee
  41
+  end
  42
+
  43
+  it 'saves the indexes' do
  44
+    analytics.indexes.should be_kind_of(Picky::Indexes)
  45
+  end
  46
+
  47
+  describe 'tokens' do
  48
+    it 'offers the method' do
  49
+      analytics.tokens
  50
+    end
  51
+    it 'calculates the number of tokens correctly' do
  52
+      analytics.tokens.should == 24
  53
+    end
  54
+  end
  55
+
  56
+  describe 'ids' do
  57
+    it 'offers the method' do
  58
+      analytics.ids
  59
+    end
  60
+    it 'calculates the number of ids correctly' do
  61
+      analytics.ids.should == 32
  62
+    end
  63
+  end
  64
+
  65
+end

0 notes on commit 5768dd6

Please sign in to comment.
Something went wrong with that request. Please try again.