Permalink
Browse files

refactoring and rearanging so that it works better

  • Loading branch information...
1 parent ae44a58 commit 6238439c0429e324e10bbc7e350562680d703a2c @dstywho dstywho committed Aug 28, 2011
Showing with 42 additions and 34 deletions.
  1. +31 −20 app/models/formula.rb
  2. +11 −14 app/views/report_definitions/show.html.erb
View
@@ -45,46 +45,57 @@ def self.parse(formula)
formula_items
end
+
+
#ajax if possible
#possibly SLOW calls Project sync_snapshots
def evaluate_by_project_datetime(project, datetime)
- evaluated_string = evaluated_string(project,datetime)
-
- result = evaluate(evaluated_string[:string])
- result[:snapshots] = evaluated_string[:snapshots]
- result[:stale_snapshots] = evaluated_string[:snapshots].select{|s| (! s.nil? ) && s.stale? }
- nil_value_snapshots = result[:snapshots].select{|s| s.value.nil? }
- result[:message] = "the #{nil_value_snapshots.length > 1 ? 'snapshot'.pluralize : 'snapshot' } for #{nil_value_snapshots.map{|s|s.metric.name}.join(',')} are null" unless nil_value_snapshots.empty?
- result
- end
-
- def evaluate(string)
result = {}
- result[:calculated_string] = string
result[:isSuccess] = true
- result[:message] = 'success'
- begin
- result[:value] = eval(string)
+ result[:message] = ''
+ result[:snapshots] = snapshots(project,datetime)
+ result[:stale_snapshots] = result[:snapshots].select{|s| (! s.nil? ) && s.stale? }
+
+ nil_value_snapshots = result[:snapshots].select{|s| s.value.nil? }
+ result[:message] = "the snapshot(s) for #{nil_value_snapshots.map{|s|s.metric.name}.join(',')} are null" unless nil_value_snapshots.empty?
+
+ begin
+ result[:calculated_string] = evaluated_string(result[:snapshots])
rescue Exception => e
result[:isSuccess] = false
- result[:message] = "error occured attempting to calculate '#{string}.'"
+ result[:message] ||= "error occured creating evaluated_string."
result[:message] = result[:message] + " #{e.message}"
result[:value] = ''
+ return result
+ end
+
+ begin
+ result[:value] = eval(result[:calculated_string])
+ rescue Exception => e
+ result[:isSuccess] = false
+ result[:message] = "error occured attempting to calculate '#{result[:calculated_string]}.'"
+ result[:message] = result[:message] + "\n#{e.message}"
+ result[:value] = ''
end
result
end
+ #SLOW
+ def snapshots(project, datetime)
+ project.snapshots(formula_items.metrics.map{|m| m.item },datetime)
+ end
+
#ajax if possible
#possibly SLOW calls Project sync_snapshots
- def evaluated_string(project,datetime)
+ def evaluated_string(snapshots)
+ raise "wrong number of snapshots, expecting #{formula_items.metrics.length} but provided #{snapshots.length}. are we missing some snapshots?" if snapshots.length != formula_items.metrics.length
to_be_evaled = to_s
- snapshots = project.snapshots(formula_items.metrics.map{|m| m.item },datetime)
-
snapshots.each do |snapshot|
+ raise if snapshot.value.nil? #"one of the snapshot, #{snapshot.metric.name}, are nil" if snapshot.value.nil?
to_be_evaled.gsub!(snapshot.metric.key, snapshot.value.to_s) unless snapshot.nil?
end
- {:snapshots => snapshots,:string => to_be_evaled}
+ to_be_evaled
end
@@ -23,7 +23,7 @@ th .formula{
display:none;
}
.hide{ display:none; }
-.popup{
+.popup,tr > td.calculated:nth-child(2) .popup{
margin-left:60px;
margin-top:-5px;
padding:10px;
@@ -38,10 +38,6 @@ th .formula{
tr > td:last-child .popup{
margin-left:-400px;
}
-tr > td:first-child .popup{/*TODO why*/
- margin-left:60px !important;
- margin-top:-5px;
-}
fieldset{
-moz-border-radius: 15px;
border-radius: 15px;
@@ -141,16 +137,17 @@ Cell.prototype.display = function(data){
var template = this.template;
template.find('.evaluated_string').text(data.calculated_string);
+
if(data.isSuccess){
template.find('.value').text(data.value);
- }
- else if(! data.isSuccess){
+ if(data.stale_snapshots.length > 0){
+ template.addClass('stale');
+ }
+ }else if(!data.isSuccess){
template.prepend(data.message);
template.addClass('error');
- }else if(data.stale_snapshots.length > 0){
- template.addClass('stale');
}
- var stale_ids = jQuery.map(data.stale_snapshots, function(stale){ return stale.metric_snapshot.metric_id; });
+
jQuery(data.snapshots).each(function(){
var snapshotTemplate = snapshotFromTemplate(this);
@@ -176,7 +173,7 @@ Cell.prototype.error = function(message){
jQuery(document).ready(function(){
jQuery('td').live('mouseover mouseout',function(e){
if(jQuery(this).has('.popup')){
- if(event.type =="mouseover"){
+ if(e.type =="mouseover"){
jQuery(this).find('.popup').show();
}else{
jQuery(this).find('.popup').hide();
@@ -199,7 +196,7 @@ jQuery(document).ready(function(){
<p id="notice"><%= notice %></p>
<div id="cell_template" class="template">
- <div><span class="label">value:</span><span class="value"></span></div>
+ <div><span class="label hide">value:</span><span class="value"></span></div>
<div class="popup">
calculation:<div class="evaluated_string"></div>
<fieldset class="snapshots"><legend>metrics used in calculation</legend></fieldset>
@@ -235,9 +232,9 @@ jQuery(document).ready(function(){
<tbody>
<% @report_definition.projects.each do |project| %>
<tr>
- <td id="<%= identify(project) %>"><%= project.name %></td>
+ <td id="<%= identify(project) %>" class="project"><%= project.name %></td>
<% evaluations.each do |evaluation| %>
- <td id="<%= cell_id(project,evaluation) %>"> </td>
+ <td id="<%= cell_id(project,evaluation) %>" class="calculated"> </td>
<% end %>
</tr>
<% end %>

0 comments on commit 6238439

Please sign in to comment.