<commit_before># -------------------------------------------------------------------------- #
# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

require 'one_helper'

class OneDatastoreHelper < OpenNebulaHelper::OneHelper
    DATASTORE = {
        :name   => "datastore",
        :short  => "-d id|name",
        :large  => "--datastore id|name" ,
        :description => "Selects the datastore",
        :format => String,
        :proc   => lambda { |o, options|
            OpenNebulaHelper.rname_to_id(o, "DATASTORE")
        }
    }

    def self.rname
        "DATASTORE"
    end

    def self.conf_file
        "onedatastore.yaml"
    end

    def format_pool(options)
        config_file = self.class.table_conf

        table = CLIHelper::ShowTable.new(config_file, self) do
            column :ID, "ONE identifier for the Datastore", :size=>4 do |d|
                d["ID"]
            end

            column :NAME, "Name of the Datastore", :left, :size=>13 do |d|
                d["NAME"]
            end

            column :SIZE, "Datastore total size", :size =>10 do |d|
                OpenNebulaHelper.unit_to_str(d['TOTAL_MB'].to_i, {}, 'M')
            end

            column :AVAIL, "Datastore free size", :left, :size =>5 do |d|
                if d['TOTAL_MB'].to_i == 0
                    "-"
                else
                    "#{((d['FREE_MB'].to_f/d['TOTAL_MB'].to_f) * 100).round()}%"
                end
            end

            column :CLUSTER, "Name of the Cluster", :left, :size=>12 do |d|
                OpenNebulaHelper.cluster_str(d["CLUSTER"])
            end

            column :IMAGES, "Number of Images", :size=>6 do |d|
                if d["IMAGES"]["ID"].nil?
                    "0"
                else
                    d["IMAGES"]["ID"].size
                end
            end

            column :TYPE, "Datastore type", :left, :size=>4 do |d|
                type = Datastore::DATASTORE_TYPES[d["TYPE"].to_i]
                Datastore::SHORT_DATASTORE_TYPES[type]
            end

            column :DS, "Datastore driver", :left, :size=>8 do |d|
                d["DS_MAD"]
            end

            column :TM, "Transfer driver", :left, :size=>8 do |d|
                d["TM_MAD"]
            end

            default :ID, :NAME, :SIZE, :AVAIL, :CLUSTER, :IMAGES, :TYPE, :DS, :TM
        end

        table
    end

    private

    def factory(id=nil)
        if id
            OpenNebula::Datastore.new_with_id(id, @client)
        else
            xml=OpenNebula::Datastore.build_xml
            OpenNebula::Datastore.new(xml, @client)
        end
    end

    def factory_pool(user_flag=-2)
        #TBD OpenNebula::UserPool.new(@client, user_flag)
        OpenNebula::DatastorePool.new(@client)
    end

    def format_resource(datastore, options = {})
        str="%-15s: %-20s"
        str_h1="%-80s"

        CLIHelper.print_header(str_h1 % "DATASTORE #{datastore['ID']} INFORMATION")
        puts str % ["ID",       datastore.id.to_s]
        puts str % ["NAME",     datastore.name]
        puts str % ["USER",     datastore['UNAME']]
        puts str % ["GROUP",    datastore['GNAME']]
        puts str % ["CLUSTER",  OpenNebulaHelper.cluster_str(datastore['CLUSTER'])]

        puts str % ["TYPE",     datastore.type_str]
        puts str % ["DS_MAD",   datastore['DS_MAD']]
        puts str % ["TM_MAD",   datastore['TM_MAD']]
        puts str % ["BASE PATH",datastore['BASE_PATH']]
        puts str % ["DISK_TYPE",Image::DISK_TYPES[datastore['DISK_TYPE'].to_i]]
        puts

        CLIHelper.print_header(str_h1 % "DATASTORE CAPACITY", false)
        puts str % ["TOTAL:", OpenNebulaHelper.unit_to_str(datastore['TOTAL_MB'].to_i, {},'M')]
        puts str % ["USED: ", OpenNebulaHelper.unit_to_str(datastore['USED_MB'].to_i, {},'M')]
        puts str % ["FREE:",  OpenNebulaHelper.unit_to_str(datastore['FREE_MB'].to_i, {},'M')]
        puts

        CLIHelper.print_header(str_h1 % "PERMISSIONS",false)<commit_msg>Feature #1678: Do not show local system DS monitoring information in onedatastore output<commit_after># -------------------------------------------------------------------------- #
# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

require 'one_helper'

class OneDatastoreHelper < OpenNebulaHelper::OneHelper
    DATASTORE = {
        :name   => "datastore",
        :short  => "-d id|name",
        :large  => "--datastore id|name" ,
        :description => "Selects the datastore",
        :format => String,
        :proc   => lambda { |o, options|
            OpenNebulaHelper.rname_to_id(o, "DATASTORE")
        }
    }

    def self.rname
        "DATASTORE"
    end

    def self.conf_file
        "onedatastore.yaml"
    end

    def format_pool(options)
        config_file = self.class.table_conf

        table = CLIHelper::ShowTable.new(config_file, self) do
            column :ID, "ONE identifier for the Datastore", :size=>4 do |d|
                d["ID"]
            end

            column :NAME, "Name of the Datastore", :left, :size=>13 do |d|
                d["NAME"]
            end

            column :SIZE, "Datastore total size", :size =>10 do |d|
                shared = d['TEMPLATE']['SHARED']
                if shared != nil && shared.upcase == 'NO'
                    "-"
                else
                    OpenNebulaHelper.unit_to_str(d['TOTAL_MB'].to_i, {}, 'M')
                end
            end

            column :AVAIL, "Datastore free size", :left, :size =>5 do |d|
                if d['TOTAL_MB'].to_i == 0
                    "-"
                else
                    "#{((d['FREE_MB'].to_f/d['TOTAL_MB'].to_f) * 100).round()}%"
                end
            end

            column :CLUSTER, "Name of the Cluster", :left, :size=>12 do |d|
                OpenNebulaHelper.cluster_str(d["CLUSTER"])
            end

            column :IMAGES, "Number of Images", :size=>6 do |d|
                if d["IMAGES"]["ID"].nil?
                    "0"
                else
                    d["IMAGES"]["ID"].size
                end
            end

            column :TYPE, "Datastore type", :left, :size=>4 do |d|
                type = Datastore::DATASTORE_TYPES[d["TYPE"].to_i]
                Datastore::SHORT_DATASTORE_TYPES[type]
            end

            column :DS, "Datastore driver", :left, :size=>8 do |d|
                d["DS_MAD"]
            end

            column :TM, "Transfer driver", :left, :size=>8 do |d|
                d["TM_MAD"]
            end

            default :ID, :NAME, :SIZE, :AVAIL, :CLUSTER, :IMAGES, :TYPE, :DS, :TM
        end

        table
    end

    private

    def factory(id=nil)
        if id
            OpenNebula::Datastore.new_with_id(id, @client)
        else
            xml=OpenNebula::Datastore.build_xml
            OpenNebula::Datastore.new(xml, @client)
        end
    end

    def factory_pool(user_flag=-2)
        #TBD OpenNebula::UserPool.new(@client, user_flag)
        OpenNebula::DatastorePool.new(@client)
    end

    def format_resource(datastore, options = {})
        str="%-15s: %-20s"
        str_h1="%-80s"

        CLIHelper.print_header(str_h1 % "DATASTORE #{datastore['ID']} INFORMATION")
        puts str % ["ID",       datastore.id.to_s]
        puts str % ["NAME",     datastore.name]
        puts str % ["USER",     datastore['UNAME']]
        puts str % ["GROUP",    datastore['GNAME']]
        puts str % ["CLUSTER",  OpenNebulaHelper.cluster_str(datastore['CLUSTER'])]

        puts str % ["TYPE",     datastore.type_str]
        puts str % ["DS_MAD",   datastore['DS_MAD']]
        puts str % ["TM_MAD",   datastore['TM_MAD']]
        puts str % ["BASE PATH",datastore['BASE_PATH']]
        puts str % ["DISK_TYPE",Image::DISK_TYPES[datastore['DISK_TYPE'].to_i]]
        puts

        CLIHelper.print_header(str_h1 % "DATASTORE CAPACITY", false)

        shared = datastore['TEMPLATE/SHARED']
        local = shared != nil && shared.upcase == 'NO'

        puts str % ["TOTAL:", local ? '-' : OpenNebulaHelper.unit_to_str(datastore['TOTAL_MB'].to_i, {},'M')]
        puts str % ["USED: ", local ? '-' : OpenNebulaHelper.unit_to_str(datastore['USED_MB'].to_i, {},'M')]
        puts str % ["FREE:",  local ? '-' : OpenNebulaHelper.unit_to_str(datastore['FREE_MB'].to_i, {},'M')]
        puts

        CLIHelper.print_header(str_h1 % "PERMISSIONS",false)
{'fraction_non_alphanumeric': 0.14976213130352045, 'fraction_numerical': 0.008372978116079923, 'mean_word_length': 1.5133907221425156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '120396', 'n_tokens_mistral': 3189, 'n_tokens_neox': 3078, 'n_words': 722}