foreman_statistics 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +619 -0
- data/README.md +40 -0
- data/Rakefile +47 -0
- data/app/controllers/concerns/foreman_statistics/parameters/trend.rb +20 -0
- data/app/controllers/foreman_statistics/api/v2/statistics_controller.rb +33 -0
- data/app/controllers/foreman_statistics/api/v2/trends_controller.rb +58 -0
- data/app/controllers/foreman_statistics/react_controller.rb +19 -0
- data/app/controllers/foreman_statistics/statistics_controller.rb +24 -0
- data/app/controllers/foreman_statistics/trends_controller.rb +58 -0
- data/app/helpers/foreman_statistics/trends_helper.rb +53 -0
- data/app/models/concerns/foreman_statistics/compute_resource_decorations.rb +9 -0
- data/app/models/concerns/foreman_statistics/environment_decorations.rb +9 -0
- data/app/models/concerns/foreman_statistics/general_setting_decorations.rb +17 -0
- data/app/models/concerns/foreman_statistics/hostgroup_decorations.rb +9 -0
- data/app/models/concerns/foreman_statistics/model_decorations.rb +9 -0
- data/app/models/concerns/foreman_statistics/operatingsystem_decorations.rb +9 -0
- data/app/models/concerns/foreman_statistics/setting_decorations.rb +9 -0
- data/app/models/foreman_statistics/fact_trend.rb +57 -0
- data/app/models/foreman_statistics/foreman_trend.rb +41 -0
- data/app/models/foreman_statistics/trend.rb +38 -0
- data/app/models/foreman_statistics/trend_counter.rb +11 -0
- data/app/services/foreman_statistics/statistics.rb +21 -0
- data/app/services/foreman_statistics/statistics/base.rb +39 -0
- data/app/services/foreman_statistics/statistics/count_facts.rb +17 -0
- data/app/services/foreman_statistics/statistics/count_hosts.rb +9 -0
- data/app/services/foreman_statistics/statistics/count_numerical_fact_pair.rb +43 -0
- data/app/services/foreman_statistics/statistics/count_puppet_classes.rb +23 -0
- data/app/services/foreman_statistics/trend_importer.rb +63 -0
- data/app/views/foreman_statistics/api/v2/trends/base.json.rabl +4 -0
- data/app/views/foreman_statistics/api/v2/trends/create.json.rabl +3 -0
- data/app/views/foreman_statistics/api/v2/trends/index.json.rabl +3 -0
- data/app/views/foreman_statistics/api/v2/trends/main.json.rabl +5 -0
- data/app/views/foreman_statistics/api/v2/trends/show.json.rabl +3 -0
- data/app/views/foreman_statistics/api/v2/trends/update.json.rabl +3 -0
- data/app/views/foreman_statistics/layouts/application_react.html.erb +16 -0
- data/app/views/foreman_statistics/trends/_empty_data.html.erb +7 -0
- data/app/views/foreman_statistics/trends/_fields.html.erb +7 -0
- data/app/views/foreman_statistics/trends/_form.html.erb +8 -0
- data/app/views/foreman_statistics/trends/_hosts.html.erb +13 -0
- data/app/views/foreman_statistics/trends/edit.html.erb +46 -0
- data/app/views/foreman_statistics/trends/index.html.erb +39 -0
- data/app/views/foreman_statistics/trends/new.html.erb +4 -0
- data/app/views/foreman_statistics/trends/show.html.erb +25 -0
- data/app/views/foreman_statistics/trends/welcome.html.erb +12 -0
- data/config/routes.rb +22 -0
- data/db/migrate/20200605153005_migrate_core_types.rb +15 -0
- data/db/migrate_foreman/20121012170851_create_trends.rb +25 -0
- data/db/migrate_foreman/20121012170936_create_trend_counters.rb +14 -0
- data/db/migrate_foreman/20150202094307_add_range_to_trend_counters.rb +6 -0
- data/db/migrate_foreman/20181031155025_add_trend_counter_created_at_unique_constraint.rb +9 -0
- data/lib/foreman_statistics.rb +4 -0
- data/lib/foreman_statistics/engine.rb +104 -0
- data/lib/foreman_statistics/version.rb +3 -0
- data/lib/tasks/foreman_statistics_tasks.rake +78 -0
- data/locale/Makefile +60 -0
- data/locale/en/foreman_statistics.po +19 -0
- data/locale/foreman_statistics.pot +19 -0
- data/locale/gemspec.rb +2 -0
- data/test/factories/foreman_statistics_factories.rb +68 -0
- data/test/fixtures/permissions.yml +26 -0
- data/test/fixtures/settings.yml +6 -0
- data/test/functional/foreman_statistics/api/v2/statistics_controller_test.rb +19 -0
- data/test/functional/foreman_statistics/api/v2/trends_controller_test.rb +74 -0
- data/test/functional/foreman_statistics/statistics_controller_test.rb +23 -0
- data/test/functional/foreman_statistics/trends_controller_test.rb +115 -0
- data/test/models/foreman_statistics/trend_counter_test.rb +10 -0
- data/test/models/foreman_statistics/trend_test.rb +22 -0
- data/test/test_plugin_helper.rb +6 -0
- data/test/unit/foreman_statistics/access_permissions_test.rb +16 -0
- data/test/unit/foreman_statistics/statistics_test.rb +82 -0
- data/test/unit/foreman_statistics_test.rb +11 -0
- data/test/unit/tasks/foreman_statistics_tasks_test.rb +205 -0
- metadata +199 -0
@@ -0,0 +1,8 @@
|
|
1
|
+
<%= form_for @trend do |f| %>
|
2
|
+
<%= base_errors_for @trend %>
|
3
|
+
<% types = trendable_types %>
|
4
|
+
<%= selectable_f f, :trendable_type, types, { }, :place_holder => _('Please Select'), :onchange => 'tfm.trends.trendTypeSelected(this)' %>
|
5
|
+
<%= select_f(f, :trendable_id, FactName.no_timestamp_fact, :id, :name, { :include_blank => true }, :disabled => types.first[1] != 'FactName') %>
|
6
|
+
<%= text_f f, :name, :disabled => types.first[1] != 'FactName' %>
|
7
|
+
<%= submit_or_cancel f %>
|
8
|
+
<% end %>
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<% @trend.find_hosts.in_groups(4, false) do |group| %>
|
2
|
+
<div class="col-md-3">
|
3
|
+
<td>
|
4
|
+
<% group.each do |host| %>
|
5
|
+
<ul class="base">
|
6
|
+
<li>
|
7
|
+
<%= link_to(host, host_path(:id => host), :title => _("Show Host")) %>
|
8
|
+
</li>
|
9
|
+
</ul>
|
10
|
+
<% end %>
|
11
|
+
</td>
|
12
|
+
</div>
|
13
|
+
<% end %>
|
@@ -0,0 +1,46 @@
|
|
1
|
+
<% title _("Edit Trend %s") % @trend.to_label %>
|
2
|
+
<div class="col-md-6">
|
3
|
+
<%= form_tag trend_path, :method => :put do %>
|
4
|
+
<% if @trend.is_a?(ForemanStatistics::FactTrend) %>
|
5
|
+
<table class="table">
|
6
|
+
<thead>
|
7
|
+
<tr>
|
8
|
+
<th style='width: 40%'><%= _("Fact Name") %></th>
|
9
|
+
<th style="width: 60%"><%= _("Display Name") %></th>
|
10
|
+
</tr>
|
11
|
+
</thead>
|
12
|
+
<tbody>
|
13
|
+
<tr>
|
14
|
+
<%= fields_for "trend[]", @trend do |f| %>
|
15
|
+
<td>
|
16
|
+
<%= @trend.fact_name %>
|
17
|
+
</td>
|
18
|
+
<td>
|
19
|
+
<%= f.text_field :name %>
|
20
|
+
</td>
|
21
|
+
<% end %>
|
22
|
+
</tr>
|
23
|
+
</tbody>
|
24
|
+
</table>
|
25
|
+
<% end %>
|
26
|
+
<table class="table">
|
27
|
+
<thead>
|
28
|
+
<tr>
|
29
|
+
<th style="width: 40%">Value</th>
|
30
|
+
<th style="width: 60%"><%= _("Display Name") %></th>
|
31
|
+
</tr>
|
32
|
+
</thead>
|
33
|
+
<tbody>
|
34
|
+
<% @trend.values.each do |trend| %>
|
35
|
+
<tr>
|
36
|
+
<%= fields_for "trend[]", trend do |f| %>
|
37
|
+
<%= render 'fields', :f => f, :trend => trend %>
|
38
|
+
<% end %>
|
39
|
+
</tr>
|
40
|
+
<% end %>
|
41
|
+
</tbody>
|
42
|
+
</table>
|
43
|
+
<%= link_to(_("Cancel"), trends_path, :class => "btn btn-default") %>
|
44
|
+
<%= submit_tag _("Submit"), :class => "btn-primary btn" %>
|
45
|
+
<% end %>
|
46
|
+
</div>
|
@@ -0,0 +1,39 @@
|
|
1
|
+
<%= javascript 'charts' %>
|
2
|
+
<% title _("Trends") %>
|
3
|
+
<% title_actions new_link(_("Add Trend Counter")),
|
4
|
+
documentation_button('4.1.3Trends') %>
|
5
|
+
<% if @trends.empty? %>
|
6
|
+
<%= alert :class => 'alert-info', :header => _("No trend counter defined"),
|
7
|
+
:text => (_("To define trend counters, use the Add Trend Counter button.</br> To start collecting trend data, set a cron job to execute 'foreman-rake foreman_statistics:trends:counter' at least every %s minutes.") % Setting[:outofsync_interval]).html_safe %>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<% if @trends.any? and ForemanStatistics::TrendCounter.unconfigured? %>
|
11
|
+
<%= alert :class => 'alert-info', :header => _("No trend counter found"),
|
12
|
+
:text => (_("To start collecting trend data, set a cron job to execute <span class='black'>foreman-rake foreman_statistics:trends:counter</span> at least every %s minutes.") % Setting[:outofsync_interval]).html_safe %>
|
13
|
+
<% end %>
|
14
|
+
|
15
|
+
<table class="<%= table_css_classes 'table-fixed' %>">
|
16
|
+
<thead>
|
17
|
+
<tr>
|
18
|
+
<th class='col-md-10'><%= _("Name") %></th>
|
19
|
+
<th class='col-md-2'><%= _("Action") %></th>
|
20
|
+
</tr>
|
21
|
+
</thead>
|
22
|
+
<tbody>
|
23
|
+
<% @trends.each do |trend| %>
|
24
|
+
<tr>
|
25
|
+
<td class="ellipsis"><%= link_to trend.to_label, trend_path(:id => trend), :title => _("Show Trends") %></td>
|
26
|
+
<td>
|
27
|
+
<%= action_buttons(
|
28
|
+
display_link_if_authorized(_("Edit"), hash_for_edit_trend_path(:id => trend)),
|
29
|
+
display_delete_if_authorized(hash_for_trend_path(:id => trend), :data => { :confirm => _("Delete all the trend history for %s?") % trend.type_name })) %>
|
30
|
+
</td>
|
31
|
+
</tr>
|
32
|
+
<% end %>
|
33
|
+
</tbody>
|
34
|
+
</table>
|
35
|
+
<%= will_paginate_with_info @trends %>
|
36
|
+
<% unless ForemanStatistics::TrendCounter.unconfigured? %>
|
37
|
+
<%= _("Last update:") %>
|
38
|
+
<%= date_time_relative(ForemanStatistics::TrendCounter.order(:created_at).last.created_at) %>
|
39
|
+
<% end %>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<%= javascript 'charts' %>
|
2
|
+
<% title(_("Trends for %s") % trend_title(@trend)) %>
|
3
|
+
<% content_for(:search_bar) { trend_days_filter(@trend) } %>
|
4
|
+
<div class="row">
|
5
|
+
<div class="stats-well col-md-12">
|
6
|
+
<h4 class="ca"><%= "#{trend_title(@trend)}" %></h4>
|
7
|
+
<h6 class="ca"><%= n_("last %s day", "last %s days", range) % range %></h6>
|
8
|
+
<%= flot_chart 'trend_graph','' ,_('Number of Hosts') , chart_data(@trend, range.days.ago), :class=>'statistics-chart stack', :legend => 'external' %>
|
9
|
+
</div>
|
10
|
+
</div>
|
11
|
+
<div id="legendContainer"></div>
|
12
|
+
<table class="table">
|
13
|
+
<% unless @trend.fact_value.nil? %>
|
14
|
+
<thead>
|
15
|
+
<tr>
|
16
|
+
<th colspan="4"><%= _("Hosts") %></th>
|
17
|
+
</tr>
|
18
|
+
</thead>
|
19
|
+
<tbody>
|
20
|
+
<tr>
|
21
|
+
<%= render 'hosts' %>
|
22
|
+
</tr>
|
23
|
+
</tbody>
|
24
|
+
<% end %>
|
25
|
+
</table>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<% content_for(:title, _("Trends")) %>
|
2
|
+
<div class="blank-slate-pf">
|
3
|
+
<div class="blank-slate-pf-icon">
|
4
|
+
<%= icon_text("area-chart", "", :kind => "fa") %>
|
5
|
+
</div>
|
6
|
+
<h1><%= _('Trends') %></h1>
|
7
|
+
<p><%= _("Trends in Foreman allow you to track changes in your infrastructure over time. It allows you to track both Foreman related information and to any fact. The Trend pages give a graph of how the number of hosts with that value have changed over time, and list the current hosts.") %></p>
|
8
|
+
<p><%= link_to _('Learn more about this in the documentation.'), documentation_url("4.1.3Trends")%></p>
|
9
|
+
<div class="blank-slate-pf-main-action">
|
10
|
+
<%= new_link(_("Add Trend Counter"), :class => 'btn-lg') %>
|
11
|
+
</div>
|
12
|
+
</div>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
ForemanStatistics::Engine.routes.draw do
|
2
|
+
resources :trends do
|
3
|
+
collection do
|
4
|
+
post 'count'
|
5
|
+
get 'help', :action => :welcome
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
resources :statistics, :only => %i[index show], constraints: ->(req) { req.format == :json }
|
10
|
+
match 'statistics' => 'react#index', :via => :get
|
11
|
+
|
12
|
+
namespace :api, defaults: { format: 'json' } do
|
13
|
+
scope '(:apiv)', module: :v2, defaults: { apiv: 'v2' }, apiv: /v1|v2/, constraints: ApiConstraints.new(version: 2, default: true) do
|
14
|
+
resources :trends, :only => %i[create index show destroy]
|
15
|
+
resources :statistics, :only => [:index]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Foreman::Application.routes.draw do
|
21
|
+
mount ForemanStatistics::Engine, at: '/foreman_statistics'
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class MigrateCoreTypes < ActiveRecord::Migration[6.0]
|
2
|
+
def up
|
3
|
+
Permission.where(:resource_type => 'Trend').update_all(:resource_type => 'ForemanStatistics::Trend')
|
4
|
+
%w[ForemanTrend FactTrend Trend].each do |t|
|
5
|
+
Trend.where(:type => t).update_all(:type => "ForemanStatistics::#{t}")
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def down
|
10
|
+
Permission.where(:resource_type => 'ForemanStatistics::Trend').update_all(:resource_type => 'Trend')
|
11
|
+
%w[ForemanTrend FactTrend Trend].each do |t|
|
12
|
+
Trend.where(:type => "ForemanStatistics::#{t}").update_all(:type => t)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class CreateTrends < ActiveRecord::Migration[6.0]
|
2
|
+
def up
|
3
|
+
create_table :trends do |t|
|
4
|
+
t.string :trendable_type, :limit => 255
|
5
|
+
t.integer :trendable_id
|
6
|
+
t.string :name, :limit => 255
|
7
|
+
t.string :type, :limit => 255
|
8
|
+
t.string :fact_value, :limit => 255
|
9
|
+
t.string :fact_name, :limit => 255
|
10
|
+
|
11
|
+
t.timestamps null: true
|
12
|
+
end
|
13
|
+
add_index :trends, :type
|
14
|
+
add_index :trends, %i[trendable_type trendable_id]
|
15
|
+
add_index :trends, :fact_value
|
16
|
+
end
|
17
|
+
|
18
|
+
def down
|
19
|
+
remove_index :trends, :type
|
20
|
+
remove_index :trends, %i[trendable_type trendable_id]
|
21
|
+
remove_index :trends, :fact_value
|
22
|
+
|
23
|
+
drop_table :trends
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateTrendCounters < ActiveRecord::Migration[6.0]
|
2
|
+
def up
|
3
|
+
create_table :trend_counters do |t|
|
4
|
+
t.references :trend, foreign_key: true
|
5
|
+
t.integer :count
|
6
|
+
|
7
|
+
t.timestamps null: true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def down
|
12
|
+
drop_table :trend_counters
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module ForemanStatistics
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace ForemanStatistics
|
4
|
+
engine_name 'foreman_statistics'
|
5
|
+
|
6
|
+
config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
|
7
|
+
config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
|
8
|
+
config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
|
9
|
+
config.autoload_paths += Dir["#{config.root}/app/overrides"]
|
10
|
+
|
11
|
+
config.paths['db/migrate'] << 'db/migrate_foreman' if Gem::Dependency.new('', '>= 2.2').match?('', SETTINGS[:version])
|
12
|
+
|
13
|
+
# Add any db migrations
|
14
|
+
initializer 'foreman_statistics.load_app_instance_data' do |app|
|
15
|
+
ForemanStatistics::Engine.paths['db/migrate'].existent.each do |path|
|
16
|
+
app.config.paths['db/migrate'] << path
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
initializer 'foreman_statistics.register_plugin', :before => :finisher_hook do |_app|
|
21
|
+
Foreman::Plugin.register :foreman_statistics do
|
22
|
+
requires_foreman '>= 2.1.0'
|
23
|
+
|
24
|
+
# ==== Core cleanups
|
25
|
+
# TODO: clean up when this gets removed from core
|
26
|
+
delete_menu_item :top_menu, :trends
|
27
|
+
delete_menu_item :top_menu, :statistics
|
28
|
+
# ====
|
29
|
+
|
30
|
+
# Add Global JS file for extending foreman-core components and routes
|
31
|
+
register_global_js_file 'fills'
|
32
|
+
|
33
|
+
# Remove core permissions
|
34
|
+
%i[view_statistics view_trends create_trends edit_trends destroy_trends update_trends].each do |perm_name|
|
35
|
+
p = Foreman::AccessControl.permission(perm_name)
|
36
|
+
Foreman::AccessControl.remove_permission(p)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Add permissions
|
40
|
+
security_block :foreman_statistics do
|
41
|
+
permission :view_statistics, { :'foreman_statistics/react' => [:index],
|
42
|
+
:'foreman_statistics/statistics' => %i[index show],
|
43
|
+
:'foreman_statistics/api/v2/statistics' => [:index] }
|
44
|
+
|
45
|
+
permission :view_trends, { :'foreman_statistics/trends' => %i[index show welcome],
|
46
|
+
:'foreman_statistics/api/v2/trends' => %i[index show] },
|
47
|
+
:resource_type => 'ForemanStatistics::Trend'
|
48
|
+
permission :create_trends, { :'foreman_statistics/trends' => %i[new create],
|
49
|
+
:'foreman_statistics/api/v2/trends' => %i[new create] },
|
50
|
+
:resource_type => 'ForemanStatistics::Trend'
|
51
|
+
permission :edit_trends, { :'foreman_statistics/trends' => %i[edit update] },
|
52
|
+
:resource_type => 'ForemanStatistics::Trend'
|
53
|
+
permission :destroy_trends, { :'foreman_statistics/trends' => [:destroy],
|
54
|
+
:'foreman_statistics/api/v2/trends' => [:destroy] },
|
55
|
+
:resource_type => 'ForemanStatistics::Trend'
|
56
|
+
permission :update_trends, { :'foreman_statistics/trends' => [:count] },
|
57
|
+
:resource_type => 'ForemanStatistics::Trend'
|
58
|
+
end
|
59
|
+
|
60
|
+
# add_resource_permissions_to_default_roles(['ForemanStatistics::Trend'])
|
61
|
+
|
62
|
+
add_menu_item :top_menu, :trends, {
|
63
|
+
:caption => N_('Trends'),
|
64
|
+
:engine => ForemanStatistics::Engine, :parent => :monitor_menu, :after => :audits,
|
65
|
+
:url_hash => { :controller => 'foreman_statistics/trends', :action => :index }
|
66
|
+
}
|
67
|
+
|
68
|
+
add_menu_item :top_menu, :statistics, {
|
69
|
+
:caption => N_('Statistics'),
|
70
|
+
:engine => ForemanStatistics::Engine, :parent => :monitor_menu, :after => :trends,
|
71
|
+
:url_hash => { :controller => 'foreman_statistics/statistics', :action => :index }
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Include concerns in this config.to_prepare block
|
77
|
+
config.to_prepare do
|
78
|
+
::ComputeResource.include ForemanStatistics::ComputeResourceDecorations
|
79
|
+
::Environment.include ForemanStatistics::EnvironmentDecorations
|
80
|
+
::Hostgroup.include ForemanStatistics::HostgroupDecorations
|
81
|
+
::Model.include ForemanStatistics::ModelDecorations
|
82
|
+
::Operatingsystem.include ForemanStatistics::OperatingsystemDecorations
|
83
|
+
::Setting.include ForemanStatistics::SettingDecorations
|
84
|
+
::Setting::General.prepend ForemanStatistics::GeneralSettingDecorations
|
85
|
+
begin
|
86
|
+
::Setting::General.load_defaults
|
87
|
+
rescue ActiveRecord::NoDatabaseError => e
|
88
|
+
Rails.logger.warn e
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
rake_tasks do
|
93
|
+
Rake::Task['db:seed'].enhance do
|
94
|
+
ForemanStatistics::Engine.load_seed
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
initializer 'foreman_statistics.register_gettext', after: :load_config_initializers do |_app|
|
99
|
+
locale_dir = File.join(File.expand_path('../..', __dir__), 'locale')
|
100
|
+
locale_domain = 'foreman_statistics'
|
101
|
+
Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
# Tasks
|
4
|
+
namespace :foreman_statistics do
|
5
|
+
namespace :trends do
|
6
|
+
desc 'Create Trend counts'
|
7
|
+
task :counter => :environment do
|
8
|
+
ForemanStatistics::TrendImporter.update!
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Reduces amount of points for each trend group'
|
12
|
+
task :reduce => :environment do
|
13
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
14
|
+
|
15
|
+
trends = ForemanStatistics::Trend.pluck(:id)
|
16
|
+
trends_count = trends.length
|
17
|
+
current_record = 0
|
18
|
+
|
19
|
+
trends.each do |trend_id|
|
20
|
+
puts "Working on trend_id #{trend_id}, #{(current_record += 1)} of #{trends_count}" unless Rails.env.test?
|
21
|
+
|
22
|
+
current_interval = ForemanStatistics::TrendCounter.where(trend_id: trend_id).order(:created_at).first
|
23
|
+
next if current_interval.nil?
|
24
|
+
|
25
|
+
current_interval.interval_start = current_interval.created_at
|
26
|
+
while (next_interval = ForemanStatistics::TrendCounter.where(trend_id: trend_id)
|
27
|
+
.where('created_at > ? and count <> ?', current_interval.created_at, current_interval.count)
|
28
|
+
.order(:created_at).first)
|
29
|
+
current_interval.interval_end = next_interval.created_at
|
30
|
+
current_interval.save!
|
31
|
+
current_interval = next_interval
|
32
|
+
current_interval.interval_start = current_interval.created_at
|
33
|
+
end
|
34
|
+
current_interval.save!
|
35
|
+
end
|
36
|
+
|
37
|
+
ForemanStatistics::TrendCounter.unscoped.where(interval_start: nil).delete_all
|
38
|
+
|
39
|
+
puts "It took #{Process.clock_gettime(Process::CLOCK_MONOTONIC) - start} seconds to complete" unless Rails.env.test?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Tests
|
45
|
+
namespace :test do
|
46
|
+
desc 'Test ForemanStatistics'
|
47
|
+
Rake::TestTask.new(:foreman_statistics) do |t|
|
48
|
+
test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
|
49
|
+
t.libs << ['test', test_dir]
|
50
|
+
t.pattern = "#{test_dir}/**/*_test.rb"
|
51
|
+
t.verbose = true
|
52
|
+
t.warning = false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
namespace :foreman_statistics do
|
57
|
+
task :rubocop do
|
58
|
+
begin
|
59
|
+
require 'rubocop/rake_task'
|
60
|
+
RuboCop::RakeTask.new(:rubocop_foreman_statistics) do |task|
|
61
|
+
task.patterns = ["#{ForemanStatistics::Engine.root}/app/**/*.rb",
|
62
|
+
"#{ForemanStatistics::Engine.root}/lib/**/*.rb",
|
63
|
+
"#{ForemanStatistics::Engine.root}/test/**/*.rb"]
|
64
|
+
end
|
65
|
+
rescue StandardError
|
66
|
+
puts 'Rubocop not loaded.'
|
67
|
+
end
|
68
|
+
|
69
|
+
Rake::Task['rubocop_foreman_statistics'].invoke
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
Rake::Task[:test].enhance ['test:foreman_statistics']
|
74
|
+
|
75
|
+
load 'tasks/jenkins.rake'
|
76
|
+
if Rake::Task.task_defined?(:'jenkins:unit')
|
77
|
+
Rake::Task['jenkins:unit'].enhance ['test:foreman_statistics', 'foreman_statistics:rubocop']
|
78
|
+
end
|