solarium 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0c5c4f5db3d02c348ed02e9b9ba42629372c598
4
- data.tar.gz: 437ee53bb835aa18834057fc8ca2f0ba72ed52bd
3
+ metadata.gz: f51387872e85c4048dbb50439732faad9a04dfc3
4
+ data.tar.gz: 4ba2ef0da46fcb385a3588752f0ac0b7775fd720
5
5
  SHA512:
6
- metadata.gz: 8ca7ba6a541d4ee16612e006ef10f4df1f047bf93bf74761042b5ca738891c3821d07732aba4d85d35fc93b48943ac58e285170f48fadabb2d4e42123179ff6f
7
- data.tar.gz: 7f4d02d5ed1658045d82cadbf07b20efcbf3c3c27810e31b046b07479075fbaee2c9c730ade15517c2a83f0c409285bc30e45eb1894e906d4684e0aeca42584a
6
+ metadata.gz: a7145c67e114dceafb1503b5c96d286e63b242533eb96b692a5a23d25853ccaecf259f87fab6ab46e09d7948536cbd55bec25ea03e3978775b731f38e343fb8a
7
+ data.tar.gz: b99846c1c02f687f0688d94bb2cbdba63da8775e2925a1379b12abecc128ee40834a0cfbe66aa7d9509316d26b98e863d5d67fa00c29ed616c8b6e3fda8e70ab
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+ # Solarium (C) 2017 Peter "SaberUK" Powell <petpow@saberuk.com>
3
+
4
+ APP_ROOT = File.dirname __dir__
5
+ if Dir.exist? "#{APP_ROOT}/.git"
6
+ $LOAD_PATH.unshift "#{APP_ROOT}/lib"
7
+ end
8
+
9
+ %w(solarium trollop).each do |lib|
10
+ begin
11
+ require lib
12
+ rescue ::Exception => error
13
+ STDERR.puts "A fatal error was encountered whilst loading #{lib}:"
14
+ STDERR.puts "#{error.class}: #{error.message}"
15
+ exit 1
16
+ end
17
+ end
18
+
19
+ APP_NAME = File.basename $PROGRAM_NAME
20
+ options = Trollop::options do
21
+ banner "Usage: #{APP_NAME} [OPTIONS]"
22
+ version Solarium::VERSION
23
+
24
+ opt :chart_format, 'The output format to use for chart generation', default: 'dygraph'
25
+ opt :database_url, 'The URL for connecting to the SQL database', default: 'sqlite://solarium.sq3'
26
+ opt :include_days, 'The number of days to include in the chart', default: 30
27
+ opt :output_dir, 'The directory to write generated charts to', default: "#{Dir.pwd}/solarium-chart"
28
+ end
29
+
30
+ Trollop::die :database_url, 'is not a valid URL' unless options[:database_url] =~ /^#{URI.regexp}$/
31
+ Trollop::die :include_days, 'is not a valid day count' unless options[:include_days] > 0
32
+
33
+ begin
34
+ name = options[:chart_format].downcase
35
+ require "solarium/#{name}/#{name}"
36
+ rescue ::Exception => error
37
+ STDERR.puts "A fatal error was encountered whilst loading the #{name} module:"
38
+ STDERR.puts "#{error.class}: #{error.message}"
39
+ exit 1
40
+ end
41
+
42
+ database = Solarium::Database.new options[:database_url]
43
+ unless database.error.nil?
44
+ STDERR.puts "A fatal error was encountered whilst connecting to the database:"
45
+ STDERR.puts "#{database.error.class}: #{database.error.message}"
46
+ STDERR.puts database.error.backtrace
47
+ exit 1
48
+ end
49
+
50
+ data = database.select_days options[:include_days]
51
+ Solarium::Chart.generate options[:output_dir], data
data/bin/solarium-collect CHANGED
@@ -21,7 +21,7 @@ options = Trollop::options do
21
21
  banner "Usage: #{APP_NAME} [OPTIONS]"
22
22
  version Solarium::VERSION
23
23
 
24
- opt :database_url, 'The URL for connecting to the SQL database', default: 'sqlite://production.sq3'
24
+ opt :database_url, 'The URL for connecting to the SQL database', default: 'sqlite://solarium.sq3'
25
25
  opt :envoy_url, 'The URL of the Envoy web interface', default: 'http://192.168.1.200/'
26
26
  opt :print_stats, 'Print generation statistics after collection', default: false
27
27
  end
@@ -31,6 +31,16 @@ module Solarium
31
31
  @connection[:solarium].insert row
32
32
  end
33
33
 
34
+ # Public: Reads the specified number of days worth of data from the database.
35
+ #
36
+ # days - The number of days worth of data to read.
37
+ def select_days days
38
+ data = @connection[:solarium].where do
39
+ time > DateTime.now - days
40
+ end
41
+ return data.to_a
42
+ end
43
+
34
44
  # Internal: Connects to the database and creates the table if required.
35
45
  #
36
46
  # url - The URL for connecting to the SQL database.
@@ -0,0 +1,39 @@
1
+ # Solarium (C) 2017 Peter "SaberUK" Powell <petpow@saberuk.com>
2
+
3
+ require 'fileutils'
4
+
5
+ module Solarium
6
+
7
+ # Public: Generates charts using the Dygraph API
8
+ module Chart
9
+
10
+ # Public: Generates charts and writes them to the output directory.
11
+ #
12
+ # output_dir - The directory to write the generated charts to.
13
+ # data - The data to use when generating the chart.
14
+ def self.generate output_dir, data
15
+ FileUtils.mkdir_p output_dir unless Dir.exist? output_dir
16
+ FileUtils.cp "#{__dir__}/index.html", output_dir
17
+
18
+ self.generate_csv "#{output_dir}/generation.csv", data, 'Watts', :now
19
+ self.generate_csv "#{output_dir}/today.csv", data, 'Watt Hours', :today
20
+ self.generate_csv "#{output_dir}/week.csv", data, 'Watt Hours', :week
21
+ self.generate_csv "#{output_dir}/lifetime.csv", data, 'Watt Hours', :lifetime
22
+ end
23
+
24
+ # Internal: Generates a CSV table for a single chart.
25
+ #
26
+ # file - The file to write the CSV table to.
27
+ # data - The data to use when generating the CSV table.
28
+ # title - The title to use for this table.
29
+ # column - The column of data to use for this CSV table.
30
+ def self.generate_csv file, data, title, column
31
+ File.open file, 'w' do |fh|
32
+ fh.puts "Time, #{title}"
33
+ data.each do |entry|
34
+ fh.puts "#{entry[:time]}, #{entry[column]}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -6,7 +6,7 @@ module Solarium
6
6
  VERSION_MAJOR = 0
7
7
 
8
8
  # Public: The version number which is incremented when new features are added.
9
- VERSION_MINOR = 1
9
+ VERSION_MINOR = 2
10
10
 
11
11
  # Public: The version number which is incremented when bugs are fixed.
12
12
  VERSION_PATCH = 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solarium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter "SaberUK" Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-14 00:00:00.000000000 Z
11
+ date: 2017-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -112,10 +112,12 @@ executables:
112
112
  extensions: []
113
113
  extra_rdoc_files: []
114
114
  files:
115
+ - bin/solarium-chart
115
116
  - bin/solarium-collect
116
117
  - lib/solarium.rb
117
118
  - lib/solarium/collector.rb
118
119
  - lib/solarium/database.rb
120
+ - lib/solarium/dygraph/dygraph.rb
119
121
  - lib/solarium/version.rb
120
122
  homepage: https://github.com/SaberUK/solarium
121
123
  licenses: