redash_dynamic_query 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +6 -0
- data/bin/console +2 -2
- data/lib/redash_dynamic_query/client.rb +60 -20
- data/lib/redash_dynamic_query/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbccab07df2c10d248989d87ea9a78fc3ae1db3258ab6f48295226f2f3af73ae
|
4
|
+
data.tar.gz: 9b41d7b04339be2ca31e362231fc1c642edd1025ba018837c0087a873963d7b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 350d58641bec262b0280b1864e50b0eaefb80d76aece85d241db583ef127cb5240764cf70ca6848492adb3c1ed10426a49aaabc655f720d23bf02c5af47647eb
|
7
|
+
data.tar.gz: 96bfd047ea1a0403fe38ea02ce60411b384af93b2e0a5d927af66a78ac4da004e440db2804ed0468900b2b0f69276904e7d47f71e0a49e53a38b006ad0bfcacc
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -6,7 +6,12 @@ PATH
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
coderay (1.1.2)
|
9
10
|
diff-lcs (1.3)
|
11
|
+
method_source (0.9.0)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
10
15
|
rake (10.5.0)
|
11
16
|
rspec (3.8.0)
|
12
17
|
rspec-core (~> 3.8.0)
|
@@ -27,6 +32,7 @@ PLATFORMS
|
|
27
32
|
|
28
33
|
DEPENDENCIES
|
29
34
|
bundler (~> 1.17)
|
35
|
+
pry
|
30
36
|
rake (~> 10.0)
|
31
37
|
redash_dynamic_query!
|
32
38
|
rspec (~> 3.0)
|
data/bin/console
CHANGED
@@ -2,49 +2,89 @@ require 'net/https'
|
|
2
2
|
require 'json'
|
3
3
|
module RedashDynamicQuery
|
4
4
|
class Client
|
5
|
-
attr_reader :api_key, :endpoint, :
|
5
|
+
attr_reader :api_key, :endpoint, :interval, :timeout
|
6
6
|
|
7
|
-
def initialize(user_apikey:, endpoint:,
|
8
|
-
@api_key
|
7
|
+
def initialize(user_apikey:, endpoint:, interval: 0.1, timeout: 10)
|
8
|
+
@api_key = user_apikey
|
9
9
|
@endpoint = endpoint
|
10
|
-
@
|
10
|
+
@interval = interval
|
11
|
+
@timeout = timeout
|
11
12
|
end
|
12
13
|
|
13
14
|
def query(query_id:, params:)
|
14
15
|
job_id = post_job_request(query_id: query_id, params: params)
|
15
16
|
|
17
|
+
raise 'job_id not fuond.' if job_id.nil?
|
18
|
+
|
16
19
|
query_result_id = poll_job(job_id: job_id)
|
17
20
|
|
21
|
+
raise 'query_result_id not fuond.' if query_result_id.nil?
|
22
|
+
|
18
23
|
get_query_result(query_result_id: query_result_id)
|
19
24
|
end
|
20
25
|
|
21
26
|
private
|
22
27
|
|
23
28
|
def post_job_request(query_id:, params:)
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
res = with_error_handling do
|
30
|
+
Net::HTTP.post_form(
|
31
|
+
post_job_url(id: query_id, queries: query_string(params: params)),
|
32
|
+
{}
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
27
36
|
json = JSON.parse(res.body)
|
28
|
-
json.dig('job','id')
|
37
|
+
json.dig('job', 'id')
|
38
|
+
end
|
39
|
+
|
40
|
+
def poll_job(job_id:, count: 0)
|
41
|
+
res = with_error_handling do
|
42
|
+
Net::HTTP.get(poll_job_url(id: job_id))
|
43
|
+
end
|
44
|
+
id = JSON.parse(res).dig('job', 'query_result_id')
|
45
|
+
|
46
|
+
return id unless id.nil?
|
47
|
+
|
48
|
+
raise "wait timeout with #{timeout} seconds." if reach_limit?(count)
|
49
|
+
|
50
|
+
sleep interval
|
51
|
+
poll_job(job_id: job_id, count: count + 1)
|
29
52
|
end
|
30
53
|
|
31
54
|
def get_query_result(query_result_id:)
|
32
|
-
|
33
|
-
|
55
|
+
res = with_error_handling do
|
56
|
+
Net::HTTP.get(query_result_url(id: query_result_id))
|
57
|
+
end
|
58
|
+
|
34
59
|
JSON.parse(res)
|
35
60
|
end
|
36
61
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
62
|
+
def post_job_url(id:, queries:)
|
63
|
+
URI.parse(
|
64
|
+
"#{endpoint}/api/queries/#{id}/refresh?api_key=#{api_key}&#{queries}"
|
65
|
+
)
|
66
|
+
end
|
41
67
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
68
|
+
def poll_job_url(id:)
|
69
|
+
URI.parse("#{endpoint}/api/jobs/#{id}?api_key=#{api_key}")
|
70
|
+
end
|
71
|
+
|
72
|
+
def query_result_url(id:)
|
73
|
+
URI.parse("#{endpoint}/api/query_results/#{id}.json?api_key=#{api_key}")
|
74
|
+
end
|
75
|
+
|
76
|
+
def query_string(params:)
|
77
|
+
URI.encode_www_form(params.transform_keys { |k| "p_#{k}" })
|
78
|
+
end
|
79
|
+
|
80
|
+
def reach_limit?(count)
|
81
|
+
count * interval > timeout
|
82
|
+
end
|
83
|
+
|
84
|
+
def with_error_handling
|
85
|
+
res = yield
|
86
|
+
raise res.body if res.is_a? Net::HTTPClientError
|
87
|
+
res
|
48
88
|
end
|
49
89
|
end
|
50
90
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redash_dynamic_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- swfz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.7.
|
100
|
+
rubygems_version: 2.7.6
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: Redash query with dynamic parameter
|