hotdog 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 551436c6cb264d187db243f60da58a14f38cb820
4
- data.tar.gz: f68bd1dd186fa326af8541b9ea3ae4ae95075d44
3
+ metadata.gz: 12b1b97f8c77f181a665ba52db0725c613d638a7
4
+ data.tar.gz: 73be8bb8526df6c4bb95811e6e56de196fae0a73
5
5
  SHA512:
6
- metadata.gz: f85d09c3f08d2cce6fec0d9d73654e86b2ac6fa8abaa6b2682047f8b4daeeedb6985dc7a0f4613bee2076caf43ecf8e0df129f45023d986b14b306ee971a2a38
7
- data.tar.gz: 2e983f2f1508ea69872aa58758ee2a7cfca55e3dcbcba2b612f26e8b543fb8b87a0b849b0d5ef198fb4919c1622c5f2f90bdeb38d73f0ea5ead1e23ad987c11c
6
+ metadata.gz: 602156cf61f52c8f8c14937dcf324b93516ac16b6f0e61c971bcccb124a7827902e313c400fdcb98eb54d83f8ae2cf7a21783d4f1937df18899c20782380224a
7
+ data.tar.gz: eb9ab65fad8b6b25e59eec751cc02b4a99cc0d69fc5f2fc61a7c6df2c9fbce5055928ed2ab8f2c85b7af2a8580f9db51e5534c277fac89a100ab7e9b7064eb27
@@ -155,7 +155,7 @@ module Hotdog
155
155
 
156
156
  def find_library(dirname, name)
157
157
  load_path = $LOAD_PATH.map { |path| File.join(path, dirname) }.select { |path| File.directory?(path) }
158
- libraries = load_path.map { |path| Dir.glob(File.join(path, "*.rb")) }.reduce(:+).select { |file| File.file?(file) }
158
+ libraries = load_path.flat_map { |path| Dir.glob(File.join(path, "*.rb")) }.select { |file| File.file?(file) }
159
159
  rbname = "#{name}.rb"
160
160
  if library = libraries.find { |file| File.basename(file) == rbname }
161
161
  library
@@ -8,13 +8,13 @@ module Hotdog
8
8
  if args.empty?
9
9
  result = execute("SELECT id FROM hosts").to_a.reduce(:+)
10
10
  else
11
- result = args.map { |host_name|
11
+ result = args.flat_map { |host_name|
12
12
  if glob?(host_name)
13
13
  execute("SELECT id FROM hosts WHERE name GLOB ?", [host_name]).to_a.reduce(:+)
14
14
  else
15
15
  execute("SELECT id FROM hosts WHERE name = ?", [host_name]).to_a.reduce(:+)
16
16
  end
17
- }.reduce(:+)
17
+ }
18
18
  end
19
19
  if result && (0 < result.length)
20
20
  result, fields = get_hosts(result)
@@ -305,12 +305,13 @@ module Hotdog
305
305
  else
306
306
  # workaround for "too many terms in compound SELECT"
307
307
  min, max = environment.execute("SELECT MIN(id), MAX(id) FROM hosts ORDER BY id LIMIT 1").first.to_a
308
- slice = 200
309
- (min / slice).upto(max / slice).map { |i|
310
- range = (slice*i)...(slice*(i+1))
308
+ (min / SQLITE_LIMIT_COMPOUND_SELECT).upto(max / SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |i|
309
+ range = (SQLITE_LIMIT_COMPOUND_SELECT*i)...(SQLITE_LIMIT_COMPOUND_SELECT*(i+1))
311
310
  selected = values.select { |n| range === n }
312
- environment.execute("SELECT id FROM hosts WHERE ? <= id AND id < ? AND id NOT IN (%s)" % selected.map { "?" }.join(", "), [range.first, range.last] + selected).map { |row| row.first }
313
- }.reduce(:+).tap do |values|
311
+ q = "SELECT id FROM hosts " \
312
+ "WHERE ? <= id AND id < ? AND id NOT IN (%s);"
313
+ environment.execute(q % selected.map { "?" }.join(", "), [range.first, range.last] + selected).map { |row| row.first }
314
+ }.tap do |values|
314
315
  environment.logger.debug("NOT expr: #{values.length} value(s)")
315
316
  end
316
317
  end
@@ -354,33 +355,32 @@ module Hotdog
354
355
  end
355
356
 
