From 6d0e0dce44cbb77e01fa9ddb95d488516a8bafdb Mon Sep 17 00:00:00 2001 From: atu Date: Wed, 5 Aug 2020 16:30:10 -0700 Subject: [PATCH] convert all utils into ts --- .../{filterHelper.js => filterHelper.ts} | 12 ++++---- .../{metricsHelpers.js => metricsHelpers.tsx} | 28 +++++++++---------- .../utils/{nodeHelpers.js => nodeHelpers.ts} | 4 +-- client/src/utils/{string.js => string.ts} | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) rename client/src/utils/{filterHelper.js => filterHelper.ts} (56%) rename client/src/utils/{metricsHelpers.js => metricsHelpers.tsx} (55%) rename client/src/utils/{nodeHelpers.js => nodeHelpers.ts} (68%) rename client/src/utils/{string.js => string.ts} (95%) diff --git a/client/src/utils/filterHelper.js b/client/src/utils/filterHelper.ts similarity index 56% rename from client/src/utils/filterHelper.js rename to client/src/utils/filterHelper.ts index 4915dbe..03b297a 100644 --- a/client/src/utils/filterHelper.js +++ b/client/src/utils/filterHelper.ts @@ -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)); }); } diff --git a/client/src/utils/metricsHelpers.js b/client/src/utils/metricsHelpers.tsx similarity index 55% rename from client/src/utils/metricsHelpers.js rename to client/src/utils/metricsHelpers.tsx index 2ba0d32..d449dcf 100644 --- a/client/src/utils/metricsHelpers.js +++ b/client/src/utils/metricsHelpers.tsx @@ -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]); } diff --git a/client/src/utils/nodeHelpers.js b/client/src/utils/nodeHelpers.ts similarity index 68% rename from client/src/utils/nodeHelpers.js rename to client/src/utils/nodeHelpers.ts index 4f2eb4a..f831669 100644 --- a/client/src/utils/nodeHelpers.js +++ b/client/src/utils/nodeHelpers.ts @@ -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; } diff --git a/client/src/utils/string.js b/client/src/utils/string.ts similarity index 95% rename from client/src/utils/string.js rename to client/src/utils/string.ts index b5018eb..8e3f894 100644 --- a/client/src/utils/string.js +++ b/client/src/utils/string.ts @@ -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()); }