Permalink
Browse files

+ analytics

  • Loading branch information...
floere committed Nov 17, 2011
1 parent b46efbb commit 5768dd6b860f8356d749cd835b39fe7a21e9d340
@@ -0,0 +1,36 @@
+module Picky
+
+ # This class is wrapped around indexes
+ # and extracts useful information to be
+ # displayed in beoootiful, live-updating
+ # graphs.
+ #
+ class Analytics
+
+ attr_reader :indexes
+
+ def initialize *indexes
+ @indexes = Indexes.new *indexes
+ end
+
+ # Returns the number of tokens in all the inverted indexes.
+ #
+ def tokens
+ total = 0
+ indexes.each_bundle do |bundle|
+ total += bundle.inverted.size
+ end
+ total
+ end
+
+ def ids
+ total = 0
+ indexes.each_bundle do |bundle|
+ total += bundle.inverted.inject(0) { |total, (_, values)| total + values.size }
+ end
+ total
+ end
+
+ end
+
+end
@@ -11,9 +11,10 @@ class Categories
each_delegate :cache,
:dump,
- :each_category,
+ :each,
:empty,
:index,
+ :inject,
:reindex,
:reset_backend,
:to => :categories
@@ -0,0 +1,15 @@
+module Picky
+
+ #
+ #
+ class Categories
+
+ each_delegate :each_bundle,
+ :to => :categories
+
+ def each_category &block
+ categories.each &block
+ end
+ end
+
+end
@@ -0,0 +1,16 @@
+module Picky
+
+ class Category
+
+ def each_bundle &block
+ if block
+ yield exact
+ yield partial
+ else
+ [exact, partial]
+ end
+ end
+
+ end
+
+end
@@ -91,7 +91,8 @@ class Index
delegate :[],
:dump,
- :each_category,
+ :each,
+ :inject,
:to => :categories
# Create a new index with a given source.
@@ -0,0 +1,13 @@
+module Picky
+
+ #
+ #
+ class Index
+
+ delegate :each_bundle,
+ :each_category,
+ :to => :categories
+
+ end
+
+end
@@ -16,11 +16,11 @@ class Indexes
:to => :indexes
each_delegate :reindex,
- :each_category,
:to => :indexes
- def initialize
+ def initialize *indexes
clear_indexes
+ indexes.each { |index| register index }
end
# Return the Indexes instance.
@@ -0,0 +1,11 @@
+module Picky
+
+ class Indexes
+
+ each_delegate :each_bundle,
+ :each_category,
+ :to => :indexes
+
+ end
+
+end
@@ -209,20 +209,24 @@ def self.load_user_interface
load_relative 'category_indexed'
load_relative 'category_indexing'
load_relative 'category_realtime'
+ load_relative 'category_convenience'
load_relative 'categories'
load_relative 'categories_indexed'
load_relative 'categories_indexing'
load_relative 'categories_realtime'
+ load_relative 'categories_convenience'
load_relative 'indexes'
load_relative 'indexes_indexed'
load_relative 'indexes_indexing'
+ load_relative 'indexes_convenience'
load_relative 'index'
load_relative 'index_indexed'
load_relative 'index_indexing'
load_relative 'index_realtime'
+ load_relative 'index_convenience'
# Results.
#
@@ -0,0 +1,65 @@
+# encoding: utf-8
+#
+require 'spec_helper'
+
+# Not loaded by default.
+#
+require File.expand_path '../../../lib/picky/analytics', __FILE__
+
+describe Picky::Analytics do
+
+ attr_reader :index1, :index2
+
+ Item = Struct.new :id, :text
+
+ before(:all) do
+ @index1 = Picky::Index.new :index1 do
+ source [
+ Item.new(1, 'test one'),
+ Item.new(2, 'test two'),
+ ]
+ category :text
+ end
+ @index1.index
+ @index1
+
+ @index2 = Picky::Index.new :index2 do
+ source [
+ Item.new(3, 'test three'),
+ Item.new(4, 'test four'),
+ ]
+ category :text
+ end
+ @index2.index
+ @index2
+ end
+
+ let(:analytics) { described_class.new index1, index2 }
+
+ it 'can be initialized' do
+ analytics # La-zee
+ end
+
+ it 'saves the indexes' do
+ analytics.indexes.should be_kind_of(Picky::Indexes)
+ end
+
+ describe 'tokens' do
+ it 'offers the method' do
+ analytics.tokens
+ end
+ it 'calculates the number of tokens correctly' do
+ analytics.tokens.should == 24
+ end
+ end
+
+ describe 'ids' do
+ it 'offers the method' do
+ analytics.ids
+ end
+ it 'calculates the number of ids correctly' do
+ analytics.ids.should == 32
+ end
+ end
+
+end

0 comments on commit 5768dd6

Please sign in to comment.