Class: Focuslight::Data
- Inherits:
-
Object
- Object
- Focuslight::Data
- Includes:
- Logger
- Defined in:
- lib/focuslight/data.rb
Instance Method Summary collapse
- #create_complex(service, section, graph, args) ⇒ Object
- #create_tables ⇒ Object
- #execute(*args) ⇒ Object
- #get(service, section, graph) ⇒ Object
- #get_all_complex_graph_all ⇒ Object
- #get_all_complex_graph_id ⇒ Object
- #get_all_complex_graph_name ⇒ Object
- #get_all_graph_all ⇒ Object
- #get_all_graph_id ⇒ Object
- #get_all_graph_name ⇒ Object
- #get_by_id(id) ⇒ Object
-
#get_by_id_for_rrdupdate(id, target = :normal) ⇒ Object
get_by_id_for_rrdupdate_short == get_by_id_for_rrdupdate(id, :short).
- #get_complex(service, section, graph) ⇒ Object
- #get_complex_by_id(id) ⇒ Object
- #get_graphs(service, section) ⇒ Object
- #get_sections(service) ⇒ Object
- #get_services ⇒ Object
-
#initialize ⇒ Data
constructor
A new instance of Data.
- #number_type ⇒ Object
- #remove(id) ⇒ Object
- #remove_complex(id) ⇒ Object
- #transaction ⇒ Object
- #update(service_name, section_name, graph_name, number, mode, color) ⇒ Object
- #update_complex(id, args) ⇒ Object
- #update_graph(id, args) ⇒ Object
- #update_graph_description(id, description) ⇒ Object
Methods included from Logger
Constructor Details
#initialize ⇒ Data
Returns a new instance of Data.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/focuslight/data.rb', line 10 def initialize @db = Sequel.connect(Focuslight::Config.get(:dburl), logger: Focuslight.logger, timeout: Focuslight::Config.get(:dbtimeout)) @datadir = Focuslight::Config.get(:datadir) @floatings = Focuslight::Config.get(:float_support) == "y" if @db.database_type == :sqlite @db.run 'PRAGMA journal_mode = WAL' @db.run 'PRAGMA synchronous = NORMAL' end @graphs = @db.from(:graphs) @complex_graphs = @db.from(:complex_graphs) end |
Instance Method Details
#create_complex(service, section, graph, args) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/focuslight/data.rb', line 203 def create_complex(service, section, graph, args) description = args[:description] sort = args[:sort] = Focuslight::ComplexGraph.(args).to_json now = Time.now.to_i @complex_graphs.insert( service_name: service, section_name: section, graph_name: graph, description: description, sort: sort.to_i, meta: , created_at: now, updated_at: now, ) get_complex(service, section, graph) end |
#create_tables ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/focuslight/data.rb', line 27 def create_tables ntype = number_type @db.transaction do @db.create_table :graphs do primary_key :id, Integer # Notice that SQLite actually creates integer primary key column :service_name, String, null: false column :section_name, String, null: false column :graph_name, String, null: false column :number, ntype, default: 0 column :mode, String, default: "gauge", null: false column :description, String, default: "", null: false column :sort, Integer, default: 0, null: false column :color, String, default: "#00CC00", null: false column :ulimit, ntype, default: 1000_0000_0000_0000, null: false column :llimit, ntype, default: 0, null: false column :type, String, default: "AREA", null: false String :meta, text: true column :created_at, Integer, null: false column :updated_at, Integer, null: false unique [:service_name, :section_name, :graph_name] end @db.create_table :complex_graphs do primary_key :id, Integer # Notice that SQLite actually creates integer primary key column :service_name, String, null: false column :section_name, String, null: false column :graph_name, String, null: false column :number, ntype, default: 0 column :description, String, default: "", null: false column :sort, Integer, default: 0, null: false String :meta, text: true column :created_at, Integer, null: false column :updated_at, Integer, null: false unique [:service_name, :section_name, :graph_name] end end end |
#execute(*args) ⇒ Object
66 67 68 |
# File 'lib/focuslight/data.rb', line 66 def execute(*args) @db.run(*args) end |
#get(service, section, graph) ⇒ Object
76 77 78 79 |
# File 'lib/focuslight/data.rb', line 76 def get(service, section, graph) data = @graphs.where(service_name: service, section_name: section, graph_name: graph).first data && Focuslight::Graph.concrete(data) end |
#get_all_complex_graph_all ⇒ Object
252 253 254 255 256 |
# File 'lib/focuslight/data.rb', line 252 def get_all_complex_graph_all rows = @complex_graphs.reverse_order(:service_name, :section_name, :graph_name) return [] unless rows rows.map{|row| Focuslight::Graph.concrete(row)} end |
#get_all_complex_graph_id ⇒ Object
244 245 246 |
# File 'lib/focuslight/data.rb', line 244 def get_all_complex_graph_id @complex_graphs.select(:id).all end |
#get_all_complex_graph_name ⇒ Object
248 249 250 |
# File 'lib/focuslight/data.rb', line 248 def get_all_complex_graph_name @complex_graphs.select(:id, :service_name, :section_name, :graph_name).reverse_order(:service_name, :section_name, :graph_name).all end |
#get_all_graph_all ⇒ Object
182 183 184 185 |
# File 'lib/focuslight/data.rb', line 182 def get_all_graph_all rows = @graphs.reverse_order(:service_name, :section_name, :graph_name).all rows.map{|row| Focuslight::Graph.concrete(row)} end |
#get_all_graph_id ⇒ Object
174 175 176 |
# File 'lib/focuslight/data.rb', line 174 def get_all_graph_id @graphs.select(:id).all end |
#get_all_graph_name ⇒ Object
178 179 180 |
# File 'lib/focuslight/data.rb', line 178 def get_all_graph_name @graphs.select(:id, :service_name, :section_name, :graph_name).reverse_order(:service_name, :section_name, :graph_name).all end |
#get_by_id(id) ⇒ Object
81 82 83 84 |
# File 'lib/focuslight/data.rb', line 81 def get_by_id(id) data = @graphs[id: id] data && Focuslight::Graph.concrete(data) end |
#get_by_id_for_rrdupdate(id, target = :normal) ⇒ Object
get_by_id_for_rrdupdate_short == get_by_id_for_rrdupdate(id, :short)
86 87 88 89 90 |
# File 'lib/focuslight/data.rb', line 86 def get_by_id_for_rrdupdate(id, target=:normal) # get_by_id_for_rrdupdate_short == get_by_id_for_rrdupdate(id, :short) data = @graphs[id: id] return nil unless data graph = Focuslight::Graph.concrete(data) end |
#get_complex(service, section, graph) ⇒ Object
193 194 195 196 |
# File 'lib/focuslight/data.rb', line 193 def get_complex(service, section, graph) data = @complex_graphs.where(service_name: service, section_name: section, graph_name: graph).first data && Focuslight::Graph.concrete(data) end |
#get_complex_by_id(id) ⇒ Object
198 199 200 201 |
# File 'lib/focuslight/data.rb', line 198 def get_complex_by_id(id) data = @complex_graphs.where(id: id).first data && Focuslight::Graph.concrete(data) end |
#get_graphs(service, section) ⇒ Object
168 169 170 171 172 |
# File 'lib/focuslight/data.rb', line 168 def get_graphs(service, section) rows1 = @graphs.order(Sequel.desc(:sort)).where(service_name: service, section_name: section).all rows2 = @complex_graphs.order(Sequel.desc(:sort)).where(service_name: service, section_name: section).all (rows1 + rows2).map{|row| Focuslight::Graph.concrete(row)}.sort{|a,b| b.sort <=> a.sort} end |
#get_sections(service) ⇒ Object
162 163 164 165 166 |
# File 'lib/focuslight/data.rb', line 162 def get_sections(service) rows1 = @graphs.select(:section_name).order(:section_name).where(service_name: service).all rows2 = @complex_graphs.select(:section_name).order(:section_name).where(service_name: service).all (rows1 + rows2).map{|row| row[:section_name]}.uniq.sort end |
#get_services ⇒ Object
156 157 158 159 160 |
# File 'lib/focuslight/data.rb', line 156 def get_services rows1 = @graphs.order(:service_name).all rows2 = @complex_graphs.order(:service_name).all (rows1 + rows2).map{|row| row[:service_name]}.uniq.sort end |
#number_type ⇒ Object
23 24 25 |
# File 'lib/focuslight/data.rb', line 23 def number_type @floatings ? Float : :Bignum end |
#remove(id) ⇒ Object
187 188 189 190 191 |
# File 'lib/focuslight/data.rb', line 187 def remove(id) @db.transaction do @graphs.where(id: id).delete end end |
#remove_complex(id) ⇒ Object
240 241 242 |
# File 'lib/focuslight/data.rb', line 240 def remove_complex(id) @complex_graphs.where(id: id).delete end |
#transaction ⇒ Object
70 71 72 73 74 |
# File 'lib/focuslight/data.rb', line 70 def transaction @db.transaction do yield @db end end |
#update(service_name, section_name, graph_name, number, mode, color) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/focuslight/data.rb', line 92 def update(service_name, section_name, graph_name, number, mode, color) data = nil @db.transaction do data = @graphs.where(service_name: service_name, section_name: section_name, graph_name: graph_name).first if data graph = Focuslight::Graph.concrete(data) if mode == 'count' number += graph.number end if mode != 'modified' || (mode == 'modified' && graph.number != number) color = graph.color if color.empty? @graphs.where(id: graph.id).update(number: number, mode: mode, color: color, updated_at: Time.now.to_i) end else color = '#' + ['33', '66', '99', 'cc'].shuffle.slice(0,3).join if color.empty? # COLUMNS = %w(service_name section_name graph_name number mode color llimit created_at updated_at) columns = Focuslight::SimpleGraph::COLUMNS.join(',') # PLACEHOLDERS = COLUMNS.map{|c| '?'} placeholders = Focuslight::SimpleGraph::PLACEHOLDERS.join(',') current_time = Time.now.to_i @graphs.insert( service_name: service_name, section_name: section_name, graph_name: graph_name, number: number, mode: mode, color: color, llimit: -1000000000, created_at: current_time, updated_at: current_time) end data = @graphs.where(service_name: service_name, section_name: section_name, graph_name: graph_name).first end # transaction Focuslight::Graph.concrete(data) end |
#update_complex(id, args) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/focuslight/data.rb', line 221 def update_complex(id, args) graph = get_complex_by_id(id) return nil unless graph graph.update(args) @complex_graphs.where(id: graph.id) .update( service_name: graph.service, section_name: graph.section, graph_name: graph.graph, description: graph.description, sort: graph.sort, meta: graph., updated_at: Time.now.to_i, ) get_complex_by_id(id) end |
#update_graph(id, args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/focuslight/data.rb', line 130 def update_graph(id, args) graph = get_by_id(id) return nil unless graph graph.update(args) @graphs.where(id: graph.id) .update( service_name: graph.service, section_name: graph.section, graph_name: graph.graph, description: graph.description, sort: graph.sort, color: graph.color, type: graph.type, llimit: graph.llimit, ulimit: graph.ulimit, meta: graph. ) true end |
#update_graph_description(id, description) ⇒ Object
151 152 153 154 |
# File 'lib/focuslight/data.rb', line 151 def update_graph_description(id, description) @graphs.where(id: id).update(description: description) true end |