loi_parser 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3bd1b7478112fcb89c4e2dbe1488e0ba2ad80660a4961bd653fbe7e13808125d
4
- data.tar.gz: c91e18d7a6eb9aba55ae689bb6e9755998157cbf720d3106713617e245fad7a7
3
+ metadata.gz: 717100c3240b162307dc6349e8bca4a99d1d9af4079c05b97663f84a298add77
4
+ data.tar.gz: 9899e509a951ba72c602f73e1bc519b1f27a553b14927d303fd79a380b3a90d1
5
5
  SHA512:
6
- metadata.gz: 4ac95c4499e291bcb0e18aea849423c06fe761bf482dc43954358bffb842704fae5f9ce07d671201a44344fcf8caa46d5766d8428587d1e462e6f349d34a5006
7
- data.tar.gz: a01c90bd3eeb38575f7070e293c6ade131d46ed0b6ad04d55ccc05910589ab0ae263a8f2bbdc9fb32467457412776c7a442ebbb69bb9e9550b06b6e1cf0dd8e0
6
+ metadata.gz: 5d67325b298b6c980f91d0899460f7920239d2b57bb741e0afd60a66924b1aeb149b5a1db4d2f90cb20472fc504bce48f2d83472dea0b75b778ab7bb676a0a63
7
+ data.tar.gz: e2b4acece31219290fa3b715bbb431fe6f33cd103f9304e12fb783b0a3179a5418c46f50aab778ff6b1860528edcde038f5c2c63c104d7aedaa059712de59d58
data/CHANGELOG.md CHANGED
@@ -6,9 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.1.0] - 2023-03-14
10
+ ### Added
11
+ - Configurable file.read length
12
+ - Thousands formater on the "founds" result
13
+
14
+ ### Changed
15
+ - Reading 5 000 bytes by default in order to speed up the reading process
16
+ - Logging data
17
+
9
18
  ## [1.0.0] - 2023-03-13
10
19
  ### Added
11
20
  - Initial import
12
21
 
13
- [Unreleased]: https://gitlab.com/pharmony/loi-parser/compare/v1.0.0...master
22
+ [Unreleased]: https://gitlab.com/pharmony/loi-parser/compare/v1.1.0...master
23
+ [1.1.0]: https://gitlab.com/pharmony/loi-parser/compare/v1.0.0...v1.1.0
14
24
  [1.0.0]: https://gitlab.com/hydrana/hydrana/tags/v1.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- loi_parser (1.0.0)
4
+ loi_parser (1.1.0)
5
5
  logger
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -42,17 +42,25 @@ Or install it yourself as:
42
42
 
43
43
  # Grabbing all the serial numbers being in opposition
44
44
  > loi.serials_in_opposition
