Class: Focuslight::Data

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/focuslight/data.rb

Instance Method Summary collapse

Methods included from Logger

included, #logger, #logger=

Constructor Details

#initializeData



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/focuslight/data.rb', line 12

def initialize
  @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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/focuslight/data.rb', line 204

def create_complex(service, section, graph, args)
  description = args[:description]
  sort = args[:sort]
  meta = Focuslight::ComplexGraph.meta_clean(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: meta,
                         created_at: now,
                         updated_at: now,
                         )
  get_complex(service, section, graph)
end

#create_tablesObject



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
65
# File 'lib/focuslight/data.rb', line 28

def create_tables
  ntype = number_type
  DB.transaction do

    DB.create_table :graphs do
      primary_key :id, Bignum # 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, Bignum, default: 0, null: false
      column :color, String, default: "#00CC00", null: false
      column :ulimit, ntype, default: 1000000000000000, null: false
      column :llimit, ntype, default: 0, null: false
      column :type, String, default: "AREA", null: false
      String :meta, text: true
      column :created_at, Bignum, null: false
      column :updated_at, Bignum, null: false
      unique [:service_name, :section_name, :graph_name]
    end

    DB.create_table :complex_graphs do
      primary_key :id, Bignum # 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, Bignum, default: 0, null: false
      String :meta, text: true
      column :created_at, Bignum, null: false
      column :updated_at, Bignum, null: false
      unique [:service_name, :section_name, :graph_name]
    end
  end
end

#execute(*args) ⇒ Object



67
68
69
# File 'lib/focuslight/data.rb', line 67

def execute(*args)
  DB.run(*args)
end

#get(service, section, graph) ⇒ Object



77
78
79
80
# File 'lib/focuslight/data.rb', line 77

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_allObject



253
254
255
256
257
# File 'lib/focuslight/data.rb', line 253

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_idObject



245
246
247
# File 'lib/focuslight/data.rb', line 245

def get_all_complex_graph_id
  @complex_graphs.select(:id).all
end

#get_all_complex_graph_nameObject



249
250
251
# File 'lib/focuslight/data.rb', line 249

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_allObject



183
184
185
186
# File 'lib/focuslight/data.rb', line 183

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_idObject



175
176
177
# File 'lib/focuslight/data.rb', line 175

def get_all_graph_id
  @graphs.select(:id).all
end

#get_all_graph_nameObject



179
180
181
# File 'lib/focuslight/data.rb', line 179

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



82
83
84
85
# File 'lib/focuslight/data.rb', line 82

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)



87
88
89
90
91
# File 'lib/focuslight/data.rb', line 87

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



194
195
196
197
# File 'lib/focuslight/data.rb', line 194

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



199
200
201
202
# File 'lib/focuslight/data.rb', line 199

def get_complex_by_id(id)
  data = @complex_graphs.where(id: id).first
  data && Focuslight::Graph.concrete(data)
end

#get_graphs(service, section) ⇒ Object



169
170
171
172
173
# File 'lib/focuslight/data.rb', line 169

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



163
164
165
166
167
# File 'lib/focuslight/data.rb', line 163

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_servicesObject



157
158
159
160
161
# File 'lib/focuslight/data.rb', line 157

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_typeObject



24
25
26
# File 'lib/focuslight/data.rb', line 24

def number_type
  @floatings ? Float : Bignum
end

#remove(id) ⇒ Object



188
189
190
191
192
# File 'lib/focuslight/data.rb', line 188

def remove(id)
  DB.transaction do
    @graphs.where(id: id).delete
  end
end

#remove_complex(id) ⇒ Object



241
242
243
# File 'lib/focuslight/data.rb', line 241

def remove_complex(id)
  @complex_graphs.where(id: id).delete
end

#transactionObject



71
72
73
74
75
# File 'lib/focuslight/data.rb', line 71

def transaction
  DB.transaction do
    yield DB
  end
end

#update(service_name, section_name, graph_name, number, mode, color) ⇒ Object



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
129
# File 'lib/focuslight/data.rb', line 93

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/focuslight/data.rb', line 222

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.meta,
            updated_at: Time.now.to_i,
            )

  get_complex_by_id(id)
end

#update_graph(id, args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/focuslight/data.rb', line 131

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.meta
            )
  true
end

#update_graph_description(id, description) ⇒ Object



152
153
154
155
# File 'lib/focuslight/data.rb', line 152

def update_graph_description(id, description)
  @graphs.where(id: id).update(description: description)
  true
end