356
357
  def evaluate(environment, options={})
357
- q = []
358
358
  if identifier?
359
359
  if attribute?
360
360
  case identifier
361
361
  when /\Ahost\z/i
362
- q << "SELECT hosts.id FROM hosts"
363
- q << "WHERE hosts.name = ?;"
364
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
362
+ q = "SELECT hosts.id FROM hosts " \
363
+ "WHERE hosts.name = ?;"
364
+ values = environment.execute(q, [attribute]).map { |row| row.first }
365
365
  else
366
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
367
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
368
- q << "WHERE tags.name = ? AND tags.value = ?;"
369
- values = environment.execute(q.join(" "), [identifier, attribute]).map { |row| row.first }
366
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
367
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
368
+ "WHERE tags.name = ? AND tags.value = ?;"
369
+ values = environment.execute(q, [identifier, attribute]).map { |row| row.first }
370
370
  end
371
371
  else
372
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
373
- q << "INNER JOIN hosts ON hosts_tags.host_id = hosts.id"
374
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
375
- q << "WHERE hosts.name = ? OR tags.name = ? OR tags.value = ?;"
376
- values = environment.execute(q.join(" "), [identifier, identifier, identifier]).map { |row| row.first }
372
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
373
+ "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
374
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
375
+ "WHERE hosts.name = ? OR tags.name = ? OR tags.value = ?;"
376
+ values = environment.execute(q, [identifier, identifier, identifier]).map { |row| row.first }
377
377
  end
378
378
  else
379
379
  if attribute?
380
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
381
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
382
- q << "WHERE tags.value = ?;"
383
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
380
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
381
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
382
+ "WHERE tags.value = ?;"
383
+ values = environment.execute(q, [attribute]).map { |row| row.first }
384
384
  else
385
385
  return []
386
386
  end
@@ -431,33 +431,32 @@ module Hotdog
431
431
 
432
432
  class TagGlobExpressionNode < TagExpressionNode
433
433
  def evaluate(environment, options={})
434
- q = []
435
434
  if identifier?
436
435
  if attribute?
437
436
  case identifier
438
437
  when /\Ahost\z/i
439
- q << "SELECT hosts.id FROM hosts"
440
- q << "WHERE hosts.name GLOB ?;"
441
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
438
+ q = "SELECT hosts.id FROM hosts " \
439
+ "WHERE hosts.name GLOB ?;"
440
+ values = environment.execute(q, [attribute]).map { |row| row.first }
442
441
  else
443
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
444
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
445
- q << "WHERE tags.name GLOB ? AND tags.value GLOB ?;"
446
- values = environment.execute(q.join(" "), [identifier, attribute]).map { |row| row.first }
442
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
443
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
444
+ "WHERE tags.name GLOB ? AND tags.value GLOB ?;"
445
+ values = environment.execute(q, [identifier, attribute]).map { |row| row.first }
447
446
  end
448
447
  else
449
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
450
- q << "INNER JOIN hosts ON hosts_tags.host_id = hosts.id"
451
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
452
- q << "WHERE hosts.name GLOB ? OR tags.name GLOB ? OR tags.value GLOB ?;"
453
- values = environment.execute(q.join(" "), [identifier, identifier, identifier]).map { |row| row.first }
448
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
449
+ "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
450
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
451
+ "WHERE hosts.name GLOB ? OR tags.name GLOB ? OR tags.value GLOB ?;"
452
+ values = environment.execute(q, [identifier, identifier, identifier]).map { |row| row.first }
454
453
  end
455
454
  else
456
455
  if attribute?
457
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
458
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
459
- q << "WHERE tags.value GLOB ?;"
460
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
456
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
457
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
458
+ "WHERE tags.value GLOB ?;"
459
+ values = environment.execute(q, [attribute]).map { |row| row.first }
461
460
  else
462
461
  return []
463
462
  end
@@ -478,33 +477,32 @@ module Hotdog
478
477
  end
479
478
 
480
479
  def evaluate(environment, options={})
481
- q = []
482
480
  if identifier?
483
481
  if attribute?
484
482
  case identifier
485
483
  when /\Ahost\z/i
486
- q << "SELECT hosts.id FROM hosts"
487
- q << "WHERE hosts.name REGEXP ?;"
488
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
484
+ q = "SELECT hosts.id FROM hosts " \
485
+ "WHERE hosts.name REGEXP ?;"
486
+ values = environment.execute(q, [attribute]).map { |row| row.first }
489
487
  else
