require "alone" require "csv" require "al_graph" class LoggerController < AlController FILENAME = "/var/log/logger/data-" def action_index() AlTemplate.run( 'index.rhtml' ) end def action_view_table() filename = FILENAME + Time.now.strftime("%Y%m%d") + ".csv" data = CSV.read(filename, :converters=>[:date_time, :float, :float]) rescue [] @data = data.pop(10) AlTemplate.run("view_table.rhtml") end def action_view_graph() @form = AlForm.new( AlDate.new("date", :label=>"日付", :value=>Time.now, :required=>true, :tag_type=>"date") ) if @form.validate target_date = @form[:date] else target_date = Time.now end filename = FILENAME + target_date.strftime("%Y%m%d") + ".csv" data = CSV.read(filename, :converters=>[:date_time, :float, :float]) rescue [] # X軸ラベルを1時間毎で生成する labels = [] 24.times {|i| labels[i * 60] = i } # 24時間分の気圧・気温データを再構築する data_p = Array.new(1440) data_t = Array.new(1440) data.each {|tm, p, t| idx = tm.hour * 60 + tm.minute data_p[idx] = p data_t[idx] = t } if !data_p.empty? graph = AlGraph.new( 700, 200 ) line = graph.add_data_line( data_p ) line.clear_marker() graph.y_axis.interval = 2 graph.y_axis.at_labels[:format] = "%.0f" graph.x_axis.interval = 60 graph.x_axis.labels = labels @trend_p = graph.draw_buffer() end if !data_t.empty? graph = AlGraph.new( 700, 200 ) line = graph.add_data_line( data_t ) line.clear_marker() graph.y_axis.max = 50 graph.y_axis.min = -10 graph.y_axis.at_labels[:format] = "%.0f" graph.x_axis.interval = 60 graph.x_axis.labels = labels @trend_t = graph.draw_buffer() end AlTemplate.run("view_graph.rhtml") end end