Get rid of lots of unnecessary lodash calls.
This commit is contained in:
parent
b734a422a7
commit
5592892f0d
16 changed files with 99 additions and 115 deletions
server/resources
|
@ -1,5 +1,3 @@
|
|||
import _ from "lodash";
|
||||
|
||||
import Constraints from "../validation/constraints";
|
||||
import ErrorTypes from "../utils/errorTypes";
|
||||
import * as MonitoringService from "../services/monitoringService";
|
||||
|
@ -13,12 +11,12 @@ import {
|
|||
CreateOrUpdateNode,
|
||||
DomainSpecificNodeResponse,
|
||||
isNodeSortField,
|
||||
isToken, JSONObject,
|
||||
isToken,
|
||||
JSONObject,
|
||||
MAC,
|
||||
NodeResponse,
|
||||
NodeStateData,
|
||||
NodeTokenResponse,
|
||||
StoredNode,
|
||||
toDomainSpecificNodeResponse,
|
||||
Token,
|
||||
toNodeResponse,
|
||||
|
@ -27,15 +25,18 @@ import {
|
|||
|
||||
const nodeFields = ['hostname', 'key', 'email', 'nickname', 'mac', 'coords', 'monitoring'];
|
||||
|
||||
// TODO: Rename
|
||||
function getNormalizedNodeData(reqData: any): CreateOrUpdateNode {
|
||||
const node: { [key: string]: any } = {};
|
||||
_.each(nodeFields, function (field) {
|
||||
for (const field of nodeFields) {
|
||||
let value = normalizeString(reqData[field]);
|
||||
if (field === 'mac') {
|
||||
value = normalizeMac(value as MAC);
|
||||
}
|
||||
node[field] = value;
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Add typeguard before cast.
|
||||
return node as CreateOrUpdateNode;
|
||||
}
|
||||
|
||||
|
@ -90,15 +91,15 @@ async function doGetAll(req: Request): Promise<{ total: number; pageNodes: any }
|
|||
|
||||
const nodes = await NodeService.getAllNodes();
|
||||
|
||||
const realNodes = _.filter(nodes, node =>
|
||||
const realNodes = nodes.filter(node =>
|
||||
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
||||
!!node.token
|
||||
!!node.token // FIXME: As node.token may not be undefined or null here, handle this when loading!
|
||||
);
|
||||
|
||||
const macs: MAC[] = _.map(realNodes, (node: StoredNode): MAC => node.mac);
|
||||
const macs: MAC[] = realNodes.map(node => node.mac);
|
||||
const nodeStateByMac = await MonitoringService.getByMacs(macs);
|
||||
|
||||
const domainSpecificNodes: DomainSpecificNodeResponse[] = _.map(realNodes, (node: StoredNode): DomainSpecificNodeResponse => {
|
||||
const domainSpecificNodes: DomainSpecificNodeResponse[] = realNodes.map(node => {
|
||||
const nodeState: NodeStateData = nodeStateByMac[node.mac] || {};
|
||||
return toDomainSpecificNodeResponse(node, nodeState);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue