git-fit 0.8.9 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9aedbd56b7a0006cad60386fa55b030857cef43497e0d798bb334f6ddaf968fb
4
- data.tar.gz: 526692de645f4cee05b8b61a3bc3018efbaad202c2d342b7376e7a90e1b1f237
3
+ metadata.gz: 0f2c48812a658488f860a9e483756ecd635b078b8f94deaf0cc20e31ab5caef9
4
+ data.tar.gz: 3a1b15b292e344df1a5a503fd3f2160e0e66ca5f3293c7449144164ca84ce53a
5
5
  SHA512:
6
- metadata.gz: 10c0908e780b6c8f01cc32af8ed20bcc344f197edcf2daeb2f799f99cad78433a7159d15e0b7dffe51bc654fd76c75ebf6e29152a9e8500aa760fb4a85d10b84
7
- data.tar.gz: e36c0f4b019e587640e220704cc82a05eb4533db5314fabfc9ea32771d0493c198df90fdf8b490ee1293924a39630dc8a8721abde3e4ebbe8233f8e722325f2e
6
+ metadata.gz: 0a6e1dec451ae23ddfc9333258f25c528db2d467dcd2b4dfd8f04ada2decaaa9d81c9d3094010c264c82a51c9e001bac7d33a761c75865dafadcc4207ddbe003
7
+ data.tar.gz: 4157de7684b83443b6b825af613e2ed0e74b8e4c2bac9378fe0edf0f3329a465ca0e1e80c540fed437d1f88825232b96a480e2fd8bbd6c210e6bad5d0319e7c4
@@ -11,6 +11,7 @@ module GitFit
11
11
  option :time, type: :numeric, aliases: '-t', desc: 'Time budget in seconds'
12
12
  option :"dry-run", type: :boolean, desc: "Don't write to DB"
13
13
  option :checkpoint, type: :boolean, desc: 'Run WAL checkpoint after detect'
14
+ option :"cache-dir", type: :string, desc: "Geo cache directory (empty = no cache)", default: "data/cache"
14
15
  def detect
15
16
  config = GitFit::Config.new
16
17
  conn = GitFit::DB::Connection.new(config.db_path)
@@ -25,6 +26,7 @@ module GitFit
25
26
  strategy: options[:strategy],
26
27
  time_budget: options[:time],
27
28
  dry_run: options[:"dry-run"],
29
+ cache_dir: options[:"cache-dir"],
28
30
  )
29
31
  detector.run
30
32
  checkpoint_db(db) if options[:checkpoint]
@@ -10,10 +10,9 @@ module GitFit
10
10
  KEEP_PLACEHOLDER = 'gqqrFurkeU??'
11
11
  SAMPLE_INTERVAL_DEG = 0.01
12
12
  CACHE_DIR = 'data/cache'
13
- POINT_CACHE_FILE = File.join(CACHE_DIR, 'geo-point.jsonl')
14
13
 
15
14
  def initialize(db:, amap_key: nil, batch: 20, limit: nil, strategy: 'proportional', time_budget: nil,
16
- dry_run: false)
15
+ dry_run: false, cache_dir: CACHE_DIR)
17
16
  @db = db
18
17
  @geocoder = ReverseGeocode.new(amap_key: amap_key)
19
18
  @amap_key = amap_key
@@ -23,6 +22,7 @@ module GitFit
23
22
  @time_budget = time_budget
24
23
  @start_time = Time.now
25
24
  @dry_run = dry_run
25
+ @cache_dir = cache_dir
26
26
  @processed = 0
27
27
  @skipped_placeholder = 0
28
28
  @skipped_empty_polyline = 0
@@ -32,7 +32,7 @@ module GitFit
32
32
  @errors = 0
33
33
  @attempted = 0
34
34
  @total = 0
35
- FileUtils.mkdir_p(CACHE_DIR)
35
+ FileUtils.mkdir_p(@cache_dir) if @cache_dir && !@cache_dir.empty?
36
36
  load_caches
37
37
  end
38
38
 
@@ -159,10 +159,11 @@ module GitFit
159
159
  key_counts = Hash.new(0)
160
160
  samples.each do |lat, lng|
161
161
  coord_key = "#{lat},#{lng}"
162
- raw = lookup_point_cache(coord_key)
162
+ provider = provider_for(lat, lng)
163
+ raw = lookup_point_cache(coord_key, provider)
163
164
  unless raw
164
165
  raw = @geocoder.lookup(lat, lng)
165
- save_point_cache(coord_key, raw) if raw[:province]
166
+ save_point_cache(coord_key, raw, provider) if raw[:province]
166
167
  end
167
168
  result = normalize_entry(raw)
168
169
  next unless result
@@ -218,9 +219,19 @@ module GitFit
218
219
  { divisions: division_list, total_sampled: total_points, source: 'amap', processed_at: Time.now.utc.iso8601 }
219
220
  end
220
221
 
221
- def lookup_point_cache(coord_key)
222
- return nil unless File.exist?(POINT_CACHE_FILE)
223
- File.open(POINT_CACHE_FILE, 'r') do |f|
222
+ def cache_path_for(provider)
223
+ return nil unless @cache_dir && !@cache_dir.empty?
224
+ File.join(@cache_dir, "#{provider}-points.jsonl")
225
+ end
226
+
227
+ def provider_for(lat, lng)
228
+ @geocoder.provider_name(lat, lng)
229
+ end
230
+
231
+ def lookup_point_cache(coord_key, provider)
232
+ path = cache_path_for(provider)
233
+ return nil unless path && File.exist?(path)
234
+ File.open(path, 'r') do |f|
224
235
  f.each_line do |line|
225
236
  entry = JSON.parse(line) rescue next
226
237
  return entry if entry['ck'] == coord_key
@@ -229,12 +240,14 @@ module GitFit
229
240
  nil
230
241
  end
231
242
 
232
- def save_point_cache(coord_key, result)
243
+ def save_point_cache(coord_key, result, provider)
244
+ path = cache_path_for(provider)
245
+ return unless path
233
246
  entry = { ck: coord_key, p: result[:province], source: result[:source] }
234
247
  entry[:city] = result[:city] if result[:city]
235
248
  entry[:dist] = result[:district] if result[:district]
236
249
  entry[:town] = result[:township] if result[:township]
237
- File.open(POINT_CACHE_FILE, 'a') { |f| f.puts(JSON.generate(entry)) }
250
+ File.open(path, 'a') { |f| f.puts(JSON.generate(entry)) }
238
251
  end
239
252
 
240
253
  def update_db(run_id, summary)
@@ -25,6 +25,10 @@ module GitFit
25
25
  in_china?(lat, lng) ? lookup_china(lat, lng) : lookup_intl(lat, lng)
26
26
  end
27
27
 
28
+ def provider_name(lat, lng)
29
+ in_china?(lat, lng) ? :amap : :nominatim
30
+ end
31
+
28
32
  private
29
33
 
30
34
  def in_china?(lat, lng)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GitFit
4
- VERSION = '0.8.9'
4
+ VERSION = '0.9.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-fit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lax