9767: need to parse the json string of the workflow yaml
[arvados.git] / apps / workbench / app / models / collection.rb
1 require "arvados/keep"
2
3 class Collection < ArvadosBase
4   MD5_EMPTY = 'd41d8cd98f00b204e9800998ecf8427e'
5
6   def default_name
7     if Collection.is_empty_blob_locator? self.uuid
8       "Empty Collection"
9     else
10       super
11     end
12   end
13
14   # Return true if the given string is the locator of a zero-length blob
15   def self.is_empty_blob_locator? locator
16     !!locator.to_s.match("^#{MD5_EMPTY}(\\+.*)?\$")
17   end
18
19   def self.goes_in_projects?
20     true
21   end
22
23   def manifest
24     if @manifest.nil? or manifest_text_changed?
25       @manifest = Keep::Manifest.new(manifest_text || "")
26     end
27     @manifest
28   end
29
30   def files
31     # This method provides backwards compatibility for code that relied on
32     # the old files field in API results.  New code should use manifest
33     # methods directly.
34     manifest.files
35   end
36
37   def content_summary
38     if total_bytes > 0
39       ApplicationController.helpers.human_readable_bytes_html(total_bytes) + " " + super
40     else
41       super + " modified at " + modified_at.to_s
42     end
43   end
44
45   def total_bytes
46     manifest.files.inject(0) { |sum, filespec| sum + filespec.last }
47   end
48
49   def files_tree
50     tree = manifest.files.group_by do |file_spec|
51       File.split(file_spec.first)
52     end
53     return [] if tree.empty?
54     # Fill in entries for empty directories.
55     tree.keys.map { |basedir, _| File.split(basedir) }.each do |splitdir|
56       until tree.include?(splitdir)
57         tree[splitdir] = []
58         splitdir = File.split(splitdir.first)
59       end
60     end
61     dir_to_tree = lambda do |dirname|
62       # First list subdirectories, with their files inside.
63       subnodes = tree.keys.select { |bd, td| (bd == dirname) and (td != '.') }
64         .sort.flat_map do |parts|
65         [parts + [nil]] + dir_to_tree.call(File.join(parts))
66       end
67       # Then extend that list with files in this directory.
68       subnodes + tree[File.split(dirname)]
69     end
70     dir_to_tree.call('.')
71   end
72
73   def editable_attributes
74     %w(name description manifest_text)
75   end
76
77   def provenance
78     arvados_api_client.api "collections/#{self.uuid}/", "provenance"
79   end
80
81   def used_by
82     arvados_api_client.api "collections/#{self.uuid}/", "used_by"
83   end
84
85   def uuid
86     if self[:uuid].nil?
87       return self[:portable_data_hash]
88     else
89       super
90     end
91   end
92
93   def friendly_link_name lookup=nil
94     name || portable_data_hash
95   end
96
97   def textile_attributes
98     [ 'description' ]
99   end
100
101 end