dimanche 24 février 2019

Change Class based on active record db query with link_to in loop in rails?

Requesting expert help.

I have an applications table which has_many metrics At some point of time, metrics will have following kind of records.

{capture_time:"08:00:10.1234",metric_name:"log",metric_value:"OK",application_id:1} {capture_time:"09:00:10.1234",metric_name:"process",metric_value:"KO",application_id:1} {capture_time:"10:00:10.1234",metric_name:"process",metric_value:"OK",application_id:1}

{capture_time:"08:00:10.1234",metric_name:"log",metric_value:"OK",application_id:2} {capture_time:"09:00:10.1234",metric_name:"process",metric_value:"OK",application_id:2} {capture_time:"10:00:10.1234",metric_name:"process",metric_value:"KO",application_id:2}

I have a bigger loop for applications and for each application , I create buttons for each metric for that application

<% applic.metric.uniq{|p|p.metric_name}.each do |m| %>
<%= link_to m.metric_name, metrics_path(m.application_id,metric_name: m.metric_name) , :class=>"btn btn-success",:role=>"button" %>
<% end %>

enter image description here

On clicking any button it shows me records only for that metrics. For e.g. if I click on process, i see all records of that metric, which is 2 records in my case.

enter image description here

So till here its fine. What I am looking help for is two folds:

  1. How to determine latest metrics(based in capture time) for that application that is KO and then use that to change the class in link_to inside the loop. Something like this:
<% applic.metric.uniq{|p|p.metric_name}.each do |m| %>
<%= link_to m.metric_name, metrics_path(m.application_id,metric_name: m.metric_name),:class=>"btn btn-success",:role=>"button" %>    
<% end %>

Class => btn-danger if latest record for this metric was KO else btn-success

  1. Then I would want to use the combined statuses of the Metrices and change the Class for the whole Application1 box. For e.g if anyone of Process, Log, Errorcounts are all green, which means any of the latest matrices of any of 3 category is KO, then the whole Application1 box has class as "btn-danger"

like this:
enter image description here

Aucun commentaire:

Enregistrer un commentaire