490
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
491
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
492
- q << "WHERE tags.name REGEXP ? AND tags.value REGEXP ?;"
493
- values = environment.execute(q.join(" "), [identifier, attribute]).map { |row| row.first }
488
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
489
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
490
+ "WHERE tags.name REGEXP ? AND tags.value REGEXP ?;"
491
+ values = environment.execute(q, [identifier, attribute]).map { |row| row.first }
494
492
  end
495
493
  else
496
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
497
- q << "INNER JOIN hosts ON hosts_tags.host_id = hosts.id"
498
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
499
- q << "WHERE hosts.name REGEXP ? OR tags.name REGEXP ? OR tags.value REGEXP ?;"
500
- values = environment.execute(q.join(" "), [identifier, identifier, identifier]).map { |row| row.first }
494
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
495
+ "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
496
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
497
+ "WHERE hosts.name REGEXP ? OR tags.name REGEXP ? OR tags.value REGEXP ?;"
498
+ values = environment.execute(q, [identifier, identifier, identifier]).map { |row| row.first }
501
499
  end
502
500
  else
503
501
  if attribute?
504
- q << "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags"
505
- q << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
506
- q << "WHERE tags.value REGEXP ?;"
507
- values = environment.execute(q.join(" "), [attribute]).map { |row| row.first }
502
+ q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
503
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
504
+ "WHERE tags.value REGEXP ?;"
505
+ values = environment.execute(q, [attribute]).map { |row| row.first }
508
506
  else
509
507
  return []
510
508
  end
@@ -7,11 +7,12 @@ require "sqlite3"
7
7
 
8
8
  module Hotdog
9
9
  module Commands
10
+ SQLITE_LIMIT_COMPOUND_SELECT = 500 # TODO: get actual value from `sqlite3_limit()`?
11
+
10
12
  class BaseCommand
11
13
  PERSISTENT_DB = "persistent.db"
12
14
  MASK_DATABASE = 0xffff0000
13
15
  MASK_QUERY = 0x0000ffff
14
- MAX_TERMS = 200
15
16
 
16
17
  def initialize(application)
17
18
  @application = application
@@ -84,22 +85,21 @@ module Hotdog
84
85
  host_names = {}
85
86
  else
86
87
  host_names = Hash[
87
- host_ids.each_slice(MAX_TERMS).map { |host_ids|
88
+ host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
88
89
  execute("SELECT id, name FROM hosts WHERE id IN (%s)" % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.to_a }
89
- }.reduce(:+)
90
+ }
90
91
  ]
91
92
  end
92
- q1 = []
93
- q1 << "SELECT tags.name, GROUP_CONCAT(tags.value, ',') FROM hosts_tags"
94
- q1 << "INNER JOIN hosts ON hosts_tags.host_id = hosts.id"
95
- q1 << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
96
- q1 << "WHERE hosts_tags.host_id = ? AND tags.name IN (%s)"
97
- q1 << "GROUP BY tags.name;"
93
+ q1 = "SELECT tags.name, GROUP_CONCAT(tags.value, ',') FROM hosts_tags " \
94
+ "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
95
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
96
+ "WHERE hosts_tags.host_id = ? AND tags.name IN (%s) " \
97
+ "GROUP BY tags.name;"
98
98
  result = host_ids.map { |host_id|
99
99
  tag_values = Hash[
100
- fields_without_host.each_slice(MAX_TERMS).map { |fields_without_host|
101
- execute(q1.join(" ") % fields_without_host.map { "?" }.join(", "), [host_id] + fields_without_host).map { |row| row.to_a }
102
- }.reduce(:+)
100
+ fields_without_host.each_slice(SQLITE_LIMIT_COMPOUND_SELECT - 1).flat_map { |fields_without_host|
101
+ execute(q1 % fields_without_host.map { "?" }.join(", "), [host_id] + fields_without_host).map { |row| row.to_a }
102
+ }
103
103
  ]
104
104
  fields.map { |tag_name|
105
105
  if tag_name == "host"
@@ -112,42 +112,40 @@ module Hotdog
112
112
  [result, fields]
113
113
  else
114
114
  if @options[:listing]
115
- q1 = []
116
- q1 << "SELECT DISTINCT tags.name FROM hosts_tags"
117
- q1 << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
118
- q1 << "WHERE hosts_tags.host_id IN (%s);"
115
+ q1 = "SELECT DISTINCT tags.name FROM hosts_tags " \
116
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
117
+ "WHERE hosts_tags.host_id IN (%s);"
119
118
  if @options[:primary_tag]
120
119
  fields = [
121
120
  @options[:primary_tag],
122
121
  "host",
123
- ] + host_ids.each_slice(MAX_TERMS).map { |host_ids|
124
- execute(q1.join(" ") % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.first }.reject { |tag_name|
122
+ ] + host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
123
+ execute(q1 % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.first }.reject { |tag_name|
125
124
  tag_name == @options[:primary_tag]
126
125
  }
127
- }.reduce(:+)
126
+ }
128
127
  else
129
128
  fields = [
130
129
  "host",
131
- ] + host_ids.each_slice(MAX_TERMS).map { |host_ids|
132
- execute(q1.join(" ") % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.first }
133
- }.reduce(:+)
130
+ ] + host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
131
+ execute(q1 % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.first }
132
+ }
134
133
  end
