Class: MingleEvents::ProjectEventFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mingle_events/project_event_fetcher.rb

Overview

fetch all unseen events and write them to disk for future processing

this class is messy and needs some cleanup, but some things are in here for a reason. specifically, for historical analysis, we can process each event, one at at time, reading it off disk, to avoid massive memory consumption.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_identifier, mingle_access, state_dir = nil) ⇒ ProjectEventFetcher

Returns a new instance of ProjectEventFetcher.



12
13
14
15
16
17
18
19
# File 'lib/mingle_events/project_event_fetcher.rb', line 12

def initialize(project_identifier, mingle_access, state_dir=nil)
  @project_identifier = project_identifier
  @mingle_access = mingle_access
  base_uri = URI.parse(mingle_access.base_url)
  @state_dir = state_dir || File.join('~', '.mingle_events', base_uri.host, base_uri.port.to_s)
  @state_dir = File.expand_path(File.join(@state_dir, project_identifier, 'fetched_events'))
  @entry_cache = EntryCache.new(@state_dir)
end

Instance Attribute Details

#entry_cacheObject (readonly)

Returns the value of attribute entry_cache.



10
11
12
# File 'lib/mingle_events/project_event_fetcher.rb', line 10

def entry_cache
  @entry_cache
end

Instance Method Details

#all_fetched_entriesObject

returns all previously fetched entries; can be used to reprocess the events for, say, various historical analyses



76
77
78
# File 'lib/mingle_events/project_event_fetcher.rb', line 76

def all_fetched_entries
  @entry_cache.all_entries
end

#clear_cache_if_first_entry_mismatch!Object



57
58
59
60
61
# File 'lib/mingle_events/project_event_fetcher.rb', line 57

def clear_cache_if_first_entry_mismatch!
  unless first_entry_cached_match?
    reset
  end
end

#fetch_latestObject

fetch the latest events from mingle, i.e., the ones not previously seen



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mingle_events/project_event_fetcher.rb', line 33

def fetch_latest
  page = page_with_latest_entries
  most_recent_new_entry = page.entries.first
  last_fetched_entry = @entry_cache.latest
  last_fetched_entry_seen = false      
  next_entry = nil
  while !last_fetched_entry_seen && page
    page.entries.each do |entry|
                
      @entry_cache.write(entry, next_entry)
      if last_fetched_entry && entry.entry_id == last_fetched_entry.entry_id
        last_fetched_entry_seen = true
        break
      end

      next_entry = entry
    end
    page = page.next
  end
              
  @entry_cache.update_current_state(next_entry, most_recent_new_entry)
  @entry_cache.entries(next_entry, most_recent_new_entry)
end

#first_entry_cached_match?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
# File 'lib/mingle_events/project_event_fetcher.rb', line 63

def first_entry_cached_match?
  if first_entry_cached = first_entry_fetched
    page = page_with_first_entries
    if first_entry = page.entries.last
      first_entry_fetched.entry_id == first_entry.entry_id
    end
  else
    true
  end
end

#first_entry_fetchedObject



80
81
82
# File 'lib/mingle_events/project_event_fetcher.rb', line 80

def first_entry_fetched      
  @entry_cache.first
end

#last_entry_fetchedObject



84
85
86
# File 'lib/mingle_events/project_event_fetcher.rb', line 84

def last_entry_fetched
  @entry_cache.latest
end

#resetObject

blow away any existing state, when next used to fetch events from mingle will crawl all the way back to time zero



23
24
25
# File 'lib/mingle_events/project_event_fetcher.rb', line 23

def reset
  @entry_cache.clear
end

#set_current_state_to_now_if_no_current_stateObject



27
28
29
30
# File 'lib/mingle_events/project_event_fetcher.rb', line 27

def set_current_state_to_now_if_no_current_state
  return if @entry_cache.has_current_state?
  @entry_cache.set_current_state(page_with_latest_entries.entries.first)
end