Get rid of lots of unnecessary lodash calls.

This commit is contained in:
baldo 2022-07-28 13:16:13 +02:00
parent b734a422a7
commit 5592892f0d
16 changed files with 99 additions and 115 deletions

View file

@ -170,7 +170,7 @@ export async function getPendingMails(restParams: RestParams): Promise<{ mails:
const mails = await db.all(
'SELECT * FROM email_queue WHERE ' + filter.query,
_.concat([], filter.params),
filter.params,
);
return {

View file

@ -97,7 +97,7 @@ export async function render(mailOptions: Mail): Promise<{subject: string, body:
try {
return {
subject: _.trim(_.template(subject.toString())(data)),
subject: _.template(subject.toString())(data).trim(),
body: _.template(body.toString())(data)
};
} catch (error) {

View file

@ -19,10 +19,13 @@ import {
Domain,
DurationSeconds,
Hostname,
isBoolean,
isDomain,
isMonitoringSortField,
isOnlineState,
isSite,
isString,
isUndefined,
MAC,
MailType,
MonitoringSortField,
@ -164,7 +167,7 @@ async function storeNodeInformation(nodeData: ParsedNode, node: StoredNode): Pro
const row = await db.get('SELECT * FROM node_state WHERE mac = ?', [node.mac]);
if (_.isUndefined(row)) {
if (isUndefined(row)) {
return await insertNodeInformation(nodeData, node);
} else {
return await updateNodeInformation(nodeData, node, row);
@ -188,7 +191,7 @@ export function parseNode(importTimestamp: UnixTimestampSeconds, nodeData: any):
}
const nodeId = nodeData.nodeinfo.node_id;
if (!nodeId || !_.isString(nodeId)) {
if (!nodeId || !isString(nodeId)) {
throw new Error(
`Invalid node id of type "${typeof nodeId}": ${nodeId}`
);
@ -212,7 +215,7 @@ export function parseNode(importTimestamp: UnixTimestampSeconds, nodeData: any):
'Node ' + nodeId + ': Unexpected flags type: ' + (typeof nodeData.flags)
);
}
if (!_.isBoolean(nodeData.flags.online)) {
if (!isBoolean(nodeData.flags.online)) {
throw new Error(
'Node ' + nodeId + ': Unexpected flags.online type: ' + (typeof nodeData.flags.online)
);
@ -558,6 +561,7 @@ async function retrieveNodeInformationForUrls(urls: string[]): Promise<RetrieveN
}
}
// FIXME: Replace any[] by type.
export async function getAll(restParams: RestParams): Promise<{ total: number, monitoringStates: any[] }> {
const filterFields = [
'hostname',
@ -571,7 +575,7 @@ export async function getAll(restParams: RestParams): Promise<{ total: number, m
const row = await db.get<{ total: number }>(
'SELECT count(*) AS total FROM node_state WHERE ' + where.query,
_.concat([], where.params),
where.params,
);
const total = row?.total || 0;
@ -585,7 +589,7 @@ export async function getAll(restParams: RestParams): Promise<{ total: number, m
const monitoringStates = await db.all(
'SELECT * FROM node_state WHERE ' + filter.query,
_.concat([], filter.params),
filter.params,
);
return {monitoringStates, total};
@ -603,7 +607,7 @@ export async function getByMacs(macs: MAC[]): Promise<Record<MAC, NodeStateData>
const rows = await db.all<NodeStateRow>(
'SELECT * FROM node_state WHERE ' + inCondition.query,
_.concat([], inCondition.params),
inCondition.params,
);
for (const row of rows) {
@ -734,7 +738,7 @@ async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds):
deletionCandidates.length
);
const deletionCandidateMacs: MAC[] = _.map(deletionCandidates, node => node.mac);
const deletionCandidateMacs: MAC[] = deletionCandidates.map(node => node.mac);
const chunks: MAC[][] = _.chunk(deletionCandidateMacs, NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE);
Logger
@ -753,10 +757,7 @@ async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds):
' MACs for deletion.'
);
const placeholders = _.join(
_.map(macs, () => '?'),
','
);
const placeholders = macs.map(() => '?').join(',');
const rows: { mac: MAC }[] = await db.all(
`SELECT * FROM node_state WHERE mac IN (${placeholders})`,
@ -773,7 +774,7 @@ async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds):
' nodes found in monitoring database. Those should be skipped.'
);
const seenMacs: MAC[] = _.map(rows, (row: { mac: MAC }) => row.mac as MAC);
const seenMacs: MAC[] = rows.map(row => row.mac);
const neverSeenMacs = _.difference(macs, seenMacs);
Logger

View file

@ -108,15 +108,15 @@ async function findFilesInPeersPath(): Promise<string[]> {
}
function parseNodeFilename(filename: string): NodeFilenameParsed {
const parts = _.split(filename, '@', filenameParts.length);
const parts = filename.split('@', filenameParts.length);
const parsed: { [key: string]: string | undefined } = {};
const zippedParts = _.zip<string, string>(filenameParts, parts);
_.each(zippedParts, part => {
for (const part of zippedParts) {
const key = part[0];
if (key) {
parsed[key] = part[1];
}
});
}
return parsed;
}
@ -565,14 +565,14 @@ export async function fixNodeFilenames(): Promise<void> {
export async function findNodesModifiedBefore(timestamp: UnixTimestampSeconds): Promise<StoredNode[]> {
const nodes = await getAllNodes();
return _.filter(nodes, node => node.modifiedAt < timestamp);
return nodes.filter(node => node.modifiedAt < timestamp);
}
export async function getNodeStatistics(): Promise<NodeStatistics> {
const nodes = await getAllNodes();
const nodeStatistics: NodeStatistics = {
registered: _.size(nodes),
registered: nodes.length,
withVPN: 0,
withCoords: 0,
monitoring: {