135
134
  host_names = Hash[
136
- host_ids.each_slice(MAX_TERMS).map { |host_ids|
135
+ host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
137
136
  execute("SELECT id, name FROM hosts WHERE id IN (%s)" % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.to_a }
138
- }.reduce(:+)
137
+ }
139
138
  ]
140
- q2 = []
141
- q2 << "SELECT tags.name, GROUP_CONCAT(tags.value, ',') FROM hosts_tags"
142
- q2 << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
143
- q2 << "WHERE hosts_tags.host_id = ? AND tags.name IN (%s)"
144
- q2 << "GROUP BY tags.name;"
139
+ q2 = "SELECT tags.name, GROUP_CONCAT(tags.value, ',') FROM hosts_tags " \
140
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
141
+ "WHERE hosts_tags.host_id = ? AND tags.name IN (%s) " \
142
+ "GROUP BY tags.name;"
145
143
  fields_without_host = fields.reject { |tag_name| tag_name == "host" }
146
144
  result = host_ids.map { |host_id|
147
145
  tag_values = Hash[
148
- fields_without_host.each_slice(MAX_TERMS).map { |fields_without_host|
149
- execute(q2.join(" ") % fields_without_host.map { "?" }.join(", "), [host_id] + fields_without_host).map { |row| row.to_a }
150
- }.reduce(:+)
146
+ fields_without_host.each_slice(SQLITE_LIMIT_COMPOUND_SELECT - 1).flat_map { |fields_without_host|
147
+ execute(q2 % fields_without_host.map { "?" }.join(", "), [host_id] + fields_without_host).map { |row| row.to_a }
148
+ }
151
149
  ]
152
150
  fields.map { |tag_name|
153
151
  if tag_name == "host"
@@ -161,20 +159,19 @@ module Hotdog
161
159
  else
162
160
  if @options[:primary_tag]
163
161
  fields = [@options[:primary_tag]]
164
- q1 = []
165
- q1 << "SELECT tags.value FROM hosts_tags"
166
- q1 << "INNER JOIN hosts ON hosts_tags.host_id = hosts.id"
167
- q1 << "INNER JOIN tags ON hosts_tags.tag_id = tags.id"
168
- q1 << "WHERE hosts_tags.host_id IN (%s) AND tags.name = ?;"
169
- result = host_ids.each_slice(MAX_TERMS).map { |host_ids|
170
- execute(q1.join(" ") % host_ids.map { "?" }.join(", "), host_ids + [@options[:primary_tag]]).map { |row| row.to_a }
171
- }.reduce(:+)
162
+ q1 = "SELECT tags.value FROM hosts_tags " \
163
+ "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
164
+ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
165
+ "WHERE hosts_tags.host_id IN (%s) AND tags.name = ?;"
166
+ result = host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT - 1).flat_map { |host_ids|
167
+ execute(q1 % host_ids.map { "?" }.join(", "), host_ids + [@options[:primary_tag]]).map { |row| row.to_a }
168
+ }
172
169
  [result, fields]
173
170
  else
174
171
  fields = ["host"]
175
- result = host_ids.each_slice(MAX_TERMS).map { |host_ids|
172
+ result = host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
176
173
  execute("SELECT name FROM hosts WHERE id IN (%s)" % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.to_a }
177
- }.reduce(:+)
174
+ }
178
175
  [result, fields]
