Class: Zillabyte::Command::Relations

Inherits:
Base
  • Object
show all
Defined in:
lib/zillabyte/cli/relations.rb

Overview

manage custom relations

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

alias_command, extract_banner, extract_description, extract_help, extract_help_from_caller, extract_options, extract_summary, inherited, #initialize, method_added, namespace

Methods included from Helpers

#ask, #display, #error, #format_with_bang, #longest, #read_multiline, #with_tty

Constructor Details

This class inherits a constructor from Zillabyte::Command::Base

Instance Method Details

#append ⇒ Object

relations:append ID FILE

adds data to an existing relation

--type TYPE # interpret file as type [csv, xlsx]



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/zillabyte/cli/relations.rb', line 136

def append

  id = options[:id] || shift_argument
  file = options[:file] || shift_argument
  type = options[:type]

  error "no id given" if id.nil?
  error "no id given" if file.nil?
  type ||= File.extname(file).gsub(".", "")

  dataset = self.api.data.get(id)
  rows = sanity_check_file(file,type,dataset)
 
  #res = self.api.data.append(id, {:row => rows})
  res = self.api.data.append(id, {:file => file})
  display "relation #{id} appended"
  
end

#create ⇒ Object

relations:create NAME

creates a new relation

--schema SCHEMA # a JSON array of column types --semantics SEMANTICS # a JSON array of semantics within the relation --public # Make the relation public



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/zillabyte/cli/relations.rb', line 104

def create
  
  name = options[:name] || shift_argument
  error "no name given" if name.nil?

  schema = JSON.parse(options[:schema]) if options[:schema]
  semantics = JSON.parse(options[:semantics]) if options[:semantics]
  is_public = options[:public] || false
 
  hash = set_relation_properties(schema,semantics,is_public) 
  res = api.data.create name, hash
  
  if res['error']
    display "error: #{res['error_message']}"
  else
    display "relation ##{res['id']} #{res['action']}. size: #{res['size']}"
  end
  
end

#index ⇒ Object

relations



11
12
13
# File 'lib/zillabyte/cli/relations.rb', line 11

def index
  self.list
end

#list ⇒ Object

relations:list

lists your custom relations



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zillabyte/cli/relations.rb', line 20

def list
  
  response = api.request(
    :expects  => 200,
    :method   => :get,
    :path     => "/relations"
  )
    
  headings = []
  rows = response.body.map do |row|
    headings = row.keys if headings.size == 0
    row["columns"] = row["columns"].map{|c| c['type']}.join(',')
    vals = row.values_at *headings
    vals 
  end
  
  display "relations:\n" + Terminal::Table.new(:headings => headings, :rows => rows).to_s
  display "Total number of relations: "+rows.length.to_s
  
  
end

#show ⇒ Object

relations:show ID

shows raw data in a relation. run 'queries' for more elaborate functionality

--no_truncation # don't truncate long strings



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/zillabyte/cli/relations.rb', line 162

def show
  
  name = options[:name] || shift_argument
  error "no id given" if name.nil?
  
  res = self.api.data.get(name)
  
  if res["rows"].size > 0
    
    headings = res["rows"].first.keys
    headings.delete("id")
    table = Terminal::Table.new :headings => headings    
    res["rows"].each do |obj|
      
      new_row = headings.map do |heading|
        if options[:no_truncation]
          obj[heading]
        else
          if obj[heading].to_s.size > 30
            obj[heading].to_s[0..30] + "..."
          else
            obj[heading]
          end
        end
      end
      
      table.add_row new_row
      
    end
    display table.to_s
    
  else
    
    display "empty relation"
    
  end
  
end

#status ⇒ Object

relations:status RELATION

shows status of a relation



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
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/zillabyte/cli/relations.rb', line 53

def status
  
  name = options[:relation_name] || shift_argument
  error "no name given" if name.nil?

  res = api.request(
    :path => "/metrics/#{name}",
    :method => :get,
    :expects => 200
  )
  
  if res['error']
    error res['error_message']
  end
  
  data = res.body.first
  
  if data.nil?
    error "metric not found"
  end
  
  data = data["datapoints"]
  
  last_date = data.last(30).first[1]
  formatted = []
  i = 0
  data.each do |tup|
    value = tup[0]
    date = tup[1] - last_date
    if date > 0 
      formatted <<  [i, value || 0]
      i += 1
    end
  end
  
  display "#{name}:"
  display AsciiCharts::Cartesian.new(formatted, :bar => true, :hide_zero => true).draw
  
  
end

#test ⇒ Object

relations:test NAME FILE

creates test relation NAME and uploads FILE to NAME



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
# File 'lib/zillabyte/cli/relations.rb', line 208

def test
  name = options[:name] || shift_argument
  error "no name given" if name.nil?
  
  file = options[:file] || shift_argument
  error "no file given" if file.nil?

  schema = options[:schema]
  relations = options[:relations]
  is_public = options[:public] || false
 
  hash = set_dataset_properties(schema,relations,is_public) 
  res = api.data.create name, hash

  type = options[:type]
  type ||= File.extname(file).gsub(".", "")

  id = res['id']
  dataset = self.api.data.get(id)
  rows = sanity_check_file(file,type,dataset)

  #res = self.api.data.append(id, {:row => rows,:for_test => 1})
  res = self.api.data.append(id, {:file => file,:for_test => 1})
  display "relation (for test) #{id} appended"
end