flare 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/flare.gemspec +66 -0
- data/lib/flare/active_record.rb +102 -0
- data/lib/flare/collection.rb +47 -0
- data/lib/flare/configuration.rb +64 -109
- data/lib/flare/index_builder.rb +24 -0
- data/lib/flare/session.rb +142 -0
- data/lib/flare/tasks.rb +18 -0
- data/lib/flare.rb +19 -408
- data/test/helper.rb +10 -0
- data/test/test_flare.rb +7 -0
- metadata +89 -228
- checksums.yaml +0 -7
- data/CHANGELOG.md +0 -5
- data/LICENSE.txt +0 -21
- data/README.md +0 -148
- data/app/controllers/flare/application_controller.rb +0 -22
- data/app/controllers/flare/jobs_controller.rb +0 -55
- data/app/controllers/flare/requests_controller.rb +0 -73
- data/app/controllers/flare/spans_controller.rb +0 -101
- data/app/helpers/flare/application_helper.rb +0 -168
- data/app/views/flare/jobs/index.html.erb +0 -69
- data/app/views/flare/jobs/show.html.erb +0 -323
- data/app/views/flare/requests/index.html.erb +0 -120
- data/app/views/flare/requests/show.html.erb +0 -498
- data/app/views/flare/spans/index.html.erb +0 -112
- data/app/views/flare/spans/show.html.erb +0 -184
- data/app/views/layouts/flare/application.html.erb +0 -126
- data/config/routes.rb +0 -20
- data/exe/flare +0 -9
- data/lib/flare/backoff_policy.rb +0 -73
- data/lib/flare/cli/doctor_command.rb +0 -129
- data/lib/flare/cli/output.rb +0 -45
- data/lib/flare/cli/setup_command.rb +0 -404
- data/lib/flare/cli/status_command.rb +0 -47
- data/lib/flare/cli.rb +0 -50
- data/lib/flare/engine.rb +0 -43
- data/lib/flare/http_metrics_config.rb +0 -101
- data/lib/flare/metric_counter.rb +0 -45
- data/lib/flare/metric_flusher.rb +0 -124
- data/lib/flare/metric_key.rb +0 -42
- data/lib/flare/metric_span_processor.rb +0 -470
- data/lib/flare/metric_storage.rb +0 -42
- data/lib/flare/metric_submitter.rb +0 -221
- data/lib/flare/source_location.rb +0 -113
- data/lib/flare/sqlite_exporter.rb +0 -279
- data/lib/flare/storage/sqlite.rb +0 -789
- data/lib/flare/storage.rb +0 -54
- data/lib/flare/version.rb +0 -5
- data/public/flare-assets/flare.css +0 -1245
- data/public/flare-assets/images/flipper.png +0 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Michael Dwan
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
= flare
|
|
2
|
+
|
|
3
|
+
Description goes here.
|
|
4
|
+
|
|
5
|
+
== Note on Patches/Pull Requests
|
|
6
|
+
|
|
7
|
+
* Fork the project.
|
|
8
|
+
* Make your feature addition or bug fix.
|
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
|
10
|
+
future version unintentionally.
|
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
|
12
|
+
(if you want to have your own version, that is fine but
|
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
15
|
+
|
|
16
|
+
== Copyright
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2009 Michael Dwan. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'jeweler'
|
|
6
|
+
Jeweler::Tasks.new do |gem|
|
|
7
|
+
gem.name = "flare"
|
|
8
|
+
gem.summary = 'This needs to get updated'
|
|
9
|
+
gem.description = 'This needs to get updated'
|
|
10
|
+
gem.email = "mpdwan@gmail.com"
|
|
11
|
+
gem.homepage = "http://github.com/michaeldwan/flare"
|
|
12
|
+
gem.authors = ["Michael Dwan"]
|
|
13
|
+
gem.add_dependency('rsolr', '>= 0.9.6')
|
|
14
|
+
gem.add_dependency('escape', '>= 0.0.4')
|
|
15
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
17
|
+
end
|
|
18
|
+
Jeweler::GemcutterTasks.new
|
|
19
|
+
rescue LoadError
|
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
require 'rake/testtask'
|
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
|
25
|
+
test.libs << 'lib' << 'test'
|
|
26
|
+
test.pattern = 'test/**/test_*.rb'
|
|
27
|
+
test.verbose = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
begin
|
|
31
|
+
require 'rcov/rcovtask'
|
|
32
|
+
Rcov::RcovTask.new do |test|
|
|
33
|
+
test.libs << 'test'
|
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
|
35
|
+
test.verbose = true
|
|
36
|
+
end
|
|
37
|
+
rescue LoadError
|
|
38
|
+
task :rcov do
|
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
task :test => :check_dependencies
|
|
44
|
+
|
|
45
|
+
task :default => :test
|
|
46
|
+
|
|
47
|
+
require 'rake/rdoctask'
|
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
50
|
+
|
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
52
|
+
rdoc.title = "flare #{version}"
|
|
53
|
+
rdoc.rdoc_files.include('README*')
|
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
55
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.0.0
|
data/flare.gemspec
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = %q{flare}
|
|
8
|
+
s.version = "1.0.0"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Michael Dwan"]
|
|
12
|
+
s.date = %q{2009-12-29}
|
|
13
|
+
s.description = %q{This needs to get updated}
|
|
14
|
+
s.email = %q{mpdwan@gmail.com}
|
|
15
|
+
s.extra_rdoc_files = [
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.rdoc"
|
|
18
|
+
]
|
|
19
|
+
s.files = [
|
|
20
|
+
".document",
|
|
21
|
+
".gitignore",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.rdoc",
|
|
24
|
+
"Rakefile",
|
|
25
|
+
"VERSION",
|
|
26
|
+
"flare.gemspec",
|
|
27
|
+
"lib/flare.rb",
|
|
28
|
+
"lib/flare/active_record.rb",
|
|
29
|
+
"lib/flare/collection.rb",
|
|
30
|
+
"lib/flare/configuration.rb",
|
|
31
|
+
"lib/flare/index_builder.rb",
|
|
32
|
+
"lib/flare/session.rb",
|
|
33
|
+
"lib/flare/tasks.rb",
|
|
34
|
+
"test/helper.rb",
|
|
35
|
+
"test/test_flare.rb"
|
|
36
|
+
]
|
|
37
|
+
s.homepage = %q{http://github.com/michaeldwan/flare}
|
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
39
|
+
s.require_paths = ["lib"]
|
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
|
41
|
+
s.summary = %q{This needs to get updated}
|
|
42
|
+
s.test_files = [
|
|
43
|
+
"test/helper.rb",
|
|
44
|
+
"test/test_flare.rb"
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
if s.respond_to? :specification_version then
|
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
49
|
+
s.specification_version = 3
|
|
50
|
+
|
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
52
|
+
s.add_runtime_dependency(%q<rsolr>, [">= 0.9.6"])
|
|
53
|
+
s.add_runtime_dependency(%q<escape>, [">= 0.0.4"])
|
|
54
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
55
|
+
else
|
|
56
|
+
s.add_dependency(%q<rsolr>, [">= 0.9.6"])
|
|
57
|
+
s.add_dependency(%q<escape>, [">= 0.0.4"])
|
|
58
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
59
|
+
end
|
|
60
|
+
else
|
|
61
|
+
s.add_dependency(%q<rsolr>, [">= 0.9.6"])
|
|
62
|
+
s.add_dependency(%q<escape>, [">= 0.0.4"])
|
|
63
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
module Flare
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend Hook
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module Hook
|
|
8
|
+
def searchable(&block)
|
|
9
|
+
Flare.indexed_models << self unless Flare.indexed_models.include?(self)
|
|
10
|
+
|
|
11
|
+
builder = IndexBuilder.new(&block)
|
|
12
|
+
solr_index = builder.index
|
|
13
|
+
|
|
14
|
+
write_inheritable_attribute :solr_index, solr_index
|
|
15
|
+
class_inheritable_reader :solr_index
|
|
16
|
+
|
|
17
|
+
after_save :solr_save
|
|
18
|
+
after_destroy :solr_destroy
|
|
19
|
+
|
|
20
|
+
include InstanceMethods
|
|
21
|
+
extend ClassMethods
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def searchable?
|
|
25
|
+
false
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module ClassMethods
|
|
30
|
+
def search_for_ids(*args)
|
|
31
|
+
options = args.extract_options!
|
|
32
|
+
options[:types] ||= []
|
|
33
|
+
options[:types] << self
|
|
34
|
+
Flare.session.search_for_ids(*[args, options].flatten)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def search(*args)
|
|
38
|
+
options = args.extract_options!
|
|
39
|
+
options[:types] ||= []
|
|
40
|
+
options[:types] << self
|
|
41
|
+
Flare.session.search(*[args, options].flatten)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def search_count(*args)
|
|
45
|
+
options = args.extract_options!
|
|
46
|
+
options[:types] ||= []
|
|
47
|
+
options[:types] << self
|
|
48
|
+
Flare.session.count(*[args, options].flatten)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def searchable?
|
|
52
|
+
true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def rebuild_solr_index
|
|
56
|
+
total = self.count
|
|
57
|
+
count = 0
|
|
58
|
+
self.find_in_batches(:batch_size => 100) do |batch|
|
|
59
|
+
Flare.session.index(batch)
|
|
60
|
+
count += batch.length
|
|
61
|
+
printf "\r#{count}/#{total} complete"
|
|
62
|
+
STDOUT.flush
|
|
63
|
+
end
|
|
64
|
+
puts
|
|
65
|
+
Flare.session.commit
|
|
66
|
+
Flare.session.optimize
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def clear_solr_index
|
|
70
|
+
Flare.session.remove_all!(self)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
module InstanceMethods
|
|
75
|
+
def to_solr_doc
|
|
76
|
+
doc = { :id => solr_document_id, :type => self.class.name }
|
|
77
|
+
solr_index[:fields].each do |field|
|
|
78
|
+
value = send(field[:source])
|
|
79
|
+
# Need to convert dates to utc xmlschema.
|
|
80
|
+
#TODO: move this translation to rsolr gem
|
|
81
|
+
if value.respond_to?(:utc)
|
|
82
|
+
value = value.utc.xmlschema
|
|
83
|
+
end
|
|
84
|
+
doc[field[:name]] = value
|
|
85
|
+
end
|
|
86
|
+
doc
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def solr_document_id
|
|
90
|
+
"#{self.class.name}:#{self.id}"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def solr_save
|
|
94
|
+
Flare.session.index!(self)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def solr_destroy
|
|
98
|
+
Flare.session.remove!(self)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Flare
|
|
2
|
+
class Collection < ::Array
|
|
3
|
+
attr_reader :total_entries, :total_pages, :current_page, :per_page
|
|
4
|
+
attr_accessor :response
|
|
5
|
+
|
|
6
|
+
def initialize(page, per_page, total)
|
|
7
|
+
@current_page, @per_page, @total_entries = page, per_page, total
|
|
8
|
+
|
|
9
|
+
@total_pages = (@total_entries / @per_page.to_f).ceil
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.ids_from_response(response, page, per_page, options)
|
|
13
|
+
collection = self.new(page, per_page, response['response']['numFound'] || 0)
|
|
14
|
+
collection.response = response
|
|
15
|
+
collection.replace(response['response']['docs'].map {|doc| doc['id']})
|
|
16
|
+
return collection
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.create_from_response(response, page, per_page, options)
|
|
20
|
+
# raise response.inspect
|
|
21
|
+
collection = self.new(page, per_page, response['response']['numFound'] || 0)
|
|
22
|
+
collection.response = response
|
|
23
|
+
collection.replace(instantiate_objects(response))
|
|
24
|
+
return collection
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def previous_page
|
|
28
|
+
current_page > 1 ? (current_page - 1) : nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def next_page
|
|
32
|
+
current_page < total_pages ? (current_page + 1): nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def offset
|
|
36
|
+
(current_page - 1) * @per_page
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
def self.instantiate_objects(response)
|
|
41
|
+
response['response']['docs'].map do |doc|
|
|
42
|
+
type, id = doc['id'].split(':')
|
|
43
|
+
type.constantize.find(id)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/flare/configuration.rb
CHANGED
|
@@ -1,121 +1,76 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require_relative "http_metrics_config"
|
|
1
|
+
require 'yaml'
|
|
4
2
|
|
|
5
3
|
module Flare
|
|
6
4
|
class Configuration
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
# Metrics: aggregated counters in memory, flushed periodically (default: production only)
|
|
15
|
-
attr_accessor :spans_enabled
|
|
16
|
-
attr_accessor :metrics_enabled
|
|
17
|
-
attr_accessor :metrics_flush_interval # seconds between flushes (default: 60)
|
|
18
|
-
|
|
19
|
-
# Metrics HTTP submission settings
|
|
20
|
-
attr_accessor :url # URL of the Flare metrics service
|
|
21
|
-
attr_accessor :key # API key for authentication
|
|
22
|
-
attr_accessor :metrics_timeout # HTTP timeout in seconds (default: 5)
|
|
23
|
-
attr_accessor :metrics_gzip # Whether to gzip payloads (default: true)
|
|
24
|
-
|
|
25
|
-
# Default patterns to auto-subscribe to for custom instrumentation
|
|
26
|
-
# Use "app." prefix in your ActiveSupport::Notifications.instrument calls
|
|
27
|
-
DEFAULT_SUBSCRIBE_PATTERNS = %w[app.].freeze
|
|
28
|
-
|
|
29
|
-
attr_accessor :subscribe_patterns
|
|
30
|
-
|
|
31
|
-
# HTTP metrics path tracking configuration.
|
|
32
|
-
# Controls which outgoing HTTP paths are tracked with detail vs collapsed to "*".
|
|
33
|
-
attr_reader :http_metrics_config
|
|
34
|
-
|
|
35
|
-
# Enable debug logging to see what Flare is doing.
|
|
36
|
-
# Set FLARE_DEBUG=1 or configure debug: true in your initializer.
|
|
37
|
-
attr_accessor :debug
|
|
38
|
-
|
|
39
|
-
def initialize
|
|
40
|
-
@enabled = true
|
|
41
|
-
@retention_hours = 24
|
|
42
|
-
@max_spans = 10_000
|
|
43
|
-
@database_path = nil
|
|
44
|
-
@ignore_request = ->(request) { false }
|
|
45
|
-
@subscribe_patterns = DEFAULT_SUBSCRIBE_PATTERNS.dup
|
|
46
|
-
@debug = ENV["FLARE_DEBUG"] == "1"
|
|
47
|
-
@http_metrics_config = HttpMetricsConfig::DEFAULT
|
|
48
|
-
|
|
49
|
-
# Environment-based defaults:
|
|
50
|
-
# - Development: spans ON (detailed debugging), metrics ON
|
|
51
|
-
# - Production: spans OFF (too expensive), metrics ON
|
|
52
|
-
# - Test: spans OFF, metrics OFF
|
|
53
|
-
@spans_enabled = rails_development?
|
|
54
|
-
@metrics_enabled = !rails_test?
|
|
55
|
-
@metrics_flush_interval = 60 # seconds
|
|
56
|
-
|
|
57
|
-
# Metrics HTTP submission defaults
|
|
58
|
-
@url = ENV.fetch("FLARE_URL", credentials_url || "https://flare.am")
|
|
59
|
-
@key = ENV["FLARE_KEY"]
|
|
60
|
-
@metrics_timeout = 5
|
|
61
|
-
@metrics_gzip = true
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# Check if metrics can be submitted (endpoint and API key configured)
|
|
65
|
-
def metrics_submission_configured?
|
|
66
|
-
!@url.nil? && !@url.empty? &&
|
|
67
|
-
!@key.nil? && !@key.empty?
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
def database_path
|
|
71
|
-
@database_path || default_database_path
|
|
72
|
-
end
|
|
5
|
+
class << self
|
|
6
|
+
attr_reader :client, :server
|
|
7
|
+
|
|
8
|
+
def server(reload = false)
|
|
9
|
+
@server = nil if reload
|
|
10
|
+
@server ||= Server.new
|
|
11
|
+
end
|
|
73
12
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
# http.host "api.stripe.com" do |h|
|
|
78
|
-
# h.allow %r{/v1/customers}
|
|
79
|
-
# h.allow %r{/v1/charges}
|
|
80
|
-
# h.map %r{/v1/connect/[\w-]+/transfers}, "/v1/connect/:account/transfers"
|
|
81
|
-
# end
|
|
82
|
-
# http.host "api.github.com", :all
|
|
83
|
-
# end
|
|
84
|
-
def http_metrics(&block)
|
|
85
|
-
# Clone defaults on first customization so user additions merge with built-in defaults
|
|
86
|
-
if @http_metrics_config.equal?(HttpMetricsConfig::DEFAULT)
|
|
87
|
-
@http_metrics_config = @http_metrics_config.dup
|
|
13
|
+
def client(reload = false)
|
|
14
|
+
@client = nil if reload
|
|
15
|
+
@client = Client.new
|
|
88
16
|
end
|
|
89
|
-
yield @http_metrics_config
|
|
90
17
|
end
|
|
91
18
|
|
|
92
19
|
private
|
|
20
|
+
class Server
|
|
21
|
+
def initialize
|
|
22
|
+
@config = YAML::load_file(File.join(Rails.root, 'config', 'solr.yml'))[Rails.env]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def port
|
|
26
|
+
@port ||= (@config['port'] || 8983).to_i
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def log_dir
|
|
30
|
+
@log_dir ||= File.expand_path(@config['log_dir'] || 'log')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def log_level
|
|
34
|
+
@log_level ||= @config['log_level'] || 'INFO'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def data_dir
|
|
38
|
+
@data_dir ||= File.expand_path(@config['data_dir'] || File.join('solr', 'data', Rails.env))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def solr_home
|
|
42
|
+
@solr_home ||= File.expand_path(@config['solr_home'] || File.join(File.dirname(__FILE__), '..', '..', 'solr', 'solr'))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def pid_dir
|
|
46
|
+
@pid_dir ||= File.expand_path(@config['pid_dir'] || 'tmp/pids')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def jvm_options
|
|
50
|
+
@jvm_options ||= @config['jvm_options']
|
|
51
|
+
end
|
|
52
|
+
end
|
|
93
53
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
115
|
-
Rails.root.join("db", "flare.sqlite3").to_s
|
|
116
|
-
else
|
|
117
|
-
"flare.sqlite3"
|
|
54
|
+
class Client
|
|
55
|
+
def initialize
|
|
56
|
+
@config = YAML::load_file(File.join(Rails.root, 'config', 'flare.yml'))[Rails.env]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def port
|
|
60
|
+
@port ||= (@config['port'] || 8983).to_i
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def host
|
|
64
|
+
@host ||= @config['host'] || '127.0.0.1'
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def path
|
|
68
|
+
@path ||= @config['path'] || 'solr'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def url
|
|
72
|
+
@url ||= "http://#{host}:#{port}/#{path}"
|
|
73
|
+
end
|
|
118
74
|
end
|
|
119
|
-
end
|
|
120
75
|
end
|
|
121
76
|
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Flare
|
|
2
|
+
class IndexBuilder
|
|
3
|
+
attr_reader :fields
|
|
4
|
+
|
|
5
|
+
def initialize(&block)
|
|
6
|
+
@fields = []
|
|
7
|
+
self.instance_eval(&block) if block_given?
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def index
|
|
11
|
+
{ :fields => @fields }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
def field(*args)
|
|
16
|
+
field, options = args.first, args.extract_options!
|
|
17
|
+
@fields << {
|
|
18
|
+
:source => field,
|
|
19
|
+
:name => options[:as] || field,
|
|
20
|
+
:boost => options[:boost] || nil
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
module Flare
|
|
2
|
+
class Session
|
|
3
|
+
RESULT_LIMIT = 1000
|
|
4
|
+
PER_PAGE = 16
|
|
5
|
+
|
|
6
|
+
def connection
|
|
7
|
+
@connection ||= RSolr.connect(:url => Flare::Configuration.client.url)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
delegate :commit, :optimize, :to => :connection
|
|
11
|
+
|
|
12
|
+
def search_for_ids(*args)
|
|
13
|
+
response = execute(*args)
|
|
14
|
+
Flare::Collection.ids_from_response(response, response[:request][:page], response[:request][:per_page], response[:request])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def search(*args)
|
|
18
|
+
response = execute(*args)
|
|
19
|
+
Flare::Collection.create_from_response(response, response[:request][:page], response[:request][:per_page], response[:request])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def count(*args)
|
|
23
|
+
execute(*args)[:response][:numFound]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def index(*objects)
|
|
27
|
+
objects = ensure_searchable(objects)
|
|
28
|
+
connection.add(objects.collect(&:to_solr_doc))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def index!(*objects)
|
|
32
|
+
index(objects)
|
|
33
|
+
commit
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def remove(*objects)
|
|
37
|
+
objects = ensure_searchable(objects)
|
|
38
|
+
connection.delete_by_id(objects.collect(&:solr_document_id))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def remove!(*objects)
|
|
42
|
+
remove(objects)
|
|
43
|
+
commit
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def remove_all(*types)
|
|
47
|
+
types = Array(types).flatten.select { |type| type.searchable? }
|
|
48
|
+
connection.delete_by_query(types.map { |type| "type:#{type.name}" }.join(' OR '))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def remove_all!(*types)
|
|
52
|
+
remove_all(types)
|
|
53
|
+
commit
|
|
54
|
+
optimize
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
def execute(*args)
|
|
59
|
+
options = args.extract_options!
|
|
60
|
+
|
|
61
|
+
options.reverse_merge!({
|
|
62
|
+
:page => 1,
|
|
63
|
+
:per_page => PER_PAGE,
|
|
64
|
+
:limit => RESULT_LIMIT,
|
|
65
|
+
:fields => '* score',
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
options.assert_valid_keys(:filter, :types, :page, :per_page, :limit, :fields, :order, :facet)
|
|
69
|
+
|
|
70
|
+
query = {
|
|
71
|
+
:q => args,
|
|
72
|
+
:fq => Array(options[:filter]).flatten,
|
|
73
|
+
:fl => options[:fields],
|
|
74
|
+
:start => start = (options[:page] -1) * options[:per_page],
|
|
75
|
+
:rows => options[:per_page]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if options[:facets]
|
|
79
|
+
query[:facets] = true
|
|
80
|
+
query['facet.field'] = options[:facets][:fields]
|
|
81
|
+
query['facet.query'] = options[:facets][:queries]
|
|
82
|
+
query['facet.mincount'] = options[:facets][:mincount] || 1
|
|
83
|
+
query["facet.limit"] = options[:facets][:limit]
|
|
84
|
+
|
|
85
|
+
query["facet.missing"] = @params[:facets][:missing]
|
|
86
|
+
query["facet.mincount"] = @params[:facets][:mincount]
|
|
87
|
+
query["facet.prefix"] = @params[:facets][:prefix]
|
|
88
|
+
query["facet.offset"] = @params[:facets][:offset]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
if options[:types]
|
|
93
|
+
query[:fq] << Array(options[:types]).map {|type| "type:#{type}"}.join(" OR ")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
query[:q] = query.delete(:fq) if query[:q].blank?
|
|
97
|
+
|
|
98
|
+
::ActiveRecord::Base.logger.debug(<<-SOLR.squish)
|
|
99
|
+
\e[4;32mSolr Query:\e[0;1m
|
|
100
|
+
#{query[:q].join(', ')}
|
|
101
|
+
#{"(#{query[:fq].join(' AND ')})," if query[:fq] }
|
|
102
|
+
sort: #{query[:order]}
|
|
103
|
+
start: #{query[:start]},
|
|
104
|
+
rows: #{query[:rows]}
|
|
105
|
+
SOLR
|
|
106
|
+
|
|
107
|
+
response = connection.select(query)
|
|
108
|
+
response[:request] = query
|
|
109
|
+
response[:request][:page] = options[:page]
|
|
110
|
+
response[:request][:per_page] = options[:per_page]
|
|
111
|
+
response.with_indifferent_access
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def ensure_searchable(*objects)
|
|
115
|
+
Array(objects).flatten.select { |object| object.class.searchable? }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def prepare_query(query)
|
|
119
|
+
query[:page] ||= query[:page] ? query[:page].to_i : 1
|
|
120
|
+
query[:per_page] ||= PER_PAGE
|
|
121
|
+
query[:limit] ||= RESULT_LIMIT
|
|
122
|
+
query[:fields] ||= '* score'
|
|
123
|
+
query
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# I dont like this... Can we move it to hash if a library like ActiveSupport doesn't already have it?
|
|
127
|
+
def symbolize_keys(hash)
|
|
128
|
+
hash.inject({}){|result, (key, value)|
|
|
129
|
+
new_key = case key
|
|
130
|
+
when String then key.to_sym
|
|
131
|
+
else key
|
|
132
|
+
end
|
|
133
|
+
new_value = case value
|
|
134
|
+
when Hash then symbolize_keys(value)
|
|
135
|
+
else value
|
|
136
|
+
end
|
|
137
|
+
result[new_key] = new_value
|
|
138
|
+
result
|
|
139
|
+
}
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|