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";
|
||||
|
@ -17,8 +15,8 @@ async function doGetAll(req: Request): Promise<{ total: number, result: any }> {
|
|||
const {monitoringStates, total} = await MonitoringService.getAll(restParams);
|
||||
return {
|
||||
total,
|
||||
result: _.map(monitoringStates, function (state) {
|
||||
state.mapId = _.toLower(state.mac).replace(/:/g, '');
|
||||
result: monitoringStates.map(state => {
|
||||
state.mapId = state.mac.toLowerCase().replace(/:/g, "");
|
||||
return state;
|
||||
})
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import _ from "lodash";
|
||||
|
||||
import CONSTRAINTS from "../validation/constraints";
|
||||
import ErrorTypes from "../utils/errorTypes";
|
||||
import * as Resources from "../utils/resources";
|
||||
import {Entity, handleJSONWithData, RequestData} from "../utils/resources";
|
||||
import {handleJSONWithData, RequestData} from "../utils/resources";
|
||||
import {getTasks, Task, TaskState} from "../jobs/scheduler";
|
||||
import {normalizeString} from "../utils/strings";
|
||||
import {forConstraint} from "../validation/validator";
|
||||
|
@ -77,11 +75,11 @@ async function setTaskEnabled(data: RequestData, enable: boolean): Promise<TaskR
|
|||
return toTaskResponse(task);
|
||||
}
|
||||
|
||||
async function doGetAll(req: Request): Promise<{ total: number, pageTasks: Entity[] }> {
|
||||
async function doGetAll(req: Request): Promise<{ total: number, pageTasks: Task[] }> {
|
||||
const restParams = await Resources.getValidRestParams('list', null, req);
|
||||
|
||||
const tasks = Resources.sort(
|
||||
_.values(getTasks()),
|
||||
Object.values(getTasks()),
|
||||
isTaskSortField,
|
||||
restParams
|
||||
);
|
||||
|
@ -104,7 +102,7 @@ export function getAll(req: Request, res: Response): void {
|
|||
doGetAll(req)
|
||||
.then(({total, pageTasks}) => {
|
||||
res.set('X-Total-Count', total.toString(10));
|
||||
Resources.success(res, _.map(pageTasks, toTaskResponse));
|
||||
Resources.success(res, pageTasks.map(toTaskResponse));
|
||||
})
|
||||
.catch(err => Resources.error(res, err));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue