zabbirc 0.0.10 → 0.0.11
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 +4 -4
- data/lib/zabbirc/id_shortener.rb +73 -0
- data/lib/zabbirc/irc/plugin.rb +1 -1
- data/lib/zabbirc/irc/plugin_methods.rb +7 -2
- data/lib/zabbirc/zabbix/event.rb +6 -1
- data/lib/zabbirc.rb +4 -0
- data/spec/bot_spec.rb +2 -1
- data/spec/id_shortener_spec.rb +37 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af897ba02297f7617cf59c4e8e3566ad2dcadd1d
|
4
|
+
data.tar.gz: ca9355720c51cc0105dcc535392729a545a5bd78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 528f8d9b60be512037ff03aca50c6f0c2424e5ac830230260fb3e332d7d9de8ddbed2f4d8e3ab7137f490494937fe48acae3aecc615e38c38e319e9abb466205
|
7
|
+
data.tar.gz: 46e162cbaaf6d118fcf98cf7880d2fa77e753d6e0120b6ebc16efe5a4c3d634ca160869274424454e2a3c1f8c404f276e468cc16e9c84af9c0ba3934ce4f0690
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Zabbirc
|
2
|
+
class IdShortener
|
3
|
+
CHAR_SET = ("A".."Z").to_a + (2..9).to_a.collect(&:to_s)
|
4
|
+
|
5
|
+
IdTranslation = Struct.new(:id, :shorten_id, :used_at) do
|
6
|
+
def touch
|
7
|
+
self.used_at = Time.now
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize max_cache_size=10_000, shorten_id_length = 3
|
12
|
+
@max_cache_size = max_cache_size
|
13
|
+
@shorten_id_length = shorten_id_length
|
14
|
+
@mutex = Mutex.new
|
15
|
+
@ids = {}
|
16
|
+
@shorten_ids = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_id shorten_id
|
20
|
+
@mutex.synchronize do
|
21
|
+
translation = @shorten_ids[shorten_id.upcase]
|
22
|
+
if translation
|
23
|
+
translation.touch
|
24
|
+
translation.id
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_shorten_id id
|
32
|
+
@mutex.synchronize do
|
33
|
+
translation = @ids[id]
|
34
|
+
return translation.shorten_id if translation
|
35
|
+
begin
|
36
|
+
shorten_id = generate_shorten_id
|
37
|
+
end while @shorten_ids.key?(shorten_id)
|
38
|
+
translation = register_translation id, shorten_id
|
39
|
+
translation.shorten_id.upcase
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def cache_size
|
44
|
+
@ids.size
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def register_translation id, shorten_id
|
50
|
+
cleanup_translations
|
51
|
+
translation = IdTranslation.new(id, shorten_id, Time.now)
|
52
|
+
@ids[id] = translation
|
53
|
+
@shorten_ids[shorten_id] = translation
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_shorten_id
|
57
|
+
shorten_id = ""
|
58
|
+
@shorten_id_length.times do
|
59
|
+
shorten_id << CHAR_SET[rand(CHAR_SET.size)]
|
60
|
+
end
|
61
|
+
shorten_id
|
62
|
+
end
|
63
|
+
|
64
|
+
def cleanup_translations
|
65
|
+
return if cache_size < @max_cache_size
|
66
|
+
translations_to_delete = Array.wrap(@ids.values.sort{|x,y| y.used_at <=> x.used_at }[@max_cache_size-1..-1])
|
67
|
+
translations_to_delete.each do |translation|
|
68
|
+
@ids.delete(translation.id)
|
69
|
+
@shorten_ids.delete(translation.shorten_id)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/zabbirc/irc/plugin.rb
CHANGED
@@ -33,7 +33,7 @@ module Zabbirc
|
|
33
33
|
|
34
34
|
# ACK
|
35
35
|
register_help "ack", "Acknowledges event with message. Usage: !ack <event-id> <ack-message>"
|
36
|
-
match /ack (
|
36
|
+
match /ack ([a-zA-Z0-9]+) (.*)/, method: :acknowledge_event
|
37
37
|
match /(ack)\s*$/, method: :zabbirc_help_detail
|
38
38
|
match /(ack) (?:[^ ]+)\s*$/, method: :zabbirc_help_detail
|
39
39
|
end
|
@@ -220,12 +220,17 @@ module Zabbirc
|
|
220
220
|
false
|
221
221
|
end
|
222
222
|
|
223
|
-
def find_event m,
|
223
|
+
def find_event m, short_eventid
|
224
224
|
op = get_op m
|
225
225
|
begin
|
226
|
+
eventid = Zabbirc.events_id_shortener.get_id short_eventid
|
227
|
+
unless eventid
|
228
|
+
m.reply "#{op.nick}: Bad event id `#{short_eventid}`"
|
229
|
+
return false
|
230
|
+
end
|
226
231
|
event = Zabbix::Event.find(eventid, {selectHosts: :extend, selectRelatedObject: :extend})
|
227
232
|
if event.nil?
|
228
|
-
m.reply "#{op.nick} Could not find event with id `#{
|
233
|
+
m.reply "#{op.nick} Could not find event with id `#{short_eventid}`"
|
229
234
|
return false
|
230
235
|
end
|
231
236
|
event
|
data/lib/zabbirc/zabbix/event.rb
CHANGED
@@ -55,6 +55,10 @@ module Zabbirc
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
def shorten_id
|
59
|
+
@shorten_id ||= Zabbirc.events_id_shortener.get_shorten_id id
|
60
|
+
end
|
61
|
+
|
58
62
|
alias_method :state, :value
|
59
63
|
|
60
64
|
def message
|
@@ -67,7 +71,7 @@ module Zabbirc
|
|
67
71
|
end
|
68
72
|
|
69
73
|
def label
|
70
|
-
format_label "|%
|
74
|
+
format_label "|%sid| %time [%priority-code] %msg - %state"
|
71
75
|
end
|
72
76
|
|
73
77
|
def format_label fmt
|
@@ -76,6 +80,7 @@ module Zabbirc
|
|
76
80
|
gsub("%time", "#{created_at.to_formatted_s(:short)}").
|
77
81
|
gsub("%msg", "#{message}").
|
78
82
|
gsub("%id", "#{id}").
|
83
|
+
gsub("%sid", "#{shorten_id}").
|
79
84
|
gsub("%state", "#{state}")
|
80
85
|
end
|
81
86
|
|
data/lib/zabbirc.rb
CHANGED
data/spec/bot_spec.rb
CHANGED
@@ -20,8 +20,9 @@ describe Zabbirc::Irc::PluginMethods do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should acknowledge event" do
|
23
|
+
shorten_id = Zabbirc.events_id_shortener.get_shorten_id event.id
|
23
24
|
expect(mock_message).to receive(:reply).with("#{mock_nick}: Event `#{event.label}` acknowledged with message: #{message}")
|
24
|
-
bot.acknowledge_event mock_message,
|
25
|
+
bot.acknowledge_event mock_message, shorten_id, message
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
describe Zabbirc::IdShortener do
|
2
|
+
let(:cache_size) { 5 }
|
3
|
+
let(:shorten_id_length) { 3 }
|
4
|
+
let(:id_shortener) { Zabbirc::IdShortener.new(cache_size, shorten_id_length) }
|
5
|
+
|
6
|
+
it "should generate shorten id" do
|
7
|
+
shorten_id = id_shortener.get_shorten_id(1234)
|
8
|
+
expect(shorten_id).to be_instance_of(String)
|
9
|
+
expect(shorten_id.length).to eq(shorten_id_length)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:original_id) { 1234 }
|
13
|
+
context "retrieving original id" do
|
14
|
+
it "should retrieve original id" do
|
15
|
+
shorten_id = id_shortener.get_shorten_id(original_id)
|
16
|
+
id = id_shortener.get_id(shorten_id)
|
17
|
+
expect(id).to eq(original_id)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be case insensitive" do
|
21
|
+
shorten_id = id_shortener.get_shorten_id(original_id)
|
22
|
+
id = id_shortener.get_id(shorten_id.downcase)
|
23
|
+
expect(id).to eq(original_id)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
it "should clean cache" do
|
29
|
+
(cache_size*2).times do |i|
|
30
|
+
id_shortener.get_shorten_id(i)
|
31
|
+
end
|
32
|
+
expect(id_shortener.cache_size).to be <= cache_size
|
33
|
+
# ensures that cache is being cleand from the end (oldest used records)
|
34
|
+
ids = id_shortener.instance_variable_get(:@ids).keys
|
35
|
+
expect(ids).not_to include(1)
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zabbirc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Filip Zachar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- config/config.rb
|
118
118
|
- lib/zabbirc.rb
|
119
119
|
- lib/zabbirc/configuration.rb
|
120
|
+
- lib/zabbirc/id_shortener.rb
|
120
121
|
- lib/zabbirc/irc/help.rb
|
121
122
|
- lib/zabbirc/irc/plugin.rb
|
122
123
|
- lib/zabbirc/irc/plugin_methods.rb
|
@@ -139,6 +140,7 @@ files:
|
|
139
140
|
- lib/zabbirc/zabbix/trigger.rb
|
140
141
|
- lib/zabbirc/zabbix/user.rb
|
141
142
|
- spec/bot_spec.rb
|
143
|
+
- spec/id_shortener_spec.rb
|
142
144
|
- spec/spec_helper.rb
|
143
145
|
- spec/support/mock_bot.rb
|
144
146
|
- templates/zabbirc_config.rb
|