"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = NodeSummary;

var _core = require("@kui-shell/core");

var _Generic = require("./Generic");

var _parse = _interopRequireDefault(require("../../../../util/parse"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
 * Copyright 2020 IBM Corporation
 *
 * 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.
 */

/**
 * Name: 10.73.230.207
 * Status: Ready
 * Roles: <none>
 * Age: 435d
 * Version: v1.15.1+IKS
 * Internal-ip: 10.73.230.207
 * External-ip: 169.62.248.114
 * Os-image: Ubuntu 16.04.6 LTS
 * Kernel-version: 4.4.0-157-generic
 * Container-runtime: 'containerd://1.2.7'
 *
 */
const strings = (0, _core.i18n)('plugin-kubectl');

function address(type, node) {
  const addr = node.status.addresses.find(_ => _.type === type);
  return addr ? addr.address : (0, _Generic.none)();
}

function tryParse(intAsString) {
  try {
    return parseInt(intAsString);
  } catch (err) {
    return intAsString;
  }
}

function free(metric, {
  status
}) {
  return strings('percentFree', _parse.default.fractionOfMemory(status.allocatable[metric], status.capacity[metric]));
}

function NodeSummary(node) {
  const {
    metadata,
    status
  } = node;
  return {
    Name: metadata.name,
    Age: (0, _Generic.age)(node),
    CPUs: tryParse(status.capacity.cpu),
    Memory: _parse.default.reformatAsBytes(status.capacity.memory) + ` (${free('memory', node)})`,
    'Ephemeral Storage': _parse.default.reformatAsBytes(status.capacity['ephemeral-storage']) + ` (${free('ephemeral-storage', node)})`,
    'External IP': address('ExternalIP', node),
    'Internal IP': address('InternalIP', node),
    'OS Image': status.nodeInfo.osImage,
    'Kernel Version': status.nodeInfo.kernelVersion,
    'Container Runtime': status.nodeInfo.containerRuntimeVersion
  };
}