redash-rails 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d728b6acf6f4418e5871eaac344cccda129c705
4
+ data.tar.gz: 6c0b59777abd5dc46081460a4e62ad9b288cb8cd
5
+ SHA512:
6
+ metadata.gz: 5a598d814ef2675cd1bc06b355347ae38502f5db981a6e66446239a2e975f5dd765aa745e7522e6d488ffb756bbcd2855e118d1c16ff06ea0faa6a9033046fb9
7
+ data.tar.gz: 1dc3885712561632e8902d8d764b5c702c9c523ac831872273fcb8d2a95a073f0fe0d77fcc4da6d2e821cb48b0c00aefd57ce60cd963bd8adab1f6671ec613df
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in redash-rails.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kenta Murata
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # Redash and Rails integration
2
+
3
+ This library helps you to integrate re:dash and your Rails apps.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'redash-rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install redash-rails
20
+
21
+ ## Usage
22
+
23
+ ### Installing rails datasource
24
+
25
+ Use `redash-rails-install` command.
26
+
27
+ ```console
28
+ $ redash-rails-install $REDASH_ROOT_DIR
29
+ ```
30
+
31
+ ### Helper library for rails queries
32
+
33
+ [lib/redash.rb](lib/redash.rb) provides helper functions to write queries.
34
+
35
+ The following script is an example usage of these helper functions:
36
+
37
+ ```ruby
38
+ require 'redash'
39
+ include Redash
40
+
41
+ result = {}
42
+
43
+ add_result_column result, 'age', 'Age', 'integer'
44
+ add_result_column result, 'survived_count', 'Survived count', 'integer'
45
+ add_result_column result, 'dead_count', 'Dead count', 'integer'
46
+
47
+ summary = Passenger.find_each.inject({}) do |h, pas|
48
+ if pas.age
49
+ age = (pas.age % 10) * 10
50
+ h[age] ||= []
51
+ h[age][pas.survived] ||= 0
52
+ h[age][pas.survived] += 1
53
+ end
54
+ h
55
+ end
56
+
57
+ summary.each do |age, age_summary|
58
+ add_result_row(result, age: age, survived_count: age_summary[1], dead_count: age_summary[0])
59
+ end
60
+
61
+ commit_to_redash(result)
62
+ ```
63
+
64
+ See [mrkn/redash_rails_demon](https://github.com/mrkn/redash_rails_demo) repository for more details.
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mrkn/redash-rails.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redash/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+
5
+ rails_py = File.expand_path("../../python/redash_rails/query_runner/rails.py", __FILE__)
6
+ redash_root = ARGV[0]
7
+
8
+ FileUtils.install(rails_py, File.join(redash_root, 'redash/query_runner/'), mode: 0644)
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module Redash
4
+ module_function
5
+
6
+ def add_result_column(result, column_name, friendly_name, column_type)
7
+ result[:columns] ||= []
8
+ result[:columns] << { name: column_name, friendly_name: friendly_name, type: column_type }
9
+ end
10
+
11
+ def add_result_row(result, values)
12
+ result[:rows] ||= []
13
+ result[:rows] << values
14
+ end
15
+
16
+ def commit_to_redash(result)
17
+ $stdout.puts JSON.dump(result)
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require "redash/rails/version"
2
+
3
+ module Redash
4
+ module Rails
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Redash
2
+ module Rails
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,83 @@
1
+ import os
2
+ import sys
3
+ import subprocess
4
+ import tempfile
5
+
6
+ from redash.query_runner import *
7
+
8
+
9
+ class Rails(BaseQueryRunner):
10
+ @classmethod
11
+ def configuration_schema(cls):
12
+ return {
13
+ 'type': 'object',
14
+ 'properties': {
15
+ 'environment': {
16
+ 'type': 'string',
17
+ 'title': 'RAILS_ENV'
18
+ },
19
+ 'workdir': {
20
+ 'type': 'string',
21
+ 'title': 'Work directory'
22
+ }
23
+ },
24
+ 'required': ['workdir']
25
+ }
26
+
27
+ @classmethod
28
+ def type(cls):
29
+ return "insecure_script"
30
+
31
+
32
+ def __init__(self, configuration):
33
+ super(Rails, self).__init__(configuration)
34
+ self.syntax = "ruby"
35
+
36
+ def test_connection(self):
37
+ pass
38
+
39
+ def run_query(self, query, user):
40
+ try:
41
+ prev_cwd = os.getcwd()
42
+ os.chdir(self.configuration['workdir'])
43
+
44
+ json_data = None
45
+ error = None
46
+
47
+ query = query.strip()
48
+ query = query.split('*/ ', 2)[1]
49
+ print('--- begin query ---')
50
+ print(query)
51
+ print('--- end query ---')
52
+
53
+ with tempfile.NamedTemporaryFile(delete=False, suffix='rb') as tf:
54
+ tf.write(query)
55
+ script_name = tf.name
56
+ output = subprocess.check_output(['bin/rails', 'runner', script_name])
57
+ os.unlink(script_name)
58
+ print('--- begin output ---')
59
+ print(output)
60
+ print('--- end output ---')
61
+ if output is not None:
62
+ output = output.strip()
63
+ if output != "":
64
+ return output, None
65
+
66
+ error = "Error reading output"
67
+ except subprocess.CalledProcessError as e:
68
+ print('--- begin output ---')
69
+ print(e.output)
70
+ print('--- end output ---')
71
+ return None, str(e)
72
+ except KeyboardInterrupt:
73
+ error = "Query cancelled by user."
74
+ json_data = None
75
+ except Exception as e:
76
+ raise sys.exc_info()[1], None, sys.exc_info()[2]
77
+ finally:
78
+ os.chdir(prev_cwd)
79
+
80
+ return json_data, error
81
+
82
+
83
+ register(Rails)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "redash/rails/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redash-rails"
8
+ spec.version = Redash::Rails::VERSION
9
+ spec.authors = ["Kenta Murata"]
10
+ spec.email = ["mrkn@mrkn.jp"]
11
+
12
+ spec.summary = %q{redash-rails}
13
+ spec.description = %q{redash-rails}
14
+ spec.homepage = "https://github.com/mrkn/redash-rails"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redash-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kenta Murata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: redash-rails
56
+ email:
57
+ - mrkn@mrkn.jp
58
+ executables:
59
+ - redash-rails-install
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/redash-rails-install
73
+ - lib/redash.rb
74
+ - lib/redash/rails.rb
75
+ - lib/redash/rails/version.rb
76
+ - python/redash_rails/query_runner/rails.py
77
+ - redash-rails.gemspec
78
+ homepage: https://github.com/mrkn/redash-rails
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.12
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: redash-rails
102
+ test_files: []