aboutsummaryrefslogtreecommitdiff
path: root/lib/longboat
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2020-03-02 10:21:32 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2020-03-02 10:21:32 +0000
commitcd0f6b103553f547bc21a8447e785c31f7ce858c (patch)
tree9002b355cb4038f6c3e31d2e65ce8e74bceca3fa /lib/longboat
parent6c192e01dad7e9947c466eff521fe22ac775fcd6 (diff)
Use classes properly
Diffstat (limited to 'lib/longboat')
-rw-r--r--lib/longboat/collector.rb44
-rw-r--r--lib/longboat/jobs/slurm_job_states.rb (renamed from lib/longboat/jobs.rb)15
-rw-r--r--lib/longboat/metrics.rb43
3 files changed, 53 insertions, 49 deletions
diff --git a/lib/longboat/collector.rb b/lib/longboat/collector.rb
new file mode 100644
index 0000000..ccf103b
--- /dev/null
+++ b/lib/longboat/collector.rb
@@ -0,0 +1,44 @@
+module Longboat
+ class Collector
+ def initialize
+ @metrics = {}
+ @jobs = []
+ end
+
+ def report!(name, value, help: nil, type: nil, labels: {})
+ @metrics[name] ||= {help: help, type: type}
+ @metrics[name][labels] = value
+ end
+
+ def metrics
+ timestamp = (Time.now.to_f * 1000).to_i
+ res = ""
+ @metrics.each do |name, metric|
+ res << "#HELP #{name} #{metric[:help]}\n" unless metric[:help].nil?
+ res << "#TYPE #{name} #{metric[:type]}\n" unless metric[:type].nil?
+
+ metric.each do |labels, value|
+ next if labels == :help
+ next if labels == :type
+ labellist = []
+ labels.each do |k, v|
+ labellist << "#{k}=\"#{v}\""
+ end
+ labellist = labellist.join(",")
+ res << "#{name}{#{labellist}} #{value} #{timestamp}\n"
+ end
+ end
+ res
+ end
+
+ def register!(job)
+ @jobs << job
+ end
+
+ def collect!
+ @jobs.each do |job|
+ job.run
+ end
+ end
+ end
+end
diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs/slurm_job_states.rb
index eadae03..74c7999 100644
--- a/lib/longboat/jobs.rb
+++ b/lib/longboat/jobs/slurm_job_states.rb
@@ -1,7 +1,12 @@
module Longboat
module Jobs
- class << self
- def collect!
+ class SlurmJobStates
+ def initialize(collector)
+ @collector = collector
+ @collector.register!(self)
+ end
+
+ def run
start_time = (Time.now - 15 * 60).strftime("%H:%M:%S")
raw = `sacct -a -P -o State -S #{start_time}`.lines.map(&:strip)[1..-1]
@@ -12,8 +17,8 @@ module Longboat
end
tally.each do |state, number|
- Longboat::Metrics.report!(
- "longboat_slurm_job_state",
+ @collector.report!(
+ "longboat_slurm_job_states",
number,
help: "Number of jobs in each state",
type: "gauge",
@@ -22,7 +27,5 @@ module Longboat
end
end
end
-
- Longboat::Metrics.register!(self)
end
end
diff --git a/lib/longboat/metrics.rb b/lib/longboat/metrics.rb
deleted file mode 100644
index 58b1cbe..0000000
--- a/lib/longboat/metrics.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-module Longboat
- module Metrics
- class << self
- def report!(name, value, help: nil, type: nil, labels: {})
- @metrics ||= {}
- @metrics[name] ||= {help: help, type: type}
- @metrics[name][labels] = value
- end
-
- def to_s
- timestamp = (Time.now.to_f * 1000).to_i
- res = ""
- (@metrics || {}).each do |name, metric|
- res << "#HELP #{name} #{metric[:help]}\n" unless metric[:help].nil?
- res << "#TYPE #{name} #{metric[:type]}\n" unless metric[:type].nil?
-
- metric.each do |labels, value|
- next if labels == :help
- next if labels == :type
- labellist = []
- labels.each do |k, v|
- labellist << "#{k}=\"#{v}\""
- end
- labellist = labellist.join(",")
- res << "#{name}{#{labellist}} #{value} #{timestamp}\n"
- end
- end
- res
- end
-
- def register!(collector)
- @collectors ||= []
- @collectors << collector
- end
-
- def collect!
- (@collectors || []).each do |collector|
- collector.collect!
- end
- end
- end
- end
-end