179
176
  end
180
177
  end
@@ -222,23 +219,22 @@ module Hotdog
222
219
 
223
220
  memory_db.transaction do
224
221
  known_tags = all_tags.keys.map { |tag| split_tag(tag) }.uniq
225
- known_tags.each_slice(MAX_TERMS) do |known_tags|
222
+ known_tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 2) do |known_tags|
226
223
  prepare(memory_db, "INSERT OR IGNORE INTO tags (name, value) VALUES %s" % known_tags.map { "(?, ?)" }.join(", ")).execute(known_tags)
227
224
  end
228
225
 
229
226
  known_hosts = all_tags.values.reduce(:+).uniq
230
- known_hosts.each_slice(MAX_TERMS) do |known_hosts|
227
+ known_hosts.each_slice(SQLITE_LIMIT_COMPOUND_SELECT) do |known_hosts|
231
228
  prepare(memory_db, "INSERT OR IGNORE INTO hosts (name) VALUES %s" % known_hosts.map { "(?)" }.join(", ")).execute(known_hosts)
232
229
  end
233
230
 
234
231
  all_tags.each do |tag, hosts|
235
- q = []
236
- q << "INSERT OR REPLACE INTO hosts_tags (host_id, tag_id)"
237
- q << "SELECT host.id, tag.id FROM"
238
- q << "( SELECT id FROM hosts WHERE name IN (%s) ) AS host,"
239
- q << "( SELECT id FROM tags WHERE name = ? AND value = ? LIMIT 1 ) AS tag;"
240
- hosts.each_slice(MAX_TERMS) do |hosts|
241
- prepare(memory_db, q.join(" ") % hosts.map { "?" }.join(", ")).execute(hosts + split_tag(tag))
232
+ q = "INSERT OR REPLACE INTO hosts_tags (host_id, tag_id) " \
233
+ "SELECT host.id, tag.id FROM " \
234
+ "( SELECT id FROM hosts WHERE name IN (%s) ) AS host, " \
235
+ "( SELECT id FROM tags WHERE name = ? AND value = ? LIMIT 1 ) AS tag;"
236
+ hosts.each_slice(SQLITE_LIMIT_COMPOUND_SELECT - 2) do |hosts|
237
+ prepare(memory_db, q % hosts.map { "?" }.join(", ")).execute(hosts + split_tag(tag))
242
238
  end
243
239
  end
244
240
  end
@@ -269,10 +265,10 @@ module Hotdog
269
265
  downtimes = all_downtimes.select { |downtime|
270
266
  # active downtimes
271
267
  downtime["active"] and ( downtime["start"].nil? or downtime["start"] < now ) and ( downtime["end"].nil? or now <= downtime["end"] )
272
- }.map { |downtime|
268
+ }.flat_map { |downtime|
273
269
  # find host scopes
274
270
  downtime["scope"].select { |scope| scope.start_with?("host:") }.map { |scope| scope.sub(/\Ahost:/, "") }
275
- }.reduce(:+) || []
271
+ }
276
272
  if not downtimes.empty?
277
273
  logger.info("ignore host(s) with scheduled downtimes: #{downtimes.inspect}")
278
274
  end
@@ -292,17 +288,17 @@ module Hotdog
292
288
  create_table_hosts_tags(dst)
293
289
 
294
290
  hosts = prepare(src, "SELECT id, name FROM hosts").execute().to_a
295
- hosts.each_slice(MAX_TERMS) do |hosts|
291
+ hosts.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 2) do |hosts|
296
292
  prepare(dst, "INSERT INTO hosts (id, name) VALUES %s" % hosts.map { "(?, ?)" }.join(", ")).execute(hosts)
297
293
  end
298
294
 
299
295
  tags = prepare(src, "SELECT id, name, value FROM tags").execute().to_a
300
- tags.each_slice(MAX_TERMS) do |tags|
296
+ tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 3) do |tags|
301
297
  prepare(dst, "INSERT INTO tags (id, name, value) VALUES %s" % tags.map { "(?, ?, ?)" }.join(", ")).execute(tags)
302
298
  end
303
299
 
304
300
  hosts_tags = prepare(src, "SELECT host_id, tag_id FROM hosts_tags").to_a
