Class: GitFit::Dedup::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/git_fit/dedup/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(db, incremental: false, log_path: nil, verbose: false) ⇒ Service

Returns a new instance of Service.



13
14
15
16
17
18
19
20
21
# File 'lib/git_fit/dedup/service.rb', line 13

def initialize(db, incremental: false, log_path: nil, verbose: false)
  @db = db
  @trajectory = Trajectory.new(db)
  @incremental = incremental
  @log_path = log_path
  @verbose = verbose
  @updates = {}
  @matched_pairs = []
end

Instance Method Details

#runObject



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
# File 'lib/git_fit/dedup/service.rb', line 23

def run
  activities = activities_scope.all
  return 0 if activities.size < 2
  puts "DedupService: scanning #{activities.size} activities" if @verbose

  start_time = Time.now.utc
  matched = 0

  activities.each_with_index do |a, i|
    a_start = parse_time(a[:start_date])
    next unless a_start
    a_range = activity_range(a)

    (i + 1...activities.size).each do |j|
      b = activities[j]
      b_start = parse_time(b[:start_date])
      next unless b_start
      break if b_start > (a_range[:end] + 86400)

      score = Matcher.match(a, b)
      next unless score && score >= 0.15

      flag = classify_flag(a, b, score)
      next unless flag

      append_info(a[:run_id], b[:source], b[:run_id], score, flag)
      append_info(b[:run_id], a[:source], a[:run_id], score, flag)
      @matched_pairs << { a: a, b: b, score: score, flag: flag }
      matched += 1
    end
  end

  flush_updates

  consolidate_groups if matched > 0

  elapsed = Time.now.utc - start_time

  if @log_path
    log_stats = {
      scanned: activities.size,
      compared: @matched_pairs.size,
      matched: matched,
      by_flag: @matched_pairs.group_by { |mp| mp[:flag] }.transform_values(&:size),
    }
    LogWriter.new(@log_path).write(
      stats: log_stats,
      matches: @matched_pairs.map.with_index do |mp, idx|
        {
          id: idx + 1,
          a: { run_id: mp[:a][:run_id], source: mp[:a][:source] },
          b: { run_id: mp[:b][:run_id], source: mp[:b][:source] },
          confidence: mp[:score],
          flag: mp[:flag],
        }
      end,
      elapsed: elapsed,
    )
  end

  puts "DedupService: #{matched} matches in #{elapsed.round(1)}s" if @verbose
  matched
end