container_entity.rb 2.26 KB
Newer Older
1
2
module Entities
  class ContainerEntity < Grape::Entity
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    expose :big_tree, merge: true

    def big_tree(container = object)
      dataset_ids = {}
      bt = container.attributes.slice('id', 'container_type', 'name')
      bt['children'] = container.hash_tree[container].map do |c1, c2s|
        as = c1.attributes.slice('id', 'container_type', 'name')
        ## mapping analysis element
        as['children'] = c2s.map do |c2, c3s|
          a = c2.attributes.slice('id', 'container_type', 'name', 'description')
          a['extended_metadata'] = get_extended_metadata(c2)
          dids = []
          ## mapping datasets
          a['children'] = c3s.map do |c3, _|
            ds = c3.attributes.slice('id', 'container_type', 'name', 'description')
            dids << ds['id']
            ds['extended_metadata'] = get_extended_metadata(c3)
            ds
          end
          dataset_ids[a['id']] = dids
          a['preview_img'] = dids
          a
        end
        as
      end
28

29
30
      attachments = Attachment.where_container(dataset_ids.values.flatten).to_a
      code_logs =  CodeLog.where(source_id: dataset_ids.keys, source: 'container').to_a
31

32
33
34
35
36
37
38
      bt.dig('children',0,'children').each do |analysis|
        analysis['preview_img'] = preview_img(dataset_ids[analysis['id']], attachments)
        analysis['code_log'] = code_logs.find {|cl| cl.source_id == analysis['id']}.attributes
        analysis['children'].each do |dataset|
          atts = attachments.select { |a| a.attachable_id == dataset['id'] }
          dataset['attachments'] = Entities::AttachmentEntity.represent(atts)
        end
39
      end
40
      bt
41
42
    end

43
44
    private
    def preview_img(container_ids, attachments)
PiTrem's avatar
PiTrem committed
45
46
47
      attachment = attachments.find { |a|
        a.thumb == true && a.attachable_type == 'Container' && container_ids.include?(a.attachable_id)
      }
48
49
      preview = attachment.read_thumbnail if attachment
      preview && Base64.encode64(preview) || "not available"
50
51
52
    end

    def get_extended_metadata(container)
53
54
55
56
      ext_mdata = container.extended_metadata || {}
      ext_mdata['report'] = (ext_mdata['report'] == 'true') || (ext_mdata == true)
      if ext_mdata['content'].present?
        ext_mdata['content'] = JSON.parse(ext_mdata['content'])
57
58
59
60
61
      end
      ext_mdata
    end
  end
end