305
- hosts_tags.each_slice(MAX_TERMS) do |hosts_tags|
301
+ hosts_tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 2) do |hosts_tags|
306
302
  prepare(dst, "INSERT INTO hosts_tags (host_id, tag_id) VALUES %s" % hosts_tags.map { "(?, ?)" }.join(", ")).execute(hosts_tags)
307
303
  end
308
304
 
@@ -313,49 +309,49 @@ module Hotdog
313
309
  end
314
310
 
315
311
  def create_table_hosts(db)
316
- logger.debug("create_table_hosts()")
317
- q = []
318
- q << "CREATE TABLE IF NOT EXISTS hosts ("
319
- q << "id INTEGER PRIMARY KEY AUTOINCREMENT,"
320
- q << "name VARCHAR(255) NOT NULL COLLATE NOCASE"
321
- q << ");"
322
- db.execute(q.join(" "))
312
+ q = "CREATE TABLE IF NOT EXISTS hosts ( " \
313
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " \
314
+ "name VARCHAR(255) NOT NULL COLLATE NOCASE " \
315
+ ");"
316
+ logger.debug(q)
317
+ db.execute(q)
323
318
  end
324
319
 
325
320
  def create_index_hosts(db)
326
- logger.debug("create_index_hosts()")
327
- db.execute("CREATE UNIQUE INDEX IF NOT EXISTS hosts_name ON hosts ( name )")
321
+ q = "CREATE UNIQUE INDEX IF NOT EXISTS hosts_name ON hosts ( name );"
322
+ logger.debug(q)
323
+ db.execute(q)
328
324
  end
329
325
 
330
326
  def create_table_tags(db)
331
- logger.debug("create_table_tags()")
332
- q = []
333
- q << "CREATE TABLE IF NOT EXISTS tags ("
334
- q << "id INTEGER PRIMARY KEY AUTOINCREMENT,"
335
- q << "name VARCHAR(200) NOT NULL COLLATE NOCASE,"
336
- q << "value VARCHAR(200) NOT NULL COLLATE NOCASE"
337
- q << ");"
338
- db.execute(q.join(" "))
327
+ q = "CREATE TABLE IF NOT EXISTS tags ( " \
328
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " \
329
+ "name VARCHAR(200) NOT NULL COLLATE NOCASE, " \
330
+ "value VARCHAR(200) NOT NULL COLLATE NOCASE " \
331
+ ");"
332
+ logger.debug(q)
333
+ db.execute(q)
339
334
  end
340
335
 
341
336
  def create_index_tags(db)
342
- logger.debug("create_index_tags()")
343
- db.execute("CREATE UNIQUE INDEX IF NOT EXISTS tags_name_value ON tags ( name, value )")
337
+ q = "CREATE UNIQUE INDEX IF NOT EXISTS tags_name_value ON tags ( name, value );"
338
+ logger.debug(q)
339
+ db.execute(q)
344
340
  end
345
341
 
346
342
  def create_table_hosts_tags(db)
347
- logger.debug("create_table_hosts_tags()")
348
- q = []
349
- q << "CREATE TABLE IF NOT EXISTS hosts_tags ("
350
- q << "host_id INTEGER NOT NULL,"
351
- q << "tag_id INTEGER NOT NULL"
352
- q << ");"
353
- db.execute(q.join(" "))
343
+ q = "CREATE TABLE IF NOT EXISTS hosts_tags ( " \
344
+ "host_id INTEGER NOT NULL, " \
345
+ "tag_id INTEGER NOT NULL " \
346
+ ");"
347
+ logger.debug(q)
348
+ db.execute(q)
354
349
  end
355
350
 
356
351
  def create_index_hosts_tags(db)
357
- logger.debug("create_index_hosts_tags()")
358
- db.execute("CREATE UNIQUE INDEX IF NOT EXISTS hosts_tags_host_id_tag_id ON hosts_tags ( host_id, tag_id )")
352
+ q = "CREATE UNIQUE INDEX IF NOT EXISTS hosts_tags_host_id_tag_id ON hosts_tags ( host_id, tag_id );"
353
+ logger.debug(q)
354
+ db.execute(q)
359
355
  end
360
356
  end
361
357
  end
@@ -1,3 +1,3 @@
1
1
  module Hotdog
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotdog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamashita Yuu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-07 00:00:00.000000000 Z
11
+ date: 2015-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler