optic-rails 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 742a1530b45eaca262ba37280f346065e5d56e5ca514fceb5fa64c1bee6b731f
4
- data.tar.gz: c325342f0774b9d3ab8ab6365816a07d1b81aa3d8a673ad6a8f030810f87bd82
3
+ metadata.gz: 8ffe914a1df59184ac225cc960634735a71a437b770ae7ab9c22b4eca2fb6f46
4
+ data.tar.gz: 24912fa3c973cd9d879a480a5f7797c3250b4cfa88e64b112aa9bcff54471efc
5
5
  SHA512:
6
- metadata.gz: f21fcbdc22b6edb52a4fb249afe3362479fe37e9771a7df4be3ece58b0764f4114e3ab343440dc7f26490f23a4feabde3dd7e3fbbcf59ed0bd662b0eb3ee0ae0
7
- data.tar.gz: 9b9fff52ff66296abc2b9b79d964f24804c9dfec1431744939d339e8a01fe7de837ded9ddc739347fae107fa05a6011f1c516c84900f61f30af7e13065dda9d6
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
- pivot_values: pivot.all.as_json, # TODO this is slow and possibly brings in sensitive info
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
- result[:entity_totals] << { name: vertex.name, total: vertex.count }
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
@@ -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 use tagged logger, make it clear which thread log messages are coming from
9
- logger = ::Rails.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.warn "Optic agent disconnected, killing client thread"
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.warn "Stopping worker thread"
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.warn "Supervisor thread detected dead worker, sleeping"
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
@@ -1,5 +1,5 @@
1
1
  module Optic
2
2
  module Rails
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.6'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optic-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Vaynshtok