activecube 0.1.42 → 0.1.45

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: 7074b394ca4949bf0bedae97ac91b59ef8d742cb7c6ab77e5409345d77caa7bd
4
- data.tar.gz: e7872e83b417e7e9d34acd329627e68d8d1424771e507fd345bd4edac0bc04e9
3
+ metadata.gz: 69c1a36a5deeddf200296ab2809a0a0c503b06480ca6fd358585f6184ae560eb
4
+ data.tar.gz: 92fad5df5d1e723f7d6f4caa361e24dcc3aa16fa1b037b2ebe6f9461e739f4de
5
5
  SHA512:
6
- metadata.gz: 693c8954a658b15c5f6f314b189fbff0a13777a8a84fbf1354d70610dabc903ef7be60475a4253e5bdd72e067a4a31aaaddb0821fa4949a4347be0abd5d28c02
7
- data.tar.gz: 2a04854410822d43e2b4e29b53c062cdf59b971137978081147133956fd1108e2bd1b633782347075e7aa81cc9d237d8da92d148f5064d4697ee20d3be56944c
6
+ metadata.gz: 2db7aeb2a92d01dcb7756cba754e4a429d3cbf79bc89fd7f6fa1fcdfdd98f2c42882644c1778aa9ad1fdee5c1f21bb1aad844203ab125e7f980b0e003a86f46d
7
+ data.tar.gz: 2e4ff8cb29e610bf49eb2be5c1b188d623e81b69cfb5bb4cb7ef9c16bd0dbea0f5625b668b4f1e17d0dca76bc37cdc239f44854da351e06aa5421b1e1f63213c
data/Gemfile.lock CHANGED
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- activecube (0.1.41)
12
+ activecube (0.1.45)
13
13
  activerecord (>= 5.2)
14
14
 
15
15
  GEM
@@ -75,11 +75,11 @@ module Activecube::Processor
75
75
  measures_by_tables = measure_tables.group_by(&:table)
76
76
  measures_by_tables.each_pair do |table, list|
77
77
  @models << table.model
78
- table_query = table.query cube_query
78
+ table_query = table.query cube_query, list.map(&:measure)
79
79
  composed_query = composed_query ? table.join(cube_query, composed_query, table_query) : table_query
80
80
  end
81
81
  composed_query
82
82
  end
83
83
 
84
84
  end
85
- end
85
+ end
@@ -6,6 +6,7 @@ module Activecube::Processor
6
6
  MAX_ITERATIONS = 3
7
7
 
8
8
  attr_reader :tables_count, :metrics_count, :cost_matrix
9
+
9
10
  def initialize cost_matrix
10
11
  @cost_matrix = cost_matrix
11
12
  @cache = ActiveSupport::Cache::MemoryStore.new
@@ -18,10 +19,9 @@ module Activecube::Processor
18
19
  @tables_count = cost_matrix.map(&:count).max
19
20
  @metrics_count = cost_matrix.count
20
21
 
21
- (tables_count==1 || metrics_count==0) ? [0]*metrics_count : do_optimize
22
-
23
- end
22
+ (tables_count == 1 || metrics_count == 0) ? [0] * metrics_count : do_optimize
24
23
 
24
+ end
25
25
 
26
26
  end
27
27
 
@@ -29,42 +29,102 @@ module Activecube::Processor
29
29
 
30
30
  def generate_variants vs, metric_i
31
31
 
32
- return vs if metric_i==metrics_count
32
+ return vs if metric_i == metrics_count
33
33
 
34
34
  metric_tables = cost_matrix[metric_i].map.with_index do |c, index|
35
35
  [index] if c
36
36
  end.compact
37
37
 
38
- vsnew = if metric_i==0
39
- metric_tables
40
- else
41
- arry = []
42
- vs.each do |v|
43
- metric_tables.each{|newv|
44
- arry << (v + newv)
45
- }
46
- end
47
- arry
48
- end
38
+ vsnew = if metric_i == 0
39
+ metric_tables
40
+ else
41
+ arry = []
42
+ vs.each do |v|
43
+ metric_tables.each { |newv|
44
+ arry << (v + newv)
45
+ }
46
+ end
47
+ arry
48
+ end
49
49
 
