optic-rails 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/optic/rails.rb +12 -4
- data/lib/optic/rails/railtie.rb +7 -6
- data/lib/optic/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ffe914a1df59184ac225cc960634735a71a437b770ae7ab9c22b4eca2fb6f46
|
4
|
+
data.tar.gz: 24912fa3c973cd9d879a480a5f7797c3250b4cfa88e64b112aa9bcff54471efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e96164ddef86ce2cf6f2351f221eb1a0dcf8d03bcfdec9aa70d90b1b160ca6f18135376bf249bcecb9a433a17400596de7e0a47e8ac1afe40b4f29021c7a4df
|
7
|
+
data.tar.gz: 9750272605b12a86c50fa356f2f7e09cf72e3889bb2cf6cea91ed34bb6ab0834409bea1611316271de6987de0441d09491fb03a7dda6d0399b0d9a484899fefc
|
data/lib/optic/rails.rb
CHANGED
@@ -103,13 +103,20 @@ module Optic
|
|
103
103
|
}
|
104
104
|
end
|
105
105
|
|
106
|
+
def self.execute_sql_query(sql)
|
107
|
+
ActiveRecord::Base.connection_pool.with_connection do |connection|
|
108
|
+
connection.execute(sql)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
106
112
|
def self.get_metrics(pivot_name)
|
107
113
|
pivot = pivot_name.constantize
|
108
114
|
|
109
115
|
result = {
|
110
116
|
entity_totals: [],
|
111
117
|
pivot_name: pivot.name,
|
112
|
-
|
118
|
+
# TODO filter columns, spread over multiple queries
|
119
|
+
pivot_values: execute_sql_query(pivot.unscoped.select("*").to_sql).to_a,
|
113
120
|
pivoted_totals: []
|
114
121
|
# TODO also return computed "spanning" tree of objects from the pivot's POV (using the dijkstra paths from below)
|
115
122
|
}
|
@@ -121,7 +128,8 @@ module Optic
|
|
121
128
|
edge_weights = lambda { |_| 1 }
|
122
129
|
|
123
130
|
graph.vertices.each do |vertex|
|
124
|
-
|
131
|
+
count_query = vertex.unscoped.select("COUNT(*)").to_sql
|
132
|
+
result[:entity_totals] << { name: vertex.name, total: execute_sql_query(count_query).first["count"] }
|
125
133
|
|
126
134
|
next if vertex == pivot # Skip pivoted metrics if this is the pivot
|
127
135
|
|
@@ -137,9 +145,9 @@ module Optic
|
|
137
145
|
end
|
138
146
|
|
139
147
|
joins = belongs_to_names.reverse.inject { |acc, elt| { elt => acc } }
|
140
|
-
query = vertex.unscoped.joins(joins).group(qualified_primary_key(pivot))
|
148
|
+
query = vertex.unscoped.joins(joins).group(qualified_primary_key(pivot)).select(qualified_primary_key(pivot), "COUNT(#{qualified_primary_key(vertex)})").to_sql
|
141
149
|
|
142
|
-
result[:pivoted_totals] << { entity_name: vertex.name, totals: query.count }
|
150
|
+
result[:pivoted_totals] << { entity_name: vertex.name, totals: Hash[execute_sql_query(query).map { |record| [record["id"], record["count"]] }] }
|
143
151
|
else
|
144
152
|
p "WARNING: No path from #{vertex.name} to #{pivot.name}"
|
145
153
|
end
|
data/lib/optic/rails/railtie.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
require "action_cable_client"
|
2
2
|
require "eventmachine"
|
3
|
+
require "logger"
|
3
4
|
|
4
5
|
module Optic
|
5
6
|
module Rails
|
6
7
|
class Railtie < ::Rails::Railtie
|
7
8
|
initializer "optic_rails.launch_client_thread" do |app|
|
8
|
-
# TODO
|
9
|
-
logger =
|
9
|
+
# TODO allow log output customization and verbose option
|
10
|
+
logger = Logger.new(STDOUT)
|
11
|
+
logger.level = Logger::WARN
|
10
12
|
|
11
13
|
# Get configuration
|
12
14
|
if !app.config.respond_to? :optic_api_key
|
@@ -30,14 +32,13 @@ module Optic
|
|
30
32
|
end
|
31
33
|
|
32
34
|
client.disconnected do
|
33
|
-
logger.
|
35
|
+
logger.info "Optic agent disconnected, killing client thread"
|
34
36
|
EventMachine.stop
|
35
37
|
end
|
36
38
|
|
37
39
|
client.errored do |msg|
|
38
40
|
logger.warn "Optic agent error: #{msg}"
|
39
41
|
EventMachine.stop
|
40
|
-
# TODO stop worker?
|
41
42
|
end
|
42
43
|
|
43
44
|
client.subscribed do
|
@@ -68,7 +69,7 @@ module Optic
|
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
71
|
-
logger.
|
72
|
+
logger.info "Stopping worker thread"
|
72
73
|
end
|
73
74
|
|
74
75
|
begin
|
@@ -77,7 +78,7 @@ module Optic
|
|
77
78
|
logger.error "Worker thread died with error: #{e}"
|
78
79
|
end
|
79
80
|
|
80
|
-
logger.
|
81
|
+
logger.info "Supervisor thread detected dead worker, sleeping"
|
81
82
|
sleep 5.0
|
82
83
|
logger.debug "Supervisor thread done sleeping"
|
83
84
|
end
|
data/lib/optic/rails/version.rb
CHANGED