Project

General

Profile

« Previous | Next » 

Revision bf89a7f3

Added by Robert Birnie over 11 years ago

  • ID bf89a7f3e86c08eb1cd38ae473a0edc754f8131e

fix #1925 single value trends pages

View differences:

.gitignore
.idea
*.pyc
public/apipie-cache
.DS_Store
app/controllers/trends_controller.rb
before_filter :find_trend, :only => %w{show edit update destroy}
def index
@trends = Trend.group(:trendable_type, :fact_name).where(:fact_value => nil).includes(:trendable).paginate(:page => params[:page])
@trends = Trend.group(:trendable_type, :fact_name).where(:fact_value => nil).includes(:trendable).sort_by {|e| e.type_name.downcase }.paginate(:page => params[:page])
end
def new
......
end
def show
if @trend.fact_value == nil
@title = "#{@trend.type_name.camelcase}"
else # Display single Trend
@trends= Trend.has_value.where(:id => @trend.id)
if @trend.is_a?(FactTrend)
@hosts = FactValue.my_facts.no_timestamp_facts.search_for(params[:search]).required_fields.sort_by {|e| e.host }
else
@hosts = @trend.trendable.hosts.find(:all, :order => 'name')
end
@title = "#{@trend.type_name.camelcase} - #{@trend.fact_value}"
end
end
def create
......
private
def find_trend
@trend = Trend.includes(:trendable).find(params[:id])
@trend = Trend.find(params[:id])
@trends = Trend.includes(:trendable).find(params[:id]).values
end
end
app/helpers/trends_helper.rb
end
def chart_data trend, from = 30.day.ago, to = Time.now
values = trend.values
values = trend
labels = {}
values.includes(:trendable).each {|v| labels[v.id] = v.to_label}
values.each {|v| labels[v.id] = v.to_label}
values.includes(:trend_counters).where(["trend_counters.created_at > ?", from]).order("trend_counters.created_at").map do |value|
data = value.trend_counters.map { |t| [t.created_at.to_i*1000, t.count] }
app/models/fact_trend.rb
end
def self.create_values(fact_name_id)
FactValue.where(:fact_name_id => fact_name_id).includes(:fact_name).map do |fact|
FactValue.group(:fact_name_id, :value).where(:fact_name_id => fact_name_id).includes(:fact_name).map do |fact|
create(:trendable_type => "FactName",
:trendable_id => fact.fact_name.id,
:fact_name => fact.fact_name.name,
app/models/trend.rb
belongs_to :trendable, :polymorphic => true
has_many :trend_counters, :dependent => :destroy
scope :has_value, where('fact_value IS NOT NULL')
scope :has_value, where('fact_value IS NOT NULL').order("LOWER(fact_value)")
scope :types, where('fact_value IS NULL')
after_save :create_values, :if => lambda{ |o| o.fact_value == nil }
app/views/trends/_fact_hosts.html.erb
<% @hosts.in_groups(4, false) do |group| %>
<div class="span3">
<td>
<% group.each do |fact| -%>
<ul class="base">
<li>
<%= link_to(fact.host, host_path(:id => fact.host), :title => "Show Host") %>
</li>
</ul>
<% end %>
</td>
</div>
<% end -%>
app/views/trends/_foreman_hosts.html.erb
<% @hosts.in_groups(4, false) do |group| %>
<div class="span3">
<td>
<% group.each do |host| -%>
<ul class="base">
<li>
<%= link_to(host, host_path(:host_id => host), :title => "Show Host") %>
</li>
</ul>
<% end %>
</td>
</div>
<% end -%>
app/views/trends/_trends.html.erb
<% @trends.in_groups(4, false) do |group| %>
<div class="span3">
<td>
<% group.each do |trend| -%>
<ul class="base">
<li>
<% opts = trend.name? ? trend.name : trend.fact_value %>
<table class="table table-condensed table-layout: fixed">
<tr>
<td style="width: 80%; border-top: none" >
<%= link_to opts, trend_path(:id => trend.id, :search => "facts.#{trend.fact_name} = \"#{trend.fact_value}\""), :title => "Details" %>
</td>
<td style="width: 20%; border-top: none">
<% unless trend.trend_counters.blank? %><%= trend.trend_counters.last.count %><% end %>
</td>
</tr>
</table>
</li>
</ul>
<% end %>
</td>
</div>
<% end -%>
app/views/trends/index.html.erb
<%= will_paginate @trend %>
<% unless TrendCounter.unconfigured? %>
Last updated <%= time_ago_in_words TrendCounter.order(:created_at).last.created_at %> ago
<% end %>
<% end %>
app/views/trends/show.html.erb
<% title "Trends for #{trend_title(@trend)}" %>
<% content_for(:search_bar) { trend_days_filter } %>
<%= trend_chart 'trend_graph', "#{trend_title(@trend)}", "last #{range} days", chart_data(@trend, range.days.ago) %>
<%= trend_chart 'trend_graph', "#{trend_title(@trend)}", "last #{range} days", chart_data(@trends, range.days.ago) %>
<table class="table">
<th colspan="4">Values</th>
<% head = @hosts ? "Hosts" : "Values" %>
<th colspan="4"><%= head %></th>
<tr>
<% @trend.values.includes(:trendable).in_groups(4, false) do |group| %>
<div class="span3">
<td>
<% group.each do |trend| -%>
<ul class="base">
<li>
<% if trend.is_a? FactTrend %>
<%= link_to_if_authorized trend, hash_for_edit_trend_path(:id => trend), :title => "Edit Fact Display Name" %>
<% else %>
<%= trend %>
<% end %>
</li>
</ul>
<% end -%>
</td>
</div>
<% end -%>
<% if @hosts && @trend.is_a?(FactTrend) %>
<%= render 'fact_hosts' %>
<% elsif @hosts %>
<%= render 'foreman_hosts' %>
<% else %>
<%= render 'trends' %>
<% end %>
</tr>
</table>

Also available in: Unified diff