Skip to content

Commit

Permalink
feat: management of model sets (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker authored May 15, 2023
1 parent aff1276 commit 1b964ca
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/gzr/commands/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module Gzr
module Commands
class Model < SubCommandBase

require_relative 'model/set'
register Gzr::Commands::Model::Set, 'set', 'set [SUBCOMMAND]', 'Commands pertaining to model sets'

namespace :model

desc 'ls', 'List the models in this server.'
Expand Down
97 changes: 97 additions & 0 deletions lib/gzr/commands/model/set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# The MIT License (MIT)

# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# frozen_string_literal: true

require_relative '../subcommandbase'

module Gzr
module Commands
class Model
class Set < SubCommandBase

namespace :'model set'

desc 'ls', 'List the model sets in this server.'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :fields, type: :string, default: 'id,name,models',
desc: 'Fields to display'
method_option :plain, type: :boolean, default: false,
desc: 'print without any extra formatting'
method_option :csv, type: :boolean, default: false,
desc: 'output in csv format per RFC4180'
def ls(*)
if options[:help]
invoke :help, ['ls']
else
require_relative 'set/ls'
Gzr::Commands::Model::Set::Ls.new(options).execute
end
end

desc 'cat MODEL_SET_ID', 'Output json information about a model set to screen or file'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :dir, type: :string,
desc: 'Directory to store output file'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
def cat(model_set_id)
if options[:help]
invoke :help, ['cat']
else
require_relative 'set/cat'
Gzr::Commands::Model::Set::Cat.new(model_set_id,options).execute
end
end

desc 'import FILE', 'Import a model set from a file'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :force, type: :boolean,
desc: 'Overwrite an existing model set'
method_option :plain, type: :boolean, default: false,
desc: 'print without any extra formatting'
def import(file)
if options[:help]
invoke :help, ['import']
else
require_relative 'set/import'
Gzr::Commands::Model::Set::Import.new(file, options).execute
end
end

desc 'rm MODEL_SET_ID', 'Delete the model_set given by MODEL_SET_ID'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def rm(model_set_id)
if options[:help]
invoke :help, ['delete']
else
require_relative 'set/rm'
Gzr::Commands::Model::Set::Delete.new(model_set_id,options).execute
end
end
end
end
end
end
60 changes: 60 additions & 0 deletions lib/gzr/commands/model/set/cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# The MIT License (MIT)

# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# frozen_string_literal: true

require_relative '../../../command'
require_relative '../../../modules/model/set'
require_relative '../../../modules/filehelper'

module Gzr
module Commands
class Model
class Set
class Cat < Gzr::Command
include Gzr::Model::Set
include Gzr::FileHelper
def initialize(model_set_id,options)
super()
@model_set_id = model_set_id
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
data = cat_model_set(@model_set_id)
if data.nil?
say_warning "Model Set #{@model_set_id} not found"
return
end
data = trim_model_set(data) if @options[:trim]

write_file(@options[:dir] ? "Model_Set_#{data[:name]}.json" : nil, @options[:dir],nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
end
end
end
end
end
end
73 changes: 73 additions & 0 deletions lib/gzr/commands/model/set/import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# The MIT License (MIT)

# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# frozen_string_literal: true

require_relative '../../../../gzr'
require_relative '../../../command'
require_relative '../../../modules/model/set'
require_relative '../../../modules/filehelper'

module Gzr
module Commands
class Model
class Set
class Import < Gzr::Command
include Gzr::Model::Set
include Gzr::FileHelper
def initialize(file, options)
super()
@file = file
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}", output: output) if @options[:debug]
with_session do
model_set = nil

read_file(@file) do |data|
search_results = search_model_sets(name: data[:name])
if search_results && search_results.length == 1
name = data[:name]
if !@options[:force]
raise Gzr::CLI::Error, "Model Set #{name} already exists\nUse --force if you want to overwrite it"
end
data.select! do |k,v|
keys_to_keep('update_model_set').include? k
end
model_set = update_model_set(search_results.first[:id], data)
else
data.select! do |k,v|
keys_to_keep('create_model_set').include? k
end
model_set = create_model_set(data)
end
output.puts "Imported model set #{model_set[:id]}" unless @options[:plain]
output.puts model_set[:id] if @options[:name]
end
end
end
end
end
end
end
end
74 changes: 74 additions & 0 deletions lib/gzr/commands/model/set/ls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# The MIT License (MIT)

# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# frozen_string_literal: true

require_relative '../../../command'
require_relative '../../../modules/model/set'
require 'tty-table'

module Gzr
module Commands
class Model
class Set
class Ls < Gzr::Command
include Gzr::Model::Set
def initialize(options)
super()
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
data = all_model_sets(@options[:fields])
begin
say_ok "No model sets found"
return nil
end unless data && data.length > 0

table_hash = Hash.new
fields = field_names(@options[:fields])
table_hash[:header] = fields unless @options[:plain]
expressions = fields.collect { |fn| field_expression(fn) }
table_hash[:rows] = data.map do |row|
expressions.collect do |e|
eval "row.#{e}"
end
end
table = TTY::Table.new(table_hash)
alignments = fields.collect do |k|
(k =~ /id$/) ? :right : :left
end
begin
if @options[:csv] then
output.puts render_csv(table)
else
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: @options[:width] || TTY::Screen.width)
end
end if table
end
end
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/gzr/commands/model/set/rm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# The MIT License (MIT)

# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# frozen_string_literal: true

require_relative '../../../command'
require_relative '../../../modules/model/set'

module Gzr
module Commands
class Model
class Set
class Delete < Gzr::Command
include Gzr::Model::Set
def initialize(model_set_id,options)
super()
@model_set_id = model_set_id
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
delete_model_set(@model_set_id)
end
end
end
end
end
end
end
10 changes: 2 additions & 8 deletions lib/gzr/commands/subcommandbase.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

# Copyright (c) 2018 Mike DeAngelo Looker Data Sciences, Inc.
# Copyright (c) 2023 Mike DeAngelo Google, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
Expand Down Expand Up @@ -29,13 +29,7 @@ class SubCommandBase < Thor
# Workaround so that help displays the right name
# base on this link
# https://github.com/erikhuda/thor/issues/261#issuecomment-69327685
def self.banner(command, namespace = nil, subcommand = false)
"#{basename} #{subcommand_prefix} #{command.usage}"
end

def self.subcommand_prefix
self.name.gsub(%r{.*::}, '').gsub(%r{^[A-Z]}) { |match| match[0].downcase }.gsub(%r{[A-Z]}) { |match| "-#{match[0].downcase}" }
end
# No longer needed
end
end
end
Loading

0 comments on commit 1b964ca

Please sign in to comment.