optic-rails 0.3.0 → 0.3.2
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 +28 -31
- data/lib/optic/rails/railtie.rb +1 -1
- 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: 06b399f733fcb87bf9a26a5fc1902e064b4ce7e5b32a35253e2b63b88cad331e
|
4
|
+
data.tar.gz: bfaff9398cf437369963fde66ac45adfd6d6f2886ea6888dfb411865943492a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0682290823c71deaf2f14433fc8c0ed0323c9f2c5756f5e6c24c6a8f292bb1abf4591cc37b6eb50a111c5145c520e14c180f653f9285713576c66107b72961e1'
|
7
|
+
data.tar.gz: a9502628fcb48228d913fc337eb0cf4cc73c82f56cae10c685f1224b9639325a2259d37ff0396104eed772860226db6eede29a56b3c4d52f7399e48c45e055b7
|
data/lib/optic/rails.rb
CHANGED
@@ -119,17 +119,15 @@ module Optic
|
|
119
119
|
|
120
120
|
def self.get_metrics(pivot_name)
|
121
121
|
with_connection do |connection|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
pivot_name
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
# TODO also return computed "spanning" tree of objects from the pivot's POV (using the dijkstra paths from below)
|
132
|
-
}
|
122
|
+
result = {entity_totals: []}
|
123
|
+
pivot = nil
|
124
|
+
|
125
|
+
if pivot_name
|
126
|
+
pivot = pivot_name.constantize
|
127
|
+
result[:pivot_name] = pivot.name
|
128
|
+
result[:pivot_values] = connection.execute(pivot.unscoped.select("*").to_sql).to_a
|
129
|
+
result[:pivoted_totals] = []
|
130
|
+
end
|
133
131
|
|
134
132
|
graph = entity_graph
|
135
133
|
|
@@ -141,32 +139,31 @@ module Optic
|
|
141
139
|
count_query = vertex.unscoped.select("COUNT(*)").to_sql
|
142
140
|
result[:entity_totals] << { name: vertex.name, total: connection.execute(count_query).first["count"] }
|
143
141
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
142
|
+
if pivot && vertex != pivot
|
143
|
+
# TODO weight edges to give preference to non-optional belongs_to (and other attributes?)
|
144
|
+
path = graph.dijkstra_shortest_path(edge_weights, vertex, pivot)
|
145
|
+
if path
|
146
|
+
# Generate a SQL query to count the number of vertex instances grouped by pivot id, with appropriate joins from the path
|
147
|
+
belongs_to_names = path.each_cons(2).map do |join_from, join_to|
|
148
|
+
# TODO we shouldn't have to look up the edge again - use a graph model that allows us to annotate the edges with the reflections
|
149
|
+
reflections = join_from.reflect_on_all_associations(:belongs_to).find_all { |reflection| !reflection.options[:polymorphic] && reflection.klass == join_to }
|
150
|
+
# TODO warn if more than one reflection
|
151
|
+
reflection = reflections.min_by { |r| r.options.size }
|
152
|
+
reflection.name
|
153
|
+
end
|
154
|
+
|
155
|
+
joins = belongs_to_names.reverse.inject { |acc, elt| { elt => acc } }
|
156
|
+
query = vertex.unscoped.joins(joins).group(qualified_primary_key(pivot)).select(qualified_primary_key(pivot), "COUNT(*)").to_sql
|
157
|
+
|
158
|
+
result[:pivoted_totals] << { entity_name: vertex.name, totals: Hash[connection.execute(query).map { |record| [record["id"], record["count"]] }] }
|
159
|
+
else
|
160
|
+
# TODO print warning that we couldn't find a path from the pivot to the vertex
|
156
161
|
end
|
157
|
-
|
158
|
-
joins = belongs_to_names.reverse.inject { |acc, elt| { elt => acc } }
|
159
|
-
query = vertex.unscoped.joins(joins).group(qualified_primary_key(pivot)).select(qualified_primary_key(pivot), "COUNT(*)").to_sql
|
160
|
-
|
161
|
-
result[:pivoted_totals] << { entity_name: vertex.name, totals: Hash[connection.execute(query).map { |record| [record["id"], record["count"]] }] }
|
162
|
-
else
|
163
|
-
p "WARNING: No path from #{vertex.name} to #{pivot.name}"
|
164
162
|
end
|
165
163
|
end
|
166
164
|
|
167
165
|
result
|
168
166
|
end
|
169
167
|
end
|
170
|
-
|
171
168
|
end
|
172
169
|
end
|
data/lib/optic/rails/railtie.rb
CHANGED
@@ -25,7 +25,7 @@ module Optic
|
|
25
25
|
end
|
26
26
|
|
27
27
|
api_key = app.config.optic_api_key
|
28
|
-
uri = app.config.respond_to?(:optic_uri) ? app.config.optic_uri : "wss://tikal-api-staging.herokuapp.com"
|
28
|
+
uri = app.config.respond_to?(:optic_uri) ? app.config.optic_uri : "wss://tikal-api-staging.herokuapp.com/cable"
|
29
29
|
|
30
30
|
logger.debug "Starting worker thread"
|
31
31
|
worker = Thread.new do
|
data/lib/optic/rails/version.rb
CHANGED