summaryrefslogtreecommitdiff
path: root/lib/postrunner/ActivitiesDB.rb
blob: 87931cdb7d66b78169e429a5b93ca419b37ea47a (plain)
1
2
3
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = ActivitiesDB.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'fileutils'
require 'yaml'

require 'fit4ruby'
require 'postrunner/Activity'
require 'postrunner/PersonalRecords'
require 'postrunner/ActivityListView'

module PostRunner

  class ActivitiesDB

    attr_reader :db_dir, :cfg, :fit_dir, :html_dir, :activities

    def initialize(db_dir, cfg)
      @db_dir = db_dir
      @cfg = cfg
      @fit_dir = File.join(@db_dir, 'fit')
      @html_dir = File.join(@db_dir, 'html')
      @archive_file = File.join(@db_dir, 'archive.yml')

      create_directories
      begin
        if File.exists?(@archive_file)
          @activities = YAML.load_file(@archive_file)
        else
          @activities = []
        end
      rescue RuntimeError
        Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}"
      end

      unless @activities.is_a?(Array)
        Log.fatal "The archive file '#{@archive_file}' is corrupted"
      end

      # Not all instance variables of Activity are stored in the file. The
      # normal constructor is not run during YAML::load_file. We have to
      # initialize those instance variables in a secondary step.
      sync_needed = false
      @activities.each do |a|
        a.late_init(self)
        # If the Activity has the data from the FIT file loaded, a value was
        # missing in the YAML file. Set the sync flag so we can update the
        # YAML file once we have checked all Activities.
        sync_needed |= !a.fit_activity.nil?
      end

      @records = PersonalRecords.new(self)
      sync if sync_needed
    end

    # Add a new FIT file to the database.
    # @param fit_file [String] Name of the FIT file.
    # @return [TrueClass or FalseClass] True if the file could be added. False
    # otherwise.
    def add(fit_file)
      base_fit_file = File.basename(fit_file)
      if @activities.find { |a| a.fit_file == base_fit_file }
        Log.debug "Activity #{fit_file} is already included in the archive"
        return false
      end

      if File.exists?(File.join(@fit_dir, base_fit_file))
        Log.debug "Activity #{fit_file} has been deleted before"
        return false
      end

      begin
        fit_activity = Fit4Ruby.read(fit_file)
      rescue Fit4Ruby::Error
        Log.error $!
        return false
      end

      begin
        FileUtils.cp(fit_file, @fit_dir)
      rescue StandardError
        Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}"
      end

      @activities << (activity = Activity.new(self, base_fit_file,
                                              fit_activity))
      @activities.sort! do |a1, a2|
        a2.timestamp <=> a1.timestamp
      end

      activity.register_records(@records)

      # Generate HTML file for this activity.
      activity.generate_html_view

      # The HTML activity views contain links to their predecessors and
      # successors. After inserting a new activity, we need to re-generate
      # these views as well.
      if (pred = predecessor(activity))
        pred.generate_html_view
      end
      if (succ = successor(activity))
        succ.generate_html_view
      end

      sync
      Log.info "#{fit_file} successfully added to archive"

      true
    end

    def delete(activity)
      pred = predecessor(activity)
      succ = successor(activity)

      @activities.delete(activity)

      # The HTML activity views contain links to their predecessors and
      # successors. After deleting an activity, we need to re-generate these
      # views as well.
      pred.generate_html_view if pred
      succ.generate_html_view if succ

      sync
    end

    def rename(activity, name)
      activity.rename(name)
      sync
    end

    def set(activity, attribute, value)
      activity.set(attribute, value)
      sync
    end

    def check
      @activities.each { |a| a.check }
      # Ensure that HTML index is up-to-date.
      ActivityListView.new(self).update_html_index
    end

    def ref_by_fit_file(fit_file)
      i = 1
      @activities.each do |activity|
        return i if activity.fit_file == fit_file
        i += 1
      end

      nil
    end

    def activity_by_fit_file(fit_file)
      @activities.find { |a| a.fit_file == fit_file }
    end

    def find(query)
      case query
      when /\A-?\d+$\z/
        index = query.to_i
        # The UI counts the activities from 1 to N. Ruby counts from 0 -
        # (N-1).
        index -= 1 if index > 0
        if (a = @activities[index])
          return [ a ]
        end
      when /\A-?\d+--?\d+\z/
        idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/)
        sidx = idxs['sidx'].to_i
        eidx = idxs['eidx'].to_i
        # The UI counts the activities from 1 to N. Ruby counts from 0 -
        # (N-1).
        sidx -= 1 if sidx > 0
        eidx -= 1 if eidx > 0
        unless (as = @activities[sidx..eidx]).empty?
          return as
        end
      else
        Log.error "Invalid activity query: #{query}"
      end

      []
    end

    # Return the next Activity after the provided activity. Note that this has
    # a lower index. If none is found, return nil.
    def successor(activity)
      idx = @activities.index(activity)
      return nil if idx.nil? || idx == 0
      @activities[idx - 1]
    end

    # Return the previous Activity before the provided activity.
    # If none is found, return nil.
    def predecessor(activity)
      idx = @activities.index(activity)
      return nil if idx.nil?
      # Activities indexes are reversed. The predecessor has a higher index.
      @activities[idx + 1]
    end

    def map_to_files(query)
      case query
      when /\A-?\d+$\z/
        index = query.to_i
        # The UI counts the activities from 1 to N. Ruby counts from 0 -
        # (N-1).
        index -= 1 if index > 0
        if (a = @activities[index])
          return [ File.join(@fit_dir, a.fit_file) ]
        end
      when /\A-?\d+--?\d+\z/
        idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/)
        sidx = idxs['sidx'].to_i
        eidx = idxs['eidx'].to_i
        # The UI counts the activities from 1 to N. Ruby counts from 0 -
        # (N-1).
        sidx -= 1 if sidx > 0
        eidx -= 1 if eidx > 0
        unless (as = @activities[sidx..eidx]).empty?
          files = []
          as.each do |a|
            files << File.join(@fit_dir, a.fit_file)
          end
          return files
        end
      else
        Log.error "Invalid activity query: #{query}"
      end

      []
    end

    # Show the activity list in a web browser.
    def show_list_in_browser
      ActivityListView.new(self).update_html_index
      show_in_browser(File.join(@html_dir, 'index.html'))
    end

    def list
      puts ActivityListView.new(self).to_s
    end

    def show_records
      puts @records.to_s
    end

    # Launch a web browser and show an HTML file.
    # @param html_file [String] file name of the HTML file to show
    def show_in_browser(html_file)
      cmd = "#{ENV['BROWSER'] || 'firefox'} \"#{html_file}\" &"

      unless system(cmd)
        Log.fatal "Failed to execute the following shell command: #{$cmd}\n" +
                  "#{$!}"
      end
    end

    # This method can be called to re-generate all HTML reports and all HTML
    # index files.
    def generate_all_html_reports
      Log.info "Re-generating all HTML report files..."
      # Generate HTML views for all activities in the DB.
      @activities.each { |a| a.generate_html_view }
      Log.info "All HTML report files have been re-generated."
      # (Re-)generate index files.
      ActivityListView.new(self).update_html_index
      Log.info "HTML index files have been updated."
    end

    private

    def sync
      begin
        File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) }
      rescue StandardError
        Log.fatal "Cannot write archive file '#{@archive_file}': #{$!}"
      end

      @records.sync
      ActivityListView.new(self).update_html_index
    end

    def create_directories
      create_directory(@db_dir, 'data')
      create_directory(@fit_dir, 'fit')
      create_directory(@html_dir, 'html')

      create_symlink('icons')
      create_symlink('jquery')
      create_symlink('flot')
      create_symlink('openlayers')
    end

    def create_directory(dir, name)
      return if Dir.exists?(dir)

      Log.info "Creating #{name} directory #{dir}"
      begin
        Dir.mkdir(dir)
      rescue StandardError
        Log.fatal "Cannot create #{name} directory #{dir}: #{$!}"
      end
    end

    def create_symlink(dir)
      # This file should be in lib/postrunner. The 'misc' directory should be
      # found in '../../misc'.
      misc_dir = File.realpath(File.join(File.dirname(__FILE__),
                                         '..', '..', 'misc'))
      unless Dir.exists?(misc_dir)
        Log.fatal "Cannot find 'misc' directory under '#{misc_dir}': #{$!}"
      end
      src_dir = File.join(misc_dir, dir)
      unless Dir.exists?(src_dir)
        Log.fatal "Cannot find '#{src_dir}': #{$!}"
      end
      dst_dir = File.join(@html_dir, dir)
      unless File.exists?(dst_dir)
        begin
          FileUtils.ln_s(src_dir, dst_dir)
        rescue IOError
          Log.fatal "Cannot create symbolic link to '#{dst_dir}': #{$!}"
        end
      end
    end

  end

end