mirror of
https://github.com/skooner-k8s/skooner.git
synced 2026-08-24 04:16:18 +00:00
convert all utils into ts
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
|
||||
export default function test(filter = '', ...values) {
|
||||
export default function test(filter = '', ...values: string[]) {
|
||||
const value = filter.toLowerCase();
|
||||
return values
|
||||
.filter(x => !!x)
|
||||
.some(x => x.toLowerCase().includes(value));
|
||||
}
|
||||
|
||||
export function filterByOwner(items, owner) {
|
||||
export function filterByOwner(items: any[], owner: {[key: string]: any}) {
|
||||
if (!items || !owner) return null;
|
||||
|
||||
const {uid} = owner.metadata;
|
||||
@@ -15,19 +15,19 @@ export function filterByOwner(items, owner) {
|
||||
if (x.involvedObject && x.involvedObject.uid === uid) return true;
|
||||
|
||||
const {ownerReferences} = x.metadata;
|
||||
return ownerReferences && ownerReferences.some(y => y.uid === uid);
|
||||
return ownerReferences && ownerReferences.some((y: {uid: string}) => y.uid === uid);
|
||||
});
|
||||
}
|
||||
|
||||
export function filterByOwners(items, owners) {
|
||||
export function filterByOwners(items: any[] , owners: {[key: string]: any}) {
|
||||
if (!items || !owners) return null;
|
||||
|
||||
const uids = owners.map(x => x.metadata.uid);
|
||||
const uids = owners.map((x: any) => x.metadata.uid);
|
||||
|
||||
return items.filter((x) => {
|
||||
if (x.involvedObject && uids.includes(x.involvedObject.uid)) return true;
|
||||
|
||||
const {ownerReferences} = x.metadata;
|
||||
return ownerReferences && ownerReferences.some(y => uids.includes(y.uid));
|
||||
return ownerReferences && ownerReferences.some((y: {uid: string}) => uids.includes(y.uid));
|
||||
});
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import {parseRam, parseCpu} from './unitHelpers';
|
||||
|
||||
export default function getMetrics(items, metrics) {
|
||||
export default function getMetrics(items: any[], metrics: any[]) {
|
||||
if (!items || !metrics) return null;
|
||||
|
||||
const names = _.map(items, x => x.metadata.name);
|
||||
@@ -12,7 +12,7 @@ export default function getMetrics(items, metrics) {
|
||||
|
||||
|
||||
// Node helpers
|
||||
export function getNodeResourceValue(node, pods, resource, type, phases) {
|
||||
export function getNodeResourceValue(node: {[key: string]: any}, pods: any[], resource: string, type: string, phases: string[]) {
|
||||
if (!node || !pods) return null;
|
||||
|
||||
return _(pods)
|
||||
@@ -21,23 +21,23 @@ export function getNodeResourceValue(node, pods, resource, type, phases) {
|
||||
.sumBy(x => getPodResourceValue(x, resource, type));
|
||||
}
|
||||
|
||||
export function getNodeResourcePercent(node, pods, resource, type) {
|
||||
export function getNodeResourcePercent(node: {[key: string]: any}, pods: any[], resource: string, type: string) {
|
||||
const used = getNodeResourceValue(node, pods, resource, type, ['Running']);
|
||||
const available = getNodeResourcesAvailable(node, resource);
|
||||
return used / available;
|
||||
return used && available ? used / available: null;
|
||||
}
|
||||
|
||||
export function getNodeUsagePercent(node, metrics, resource) {
|
||||
export function getNodeUsagePercent(node: {[key: string]: any}, metrics: {[key: string]: any}, resource: string) {
|
||||
const used = getNodeUsage(node, metrics, resource);
|
||||
const available = getNodeResourcesAvailable(node, resource);
|
||||
return used / available;
|
||||
return used && available ? used / available: null;
|
||||
}
|
||||
|
||||
export function getNodeResourcesAvailable(node, resource) {
|
||||
export function getNodeResourcesAvailable(node: {[key: string]: any}, resource: string) {
|
||||
return node ? parse(resource, node.status.capacity) : null;
|
||||
}
|
||||
|
||||
export function getNodeUsage(node, metrics, resource) {
|
||||
export function getNodeUsage(node: {[key: string]: any}, metrics: {[key: string]: any}, resource: string) {
|
||||
if (!node || !metrics) return null;
|
||||
|
||||
const result = metrics[node.metadata.name];
|
||||
@@ -46,26 +46,26 @@ export function getNodeUsage(node, metrics, resource) {
|
||||
|
||||
|
||||
// Pod helpers
|
||||
export function getPodResourcePercent(item, metrics, resource, type) {
|
||||
export function getPodResourcePercent(item: any, metrics: {[key: string]: any}, resource: string, type: string) {
|
||||
const actual = getPodUsage(item, metrics, resource);
|
||||
const request = getPodResourceValue(item, resource, type);
|
||||
return actual / request;
|
||||
return actual ? actual / request: null;
|
||||
}
|
||||
|
||||
export function getPodUsage(pod, metrics, resource) {
|
||||
export function getPodUsage(pod: {[key: string]: any}, metrics: {[key: string]: any}, resource: string) {
|
||||
if (!pod || !metrics) return null;
|
||||
|
||||
const metric = metrics[pod.metadata.name] || {};
|
||||
return _.sumBy(metric.containers, x => parse(resource, x.usage));
|
||||
return _.sumBy(metric.containers, (x: any) => parse(resource, x.usage));
|
||||
}
|
||||
|
||||
export function getPodResourceValue(pod, resource, type) {
|
||||
export function getPodResourceValue(pod: {[key: string]: any}, resource: string, type: string) {
|
||||
return _(pod.spec.containers)
|
||||
.filter(x => x.resources && x.resources[type])
|
||||
.sumBy(x => parse(resource, x.resources[type]));
|
||||
}
|
||||
|
||||
function parse(resource, target) {
|
||||
function parse(resource: string, target: {[key: string]: string}) {
|
||||
const parser = resource === 'cpu' ? parseCpu : parseRam;
|
||||
return parser(target[resource]);
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
* @param {*status* object with a status field (most likely the row from the TableBody)}
|
||||
* @returns the status text, as defined in https://kubernetes.io/docs/concepts/architecture/nodes/#condition
|
||||
*/
|
||||
function getReadyStatus({status}) {
|
||||
function getReadyStatus({status}: {status: {[key: string]: any}}) {
|
||||
if (!status.conditions) return null;
|
||||
const ready = status.conditions.find(y => y.type === 'Ready');
|
||||
const ready = status.conditions.find((y: any) => y.type === 'Ready');
|
||||
return ready && ready.status;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ const URL_REGEXP = new RegExp(
|
||||
/**
|
||||
* Check whether a string is a valid URL
|
||||
*/
|
||||
export function isAValidURL(str) {
|
||||
export function isAValidURL(str: string) {
|
||||
return URL_REGEXP.test(str.trim());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user