45
- I, [2023-03-08T16:04:19.286193 #317] INFO -- LoiParser: (0%) current_sn: 255999999/256000000 (Elapsed: 0 seconds | Found: 0) ...
46
- I, [2023-03-08T16:04:25.387373 #317] INFO -- LoiParser: (1%) current_sn: 253440000/256000000 (Elapsed: 6 seconds | Found: 0) ...
47
- I, [2023-03-08T16:04:31.418921 #317] INFO -- LoiParser: (2%) current_sn: 250880000/256000000 (Elapsed: 12 seconds | Found: 0) ...
45
+ I, [2023-03-14T14:33:12.606494 #135] INFO -- LoiParser: 0% Found: 0 (Elapsed: 0 ms)
46
+ I, [2023-03-14T14:33:12.610240 #135] INFO -- LoiParser: 1% Found: 0 (Elapsed: 3 ms)
47
+ I, [2023-03-14T14:33:12.616639 #135] INFO -- LoiParser: 2% Found: 0 (Elapsed: 10 ms)
48
+ I, [2023-03-14T14:33:12.621245 #135] INFO -- LoiParser: 3% Found: 0 (Elapsed: 14 ms)
49
+ I, [2023-03-14T14:33:12.625052 #135] INFO -- LoiParser: 4% Found: 0 (Elapsed: 18 ms)
48
50
  ...
49
- I, [2023-03-08T11:02:13.052564 #99340] INFO -- LoiParser: (99%) current_sn: 2560000/256000000 ...
50
- I, [2023-03-08T11:02:18.651785 #99340] INFO -- LoiParser: (100%) current_sn: 0/256000000 ...
51
- I, [2023-03-08T11:02:18.651856 #99340] INFO -- LoiParser: Gathered 50 serial number(s) in opposition in 578.848939881
52
- I, [2023-03-08T11:02:18.651881 #99340] INFO -- LoiParser: Serials in opposition: [99999999, ...]
53
- => [99999999, ...]
51
+ I, [2023-03-14T14:33:13.116982 #135] INFO -- LoiParser: 99% Found: 50 (Elapsed: 510 ms)
52
+ I, [2023-03-14T14:33:13.120837 #135] INFO -- LoiParser: 100% Found: 50 (Elapsed: 514 ms)
53
+ I, [2023-03-14T14:33:13.130306 #135] INFO -- LoiParser: Gathered 5113 serial number(s) in opposition in 523 ms.
54
54
  ```
55
55
 
56
+ **WARNING:** This gem doesn't have a _callback_ mechanism to pass found serial
57
+ numbers on the go. Instead it stores all the serial numbers found in an Array
58
+ which leads to a overflow failure depending on the machine's memory size.
59
+
60
+ Extracting all the serial numbers was an experiment we did in order to store
61
+ the serial numbers in a database, but we have abondonned this way since it is
62
+ very slow.
63
+
56
64
  ## Development
57
65
 
58
66
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LoiParser
4
+ #
5
+ # LoiParser gem configuration class
6
+ #
7
+ class Configuration
8
+ # file.read length to be used to read the LOI file
9
+ attr_accessor :read_length
10
+
11
+ def initialize
12
+ @read_length = 5000
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LoiParser
4
+ #
5
+ # This module contain various helper methods
6
+ #
7
+ module Helpers
8
+ def self.elapsed_time(elapsed)
9
+ if elapsed < 1
10
+ "#{(elapsed * 1000).to_i} ms"
11
+ elsif elapsed <= 60
12
+ "#{elapsed.to_i} seconds"
13
+ else
14
+ min, sec = elapsed.divmod(60)
15
+
16
+ "#{min} minutes #{sec.to_i} seconds"
17
+ end
18
+ end
19
+
20
+ # 1000 #=> 1,000
21
+ def self.formatted_thousands(number)
22
+ number.to_s.gsub(/\B(?=(...)*\b)/, ',')
23
+ end
24
+ end
25
+ end
@@ -69,9 +69,8 @@ module LoiParser
69
69
 
70
70
  LoiParser.logger.info(
71
71
  "Gathered #{serial_numbers.size} serial number(s) " \
72
- "in opposition in #{elapsed_time_in_minutes} minutes."
72
+ "in opposition in #{elapsed_time_in_minutes}."
73
73
  )
74
- LoiParser.logger.info "Serials in opposition: #{serial_numbers.inspect}"
75
74
 
76
75
  serial_numbers
77
76
  end
@@ -93,43 +92,46 @@ module LoiParser
93
92
  header
94
93
  end
95
94
 
96
- def current_byte_pos
95
+ def current_bytes_pos
97
96
  @file.pos - @list_start_position - 1
98
97
  end
99
98
 
100
99
  def elapsed_time_in_minutes
101
- # https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
102
- elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @starting
100
+ Helpers.elapsed_time(
101
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - @starting
102
+ )
103
+ end
103
104
 
104
- if elapsed <= 60
105
- "#{elapsed.to_i} seconds"
106
- else
107
- min, sec = elapsed.divmod(60)
105
+ def extract_serial_mumbers_from(bytes)
106
+ # Splits bytes in groups of 8 chars
107
+ splited_bytes = bytes.scan(/.{1,8}/)
108
108
 
109
- # https://stackoverflow.com/a/33684387/1996540
110
- "#{min} minutes #{sec.to_i} seconds"
111
- end
112
- end
109
+ splited_bytes.each.with_index(1).flat_map do |byte, bindex|
110
+ # Calculates the position in file for the current byte
111
+ current_byte_pos = current_bytes_pos - (splited_bytes.size - bindex)
113
112
 
114
- def extract_serial_mumbers_from(byte)
115
- byte.each_char.with_index(1).map do |bit, index|
116
- next if bit == '0'
113
+ # Go through the 8 bites looking for '1'
114
+ byte.each_char.with_index(1).map do |bit, index|
115
+ next if bit == '0'
117
116
 
118
- ((current_byte_pos * 8) + (8 - index))
117
+ # Calculates the serial number from the position in the file plus the
118
+ # offset of the bit
119
+ ((current_byte_pos * 8) + (8 - index))
120
+ end
119
121
  end.compact
120
122
  end
121
123
 
122
124
  def find_all_serial_numbers_in_opposition
123
125
  serial_numbers = []
124
126
 
125
- until (readed = @file.read(1)).nil?
126
- byte = readed.unpack1('B*')
127
+ until (readed = @file.read(LoiParser.configuration.read_length)).nil?
128
+ bytes = readed.unpack1('B*')
127
129
 
128
130
  print_percentage(serial_numbers.size)
129
131
 
130
- next if byte == '00000000'
132
+ next unless bytes.include?('1')
131
133
 
132
- serial_numbers |= extract_serial_mumbers_from(byte)
134
+ serial_numbers |= extract_serial_mumbers_from(bytes)
133
135
  end
134
136
 
135
137
  serial_numbers
@@ -149,8 +151,8 @@ module LoiParser
149
151
  return if percentage == @current_percentage
150
152
 
151
153
  LoiParser.logger.info(
152
- "(#{percentage}%) pos: #{@file.pos} (Elapsed: " \
153
- "#{elapsed_time_in_minutes} | Found: #{found}) ..."
154
+ "#{percentage}% Found: #{Helpers.formatted_thousands(found)} (" \
155
+ "Elapsed: #{elapsed_time_in_minutes})"
154
156
  )
155
157
 
156
158
  @current_percentage = percentage
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LoiParser
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/loi_parser.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'logger'
4
4
 
5
+ require_relative 'loi_parser/configuration'
6
+ require_relative 'loi_parser/helpers'
5
7
  require_relative 'loi_parser/reader'
6
8
  require_relative 'loi_parser/version'
7
9
 
@@ -9,6 +11,14 @@ module LoiParser # rubocop:disable Style/Documentation
9
11
  class << self
10
12
  attr_writer :logger
11
13
 
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(configuration)
20
+ end
21
+
12
22
  def logger
13
23
  @logger ||= Logger.new($stdout).tap do |log|
14
24
  log.progname = name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loi_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pharmony
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-13 00:00:00.000000000 Z
11
+ date: 2023-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logger
@@ -46,6 +46,8 @@ files:
46
46
  - bin/console
47
47
  - bin/setup
48
48
  - lib/loi_parser.rb
49
+ - lib/loi_parser/configuration.rb
50
+ - lib/loi_parser/helpers.rb
49
51
  - lib/loi_parser/reader.rb
50
52
  - lib/loi_parser/version.rb
51
53
  - loi_parser.gemspec