flukso4r 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of flukso4r might be problematic. Click here for more details.

data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env ruby
2
+ # This file is part of fluksobot
3
+ # (c) 2009 Mathias Dalheimer, md@gonium.net
4
+ #
5
+ # FluksoBot is free software; you can
6
+ # redistribute it and/or modify it under the terms of the GNU General Public
7
+ # License as published by the Free Software Foundation; either version 2 of
8
+ # the License, or any later version.
9
+ #
10
+ # FluksoBot is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with FluksoBot; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ # Read the fluksobot location
20
+ libpath=File.join(File.dirname(__FILE__), '..', 'lib')
21
+ $:.unshift << libpath
22
+ #puts "Using libraty path #{$:.join(":")}"
23
+
24
+ require 'rubygems'
25
+ require 'optparse'
26
+ require 'ostruct'
27
+ require 'flukso'
28
+
29
+ ###
30
+ ## Commandline parser
31
+ #
32
+ class Optparser
33
+ CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
34
+ CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
35
+ #
36
+ # Return a structure describing the options.
37
+ #
38
+ def self.parse(args)
39
+ # The options specified on the command line will be collected in *options*.
40
+ # We set default values here.
41
+ options = OpenStruct.new
42
+ options.inplace = false
43
+ options.encoding = "utf8"
44
+ options.verbose = false
45
+ opts = OptionParser.new do |opts|
46
+ opts.banner = "Usage: #{$0} [options]"
47
+ opts.separator ""
48
+ opts.separator "Specific options:"
49
+ opts.on("-c", "--config FILE", "The configuration file to use.") do |file|
50
+ options.config_file = file
51
+ end
52
+ opts.on("-f", "--timeformat {utc|local}", "What format to use for the timestamp.") do |timeformat|
53
+ options.timeformat=timeformat
54
+ end
55
+ opts.on("-s", "--starttime UTC_TIMESTAMP", "Timestamp of the first reading to export.") do |starttime|
56
+ options.starttime=starttime
57
+ end
58
+ opts.on("-e", "--endtime UTC_TIMESTAMP", "Timestamp of the last reading to export.") do |endtime|
59
+ options.endtime=endtime
60
+ end
61
+
62
+ # Boolean switch.
63
+ opts.on("-f", "--force", "Ignore warnings") do |f|
64
+ options.force = f
65
+ end
66
+ opts.on("-v", "--verbose", "Run verbosely") do |v|
67
+ options.verbose = v
68
+ end
69
+ opts.on_tail("-h", "--help", "Show this message") do
70
+ puts opts
71
+ exit
72
+ end
73
+ end
74
+ opts.parse!(args)
75
+ options
76
+ end
77
+ end
78
+
79
+ ###
80
+ ## Script startup
81
+ #
82
+ options = Optparser.parse(ARGV)
83
+ $verbose = options.verbose
84
+ $force = options.force
85
+
86
+ $stderr.puts "Flukso data export script."
87
+ # check date format parameters.
88
+ timeformat=nil;
89
+ options.timeformat = "utc" unless options.timeformat != nil
90
+ exporter=nil;
91
+ if options.timeformat.upcase == "UTC"
92
+ exporter=Flukso::UTC_Exporter.new();
93
+ elsif options.timeformat.upcase == "LOCAL"
94
+ exporter=Flukso::Local_Exporter.new();
95
+ elsif options.timeformat.upcase == "DETAIL"
96
+ exporter=Flukso::Detail_Exporter.new();
97
+ else
98
+ puts "You specified an invalid timeformat. Valid formats are 'utc' and 'local'."
99
+ exit (-4)
100
+ end
101
+ # check config file.
102
+ if options.config_file == nil
103
+ puts "Please provide a configuration file... (-h for details)."
104
+ exit(-1);
105
+ end
106
+ if not File.exists?(options.config_file)
107
+ puts " Configuration file #{options.config_file} not found - no database configuration available!"
108
+ exit(-3);
109
+ else
110
+ $CONFIG=YAML.load_file(options.config_file);
111
+ puts "Using this configuration:" if $verbose
112
+ puts "#{$CONFIG}" if $verbose
113
+ dbfile=$CONFIG['DB_FILE']
114
+ end
115
+ if $verbose
116
+ puts " Using database file #{dbfile}"
117
+ end
118
+ # check and expand database path.
119
+ if not File.exists?(dbfile)
120
+ puts " Database does not exists. Aborting."
121
+ exit(-2);
122
+ end
123
+
124
+ # TODO: Rewrite this, it should reflect a pipe pattern...
125
+
126
+ select_starttime=nil
127
+ select_endtime=nil
128
+ # do we need to select a timerange?
129
+ if options.starttime != nil
130
+ select_starttime = options.starttime.to_i
131
+ if options.endtime != nil
132
+ select_endtime=options.endtime.to_i
133
+ if select_endtime < select_starttime
134
+ puts "The end time is earlier than the start time - please check your values."
135
+ exit(-6);
136
+ end
137
+ else
138
+ puts "You specified a start time to select, but the end time is missing."
139
+ exit(-5);
140
+ end
141
+ end
142
+
143
+ db=Flukso::FluksoDB.open($CONFIG["DB_FILE"], $CONFIG["DB_TABLE_NAME"]);
144
+ exporter.print_header
145
+ begin
146
+ if select_starttime != nil
147
+ # We need to select the values in the specified timeframe.
148
+ db.each_reading_between(select_starttime, select_endtime) {|reading|
149
+ exporter.format_reading(reading);
150
+ }
151
+ else
152
+ # No selection was given. Iterate over all values in the DB.
153
+ db.each_reading{|reading|
154
+ exporter.format_reading(reading);
155
+ }
156
+ end
157
+ rescue Flukso::ElementNotFoundError => e
158
+ puts "Empty database - not able to provide last reading."
159
+ end
160
+ db.close
161
+
@@ -118,6 +118,35 @@ SQL
118
118
  def find_reading_last
