i_wonder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +24 -0
- data/README.rdoc +147 -0
- data/Rakefile +39 -0
- data/app/assets/javascripts/i_wonder/application.js +9 -0
- data/app/assets/javascripts/i_wonder/dashboard.js +2 -0
- data/app/assets/javascripts/i_wonder/events.js +2 -0
- data/app/assets/javascripts/i_wonder/metrics.js +19 -0
- data/app/assets/javascripts/i_wonder/reports.js +4 -0
- data/app/assets/stylesheets/i_wonder/_shared.css.scss +28 -0
- data/app/assets/stylesheets/i_wonder/application.css.scss +90 -0
- data/app/assets/stylesheets/i_wonder/dashboard.css.scss +6 -0
- data/app/assets/stylesheets/i_wonder/events.css.scss +10 -0
- data/app/assets/stylesheets/i_wonder/metrics.css.scss +11 -0
- data/app/assets/stylesheets/i_wonder/reports.css.scss +19 -0
- data/app/controllers/i_wonder/application_controller.rb +4 -0
- data/app/controllers/i_wonder/dashboard_controller.rb +9 -0
- data/app/controllers/i_wonder/events_controller.rb +14 -0
- data/app/controllers/i_wonder/metrics_controller.rb +49 -0
- data/app/controllers/i_wonder/reports_controller.rb +53 -0
- data/app/helpers/i_wonder/application_helper.rb +4 -0
- data/app/helpers/i_wonder/dashboard_helper.rb +4 -0
- data/app/helpers/i_wonder/events_helper.rb +4 -0
- data/app/helpers/i_wonder/metrics_helper.rb +4 -0
- data/app/helpers/i_wonder/reports_helper.rb +4 -0
- data/app/models/i_wonder/event.rb +61 -0
- data/app/models/i_wonder/metric.rb +223 -0
- data/app/models/i_wonder/report.rb +85 -0
- data/app/models/i_wonder/report_membership.rb +6 -0
- data/app/models/i_wonder/snapshot.rb +32 -0
- data/app/views/i_wonder/dashboard/landing.html.erb +5 -0
- data/app/views/i_wonder/events/index.html.erb +15 -0
- data/app/views/i_wonder/events/show.html.erb +5 -0
- data/app/views/i_wonder/metrics/_form.html.erb +66 -0
- data/app/views/i_wonder/metrics/_options_for_custom.html.erb +36 -0
- data/app/views/i_wonder/metrics/_options_for_event_counter.html.erb +6 -0
- data/app/views/i_wonder/metrics/_options_for_model_counter.html.erb +35 -0
- data/app/views/i_wonder/metrics/edit.html.erb +7 -0
- data/app/views/i_wonder/metrics/index.html.erb +25 -0
- data/app/views/i_wonder/metrics/new.html.erb +7 -0
- data/app/views/i_wonder/metrics/show.html.erb +28 -0
- data/app/views/i_wonder/reports/_form.html.erb +46 -0
- data/app/views/i_wonder/reports/_line_report.html.erb +38 -0
- data/app/views/i_wonder/reports/edit.html.erb +7 -0
- data/app/views/i_wonder/reports/index.html.erb +20 -0
- data/app/views/i_wonder/reports/new.html.erb +7 -0
- data/app/views/i_wonder/reports/show.html.erb +16 -0
- data/app/views/layouts/i_wonder.html.erb +28 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20111022230720_i_wonder_migrations.rb +84 -0
- data/lib/i_wonder/configuration.rb +31 -0
- data/lib/i_wonder/core_ext.rb +5 -0
- data/lib/i_wonder/engine.rb +23 -0
- data/lib/i_wonder/logging/middleware.rb +114 -0
- data/lib/i_wonder/logging/mixins/action_controller_mixins.rb +31 -0
- data/lib/i_wonder/logging/mixins/active_record_mixins.rb +24 -0
- data/lib/i_wonder/version.rb +3 -0
- data/lib/i_wonder.rb +18 -0
- data/lib/tasks/i_wonder_tasks.rake +8 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +9 -0
- data/test/dummy/app/assets/javascripts/highcharts.js +170 -0
- data/test/dummy/app/assets/stylesheets/application.css.scss +78 -0
- data/test/dummy/app/controllers/application_controller.rb +12 -0
- data/test/dummy/app/controllers/test_controller.rb +32 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/account.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +23 -0
- data/test/dummy/app/views/test/landing.html.erb +3 -0
- data/test/dummy/config/application.rb +45 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +11 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +30 -0
- data/test/dummy/config/environments/production.rb +60 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +11 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20111023024745_create_delayed_jobs.rb +21 -0
- data/test/dummy/db/migrate/20111023231947_create_accounts.rb +9 -0
- data/test/dummy/db/schema.rb +91 -0
- data/test/dummy/log/development.log +48142 -0
- data/test/dummy/log/test.log +28374 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/delayed_job +5 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/C41/5B0/sprockets%2F095415e1141a435237aa9464752d21fd +0 -0
- data/test/dummy/tmp/cache/assets/C5B/140/sprockets%2F5605473920cc85586d2a795b1e2a7990 +0 -0
- data/test/dummy/tmp/cache/assets/C78/210/sprockets%2F26c220614bd9237d671ec621f0428f19 +0 -0
- data/test/dummy/tmp/cache/assets/C79/390/sprockets%2F7b9a6409d013f75c40390a26006885ae +0 -0
- data/test/dummy/tmp/cache/assets/C81/600/sprockets%2F30b4f9798169b0d0157b48c09ea23472 +0 -0
- data/test/dummy/tmp/cache/assets/C94/140/sprockets%2F69a79619df29670102b726d3e7547c6f +0 -0
- data/test/dummy/tmp/cache/assets/CA6/380/sprockets%2Fe8e1b47410e6440341afc3903896bc23 +0 -0
- data/test/dummy/tmp/cache/assets/CBF/EB0/sprockets%2F279e977af0f392b36126a520569ec7d7 +0 -0
- data/test/dummy/tmp/cache/assets/CE5/650/sprockets%2F8ab512c9b33ac90e699b73469d71f061 +0 -0
- data/test/dummy/tmp/cache/assets/CE9/220/sprockets%2Fdf40ed258e785336580ff8d3f17504a2 +0 -0
- data/test/dummy/tmp/cache/assets/CFE/130/sprockets%2Fa510c5643a4c0c59b74c1746df2c007e +0 -0
- data/test/dummy/tmp/cache/assets/D20/120/sprockets%2F73fcb7f2f97642793feb2c3d871c1482 +0 -0
- data/test/dummy/tmp/cache/assets/D2B/280/sprockets%2F921f114bc5b7aa015994bdda8421ae15 +0 -0
- data/test/dummy/tmp/cache/assets/D2F/9D0/sprockets%2F50ce229bd4200bed47618724bcab6c73 +0 -0
- data/test/dummy/tmp/cache/assets/D30/9C0/sprockets%2F3c0162b1955a8f419b14bfc9c21f1bc5 +0 -0
- data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/D38/3B0/sprockets%2Fe28cd7db24b3e4719e539371a331cb3c +0 -0
- data/test/dummy/tmp/cache/assets/D3C/510/sprockets%2F59ddac34690f13d66d518c042e5af2a7 +0 -0
- data/test/dummy/tmp/cache/assets/D41/880/sprockets%2Fb9fe431461f3a6a728e7c8b57e6201eb +0 -0
- data/test/dummy/tmp/cache/assets/D52/330/sprockets%2F575f448badfc9cc6d2618370956d4bf7 +0 -0
- data/test/dummy/tmp/cache/assets/D54/ED0/sprockets%2F71c9fa01091d432b131da3bb73faf3d4 +0 -0
- data/test/dummy/tmp/cache/assets/D58/7E0/sprockets%2Fc3fefb1d72d7614af02a38ac7071c040 +0 -0
- data/test/dummy/tmp/cache/assets/D68/240/sprockets%2Fdeadb83d125366600d6a6a69d3f4a19c +0 -0
- data/test/dummy/tmp/cache/assets/D6F/B90/sprockets%2F81dd44ced51e0fe8f2615068bb619a5d +0 -0
- data/test/dummy/tmp/cache/assets/D84/210/sprockets%2Fabd0103ccec2b428ac62c94e4c40b384 +0 -0
- data/test/dummy/tmp/cache/assets/D8E/250/sprockets%2Fefe57e932e1d26c3b6af42a5311f0a1c +0 -0
- data/test/dummy/tmp/cache/assets/D97/740/sprockets%2F9b6d08ad548c1934113d1bcbd8bdc0e9 +0 -0
- data/test/dummy/tmp/cache/assets/D9E/120/sprockets%2Fae9efac5ef9a736d5772b07a06723c1a +0 -0
- data/test/dummy/tmp/cache/assets/DA3/CD0/sprockets%2Ff4833fc8289edf9e6497c7fe9c19d8d9 +0 -0
- data/test/dummy/tmp/cache/assets/DA7/BA0/sprockets%2F463a9dd811f2418d96fa72cfe05fb8cd +0 -0
- data/test/dummy/tmp/cache/assets/DAD/040/sprockets%2Fc59fd78ac2642a5f3c1b6ef899d50cf3 +0 -0
- data/test/dummy/tmp/cache/assets/DB2/850/sprockets%2F8aca540c22973c1123f4c7b0ebca5ada +0 -0
- data/test/dummy/tmp/cache/assets/DBA/4A0/sprockets%2Fffc3120e0bea9134ca5ce8ec74aa2173 +0 -0
- data/test/dummy/tmp/cache/assets/DBA/C20/sprockets%2Fa5bfa522bf4221a5bb62ed4feb5093e3 +0 -0
- data/test/dummy/tmp/cache/assets/DC7/3C0/sprockets%2F6c54b5103be38cc9ae0ef05c75a92bdf +0 -0
- data/test/dummy/tmp/cache/assets/DC8/270/sprockets%2F3ff28a9f19a1be0c377fea3d02e066db +0 -0
- data/test/dummy/tmp/cache/assets/DD1/B10/sprockets%2F85b9bce1c5bc3fb86a83d8c8c34e05e0 +0 -0
- data/test/dummy/tmp/cache/assets/DDF/BA0/sprockets%2F187ad995e2ff3588c339fdbecceba119 +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/E8D/B70/sprockets%2Fd62ab1e993c1edcadba5317fe8c7dffb +0 -0
- data/test/dummy/tmp/pids/server.pid +1 -0
- data/test/factories/metric_factory.rb +13 -0
- data/test/factories/report_factory.rb +13 -0
- data/test/factories/snapshot_factory.rb +11 -0
- data/test/functional/test_controller_test.rb +23 -0
- data/test/i_wonder_test.rb +7 -0
- data/test/integration/i_wonder/dashboard_controller_test.rb +11 -0
- data/test/integration/i_wonder/events_controller_test.rb +14 -0
- data/test/integration/i_wonder/metrics_controller_test.rb +18 -0
- data/test/integration/i_wonder/reports_controller_test.rb +11 -0
- data/test/integration/middleware_test.rb +88 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/support/extra_assertions.rb +15 -0
- data/test/test_helper.rb +16 -0
- data/test/unit/configuration_test.rb +13 -0
- data/test/unit/helpers/i_wonder/dashboard_helper_test.rb +6 -0
- data/test/unit/helpers/i_wonder/events_helper_test.rb +6 -0
- data/test/unit/helpers/i_wonder/metrics_helper_test.rb +6 -0
- data/test/unit/helpers/i_wonder/reports_helper_test.rb +6 -0
- data/test/unit/i_wonder/event_test.rb +56 -0
- data/test/unit/i_wonder/metric_collection_test.rb +124 -0
- data/test/unit/i_wonder/metric_creation_test.rb +105 -0
- data/test/unit/i_wonder/report_test.rb +33 -0
- data/test/unit/i_wonder/snapshot_test.rb +9 -0
- metadata +405 -0
@@ -0,0 +1,124 @@
|
|
1
|
+
require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module IWonder
|
4
|
+
class MetricCollectionTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "scope for metrics requiring updates" do
|
7
|
+
IWonder::Metric.delete_all
|
8
|
+
@m1 = Factory(:metric, :frequency => 1.hour, :last_measurement => nil)
|
9
|
+
@m2 = Factory(:metric, :frequency => 1.hour, :last_measurement => Time.now - 30.minutes)
|
10
|
+
@m3 = Factory(:metric, :frequency => 1.hour, :last_measurement => Time.now - 90.minutes)
|
11
|
+
@m4 = Factory(:metric, :frequency => 1.day, :last_measurement => Time.now - 90.minutes)
|
12
|
+
@m5 = Factory(:metric, :frequency => 1.day, :last_measurement => Time.now - 2.days)
|
13
|
+
@m6 = Factory(:metric, :frequency => -1, :last_measurement => nil)
|
14
|
+
@m7 = Factory(:metric, :frequency => 1.hour, :last_measurement => nil, :archived => true)
|
15
|
+
|
16
|
+
assert_equal 3, IWonder::Metric.needs_to_be_measured.count
|
17
|
+
assert_include IWonder::Metric.needs_to_be_measured, @m1
|
18
|
+
assert_include IWonder::Metric.needs_to_be_measured, @m3
|
19
|
+
assert_include IWonder::Metric.needs_to_be_measured, @m5
|
20
|
+
end
|
21
|
+
|
22
|
+
test "metric method is sandboxed in the transaction" do
|
23
|
+
IWonder::Metric.destroy_all
|
24
|
+
@report = Factory(:report)
|
25
|
+
|
26
|
+
@metric = Factory(:metric, :collection_type => "custom")
|
27
|
+
@metric.instance_eval do
|
28
|
+
def avoid_dangerous_words
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
@metric.collection_method = "IWonder::Report.find(#{@report.id}).destroy; 1"
|
33
|
+
@metric.save
|
34
|
+
assert @metric.valid?
|
35
|
+
|
36
|
+
@metric.send(:take_snapshot)
|
37
|
+
@metric.reload
|
38
|
+
|
39
|
+
assert IWonder::Report.exists?(@report.id)
|
40
|
+
assert_equal 1, @metric.snapshots.count
|
41
|
+
end
|
42
|
+
|
43
|
+
test "grabbing most recent snapshot and calculating time range" do
|
44
|
+
Timecop.freeze(2012, 10, 24) do
|
45
|
+
|
46
|
+
@metric_1 = Factory(:metric, :frequency => 1.day)
|
47
|
+
@metric_2 = Factory(:metric, :frequency => 1.day)
|
48
|
+
|
49
|
+
@snapshot_1 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 4.days)
|
50
|
+
@snapshot_2 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 3.days)
|
51
|
+
@snapshot_3 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 2.days)
|
52
|
+
|
53
|
+
assert_equal @snapshot_3, @metric_1.snapshots.most_recent
|
54
|
+
assert_nil @metric_2.snapshots.most_recent
|
55
|
+
assert_equal [Time.zone.now - 2.day, Time.zone.now - 1.day], @metric_1.send(:timeframe_for_next_snapshot), "Not calculating times from snapshot correctly"
|
56
|
+
assert_equal [Time.zone.now - 1.day, Time.zone.now], @metric_2.send(:timeframe_for_next_snapshot), "Not calculating times without snapshot correctly"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "grabbing values from snapshots in timerange (integers)" do
|
61
|
+
Timecop.freeze(2012, 10, 24) do
|
62
|
+
@metric_1 = Factory(:metric, :name => "Test Metric", :frequency => 1.day)
|
63
|
+
@snapshot_1 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 5.days, :data => 1)
|
64
|
+
@snapshot_2 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 4.days, :data => 2)
|
65
|
+
@snapshot_3 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 3.days, :data => 3)
|
66
|
+
@snapshot_4 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 2.days, :data => 4)
|
67
|
+
@snapshot_5 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 1.days, :data => 5)
|
68
|
+
|
69
|
+
|
70
|
+
res = @metric_1.value_from(Time.zone.now - 4.days, Time.zone.now - 2.days + 1.second)
|
71
|
+
assert res.eql?({"Test Metric" => 9.0})
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
test "grabbing values from snapshots in timerange (hashes)" do
|
76
|
+
Timecop.freeze(2012, 10, 24) do
|
77
|
+
@metric_1 = Factory(:metric, :name => "Test Metric", :frequency => 1.day)
|
78
|
+
@snapshot_1 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 5.days, :count => nil, :data => {"key_1" => 1})
|
79
|
+
@snapshot_2 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 4.days, :count => nil, :data => {"key_1" => 2})
|
80
|
+
@snapshot_3 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 3.days, :count => nil, :data => {"key_1" => 3, "key_2" => 1})
|
81
|
+
@snapshot_4 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 2.days, :count => nil, :data => {"key_1" => 4, "key_2" => 2})
|
82
|
+
@snapshot_5 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 1.days, :count => nil, :data => {"key_1" => 5, "key_2" => 3})
|
83
|
+
|
84
|
+
|
85
|
+
res = @metric_1.value_from(Time.zone.now - 4.days, Time.zone.now - 2.days + 1.second)
|
86
|
+
assert res.eql?({"key_1" => 9.0, "key_2" => 3.0})
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
test "grabbing values from no-snapshot collection_method" do
|
91
|
+
Timecop.freeze(2012, 10, 24) do
|
92
|
+
@metric_1 = Factory(:metric, :name => "Test Metric", :frequency => -1, :collection_method => "3")
|
93
|
+
assert !@metric_1.takes_snapshots?
|
94
|
+
|
95
|
+
res = @metric_1.value_from(Time.zone.now - 4.days, Time.zone.now - 2.days + 1.second)
|
96
|
+
assert res.eql?({"Test Metric" => 3.0}), res.inspect
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
test "merging snapshot methods (sumation vs averaging)" do
|
101
|
+
|
102
|
+
Timecop.freeze(2012, 10, 24) do
|
103
|
+
# with the default summation set
|
104
|
+
@metric_1 = Factory(:metric, :name => "Test Metric", :frequency => 1.day)
|
105
|
+
@snapshot_1 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 2.days, :data => 1)
|
106
|
+
@snapshot_2 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 1.days, :data => 3)
|
107
|
+
res = @metric_1.value_from(Time.zone.now - 2.days, Time.zone.now - 1.day + 1.second)
|
108
|
+
assert res.eql?({"Test Metric" => 4.0})
|
109
|
+
|
110
|
+
# with the default summation set
|
111
|
+
@metric_1.combination_rule = "average"
|
112
|
+
@metric_1.save
|
113
|
+
|
114
|
+
@snapshot_1 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 2.days, :data => 1)
|
115
|
+
@snapshot_2 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.zone.now - 1.days, :data => 3)
|
116
|
+
res = @metric_1.value_from(Time.zone.now - 2.days, Time.zone.now)
|
117
|
+
assert res.eql?({"Test Metric" => 2.0})
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
#TODO: check the earliest_measurement in tests above
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module IWonder
|
4
|
+
class MetricCreationTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "Check that dangerous words filtering is working" do
|
7
|
+
@metric = Factory(:metric, :frequency => 1.hour, :last_measurement => nil)
|
8
|
+
assert @metric.valid?
|
9
|
+
@metric.collection_method = "something.create"
|
10
|
+
assert !@metric.valid?
|
11
|
+
assert_include @metric.errors.full_messages.join(" "), "create"
|
12
|
+
|
13
|
+
|
14
|
+
@metric.collection_method = "where(\"created_at = ?\")"
|
15
|
+
assert @metric.valid?
|
16
|
+
end
|
17
|
+
|
18
|
+
test "frequency and takes_snapshots is being set correctly" do
|
19
|
+
@metric = Metric.create(:name => "test name")
|
20
|
+
assert_valid @metric
|
21
|
+
assert !@metric.takes_snapshots?
|
22
|
+
assert_equal -1, @metric.frequency
|
23
|
+
|
24
|
+
@metric.update_attributes(:takes_snapshots => true, :frequency => 1.hour)
|
25
|
+
assert_valid @metric
|
26
|
+
assert @metric.takes_snapshots?
|
27
|
+
assert_equal 1.hour, @metric.frequency
|
28
|
+
|
29
|
+
@metric.update_attributes(:takes_snapshots => false)
|
30
|
+
assert !@metric.takes_snapshots?
|
31
|
+
assert_equal -1, @metric.frequency
|
32
|
+
|
33
|
+
@metric.update_attributes(:takes_snapshots => true, :frequency => 1.hour)
|
34
|
+
assert_valid @metric
|
35
|
+
|
36
|
+
@metric.update_attributes(:frequency => -1)
|
37
|
+
assert_valid @metric
|
38
|
+
assert !@metric.takes_snapshots?
|
39
|
+
assert_equal -1, @metric.frequency
|
40
|
+
end
|
41
|
+
|
42
|
+
test "model scope only works for actual models and actual scopes" do
|
43
|
+
@metric = Factory(:metric, :frequency => 1.hour, :last_measurement => nil)
|
44
|
+
@metric.collection_type = "model_counter"
|
45
|
+
@metric.model_counter_class = "FakeModel"
|
46
|
+
assert !@metric.valid?
|
47
|
+
|
48
|
+
@metric.model_counter_class = "IWonder::Metric"
|
49
|
+
assert @metric.valid?
|
50
|
+
|
51
|
+
@metric.model_counter_scopes = "fake_scope"
|
52
|
+
assert !@metric.valid?
|
53
|
+
|
54
|
+
@metric.model_counter_scopes = "archived"
|
55
|
+
assert @metric.valid?
|
56
|
+
|
57
|
+
@metric.model_counter_scopes = "archived.fake_scope"
|
58
|
+
assert !@metric.valid?
|
59
|
+
|
60
|
+
@metric.model_counter_scopes = "archived.takes_snapshots"
|
61
|
+
assert @metric.valid?
|
62
|
+
end
|
63
|
+
|
64
|
+
test "set_event_collection_method works" do
|
65
|
+
@metric = Metric.create(:name => "Track Hits", :collection_type => "event_counter", :event_counter_event => "hit")
|
66
|
+
Timecop.travel(Time.zone.now - 2.days) do
|
67
|
+
Event.create(:event_type => "hit")
|
68
|
+
end
|
69
|
+
Timecop.travel(Time.zone.now - 2.hours) do
|
70
|
+
# This shouldn't make any difference, so why not throw it into the test
|
71
|
+
Event.create(:event_type => :hit)
|
72
|
+
Event.create(:event_type => "hit")
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal "IWonder::Event.where(:event_type => \"hit\").where(\"created_at >= ? AND created_at < ?\", start_time, end_time).count", @metric.collection_method
|
76
|
+
assert_equal 2, @metric.send(:run_collection_method_from, Time.zone.now-1.day, Time.zone.now)
|
77
|
+
end
|
78
|
+
|
79
|
+
test "set_model_collection_method works (both collection methods) (with and without scopes)" do
|
80
|
+
@metric = Metric.create(:name => "Track Accounts", :collection_type => "model_counter", :model_counter_class => "Account")
|
81
|
+
Timecop.travel(Time.zone.now - 2.days) do
|
82
|
+
Account.create!
|
83
|
+
end
|
84
|
+
Timecop.travel(Time.zone.now - 2.hours) do
|
85
|
+
Account.create!
|
86
|
+
Account.create!
|
87
|
+
end
|
88
|
+
|
89
|
+
assert_equal "Account.where(\"created_at >= ? AND created_at < ?\", start_time, end_time).count", @metric.collection_method
|
90
|
+
assert_equal 2, @metric.send(:run_collection_method_from, Time.zone.now-1.day, Time.zone.now)
|
91
|
+
|
92
|
+
@metric.update_attributes(:model_counter_method => "Total Number")
|
93
|
+
assert_equal "Account.count", @metric.collection_method
|
94
|
+
assert_equal 3, @metric.send(:run_collection_method_from, Time.zone.now-1.day, Time.zone.now)
|
95
|
+
|
96
|
+
@metric.update_attributes(:model_counter_scopes => ".archived")
|
97
|
+
assert_equal "Account.archived.count", @metric.collection_method
|
98
|
+
|
99
|
+
@metric.update_attributes(:model_counter_scopes => "archived")
|
100
|
+
assert_equal "Account.archived.count", @metric.collection_method
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module IWonder
|
4
|
+
class ReportTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "gathering series data for a line chart" do
|
7
|
+
Timecop.freeze(2012, 10, 24) do
|
8
|
+
@metric_1 = Factory(:metric, :name => "Test Metric 1", :frequency => 1.day)
|
9
|
+
@snapshot_11 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.now - 2.days, :data => 1)
|
10
|
+
@snapshot_12 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.now - 1.days, :data => 2)
|
11
|
+
@snapshot_13 = Factory(:snapshot, :metric => @metric_1, :created_at => Time.now, :data => 3)
|
12
|
+
|
13
|
+
@metric_2 = Factory(:metric, :name => "Test Metric 2", :frequency => 1.day)
|
14
|
+
@snapshot_22 = Factory(:snapshot, :metric => @metric_2, :created_at => Time.now - 1.days, :count => nil, :data => {"key_1" => 3, "key_2" => 1})
|
15
|
+
@snapshot_23 = Factory(:snapshot, :metric => @metric_2, :created_at => Time.now, :count => nil, :data => {"key_1" => 3, "key_2" => 2})
|
16
|
+
|
17
|
+
@report = Factory(:report, :report_type => "line", :metric_ids => [@metric_1.id, @metric_2.id])
|
18
|
+
assert_equal 2, @report.metrics.count
|
19
|
+
|
20
|
+
series_data = @report.send :collect_series_data, Time.now - 2.days, Time.now, 1.day
|
21
|
+
|
22
|
+
proper_results = [
|
23
|
+
{:name=>"Test Metric 1", :pointInterval=>86400000, :data=>[1.0, 2.0, 3.0]},
|
24
|
+
{:name=>"key_1", :pointInterval=>86400000, :data=>[0.0, 3.0, 3.0]},
|
25
|
+
{:name=>"key_2", :pointInterval=>86400000, :data=>[0.0, 1.0, 2.0]}
|
26
|
+
]
|
27
|
+
|
28
|
+
assert_equal proper_results, series_data
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|