Class: Geostats::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/geostats/stats.rb

Class Method Summary collapse

Class Method Details

.archived_total_ratioObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/geostats/stats.rb', line 151

def archived_total_ratio
  @archived_total_ratio ||= begin
    archived = Cache
      .joins(:logs)
      .found_or_attended
      .where("caches.is_archived = ?", true)
      .count

    total = Cache
      .joins(:logs)
      .found_or_attended
      .count

    {
      :archived => archived,
      :total => total,
      :percentage => (archived.to_f / total.to_f * 100.0).round
    }
  end
end

.avg_founds_per_caching_dayObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/geostats/stats.rb', line 56

def avg_founds_per_caching_day
  Cache
    .joins(:logs)
    .found_or_attended
    .group("logs.logged_at")
    .count
    .to_a
    .map { |a| a[1] }
    .avg
end

.cache_with_most_dnfObject



143
144
145
146
147
148
149
# File 'lib/geostats/stats.rb', line 143

def cache_with_most_dnf
  @cache_with_most_dnf ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.dnf_count DESC")
    .first
end

.cache_with_most_foundsObject



135
136
137
138
139
140
141
# File 'lib/geostats/stats.rb', line 135

def cache_with_most_founds
  @cache_with_most_founds ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.founds_count DESC")
    .first
end

.caching_daysObject



47
48
49
50
51
52
53
54
# File 'lib/geostats/stats.rb', line 47

def caching_days
  Cache
    .joins(:logs)
    .found_or_attended
    .group("logs.logged_at")
    .count
    .length
end

.daily_founds(days = 30) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/geostats/stats.rb', line 82

def daily_founds(days=30)
  today = Time.parse(Time.now.strftime("%Y-%m-%d"))

  1.upto(days).map do |x|
    day = today - x.days

    Cache
      .joins(:logs)
      .found_or_attended
      .where("logs.logged_at >= ? AND logs.logged_at < ?", day, day + 1.day)
      .count
  end
end

.day_with_most_foundsObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/geostats/stats.rb', line 96

def day_with_most_founds
  @day_with_most_founds ||= Cache
    .joins(:logs)
    .found_or_attended
    .group("logs.logged_at")
    .count
    .to_a
    .sort { |a, b| a[1] <=> b[1] }
    .last
end

.difficulty_terrain_matrixObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/geostats/stats.rb', line 204

def difficulty_terrain_matrix
  [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5].map do |difficulty|
    result = Cache
      .joins(:logs)
      .found_or_attended
      .where("caches.difficulty = ?", difficulty)
      .group(:terrain)
      .count

    [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5].map do |terrain|
      result[terrain.to_f] || 0
    end
  end
end

.founds_by_cache_typeObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/geostats/stats.rb', line 107

def founds_by_cache_type
  CacheType.all.map do |type|
    count = Cache
      .joins(:logs)
      .found_or_attended
      .where("caches.cache_type_id = ?", type.id)
      .count

    [type, count]
  end
end

.milestones(step = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geostats/stats.rb', line 4

def milestones(step=nil)
  logs = Log
    .joins(:cache)
    .found_or_attended
    .order("logs.logged_at ASC")
    .all
  total = logs.length
  milestones = []

  step ||= case total
    when 0 then 0
    when 1..100 then 50
    when 101..1000 then 100
    else 500
  end

  milestones << { :milestone => 1, :log => logs.first }

  1.upto(total / step) do |i|
    i *= step
    log = logs[i - 1]

    milestones << {
      :milestone => i,
      :log => logs[i - 1],
      :distance => (log.logged_at - milestones.last[:log].logged_at) / 60 / 60 / 24
    }
  end

  milestones << {
    :milestone => total,
    :log => logs.last,
    :distance => (logs.last.logged_at - milestones.last[:log].logged_at) / 60 / 60 / 24
  }
end

.monthly_founds(months = 6) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geostats/stats.rb', line 67

def monthly_founds(months=6)
  beginning_of_current_month = Time.parse("#{Time.now.strftime("%Y-%m")}-01")

  0.upto(months - 1).map do |x|
    start = beginning_of_current_month - x.months
    count = Cache
      .joins(:logs)
      .found_or_attended
      .where("logs.logged_at >= ? AND logs.logged_at < ?", start, start + 1.month)
      .count

    [start, count]
  end
end

.most_easterly_cacheObject



196
197
198
199
200
201
202
# File 'lib/geostats/stats.rb', line 196

def most_easterly_cache
  @most_easterly_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.longitude DESC")
    .first
end

.most_northerly_cacheObject



172
173
174
175
176
177
178
# File 'lib/geostats/stats.rb', line 172

def most_northerly_cache
  @most_northerly_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.latitude DESC")
    .first
end

.most_southerly_cacheObject



180
181
182
183
184
185
186
# File 'lib/geostats/stats.rb', line 180

def most_southerly_cache
  @most_southerly_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.latitude ASC")
    .first
end

.most_westerly_cacheObject



188
189
190
191
192
193
194
# File 'lib/geostats/stats.rb', line 188

def most_westerly_cache
  @most_westerly_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.longitude ASC")
    .first
end

.newest_cacheObject



119
120
121
122
123
124
125
# File 'lib/geostats/stats.rb', line 119

def newest_cache
  @newest_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.hidden_at DESC")
    .first
end

.oldest_cacheObject



127
128
129
130
131
132
133
# File 'lib/geostats/stats.rb', line 127

def oldest_cache
  @oldest_cache ||= Cache
    .joins(:logs)
    .found_or_attended
    .order("caches.hidden_at ASC")
    .first
end

.total_foundsObject



40
41
42
43
44
45
# File 'lib/geostats/stats.rb', line 40

def total_founds
  Cache
    .joins(:logs)
    .found_or_attended
    .count
end