119
119
  return find_last_reading(1)[0];
120
120
  end
121
+ # Avoid redundant code. Takes a block and executes it on the query
122
+ # data. See http://innig.net/software/ruby/closures-in-ruby.rb
123
+ def helper_yield_results(querystatement, block)
124
+ @db.execute(querystatement) {|row|
125
+ value=row['VALUE'].to_f;
126
+ #timestamp=Time.at(row['EPOCHTIME'].to_f);
127
+ timestamp=row['EPOCHTIME'].to_f;
128
+ #puts "Creating new UTCReading: #{timestamp}, #{value}"
129
+ reading=UTCReading.new(timestamp, value);
130
+ block.call(reading);
131
+ }
132
+ end
133
+ def each_reading_between(starttime, endtime, &block)
134
+ stmt=<<-SQL
135
+ SELECT * FROM #{@tablename}
136
+ WHERE epochtime >= #{starttime} AND epochtime <= #{endtime}
137
+ ORDER BY epochtime ASC;
138
+ SQL
139
+ #puts "Using statement #{stmt}" if $verbose
140
+ helper_yield_results(stmt, block);
141
+ end
142
+ def each_reading(&block)
143
+ stmt=<<-SQL
144
+ SELECT * FROM #{@tablename}
145
+ order by epochtime ASC;
146
+ SQL
147
+ #puts "Using statement #{stmt}" if $verbose
148
+ helper_yield_results(stmt, block);
149
+ end
121
150
  def find_last_reading(amount)
122
151
  if not amount.class==Fixnum
123
152
  raise "Must provide the number of last readings desired as an Fixnum."
@@ -0,0 +1,54 @@
1
+ # This file is part of flukso4r
2
+ # (c) 2010 Mathias Dalheimer, md@gonium.net
3
+ #
4
+ # flukso4r is free software; you can
5
+ # redistribute it and/or modify it under the terms of the GNU General Public
6
+ # License as published by the Free Software Foundation; either version 2 of
7
+ # the License, or any later version.
8
+ #
9
+ # FluksoBot is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with FluksoBot; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Flukso
19
+ class UTC_Exporter
20
+ def initialize()
21
+ $stderr.puts("Using UTC Exporter");
22
+ end
23
+ def print_header()
24
+ puts "timestamp\tvalue"
25
+ end
26
+ def format_reading(reading)
27
+ puts "#{reading.utc_timestamp}\t#{reading.value}"
28
+ end
29
+ end
30
+ class Local_Exporter
31
+ def initialize()
32
+ $stderr.puts("Using Local Time Exporter");
33
+ end
34
+ def print_header()
35
+ puts "localtime\tvalue"
36
+ end
37
+ def format_reading(reading)
38
+ puts "#{Time.at(reading.utc_timestamp)}\t#{reading.value}"
39
+ end
40
+ end
41
+ class Detail_Exporter
42
+ def initialize()
43
+ $stderr.puts("Using Detail Exporter");
44
+ end
45
+ def print_header()
46
+ puts "date \ttime\tdayofweek\tperiod\tvalue"
47
+ end
48
+ def format_reading(reading)
49
+ currenttime=Time.at(reading.utc_timestamp);
50
+ period=(currenttime.hour * 4) + (currenttime.min / 15);
51
+ puts "#{currenttime.strftime("%Y-%m-%d\t%H:%M\t%a")}\t#{period}\t#{reading.value}"
52
+ end
53
+ end
54
+ end
data/lib/flukso.rb CHANGED
@@ -55,3 +55,4 @@ require File.join(directory, 'flukso', 'reading')
55
55
  require File.join(directory, 'flukso', 'request')
56
56
  require File.join(directory, 'flukso', 'api')
57
57
  require File.join(directory, 'flukso', 'database')
58
+ require File.join(directory, 'flukso', 'export')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flukso4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Dalheimer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-10 00:00:00 +01:00
12
+ date: 2010-03-28 00:00:00 +01:00
13
13
  default_executable: flukso_query
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -50,10 +50,12 @@ files:
50
50
  - VERSION
51
51
  - bin/flukso_archive_watts
52
52
  - bin/flukso_create_db
53
+ - bin/flukso_export_db
53
54
  - bin/flukso_query
54
55
  - lib/flukso.rb
55
56
  - lib/flukso/api.rb
56
57
  - lib/flukso/database.rb
58
+ - lib/flukso/export.rb
57
59
  - lib/flukso/http_auth.rb
58
60
  - lib/flukso/reading.rb
59
61
  - lib/flukso/request.rb