Class: Cohortly::TagReport

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
app/models/cohortly/tag_report.rb

Defined Under Namespace

Classes: Product

Instance Method Summary collapse

Instance Method Details

#cell_iter(cell_starting_time) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/cohortly/tag_report.rb', line 45

def cell_iter(cell_starting_time)
  cohort_time = start_time
  while cohort_time <= Time.now
    cell_time = [cohort_time, cell_starting_time].max
    while cell_time <= Time.now
      yield cohort_time..(cohort_time + 1.week), cell_time..(cell_time + 1.week)
      cell_time += 1.week
    end
    cohort_time += 1.week
  end
end

#cell_query(cohort_range, cell_range) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'app/models/cohortly/tag_report.rb', line 22

def cell_query(cohort_range, cell_range)
  { :created_at => {
      :$gt => cell_range.begin,
      :$lt => cell_range.end},
    :user_start_date => {
      :$gt => cohort_range.begin,
      :$lt => cohort_range.end } }.tap { |x|
    self.tags ? x.merge!( tag_query ) : x  
  }
end

#cohort_iter(starting_time) ⇒ Object



37
38
39
40
41
42
43
# File 'app/models/cohortly/tag_report.rb', line 37

def cohort_iter(starting_time)
  cohort_time = starting_time
  while cohort_time <= Time.now
    yield cohort_time..(cohort_time + 1.week)
    cohort_time += 1.week
  end
end

#merge(tag_report) ⇒ Object



80
81
82
# File 'app/models/cohortly/tag_report.rb', line 80

def merge(tag_report)
  TagReport::Product.new(:tag_report => self, :tags => self.tags, :data => self.data).merge(tag_report)
end

#recalc_tableObject



57
58
59
60
61
# File 'app/models/cohortly/tag_report.rb', line 57

def recalc_table
  self.data = { }
  self.last_update_on = Time.now
  self.cell_iter(self.start_time) { |cohort_range, cell_range| self.store_cell(cohort_range, cell_range)}
end

#runObject



10
11
12
13
14
15
16
# File 'app/models/cohortly/tag_report.rb', line 10

def run
  if self.last_update_on.nil?
    self.recalc_table 
  else
    self.update_table
  end
end

#start_timeObject



33
34
35
# File 'app/models/cohortly/tag_report.rb', line 33

def start_time
  Cohortly::Metric.where(tag_query).sort(:user_start_date).limit(1).first.user_start_date.utc.beginning_of_week
end

#store_cell(cohort_range, cell_range) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'app/models/cohortly/tag_report.rb', line 69

def store_cell(cohort_range, cell_range)
  cohort_key = cohort_range.begin.strftime('%Y-%W')
  cell_key = cell_range.begin.strftime('%Y-%W')        
  p cohort_key + " " + cell_key 
  self.data[cohort_key] ||= { }
  self.data[cohort_key][cell_key] ||= { }
  Cohortly::Metric.collection.distinct( :user_id, cell_query(cohort_range, cell_range) ).each do |user_id|
    self.data[cohort_key][cell_key][user_id.to_s] = 1
  end
end

#tag_queryObject



18
19
20
# File 'app/models/cohortly/tag_report.rb', line 18

def tag_query
  self.tags.any? ? { :tags => self.tags.first } : { }  
end

#update_tableObject



63
64
65
66
67
# File 'app/models/cohortly/tag_report.rb', line 63

def update_table
  starting_time = self.last_update_on.utc.beginning_of_week
  self.last_update_on = Time.now      
  self.cell_iter(starting_time) { |cohort_range, cell_range| self.store_cell(cohort_range, cell_range)} 
end