Module: GoodData::UserFilterBuilder
- Defined in:
- lib/gooddata/models/user_filters/user_filter_builder.rb
Class Method Summary collapse
-
.collect_labels(data) ⇒ Object
Groups the values by particular label.
-
.collect_values(data) ⇒ Object
Collects specific values and deduplicates if necessary.
- .create_attrs_cache(filters, options = {}) ⇒ Object
- .create_cache(data, key) ⇒ Object
-
.create_expression(filter, labels_cache, lookups_cache, attr_cache, options = {}) ⇒ Object
Creates a MAQL expression(s) based on the filter defintion.
- .create_filter(label, values) ⇒ Object
- .create_label_cache(result, options = {}) ⇒ Object
- .create_lookups_cache(small_labels) ⇒ Object
-
.create_user_filter(expression, related) ⇒ Object
Encapuslates the creation of filter.
- .execute_mufs(user_filters, options = {}) ⇒ Object
-
.execute_variables(filters, var, options = {}) ⇒ Array
Executes the update for variables.
-
.get_filters(file, options = {}) ⇒ Boolean
Main Entry function.
- .get_missing_users(filters, options = {}) ⇒ Object
-
.get_small_labels(labels_cache) ⇒ Object
Walks over provided labels and picks those that have fewer than certain amount of values This tries to balance for speed when working with small datasets (like users) so it precaches the values and still be able to function for larger ones even though that would mean tons of requests.
-
.maqlify_filters(filters, options = {}) ⇒ Array
Resolves and creates maql statements from filter definitions.
-
.process_line(line, options = {}) ⇒ Object
Processes a line from source file.
- .read_file(file, options = {}) ⇒ Object
-
.reduce_results(data) ⇒ Array
Processes values in a map reduce way so the result is as readable as possible and poses minimal impact on the API.
- .resolve_user_filter(user = [], project = []) ⇒ Object
-
.resolve_user_filters(user_filters, vals) ⇒ Object
Gets user defined filters and values from project regardless if they come from Mandatory Filters or Variable filters and tries to resolve what needs to be removed an what needs to be updated.
-
.row_based?(options = {}) ⇒ Boolean
Function that tells you if the file should be read line_wise.
- .verify_existing_users(filters, options = {}) ⇒ Object
Class Method Details
.collect_labels(data) ⇒ Object
Groups the values by particular label. And passes each group to deduplication
97 98 99 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 97 def self.collect_labels(data) data.group_by { |x| [x[:label], x[:over], x[:to]] }.map { |l, v| { label: l[0], over: l[1], to: l[2], values: UserFilterBuilder.collect_values(v) } } end |
.collect_values(data) ⇒ Object
Collects specific values and deduplicates if necessary
102 103 104 105 106 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 102 def self.collect_values(data) data.mapcat do |e| e[:values] end.uniq end |
.create_attrs_cache(filters, options = {}) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 159 def self.create_attrs_cache(filters, = {}) project = [:project] labels = filters.flat_map do |f| f[:filters] end over_cache = labels.reduce({}) do |a, e| a[e[:over]] = e[:over] a end to_cache = labels.reduce({}) do |a, e| a[e[:to]] = e[:to] a end cache = over_cache.merge(to_cache) attr_cache = {} cache.each_pair do |k, v| begin attr_cache[k] = project.attributes(v) rescue nil end end attr_cache end |
.create_cache(data, key) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 108 def self.create_cache(data, key) data.reduce({}) do |a, e| a[e.send(key)] = e a end end |
.create_expression(filter, labels_cache, lookups_cache, attr_cache, options = {}) ⇒ Object
Creates a MAQL expression(s) based on the filter defintion. Takes the filter definition looks up any necessary values and provides API executable MAQL
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 196 def self.create_expression(filter, labels_cache, lookups_cache, attr_cache, = {}) errors = [] values = filter[:values] label = labels_cache[filter[:label]] element_uris = values.map do |v| begin if lookups_cache.key?(label.uri) if lookups_cache[label.uri].key?(v) lookups_cache[label.uri][v] else fail end else label.find_value_uri(v) end rescue errors << { type: :error, label: label.title, value: v } nil end end expression = if element_uris.compact.empty? && [:restrict_if_missing_all_values] && [:type] == :muf '1 <> 1' elsif element_uris.compact.empty? && [:restrict_if_missing_all_values] && [:type] == :variable nil elsif element_uris.compact.empty? 'TRUE' elsif filter[:over] && filter[:to] over = attr_cache[filter[:over]] to = attr_cache[filter[:to]] "([#{label.attribute_uri}] IN (#{element_uris.compact.sort.map { |e| '[' + e + ']' }.join(', ')})) OVER [#{over && over.uri}] TO [#{to && to.uri}]" else "[#{label.attribute_uri}] IN (#{element_uris.compact.sort.map { |e| '[' + e + ']' }.join(', ')})" end if [:ignore_missing_values] [expression, []] else [expression, errors] end end |
.create_filter(label, values) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 76 def self.create_filter(label, values) { :label => label[:label], :values => values, :over => label[:over], :to => label[:to] } end |
.create_label_cache(result, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 137 def self.create_label_cache(result, = {}) project = [:project] result.reduce({}) do |a, e| e[:filters].map do |filter| a[filter[:label]] = project.labels(filter[:label]) unless a.key?(filter[:label]) end a end end |
.create_lookups_cache(small_labels) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 148 def self.create_lookups_cache(small_labels) small_labels.reduce({}) do |a, e| lookup = e.values(:limit => 1_000_000).reduce({}) do |a1, e1| a1[e1[:value]] = e1[:uri] a1 end a[e.uri] = lookup a end end |
.create_user_filter(expression, related) ⇒ Object
Encapuslates the creation of filter
241 242 243 244 245 246 247 248 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 241 def self.create_user_filter(expression, ) { related: , level: :user, expression: expression, type: :filter } end |
.execute_mufs(user_filters, options = {}) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 364 def self.execute_mufs(user_filters, = {}) client = [:client] project = [:project] ignore_missing_values = [:ignore_missing_values] users_must_exist = [:users_must_exist] == false ? false : true dry_run = [:dry_run] project_log_formatter = GoodData::ProjectLogFormatter.new(project) filters = normalize_filters(user_filters) user_filters, errors = maqlify_filters(filters, .merge(users_must_exist: users_must_exist, type: :muf)) fail GoodData::FilterMaqlizationError, errors if !ignore_missing_values && !errors.empty? filters = user_filters.map { |data| client.create(MandatoryUserFilter, data, project: project) } to_create, to_delete = resolve_user_filters(filters, project.) GoodData.logger.warn("Data permissions computed: #{to_create.count} to create and #{to_delete.count} to delete") return { created: to_create, deleted: to_delete } if dry_run create_results = to_create.each_slice(100).flat_map do |batch| batch.pmapcat do |, group| group.each(&:save) res = client.get("/gdc/md/#{project.pid}/userfilters?users=#{}") items = res['userFilters']['items'].empty? ? [] : res['userFilters']['items'].first['userFilters'] payload = { 'userFilters' => { 'items' => [{ 'user' => , 'userFilters' => items.concat(group.map(&:uri)) }] } } res = client.post("/gdc/md/#{project.pid}/userfilters", payload) # turn the errors from hashes into array of hashes res['userFiltersUpdateResult'].flat_map { |k, v| v.map { |r| { status: k.to_sym, user: r, type: :create } } }.map { |result| result[:status] == :failed ? result.merge(GoodData::Helpers.symbolize_keys(result[:user])) : result } end end project_log_formatter.log_user_filter_results(create_results, to_create) delete_results = unless [:do_not_touch_filters_that_are_not_mentioned] to_delete.each_slice(100).flat_map do |batch| batch.flat_map do |, group| results = [] if res = client.get("/gdc/md/#{project.pid}/userfilters?users=#{}") items = res['userFilters']['items'].empty? ? [] : res['userFilters']['items'].first['userFilters'] payload = { 'userFilters' => { 'items' => [ { 'user' => , 'userFilters' => items - group.map(&:uri) } ] } } res = client.post("/gdc/md/#{project.pid}/userfilters", payload) results.concat(res['userFiltersUpdateResult'] .flat_map { |k, v| v.map { |r| { status: k.to_sym, user: r, type: :delete } } } .map { |result| result[:status] == :failed ? result.merge(GoodData::Helpers.symbolize_keys(result[:user])) : result }) end group.peach(&:delete) results end end end project_log_formatter.log_user_filter_results(delete_results, to_delete) { created: to_create, deleted: to_delete, results: create_results + (delete_results || []) } end |
.execute_variables(filters, var, options = {}) ⇒ Array
Executes the update for variables. It resolves what is new and needed to update.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 345 def self.execute_variables(filters, var, = {}) client = [:client] project = [:project] dry_run = [:dry_run] to_create, to_delete = execute(filters, var.user_values, VariableUserFilter, .merge(type: :variable)) return [to_create, to_delete] if dry_run # TODO: get values that are about to be deleted and created and update them. # This will make sure there is no downitme in filter existence unless [:do_not_touch_filters_that_are_not_mentioned] to_delete.each { |_, group| group.each(&:delete) } end data = to_create.values.flatten.map(&:to_hash).map { |var_val| var_val.merge(prompt: var.uri) } data.each_slice(200) do |slice| client.post("/gdc/md/#{project.obj_id}/variables/user", :variables => slice) end [to_create, to_delete] end |
.get_filters(file, options = {}) ⇒ Boolean
Main Entry function. Gets values and processes them to get filters that are suitable for other function to process. Values can be read from file or provided inline as an array. The results are then preprocessed. It is possible to provide multiple values for an attribute tries to deduplicate the values if they are not unique. Allows for setting over/to filters and allows for setting up filters from multiple columns. It is specially designed so many aspects of configuration are modifiable so you do have to preprocess the data as little as possible ideally you should be able to use data that came directly from the source system and that are intended for use in other parts of ETL.
25 26 27 28 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 25 def self.get_filters(file, = {}) values = get_values(file, ) reduce_results(values) end |
.get_missing_users(filters, options = {}) ⇒ Object
115 116 117 118 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 115 def self.get_missing_users(filters, = {}) users_cache = [:users_cache] filters.reject { |u| users_cache.key?(u[:login]) } end |
.get_small_labels(labels_cache) ⇒ Object
Walks over provided labels and picks those that have fewer than certain amount of values This tries to balance for speed when working with small datasets (like users) so it precaches the values and still be able to function for larger ones even though that would mean tons of requests
190 191 192 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 190 def self.get_small_labels(labels_cache) labels_cache.values.select { |label| label && label.values_count && label.values_count < 100_000 } end |
.maqlify_filters(filters, options = {}) ⇒ Array
Resolves and creates maql statements from filter definitions. This method does not perform any modifications on API but collects all the information that is needed to do so. Method collects all info from the user and current state in project and compares. Returns suggestion of what should be deleted and what should be created If there is some discrepancies in the data (missing values, nonexistent users) it finishes and collects all the errors at once
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 260 def self.maqlify_filters(filters, = {}) fail_early = [:fail_early] == false ? false : true users_cache = [:users_cache] labels_cache = create_label_cache(filters, ) small_labels = get_small_labels(labels_cache) lookups_cache = create_lookups_cache(small_labels) attrs_cache = create_attrs_cache(filters, ) users = Hash[ [:project].users.map do |user| [user.login, user.profile_url] end ] create_filter_proc = proc do |login, f| expression, errors = create_expression(f, labels_cache, lookups_cache, attrs_cache, ) profiles_uri = if [:type] == :muf uri = users[login] uri.nil? ? ('/gdc/account/profile/' + login) : uri elsif [:type] == :variable (users_cache[login] && users_cache[login].uri) else fail 'Unsuported type in maqlify_filters.' end if profiles_uri && expression [create_user_filter(expression, profiles_uri)] + errors else [] + errors end end # if fail early process until first error results = if fail_early x = filters.inject([true, []]) do |(enough, a), e| login = e[:login] if enough y = e[:filters].pmapcat { |f| create_filter_proc.call(login, f) } [!y.any? { |r| r[:type] == :error }, a.concat(y)] else [false, a] end end x.last else filters.flat_map do |filter| login = filter[:login] filter[:filters].pmapcat { |f| create_filter_proc.call(login, f) } end end results.group_by { |i| i[:type] }.values_at(:filter, :error).map { |i| i || [] } end |
.process_line(line, options = {}) ⇒ Object
Processes a line from source file. It is processed in 2 formats. First mode is column_based. It means getting all specific columns. These are specified either by index or name. Multiple values are provided by several rows for the same user
Second mode is row based which means there are no headers and number of columns can be variable. Each row specifies multiple values for one user. It is implied that the file provides values for just one label
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 64 def self.process_line(line, = {}) index = [:user_column] || 0 login = line[index] results = [:labels].mapcat do |label| column = label[:column] || Range.new(1, -1) values = column.is_a?(Range) ? line.slice(column) : [line[column]] [create_filter(label, values.compact)] end [login, results] end |
.read_file(file, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 39 def self.read_file(file, = {}) memo = {} params = row_based?() ? { headers: false } : { headers: true } CSV.foreach(file, params.merge(return_headers: false)) do |e| key, data = process_line(e, ) memo[key] = [] unless memo.key?(key) memo[key].concat(data) end memo end |
.reduce_results(data) ⇒ Array
Processes values in a map reduce way so the result is as readable as possible and poses minimal impact on the API
90 91 92 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 90 def self.reduce_results(data) data.map { |k, v| { login: k, filters: UserFilterBuilder.collect_labels(v) } } end |
.resolve_user_filter(user = [], project = []) ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 311 def self.resolve_user_filter(user = [], project = []) user ||= [] project ||= [] to_create = user - project to_delete = project - user { :create => to_create, :delete => to_delete } end |
.resolve_user_filters(user_filters, vals) ⇒ Object
Gets user defined filters and values from project regardless if they come from Mandatory Filters or Variable filters and tries to resolve what needs to be removed an what needs to be updated
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 322 def self.resolve_user_filters(user_filters, vals) project_vals_lookup = vals.group_by(&:related_uri) user_vals_lookup = user_filters.group_by(&:related_uri) a = vals.map(&:related_uri) b = user_filters.map(&:related_uri) users_to_try = (a + b).uniq results = users_to_try.map do |user| resolve_user_filter(user_vals_lookup[user], project_vals_lookup[user]) end to_create = results.map { |x| x[:create] }.flatten.group_by(&:related_uri) to_delete = results.map { |x| x[:delete] }.flatten.group_by(&:related_uri) [to_create, to_delete] end |
.row_based?(options = {}) ⇒ Boolean
Function that tells you if the file should be read line_wise. This happens if you have only one label defined and you do not have columns specified
35 36 37 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 35 def self.row_based?( = {}) [:labels].count == 1 && ![:labels].first.key?(:column) end |
.verify_existing_users(filters, options = {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gooddata/models/user_filters/user_filter_builder.rb', line 120 def self.verify_existing_users(filters, = {}) users_must_exist = [:users_must_exist] == false ? false : true users_cache = [:users_cache] domain = [:domain] if users_must_exist missing_users = filters.reject do |u| next true if users_cache.key?(u[:login]) domain_user = (domain && domain.find_user_by_login(u[:login])) users_cache[domain_user.login] = domain_user if domain_user next true if domain_user false end fail "#{missing_users.count} users are not part of the project and variable cannot be resolved since :users_must_exist is set to true (#{missing_users.join(', ')})" unless missing_users.empty? end end |