forest_liana 1.1.4 → 1.1.5

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
  SHA1:
3
- metadata.gz: 5975bb644deb9a1deb05b7f09f8cd868cb773106
4
- data.tar.gz: b804bb7a0556460a562a6868ffded6db950fae6f
3
+ metadata.gz: ab193b4d5ba1ab3f423b97f0a63a84d07f4d3a76
4
+ data.tar.gz: 4f81f272beb7a53fb8bed1957794d3bed6c3d842
5
5
  SHA512:
6
- metadata.gz: 6c964bb3bf9be24f4116c13ed3985b8ca3125cf1eff8b2b41c0282141cfd8648b13fb35a41c54cc7315cece54e6ae5f628d85acc5de14a42fa73eaf42f573a10
7
- data.tar.gz: 5329c9f914afeb1119f0d408020d08cc53325583d4c21a9d737a0a8fcacde4aa8d2741d5ef6011085b6511bae7ef11d5089d9281ce4731d3999d147cbbcc3345
6
+ metadata.gz: be7c4e2f3c385efa39cebc0b6be9f8210ebe2ffa01be7f18e17b0513f204ec3546b4d9066af36e4f4a7fe74ce8caa14307b637bab8c77169d22f787728503cef
7
+ data.tar.gz: d9e62a3fd3231f2fdc260baf6babc43ba0e939d9420a1a0d24a3b21d7a2636b270ccd571b62260269ef4d1bc6397d74c6841f2ec7b208acfbbe126df73c15b4c
@@ -0,0 +1,43 @@
1
+ module ForestLiana
2
+ class StatsController < ForestLiana::ApplicationController
3
+ before_filter :find_resource
4
+
5
+ def show
6
+ case stat_params[:type].try(:downcase)
7
+ when 'value'
8
+ stat = ValueStatGetter.new(@resource, stat_params)
9
+ when 'pie'
10
+ stat = PieStatGetter.new(@resource, stat_params)
11
+ when 'line'
12
+ stat = LineStatGetter.new(@resource, stat_params)
13
+ end
14
+
15
+ stat.perform
16
+ if stat.record
17
+ render json: serialize_model(stat.record)
18
+ else
19
+ render json: {status: 404}, status: :not_found
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def find_resource
26
+ @resource = SchemaUtils.find_model_from_table_name(params[:collection])
27
+
28
+ if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
29
+ render json: {status: 404}, status: :not_found
30
+ end
31
+ end
32
+
33
+ def stat_params
34
+ params.require(:stat).permit(:type, :collection, :aggregate, :time_range,
35
+ :aggregate_field, :group_by_field,
36
+ :group_by_date_field, :filters => [
37
+ :field, :value
38
+ ])
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -0,0 +1,26 @@
1
+ module ForestLiana
2
+ class Stat
3
+
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+ include ActiveModel::Serialization
7
+ extend ActiveModel::Naming
8
+
9
+ attr_accessor :value
10
+
11
+ def initialize(attributes = {})
12
+ attributes.each do |name, value|
13
+ send("#{name}=", value)
14
+ end
15
+ end
16
+
17
+ def persisted?
18
+ false
19
+ end
20
+
21
+ def id
22
+ SecureRandom.uuid
23
+ end
24
+
25
+ end
26
+ end
@@ -20,6 +20,8 @@ module ForestLiana
20
20
  "ForestLiana::StripeCardSerializer"
21
21
  elsif active_record_class == Stripe::Invoice
22
22
  "ForestLiana::StripeInvoiceSerializer"
23
+ elsif active_record_class == ForestLiana::Stat
24
+ "ForestLiana::StatSerializer"
23
25
  else
24
26
  class_name = active_record_class.table_name.classify
25
27
  module_name = class_name.deconstantize
