Class: Chronicle::ETL::JobLog

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chronicle/etl/job_log.rb

Overview

A record of what happened in the running of a job. We're interested in tracking when it ran, if it was successful, and what the latest record we found is (to use as a cursor for the next time)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ JobLog

Create a new JobLog for a given Job

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
27
# File 'lib/chronicle/etl/job_log.rb', line 23

def initialize
  @num_records_processed = 0
  @success = false
  yield self if block_given?
end

Instance Attribute Details

#finished_atObject

Returns the value of attribute finished_at.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def finished_at
  @finished_at
end

#highest_timestampObject

Returns the value of attribute highest_timestamp.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def highest_timestamp
  @highest_timestamp
end

#jobObject

Returns the value of attribute job.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def job
  @job
end

#job_idObject

Returns the value of attribute job_id.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def job_id
  @job_id
end

#last_idObject

Returns the value of attribute last_id.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def last_id
  @last_id
end

#num_records_processedObject

Returns the value of attribute num_records_processed.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def num_records_processed
  @num_records_processed
end

#started_atObject

Returns the value of attribute started_at.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def started_at
  @started_at
end

#successObject

Returns the value of attribute success.



11
12
13
# File 'lib/chronicle/etl/job_log.rb', line 11

def success
  @success
end

Class Method Details

.build_from_serialized(attrs) ⇒ Object

Create a new JobLog and set its instance variables from a serialized hash



82
83
84
85
86
87
88
89
90
# File 'lib/chronicle/etl/job_log.rb', line 82

def self.build_from_serialized(attrs)
  attrs.delete(:id)
  new do |job_log|
    attrs.each do |key, value|
      setter = :"#{key}="
      job_log.send(setter, value)
    end
  end
end

Instance Method Details

#durationObject



62
63
64
65
66
# File 'lib/chronicle/etl/job_log.rb', line 62

def duration
  return unless @finished_at && @started_at

  @finished_at - @started_at
end

#errorObject



53
54
55
# File 'lib/chronicle/etl/job_log.rb', line 53

def error
  @finished_at = Time.now
end

#finishObject

Indicate that a job has finished



48
49
50
51
# File 'lib/chronicle/etl/job_log.rb', line 48

def finish
  @finished_at = Time.now
  @success = true
end

#log_transformation(_transformer) ⇒ Object

Log the result of a single transformation in a job

Parameters:

  • transformer (Chronicle::ETL::Tranformer)

    The transformer that ran



31
32
33
34
35
36
37
38
39
40
# File 'lib/chronicle/etl/job_log.rb', line 31

def log_transformation(_transformer)
  # @last_id = transformer.id if transformer.id

  # Save the highest timestamp that we've encountered so far
  # @highest_timestamp = [transformer.timestamp, @highest_timestamp].compact.max if transformer.timestamp

  # TODO: a transformer might yield nil. We might also want certain transformers to explode
  # records into multiple new ones. Therefore, this this variable will need more subtle behaviour
  @num_records_processed += 1
end

#serializeObject

Take a JobLog's instance variables and turn them into a hash representation



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chronicle/etl/job_log.rb', line 69

def serialize
  {
    job_id: @job_id,
    last_id: @last_id,
    highest_timestamp: @highest_timestamp,
    num_records_processed: @num_records_processed,
    started_at: @started_at,
    finished_at: @finished_at,
    success: @success
  }
end

#startObject

Indicate that a job has started



43
44
45
# File 'lib/chronicle/etl/job_log.rb', line 43

def start
  @started_at = Time.now
end