Class: Fiveruns::Tuneup::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/fiveruns/tuneup/run.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, data, environment = self.class.environment, collected_at = Time.now) ⇒ Run

Returns a new instance of Run.



84
85
86
87
88
89
90
# File 'lib/fiveruns/tuneup/run.rb', line 84

def initialize(url, data, environment = self.class.environment, collected_at = Time.now)
  @url = url
  @data = data
  @environment = environment
  @collected_at = collected_at
  validate!
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/fiveruns/tuneup/run.rb', line 8

def api_key
  @api_key
end

.directoryObject

Returns the value of attribute directory.



7
8
9
# File 'lib/fiveruns/tuneup/run.rb', line 7

def directory
  @directory
end

Instance Attribute Details

#collected_atObject (readonly)

Returns the value of attribute collected_at.



83
84
85
# File 'lib/fiveruns/tuneup/run.rb', line 83

def collected_at
  @collected_at
end

#dataObject (readonly)

Returns the value of attribute data.



83
84
85
# File 'lib/fiveruns/tuneup/run.rb', line 83

def data
  @data
end

#environmentObject (readonly)

Returns the value of attribute environment.



83
84
85
# File 'lib/fiveruns/tuneup/run.rb', line 83

def environment
  @environment
end

#urlObject (readonly)

Returns the value of attribute url.



83
84
85
# File 'lib/fiveruns/tuneup/run.rb', line 83

def url
  @url
end

Class Method Details

.all(format = :slug) ⇒ Object



25
26
27
28
# File 'lib/fiveruns/tuneup/run.rb', line 25

def self.all(format = :slug)
  files = Dir[File.join(directory, '*', '*.json.gz')]
  format == :slug ? files.map { |file| slug_of(file) } : files
end

.all_for(url, format = :slug) ⇒ Object



30
31
32
33
34
# File 'lib/fiveruns/tuneup/run.rb', line 30

def self.all_for(url, format = :slug)
  run_directory = Digest::SHA1.hexdigest(url.to_s)
  files = Dir[File.join(directory, run_directory, '*.json.gz')]
  format == :slug ? files.map { |file| slug_of(file) } : files
end

.api_key?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/fiveruns/tuneup/run.rb', line 73

def self.api_key?
  @api_key
end

.environmentObject



11
12
13
14
15
16
17
18
# File 'lib/fiveruns/tuneup/run.rb', line 11

def self.environment
  @environment ||= {
    # :framework needs to be set, as well as <framework>_version
    :framework => nil, 
    :ruby_version => RUBY_VERSION,
    :ruby_platform => RUBY_PLATFORM
  }
end

.last(format = :slug) ⇒ Object



36
37
38
# File 'lib/fiveruns/tuneup/run.rb', line 36

def self.last(format = :slug)
  all(format).sort_by { |f| File.basename(f) }.last
end

.load(compressed) ⇒ Object



77
78
79
80
81
# File 'lib/fiveruns/tuneup/run.rb', line 77

def self.load(compressed)
  file = JSON.load(Zlib::Inflate.inflate(compressed))
  step = Fiveruns::Tuneup::Step.load(file['data'])
  new(file['url'], step, file['environment'], Time.at(file['collected_at']))
end

.service_uriObject



40
41
42
# File 'lib/fiveruns/tuneup/run.rb', line 40

def self.service_uri
  @service_uri = URI.parse(ENV['TUNEUP_COLLECTOR'] || 'https://tuneup-collector.fiveruns.com')
end

.share(slug) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fiveruns/tuneup/run.rb', line 44

def self.share(slug)
  if api_key?
    file = Dir[File.join(directory, "%s.json.gz" % slug)].first
    if file
      run = load(File.open(file, 'rb') { |f| f.read })
      http = Net::HTTP.new(service_uri.host, service_uri.port)
      http.use_ssl = true if service_uri.scheme == 'https'
      body = "api_key=#{api_key}&run=#{CGI.escape(run.to_json)}"
      begin
        resp = http.post('/runs.json', body, "Content-Type" => 'application/x-www-form-urlencoded')
        case resp.code.to_i
        when 201
          return JSON.load(resp.body)['run_id']
        else
          # TODO: return error info
          return false
        end
      rescue Exception => e
        # TODO: return error info
        return false
      end
    else
      raise ArgumentError, "Invalid run: #{slug}"
    end
  else
    raise ArgumentError, "No API Key set"
  end
end

.slug_of(filename) ⇒ Object



20
21
22
23
# File 'lib/fiveruns/tuneup/run.rb', line 20

def self.slug_of(filename)
  url_directory, file = filename.split(File::SEPARATOR)[-2, 2]
  File.join(url_directory, File.basename(file, '.json.gz'))
end

Instance Method Details

#full_pathObject



104
105
106
# File 'lib/fiveruns/tuneup/run.rb', line 104

def full_path
  File.join(self.class.directory, path)
end

#pathObject



116
117
118
# File 'lib/fiveruns/tuneup/run.rb', line 116

def path
  "%s.json.gz" % slug
end

#saveObject



98
99
100
101
102
# File 'lib/fiveruns/tuneup/run.rb', line 98

def save
  compressed = Zlib::Deflate.deflate(to_json)
  create_directory
  File.open(full_path, 'wb') { |f| f.write compressed }
end

#slugObject



108
109
110
111
112
113
114
# File 'lib/fiveruns/tuneup/run.rb', line 108

def slug
  @slug ||= "%s/%d-%d" % [
    Digest::SHA1.hexdigest(url),
    (collected_at.to_f * 1000),
    (data.time * 1000)
  ]
end

#to_jsonObject



120
121
122
123
124
125
126
127
128
# File 'lib/fiveruns/tuneup/run.rb', line 120

def to_json
  {
    :id => slug,
    :url => url,
    :environment => environment,
    :collected_at => collected_at.to_f,
    :data => data
  }.to_json
end

#validate!Object



92
93
94
95
96
# File 'lib/fiveruns/tuneup/run.rb', line 92

def validate!
  unless environment.is_a?(Hash)
    raise ArgumentError, "Invalid environment information (must be a Hash): #{environment.inspect}"
  end
end