@@ -0,0 +1,31 @@
1
+ module ForestLiana
2
+ class StatSerializer
3
+ include JSONAPI::Serializer
4
+
5
+ attribute :value
6
+
7
+ def type
8
+ 'stats'
9
+ end
10
+
11
+ def format_name(attribute_name)
12
+ attribute_name.to_s
13
+ end
14
+
15
+ def unformat_name(attribute_name)
16
+ attribute_name.to_s.underscore
17
+ end
18
+
19
+ def self_link
20
+ nil
21
+ end
22
+
23
+ def relationship_self_link(attribute_name)
24
+ nil
25
+ end
26
+
27
+ def relationship_related_link(attribute_name)
28
+ nil
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,76 @@
1
+ module ForestLiana
2
+ class LineStatGetter
3
+ attr_accessor :record
4
+
5
+ def initialize(resource, params)
6
+ @resource = resource
7
+ @params = params
8
+ @populates = {}
9
+ end
10
+
11
+ def perform
12
+ value = @resource
13
+
14
+ @params[:filters].try(:each) do |filter|
15
+ operator, filter_value = OperatorValueParser.parse(filter[:value])
16
+ value = value.where("#{filter[:field]} #{operator} '#{filter_value}'")
17
+ end
18
+
19
+ value = value.send(time_range, @params[:group_by_date_field])
20
+ value = value.group(group_by_field || :id) if group_by_field
21
+
22
+ value = value.send(@params[:aggregate].downcase, @params[:aggregate_field])
23
+ .map do |k, v|
24
+ if k.kind_of?(Array)
25
+ {
26
+ label: k[0],
27
+ values: {
28
+ key: populate(k[1]),
29
+ value: v
30
+ }
31
+ }
32
+ else
33
+ {
34
+ label: k,
35
+ values: {
36
+ value: v
37
+ }
38
+ }
39
+ end
40
+ end
41
+
42
+ @record = Stat.new(value: value)
43
+ end
44
+
45
+ private
46
+
47
+ def group_by_field
48
+ field_name = @params[:group_by_field]
49
+ association = @resource.reflect_on_association(field_name)
50
+
51
+ if association
52
+ association.foreign_key
53
+ else
54
+ field_name
55
+ end
56
+ end
57
+
58
+ def populate(id)
59
+ @populates[id] ||= begin
60
+ field_name = @params[:group_by_field]
61
+ association = @resource.reflect_on_association(field_name)
62
+
63
+ if association
64
+ association.klass.find(id)
65
+ else
66
+ id
67
+ end
68
+ end
69
+ end
70
+
71
+ def time_range
72
+ "group_by_#{@params[:time_range].try(:downcase) || 'month'}"
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ module ForestLiana
2
+ class PieStatGetter
3
+ attr_accessor :record
4
+
5
+ def initialize(resource, params)
6
+ @resource = resource
7
+ @params = params
8
+ end
9
+
10
+ def perform
11
+ if @params[:group_by_field]
12
+ value = @resource
13
+
14
+ @params[:filters].try(:each) do |filter|
15
+ operator, filter_value = OperatorValueParser.parse(filter[:value])
16
+ value = value.where("#{filter[:field]} #{operator} '#{filter_value}'")
17
+ end
18
+
19
+
20
+ value = value.group(@params[:group_by_field])
21
+ .send(@params[:aggregate].downcase, @params[:aggregate_field])
22
+ .map do |k, v|
23
+ { key: k, value: v }
24
+ end
25
+
26
+ @record = Stat.new(value: value)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ module ForestLiana
2
+ class ValueStatGetter
3
+ attr_accessor :record
4
+
5
+ def initialize(resource, params)
6
+ @resource = resource
7
+ @params = params
8
+ end
9
+
10
+ def perform
11
+ return if @params[:aggregate].blank?
12
+ value = @resource
13
+
14
+ @params[:filters].try(:each) do |filter|
15
+ operator, filter_value = OperatorValueParser.parse(filter[:value])
16
+ value = value.where("#{filter[:field]} #{operator} '#{filter_value}'")
17
+ end
18
+
19
+ @record = Stat.new(value: count(value))
20
+ end
21
+
22
+ private
23
+
24
+ def count(value)
25
+ uniq = @params[:aggregate].downcase == 'count'
26
+
27
+ if Rails::VERSION::MAJOR == 4
28
+ value = value.uniq if uniq
29
+ value.send(@params[:aggregate].downcase, aggregate_field)
30
+ else
31
+ value.send(@params[:aggregate], aggregate_field, distinct: uniq)
32
+ end
33
+ end
34
+
35
+ def aggregate_field
36
+ @params[:aggregate_field] || :id
37
+ end
38
+
39
+ end
40
+ end
data/config/routes.rb CHANGED
@@ -5,6 +5,9 @@ ForestLiana::Engine.routes.draw do
5
5
  get 'stripe_cards' => 'stripe#cards'
