Class: Cuboid::Report

Inherits:
Object show all
Includes:
Utilities
Defined in:
lib/cuboid/report.rb

Overview

Author:

Constant Summary collapse

EXTENSION =
'crf'
INTEGER_SIZE =
4
UNPACK =
'N'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#available_port, available_port_mutex, #bytes_to_kilobytes, #bytes_to_megabytes, #caller_name, #caller_path, #exception_jail, #generate_token, #hms_to_seconds, #port_available?, #rand_port, #random_seed, #regexp_array_match, #remove_constants, #seconds_to_hms

Constructor Details

#initialize(options = {}) ⇒ Report

Returns a new instance of Report.



44
45
46
47
48
49
50
51
# File 'lib/cuboid/report.rb', line 44

def initialize( options = {} )
    options.each { |k, v| send( "#{k}=", v ) }

    @version ||= Cuboid::VERSION

    @start_datetime  ||= Time.now
    @finish_datetime ||= Time.now
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



20
21
22
# File 'lib/cuboid/report.rb', line 20

def application
  @application
end

#dataObject

Arbitrary data from the Application.



34
35
36
# File 'lib/cuboid/report.rb', line 34

def data
  @data
end

#finish_datetimeTime

Returns The date and time when the scan finished.

Returns:

  • (Time)

    The date and time when the scan finished.



42
43
44
# File 'lib/cuboid/report.rb', line 42

def finish_datetime
  @finish_datetime
end

#optionsHash

Returns Options#to_h.

Returns:



31
32
33
# File 'lib/cuboid/report.rb', line 31

def options
  @options
end

#seedString

Returns Scan seed.

Returns:

  • (String)

    Scan seed.



27
28
29
# File 'lib/cuboid/report.rb', line 27

def seed
  @seed
end

#start_datetimeTime

Returns The date and time when the scan started.

Returns:

  • (Time)

    The date and time when the scan started.



38
39
40
# File 'lib/cuboid/report.rb', line 38

def start_datetime
  @start_datetime
end

#statusSymbol

Returns:

  • (Symbol)


23
24
25
# File 'lib/cuboid/report.rb', line 23

def status
  @status
end

#versionString

Returns VERSION.

Returns:



18
19
20
# File 'lib/cuboid/report.rb', line 18

def version
  @version
end

Class Method Details

.from_crf(data) ⇒ Object



127
128
129
130
131
# File 'lib/cuboid/report.rb', line 127

def self.from_crf( data )
    from_rpc_data RPC::Serializer.load(
      self.crf_without_summary( StringIO.new( data ) )
    )
end

.from_rpc_data(data) ⇒ DOM

Parameters:

Returns:

  • (DOM)


183
184
185
186
187
188
189
190
191
192
# File 'lib/cuboid/report.rb', line 183

def self.from_rpc_data( data )
    data['start_datetime']  = Time.parse( data['start_datetime'] )
    data['finish_datetime'] = Time.parse( data['finish_datetime'] )

    data['application'] = ObjectSpace.const_get( data['application'] )
    data['data']        = data['application'].serializer.load( data['data'] )
    data['options']     = data['application'].serializer.load( data['options'] )

    new data
end

.load(file) ⇒ Report

Loads and a saved Cuboid::Report object from file.

Parameters:

  • file (String)

    File created by #save.

Returns:

  • (Report)

    Loaded instance.



86
87
88
89
90
# File 'lib/cuboid/report.rb', line 86

def self.load( file )
    File.open( file, 'rb' ) do |f|
        from_rpc_data RPC::Serializer.load( self.crf_without_summary( f ) )
    end
end

.read_summary(report) ⇒ Hash

Returns #summary associated with the given report.

Parameters:

  • report (String)

    Location of the report.

Returns:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cuboid/report.rb', line 67

