Package com.cloudbees.sdk.pool

Source Code of com.cloudbees.sdk.pool.PoolsStats

/*
* Copyright 2010-2014, CloudBees Inc.
*
* 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.
*/

package com.cloudbees.sdk.pool;

import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ServerInfo;
import com.cloudbees.api.ServerPoolInfo;
import com.cloudbees.api.ServerPoolListResponse;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.server.ServerStatsBase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author Fabian Donze
*/
@BeesCommand(group = "Server", description = "Get pool capacity information")
@CLICommand("app:pool:stats")
public class PoolsStats extends ServerStatsBase {
    Boolean details;

    public PoolsStats() {
        super();
        setArgumentExpected(0);
    }

    public boolean printDetails() {
        return details == null ? false : details;
    }

    public void setDetails(Boolean details) {
        this.details = details;
    }

    @Override
    protected String getUsageMessage() {
        return "POOL_ID";
    }

    @Override
    protected boolean preParseCommandLine() {
        if (super.preParseCommandLine()) {
            addOption( null, "details", false, "Print more detailed information" );
            return true;
        }
        return false;
    }

    @Override
    protected boolean execute() throws Exception {
        BeesClient client = getBeesClient(BeesClient.class);

        String poolId = getParameterName();
        if (poolId != null) {
            poolId = getParameterID();
            ServerPoolInfo poolInfo = client.serverPoolInfo(poolId, false);
            printPoolStats(poolInfo, printDetails());
        } else {
            ServerPoolListResponse res = client.serverPoolList(getAccount());
            for (ServerPoolInfo poolInfo : res.getPools()) {
                if (poolInfo.getState() != null && poolInfo.getState().equalsIgnoreCase("active")) {
                    printPoolStats(poolInfo, printDetails());
                }
            }
        }
        return true;
    }

    private void printPoolStats(ServerPoolInfo poolInfo, boolean details) throws Exception {
        System.out.print("Pool            : " + poolInfo.getName());
        if (details) System.out.println(" (" + poolInfo.getId() + ")");
        else System.out.println();
        List<ServerInfo> servers = poolInfo.getServers();
        if (servers != null && servers.size() > 0) {
            List<ServerInfo> activeServers = new ArrayList<ServerInfo>();
            for (ServerInfo server : servers) {
                if (server.getState() != null && server.getState().equalsIgnoreCase("running")) {
                    Map<String, String> parameters = server.getParameters();
                    String status = null;
                    if (parameters != null)
                        status = parameters.get("status");
                    if (status != null && status.equalsIgnoreCase("active")) {
                        activeServers.add(server);
                    }
                }
            }
            System.out.println("  active servers: " + activeServers.size());
            for (ServerInfo server : activeServers) {
                printServerStats(server, details, poolInfo);
                System.out.println();
            }
        }
    }
}
TOP

Related Classes of com.cloudbees.sdk.pool.PoolsStats

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.