6
6
  get 'stripe_invoices' => 'stripe#invoices'
7
7
 
8
+ # Stats
9
+ post 'stats' => 'stats#show'
10
+
8
11
  # CRUD
9
12
  get '/' => 'apimaps#index'
10
13
  get ':collection' => 'resources#index'
@@ -1,6 +1,7 @@
1
1
  require 'rack/cors'
2
2
  require 'stripe'
3
3
  require 'jsonapi-serializers'
4
+ require 'groupdate'
4
5
 
5
6
  module ForestLiana
6
7
  class Engine < ::Rails::Engine
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-23 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: groupdate
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Forest Rails Liana
98
112
  email:
99
113
  - sandro@munda.me
@@ -111,17 +125,22 @@ files:
111
125
  - app/controllers/forest_liana/application_controller.rb
112
126
  - app/controllers/forest_liana/associations_controller.rb
113
127
  - app/controllers/forest_liana/resources_controller.rb
128
+ - app/controllers/forest_liana/stats_controller.rb
114
129
  - app/controllers/forest_liana/stripe_controller.rb
115
130
  - app/deserializers/forest_liana/resource_deserializer.rb
116
131
  - app/helpers/forest_liana/application_helper.rb
117
132
  - app/models/forest_liana/collection.rb
133
+ - app/models/forest_liana/stat.rb
118
134
  - app/serializers/forest_liana/apimap_serializer.rb
119
135
  - app/serializers/forest_liana/serializer_factory.rb
136
+ - app/serializers/forest_liana/stat_serializer.rb
120
137
  - app/serializers/forest_liana/stripe_card_serializer.rb
121
138
  - app/serializers/forest_liana/stripe_invoice_serializer.rb
122
139
  - app/serializers/forest_liana/stripe_payment_serializer.rb
123
140
  - app/services/forest_liana/has_many_getter.rb
141
+ - app/services/forest_liana/line_stat_getter.rb
124
142
  - app/services/forest_liana/operator_value_parser.rb
143
+ - app/services/forest_liana/pie_stat_getter.rb
125
144
  - app/services/forest_liana/resource_getter.rb
126
145
  - app/services/forest_liana/resource_updater.rb
127
146
  - app/services/forest_liana/resources_getter.rb
@@ -132,6 +151,7 @@ files:
132
151
  - app/services/forest_liana/stripe_invoices_getter.rb
133
152
  - app/services/forest_liana/stripe_payment_refunder.rb
134
153
  - app/services/forest_liana/stripe_payments_getter.rb
154
+ - app/services/forest_liana/value_stat_getter.rb
135
155
  - app/views/layouts/forest_liana/application.html.erb
136
156
  - config/initializers/arel-helpers.rb
137
157
  - config/initializers/mimetype.rb
@@ -320,4 +340,3 @@ test_files:
320
340
  - test/services/forest_liana/resources_getter_test.rb
321
341
  - test/services/forest_liana/schema_adapter_test.rb
322
342
  - test/test_helper.rb
323
- has_rdoc: