optic-rails 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4862c212205f52f736e51c5b74da8d0bd141d941fe27adb6b872efa715a59aac
4
- data.tar.gz: b77ac04f9ae6cccc80168fc5a77ef27c249c75a6767859611022e7aac941d1ef
3
+ metadata.gz: 318832502d533bdc4808c18673502e01d2d6328045d6584b2dfe50466bff7adb
4
+ data.tar.gz: ef480667fe0aedd7abd18d56b38dd29562f3718777bda9cc71c3705d066fc684
5
5
  SHA512:
6
- metadata.gz: 93a490e91f071a3c1089b4072a6ef273f291115d27c828d5e73593329e6ca924833a64991fca2ad0a6534bc52dcef2c357bca92bee4c53b0dbd9959967451a1a
7
- data.tar.gz: 5298316a14f2c3668c7a1f2968ee7d0ca5df05a797b8ea75d32f165d54672de3e5441233c327171f5d86b627fb6152e1615f45b3fc7cdc56e5a217de4448e0c1
6
+ metadata.gz: 7594caeb737ca76c140c718d8cecc85080f3a7c3b21c9c547602b98f56630df389c17e299610a9452015f5d1222a3f80a97f6c61bde4072f137f4addcf1ceb23
7
+ data.tar.gz: a02c3eac8e8f416c633961ff71e8cbe23c3193ca426ae80170436761d422a4ca1740c15fca08ca0ea46b0a09a213facef9a0a1e76d45ea10dae92d7b55555c39
@@ -13,7 +13,7 @@ module Optic
13
13
  {
14
14
  name: klass.name,
15
15
  table_name: klass.table_name,
16
- attribute_names: klass.attribute_names,
16
+ entity_attribute_names: klass.attribute_names,
17
17
  table_exists: klass.table_exists?,
18
18
  associations: klass.reflect_on_all_associations.map do |reflection|
19
19
  {
@@ -29,15 +29,35 @@ module Optic
29
29
  end
30
30
  end
31
31
 
32
- def metrics
32
+ def metrics(instructions)
33
33
  with_connection do |connection|
34
- result = { entity_totals: [] }
35
- active_record_klasses.find_all(&:table_exists?).each do |klass|
36
- count_query = klass.unscoped.select("COUNT(*)").to_sql
37
- result[:entity_totals] << { name: klass.name, total: connection.execute(count_query).first["count"] }
34
+ instructions.map do |instruction|
35
+ name = instruction["entity"]
36
+ entity = name.constantize.unscoped
37
+
38
+ query =
39
+ if pivot_name = instruction["pivot"]
40
+ pivot = pivot_name.constantize
41
+ join_path = instruction["join_path"]
42
+ joins = join_path.reverse.map(&:to_sym).inject { |acc, elt| { elt => acc } }
43
+ entity
44
+ .joins(joins)
45
+ .group(qualified_primary_key(pivot))
46
+ .select(qualified_primary_key(pivot), qualified_column(pivot, instruction["pivot_attribute_name"]), "COUNT(*)")
47
+ .to_sql
48
+ else
49
+ entity.select("COUNT(*)").to_sql
50
+ end
51
+
52
+ { metric_configuration_id: instruction["metric_configuration_id"], result: connection.execute(query).to_a }
38
53
  end
54
+ end
55
+ end
39
56
 
40
- result
57
+ def instances(pivot_name)
58
+ with_connection do |connection|
59
+ pivot = pivot_name.constantize
60
+ { instances: connection.execute(pivot.unscoped.select("*").to_sql).to_a }
41
61
  end
42
62
  end
43
63
 
@@ -57,6 +77,14 @@ module Optic
57
77
  end
58
78
  end
59
79
 
80
+ def qualified_column(entity, attribute)
81
+ %Q|"#{entity.table_name}"."#{attribute}"|
82
+ end
83
+
84
+ def qualified_primary_key(entity)
85
+ qualified_column(entity, entity.primary_key)
86
+ end
87
+
60
88
  def active_record_klasses
61
89
  base_klass = ApplicationRecord rescue ActiveRecord::Base
62
90
  ObjectSpace.each_object(Class).find_all { |klass| klass < base_klass }
@@ -44,8 +44,8 @@ module Optic
44
44
  EventMachine.stop
45
45
  end
46
46
 
47
- client.errored do |msg|
48
- logger.warn "Optic agent error: #{msg}"
47
+ client.errored do |data|
48
+ logger.warn "Optic agent error: #{data}"
49
49
  EventMachine.stop
50
50
  end
51
51
 
@@ -53,28 +53,29 @@ module Optic
53
53
  logger.debug "Optic agent subscribed"
54
54
  end
55
55
 
56
- client.pinged do |msg|
57
- logger.debug "Optic agent pinged: #{msg}"
56
+ client.pinged do |data|
57
+ logger.debug "Optic agent pinged: #{data}"
58
58
  client.perform "pong", message: {}
59
59
  end
60
60
 
61
61
  # called whenever a message is received from the server
62
- client.received do |message|
63
- logger.debug "Optic agent received: #{message}"
64
- command = message["message"]["command"]
62
+ client.received do |data|
63
+ logger.debug "Optic agent received: #{data}"
64
+ message = data["message"]
65
+ command = message["command"]
65
66
 
66
67
  case command
67
68
  when "request_schema"
68
- logger.debug "Optic agent got schema request"
69
69
  client.perform "schema", message: Optic::Rails.entities
70
- logger.debug "Optic agent sent schema"
71
70
  when "request_metrics"
72
- logger.debug "Optic agent got metrics request"
73
- client.perform "metrics", message: Optic::Rails.metrics
74
- logger.debug "Optic agent sent metrics"
71
+ client.perform "metrics", message: Optic::Rails.metrics(message["instructions"])
72
+ when "request_instances"
73
+ client.perform "instances", message: Optic::Rails.instances(message["pivot"])
75
74
  else
76
75
  logger.warn "Optic agent got unknown command: #{command}"
77
76
  end
77
+
78
+ logger.debug "Optic agent completed command: #{command}"
78
79
  end
79
80
  end
80
81
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Optic
4
4
  module Rails
5
- VERSION = "1.0.1"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optic-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Vaynshtok
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-01 00:00:00.000000000 Z
11
+ date: 2018-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: action_cable_client