50
- generate_variants vsnew, metric_i+1
50
+ generate_variants vsnew, metric_i + 1
51
51
 
52
52
  end
53
53
 
54
54
  def cost_for variant
55
55
  variant.each_with_index.group_by(&:first).collect do |table_index, arry|
56
- arry.map(&:second).map{|metric_index| cost_matrix[metric_index][table_index] }.max
56
+ arry.map(&:second).map { |metric_index| cost_matrix[metric_index][table_index] }.max
57
57
  end.sum
58
58
  end
59
59
 
60
60
  def do_optimize
61
61
 
62
- variants = generate_variants [], 0
63
- variant_costs = variants.map{|v| cost_for v}
62
+ # variants = generate_variants [], 0
63
+ variants = gen_reduced_variants
64
+ variant_costs = variants.map { |v| cost_for v }
64
65
  variants[variant_costs.each_with_index.min.second]
65
66
 
66
67
  end
67
68
 
68
- end
69
+ def gen_permutations(n, k)
70
+ perm = Array.new(k, 0)
71
+ perms = []
72
+
73
+ while true
74
+ perms.push perm.dup
75
+ flag = true
76
+ i = 0
77
+ ((k - 1)...-1).step(-1).each do |ii|
78
+ i = ii
79
+ if perm[ii] < n - 1
80
+ flag = false
81
+ break
82
+ end
83
+ end
84
+ return perms if flag
85
+
86
+ perm[i] += 1
87
+ ((i + 1)...k).each { |j| perm[j] = 0 }
88
+ end
89
+ end
90
+
91
+ def gen_reduced_variants
92
+ # reduce size of cost_matrix deleting duplicates
93
+ uniq_rows = []
94
+ rows_indices = {}
95
+ possible_tables = {}
96
+ cost_matrix.each_with_index do |row, i|
97
+ flag = false
98
+
99
+ uniq_rows.each_with_index do |u_row, j|
100
+ if u_row.eql? row
101
+ flag = true
102
+ rows_indices[i] = j
103
+ end
104
+ end
105
+
106
+ unless flag
107
+ rows_indices[i] = uniq_rows.length
108
+ possible_tables[uniq_rows.length] = Hash[row.map.with_index { |c, index| [index, true] if c }.compact]
109
+ uniq_rows.push(row)
110
+ end
111
+ end
112
+
113
+ # generating variants for reduced matrix
114
+ vars = gen_permutations(tables_count, uniq_rows.length)
69
115
 
116
+ # filter possible variants
117
+ vars = vars.filter do |v|
118
+ v.map.with_index.all? {|t_n, i| possible_tables[i][t_n]}
119
+ end
120
+
121
+ # restore variants for full matrix
122
+ vars.map do |variant|
123
+ full_v = Array.new(metrics_count)
124
+ rows_indices.each { |k, v| full_v[k] = variant[v] }
125
+
126
+ full_v
127
+ end
128
+ end
129
+ end
70
130
  end
@@ -20,12 +20,12 @@ module Activecube::Processor
20
20
  (measure.required_column_names - model.attribute_types.keys).empty?
21
21
  end
22
22
 
23
- def query cube_query
23
+ def query cube_query, measures = cube_query.measures
24
24
 
25
25
  table = model.arel_table
26
26
  query = table
27
27
 
28
- (cube_query.slices + cube_query.measures + cube_query.selectors + cube_query.options).each do |s|
28
+ (cube_query.slices + measures + cube_query.selectors + cube_query.options).each do |s|
29
29
  query = s.append_query model, cube_query, table, query
30
30
  end
31
31
 
@@ -52,4 +52,4 @@ module Activecube::Processor
52
52
 
53
53
 
54
54
  end
55
- end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module Activecube
2
- VERSION = "0.1.42"
2
+ VERSION = "0.1.45"
3
3
  end
@@ -22,7 +22,7 @@ module Activecube
22
22
  true
23
23
  end
24
24
 
25
- def query _cube_query
25
+ def query _cube_query, measures = _cube_query.measures
26
26
  raise "query method have to be implemented in #{name}"
27
27
  end
28
28
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activecube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.42
4
+ version: 0.1.45
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksey Studnev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2022-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord