Cleanup BenchmarkBatch a little bit
authorMarcel Hlopko <marcel@hlopko.com>
Tue, 28 Oct 2014 11:09:21 +0100
changeset 231 f0dabe3a2368
parent 230 6f2b57a50b89
child 232 3c00bf7e223f
Cleanup BenchmarkBatch a little bit
web/app/controllers/index_controller.rb
web/app/models/benchmark_batch.rb
web/app/models/benchmark_result.rb
web/app/views/index/index.html.erb
--- a/web/app/controllers/index_controller.rb	Tue Oct 28 10:39:00 2014 +0100
+++ b/web/app/controllers/index_controller.rb	Tue Oct 28 11:09:21 2014 +0100
@@ -1,4 +1,5 @@
 class IndexController < ApplicationController
+
   def index
     @current_results = BenchmarkBatch.last
   end
--- a/web/app/models/benchmark_batch.rb	Tue Oct 28 10:39:00 2014 +0100
+++ b/web/app/models/benchmark_batch.rb	Tue Oct 28 11:09:21 2014 +0100
@@ -1,16 +1,3 @@
-require 'json'
-
-# == Schema Information
-#
-# Table name: benchmark_batches
-#
-#  id               :integer          not null, primary key
-#  performed_at     :datetime
-#  created_at       :datetime
-#  updated_at       :datetime
-#  configuration_id :integer
-#
-
 class BenchmarkBatch < ActiveRecord::Base
 
   has_many :benchmark_results, dependent: :destroy, inverse_of: :benchmark_batch
@@ -19,45 +6,21 @@
 
   validates_presence_of :performed_at
 
-  def result_for(info)
-    benchmark_results.where(benchmark_info: info).first
-  end
+  def self.archive_directory
+    archive_dir = File.join(Rails.root, 'public', 'archive')
 
-  def all_benchmark_infos
-    benchmark_results.map(&:benchmark_info)
-  end
+    Dir.mkdir(archive_dir) unless File.exist? archive_dir
 
-  def BenchmarkBatch.archive_directory
-    archive_dir = File.join(Rails.root, 'public', 'archive')
-    if (not File.exist? archive_dir) then
-      Dir.mkdir(archive_dir)
-    end
-    return archive_dir
+    archive_dir
   end
 
-  # Returns full path to archive file
-  def archive_path
-    return File.join(BenchmarkBatch.archive_directory, self.archive_filename)
-  end
-
-  def to_json()
-    return JSON.parse( IO.read( self.archive_path ) )
-  end
-
-  def BenchmarkBatch.from_json(data)
-    archive_name = "#{data["timestamp"]}_#{(data["tags"] || data["name"] || 'default').split(",").join('-')}_#{data["configuration"]["language"]}_#{data["configuration"]["os"]}_#{data["configuration"]["runtime"]}.json"
-    archive_name = archive_name.tr(' :/', '___')
-
-    # Step 2: store data to database for faster access
-    # ------------------------------------------------
+  def self.from_json(data)
     batch = BenchmarkBatch.create!(
       benchmark_configuration: BenchmarkConfiguration.from_json(data['configuration']),
       performed_at: DateTime.parse(data["timestamp"]),
-      archive_filename: archive_name)
+      archive_filename: archive_name_for_data(data))
 
-    data["outcomes"].each do | outcome |
-
-
+    data["outcomes"].each do |outcome|
       bench_info = BenchmarkInfo.from_json(outcome["benchmark"])
 
       result = BenchmarkResult.create!(
@@ -75,15 +38,35 @@
 
     end
 
-    (data["tags"] || data["name"] || 'default').split(",").each do | tag |
-      batch.tags.append(Tag.find_by_name(tag) || Tag.create!(name: tag))
+    (data["tags"] || data["name"] || "default").split(",").each do | tag |
+      batch.tags.append(Tag.find_or_create_by(name: tag))
     end
 
-    File.open( batch.archive_path, 'w') { |file| file.write(JSON.pretty_generate(data))  }
+    File.write(batch.archive_path, JSON.pretty_generate(data))
+
+    batch
+  end
 
-    return batch
+  def self.for_dashboard
+    includes(benchmark_results: [ :benchmark_durations, :benchmark_info ]).last
+  end
+
+  def result_for(info)
+    benchmark_results.where(benchmark_info: info).first
   end
 
+  def all_benchmark_infos
+    benchmark_results.map(&:benchmark_info)
+  end
+
+  # Returns full path to archive file
+  def archive_path
+    return File.join(BenchmarkBatch.archive_directory, archive_filename)
+  end
+
+  def to_json
+    return JSON.parse(IO.read(archive_path))
+  end
 
   def performed_at_ensured
     if performed_at.present?
@@ -92,4 +75,31 @@
       Date.today
     end
   end
+
+  private
+
+  def self.archive_name_for_data(data)
+    timestamp = data["timestamp"]
+    name_or_tags = data["tags"] || data["name"] || "default"
+    nice_name_or_tags = name_or_tags.tr(",", "-")
+    language = data["configuration"]["language"]
+    os = data["configuration"]["os"]
+    runtime = data["configuration"]["runtime"]
+
+    archive_name = [ timestamp, nice_name_or_tags, language, os, runtime ].join("_")
+
+    "#{archive_name}.json".tr(" :/", "___")
+  end
+
 end
+
+# == Schema Information
+#
+# Table name: benchmark_batches
+#
+#  id               :integer          not null, primary key
+#  performed_at     :datetime
+#  created_at       :datetime
+#  updated_at       :datetime
+#  configuration_id :integer
+#
--- a/web/app/models/benchmark_result.rb	Tue Oct 28 10:39:00 2014 +0100
+++ b/web/app/models/benchmark_result.rb	Tue Oct 28 11:09:21 2014 +0100
@@ -30,10 +30,10 @@
     benchmark_durations.map { |duration| duration.duration }.max
   end
 
-  
+
   def to_json
-    { 
-      timestamp: performed_at.to_i, 
+    {
+      timestamp: performed_at.to_i,
       value: self.min_duration
     }
   end
--- a/web/app/views/index/index.html.erb	Tue Oct 28 10:39:00 2014 +0100
+++ b/web/app/views/index/index.html.erb	Tue Oct 28 11:09:21 2014 +0100
@@ -6,22 +6,22 @@
   <% if @current_results.present? %>
     <h2>Progress</h2>
     <p>
-      Showing all results for 
-      <%= @current_results.benchmark_configuration.name %> 
+      Showing all results for
+      <%= @current_results.benchmark_configuration.name %>
     </p>
 
-    <%= render 'timeline_table', 
+    <%= render "timeline_table",
       benchmark_configuration: @current_results.benchmark_configuration  %>
 
     <h2>Latest Test Results</h2>
     <p>
-      Showing latest results for 
-      <%= @current_results.benchmark_configuration.name %> 
+      Showing latest results for
+      <%= @current_results.benchmark_configuration.name %>
       performed at
       <%= t @current_results.performed_at %>
     </p>
 
-    <%= render 'benchmarks_table', results: @current_results %>
+    <%= render "benchmarks_table", results: @current_results %>
 
   <% end %>