def self.read_summary( report )
    File.open( report ) do |f|
        f.seek -INTEGER_SIZE, IO::SEEK_END
        summary_size = f.read( INTEGER_SIZE ).unpack( 'N' ).first

        f.seek -summary_size-INTEGER_SIZE, IO::SEEK_END
        summary = RPC::Serializer.load( f.read( summary_size ) ).my_symbolize_keys
        summary[:application] = ObjectSpace.const_get( summary[:application].to_sym )
        summary
    end
end

Instance Method Details

#==(other) ⇒ Object



194
195
196
# File 'lib/cuboid/report.rb', line 194

def ==( other )
    hash == other.hash
end

#delta_timeString

Note:

If no #finish_datetime has been provided, it will use ‘Time.now`.

Returns ‘#start_datetime - #finish_datetime` in `00:00:00` (`hours:minutes:seconds`) format.

Returns:



58
59
60
# File 'lib/cuboid/report.rb', line 58

def delta_time
    seconds_to_hms( (@finish_datetime || Time.now) - @start_datetime )
end

#hashObject



198
199
200
201
202
203
204
# File 'lib/cuboid/report.rb', line 198

def hash
    h = to_hash
    [:start_datetime, :finish_datetime, :delta_datetime].each do |k|
        h.delete k
    end
    h.hash
end

#save(location = nil) ⇒ String

Returns Absolute location of the report.

Parameters:

  • location (String) (defaults to: nil)

    Location for the dumped report file.

Returns:

  • (String)

    Absolute location of the report.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cuboid/report.rb', line 97

def save( location = nil )
    if !location
        location = default_filename
    elsif File.directory? location
        location += "/#{default_filename}"
    end

    # We do it this way to prevent FS watchers from grabbing files that
    # are in the process of being written and thus partial.
    tmp = "#{Options.paths.tmpdir}/#{Utilities.generate_token}"
    IO.binwrite( tmp, to_crf )
    FileUtils.mv tmp, location

    File.expand_path( location )
end

#summaryHash

Returns Summary data of the report.

Returns:

  • (Hash)

    Summary data of the report.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cuboid/report.rb', line 152

def summary
    {
        application:     @application,
        version:         @version,
        status:          @status,
        seed:            @seed,
        start_datetime:  @start_datetime.to_s,
        finish_datetime: @finish_datetime.to_s,
        delta_time:      delta_time
    }
end

#to_crfString

Returns Report serialized in the Cuboid Report format.

Returns:

  • (String)

    Report serialized in the Cuboid Report format.



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cuboid/report.rb', line 115

def to_crf
    crf = RPC::Serializer.dump( self )

    sum = summary
    sum[:application] = sum[:application].to_s
    # Append metadata to the end of the dump.
     = RPC::Serializer.dump( sum )
    crf << [, .size].pack( "a*#{UNPACK}" )

    crf
end

#to_hHash Also known as: to_hash

Returns Hash representation of ‘self`.

Returns:

  • (Hash)

    Hash representation of ‘self`.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cuboid/report.rb', line 135

def to_h
    h = {
        application:     @application,
        version:         @version,
        status:          @status,
        seed:            @seed,
        data:            @data,
        options:         Cuboid::Options.hash_to_rpc_data( @options ),
        start_datetime:  @start_datetime.to_s,
        finish_datetime: @finish_datetime.to_s,
        delta_time:      delta_time
    }
end

#to_rpc_dataHash

Returns Data representing this instance that are suitable the RPC transmission.

Returns:

  • (Hash)

    Data representing this instance that are suitable the RPC transmission.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cuboid/report.rb', line 166

def to_rpc_data
    data = {}
    instance_variables.each do |ivar|
        data[ivar.to_s.gsub('@','')] = instance_variable_get( ivar )
    end

    data['application'] = data['application'].to_s
    data['data']        = @application.serializer.dump( data['data'] )
    data['options']     = @application.serializer.dump( data['options'] )

    data['start_datetime']  = data['start_datetime'].to_s
    data['finish_datetime'] = data['finish_datetime'].to_s
    data
end