Class: Eco::API::Session::Config::TagTree

Inherits:
BaseConfig show all
Defined in:
lib/eco/api/session/config/tagtree.rb

Defined Under Namespace

Classes: MissingTagtree

Instance Attribute Summary

Attributes inherited from BaseConfig

#config

Instance Method Summary collapse

Methods inherited from BaseConfig

#apis, attr_key, #clone, #file_manager, #initialize

Methods inherited from Hash

#deep_merge, #deep_merge!

Constructor Details

This class inherits a constructor from Eco::API::Session::Config::BaseConfig

Instance Method Details

#live_tree(id: nil, enviro: nil, include_archived: false, **kargs, &block) ⇒ Object

Among all the locations structures it selects the one with more location nodes If id is provided, it only retrieves this locations structure.

Parameters:

  • used (enviro)

    for re-caching



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/eco/api/session/config/tagtree.rb', line 43

def live_tree(id: nil, enviro: nil, include_archived: false, **kargs, &block)
  existing_cache   = !!@live_tree
  first_load       = !existing_cache

  target_change    = existing_cache && id     && @live_tree.id != id
  enviro_change    = existing_cache && enviro && @live_tree.enviro != enviro

  switching_target = existing_cache && (target_change || enviro_change)
  refresh_cache    = existing_cache && !switching_target

  kargs = {
    includeArchivedNodes: include_archived
  }.merge(kargs)

  if id
    args       = { id: id }.merge(kargs)
    @live_tree = live_tree_get(**args, &block)
  else
    trees      = live_trees(**kargs, &block)
    @live_tree = trees.reject do |tree|
      tree.empty?
    end.max do |a,b|
      a.count <=> b.count
    end
  end.tap do |tree|
    if tree
      msg = "LIVE LOCATIONS Structure (#{tree.id}): '#{tree.name}' (#{tree.count} nodes)"
      if first_load
        session_logger.info("Using #{msg}")
      elsif switching_target
        session_logger.info("Switched to #{msg}")
      else # refresh_cache
        session_logger.debug("Reloading #{msg}")
      end
    else
      session_logger.info "Could not retrive live tree (#{id})"
    end
  end
end

#live_tree_get(id: nil, include_archived: false, **kargs, &block) ⇒ Eco::API::Organization::TagTree, NilClass

Note:

it does not memoize

Gets a single locations structure

Parameters:

  • include_archived (Boolean) (defaults to: false)

    whether or not to include archived nodes

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/eco/api/session/config/tagtree.rb', line 87

def live_tree_get(id: nil, include_archived: false, **kargs, &block)
  return nil unless apis.active_api.version_available?(:graphql)
  return nil unless graphql = apis.api(version: :graphql)

  kargs = {
    id:                   id,
    includeArchivedNodes: include_archived
  }.merge(kargs)

  start = Time.now
  return nil unless tree = graphql.currentOrganization.locationStructure(**kargs, &block)
  end_time = Time.now
  secs    = (end_time - start).round(3)

  Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name).tap do |eco_tree|
    cnt     = eco_tree.count
    per_sec = (cnt.to_f / secs).round(2)
    session_logger.info("Loaded #{cnt} location nodes in #{secs} seconds (#{per_sec} nodes/sec)")
  end
end

#live_trees(include_archived: false, **kargs, &block) ⇒ Array<Eco::API::Organization::TagTree>

Retrieves all the location structures of the organisation

Parameters:

  • include_archived (Boolean) (defaults to: false)

    whether or not to include archived nodes

Returns:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/eco/api/session/config/tagtree.rb', line 111

def live_trees(include_archived: false, **kargs, &block)
  [].tap do |eco_trees|
    next unless apis.active_api.version_available?(:graphql)
    next unless graphql = apis.api(version: :graphql)

    kargs = {
      includeArchivedNodes: include_archived
    }.merge(kargs)

    start = Time.now
    next unless trees = graphql.currentOrganization.locationStructures(**kargs, &block)
    end_time = Time.now
    secs     = (end_time - start).round(3)
    cnt      = 0
    trees.each do |tree|
      eco_tree = Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name)
      cnt += eco_tree.count
      eco_trees.push(eco_tree)
    end
    per_sec = (cnt.to_f / secs).round(2)
    session_logger.info("Loaded #{cnt} location nodes in #{secs} seconds (#{per_sec} nodes/sec)")
  end
end

#scope_tree(enviro: nil, include_archived: true, raise_on_missing: true) ⇒ Eco::API::Organization::TagTree

Note:

it retrieves the tree this way:

  1. If there's a file tagtree.json file, it uses it
  2. If no file, retrieves structure_id (config)

Parameters:

  • include_archived (Boolean) (defaults to: true)

    whether or not it should include archived nodes.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eco/api/session/config/tagtree.rb', line 16

def scope_tree(enviro: nil, include_archived: true, raise_on_missing: true)
  return @tagtree if instance_variable_defined?(:@tagtree) && @tagtree.enviro == enviro

  kargs = {
    enviro:               enviro,
    includeArchivedNodes: include_archived
  }

  if tree_file = self.file
    if (tree = file_manager.load_json(tree_file)) && !tree.empty?
      @tagtree = Eco::API::Organization::TagTree.new(tree)
    end
  elsif self.structure_id
    kargs.merge(id: self.structure_id)
  end

  @tagtree ||= live_tree(**kargs).tap do |tree|
    unless tree && !tree.empty?
      msg = "Could not find a local or live locations structure."
      raise MissingTagtree, msg
    end
  end
end