Class: GHAProvider

Inherits:
Object
  • Object
show all
Includes:
GHAUtils
Defined in:
lib/gh-archive.rb

Direct Known Subclasses

FolderGHAProvider, OnlineGHAProvider

Defined Under Namespace

Classes: GHAException

Instance Method Summary collapse

Methods included from GHAUtils

#each_time, #get_gha_filename, #read_gha_file, #read_gha_file_content

Constructor Details

#initializeGHAProvider

Returns a new instance of GHAProvider.



119
120
121
122
123
124
125
126
127
# File 'lib/gh-archive.rb', line 119

def initialize
    @logger = Logger.new(STDOUT)
    
    @includes = {}
    @excludes = {}
    
    @checkpoint_name = nil
    @use_json = true
end

Instance Method Details

#each(from = Time.gm(2015, 1, 1), to = Time.now) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gh-archive.rb', line 199

def each(from = Time.gm(2015, 1, 1), to = Time.now)
    exceptions = []
    
    from = restore_checkpoint(from)
    
    self.each_time(from, to) do |current_time|
        events = []
        
        update_checkpoint(current_time)
        
        begin
            events = self.get(current_time)
        rescue GHAException => e
            @logger.warn(e.message)
            next
        rescue => e
            @logger.error("An exception occurred for #{current_time}: #{e.message}")
            exceptions << e
            next
        end
        
        events.each do |event|
            skip = false
            @includes.each do |key, value|
                skip = true unless value.include?(event[key])
            end
            
            @excludes.each do |key, value|
                skip = true if value.include?(event[key])
            end
            next if skip
            
            if @use_json
                yield event, current_time
            else
                yield GHArchive::Event.parse(event), current_time
            end
        end
        
        @logger.info("Scanned #{current_time}")
        
        events.clear
        GC.start
    end
    
    update_checkpoint(to)
    
    return exceptions
end

#exclude(**args) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/gh-archive.rb', line 161

def exclude(**args)
    args.each do |key, value|
        @excludes[key.to_s] = [] unless @excludes[key.to_s]
        @excludes[key.to_s] << value
    end
    
    return self
end

#get(date) ⇒ Object



148
149
150
# File 'lib/gh-archive.rb', line 148

def get(date)
    raise "Not implemented"
end

#include(**args) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/gh-archive.rb', line 152

def include(**args)
    args.each do |key, value|
        @includes[key.to_s] = [] unless @includes[key.to_s]
        @includes[key.to_s] << value
    end
    
    return self
end

#logger=(logger) ⇒ Object Also known as: use_logger



141
142
143
144
145
# File 'lib/gh-archive.rb', line 141

def logger=(logger)
    @logger = logger
    
    return self
end

#parse_eventsObject



135
136
137
138
139
# File 'lib/gh-archive.rb', line 135

def parse_events
    @use_json = false
    
    return self
end

#restore_checkpoint(from) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/gh-archive.rb', line 170

def restore_checkpoint(from)
    if @checkpoint_name && FileTest.exist?(@checkpoint_name)
        # Note that this throws an exception if the file is not readable. This is the intended behavior.
        # As opposed to that, failing to save the checkpoint information just results in a warning on the log.
        loaded_from = Marshal.load(File.read(@checkpoint_name))
        raise "The loaded checkpoint (#{loaded_from}) occurs before the current from date (#{from})" if loaded_from < from
        
        @logger.info("Valid checkpoint loaded. Restored execution from #{loaded_from}.")
        
        return loaded_from
    else
        return from
    end
end

#update_checkpoint(current_time) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/gh-archive.rb', line 185

def update_checkpoint(current_time)
    if @checkpoint_name
        begin
            File.open(@checkpoint_name, "wb") do |f|
                f.write(Marshal.dump(current_time))
            end
        rescue
            @logger.warn(
                "Unable to save the checkpoint at the specified location (#{File.expand_path(@checkpoint_name)})."
            )
        end
    end
end

#use_checkpoint(filename) ⇒ Object



129
130
131
132
133
# File 'lib/gh-archive.rb', line 129

def use_checkpoint(filename)
    @checkpoint_name = filename
    
    return self
end