Merge branch 'main' into new-admin

This commit is contained in:
baldo 2022-08-25 23:08:58 +02:00
commit 8cf692b523
2 changed files with 126 additions and 102 deletions

View file

@ -8,7 +8,6 @@ import {
hasOwnProperty,
isLayerConfig,
isPlainObject,
isString,
Url,
Version,
} from "./types";

View file

@ -32,20 +32,16 @@ import {
isUndefined,
JSONValue,
MAC,
MailId,
MailType,
MapId,
mapIdFromMAC,
MonitoringSortField,
MonitoringState,
MonitoringToken,
NodeId,
NodeMonitoringStateResponse,
NodeStateData,
NodeStateId,
OnlineState,
parseJSON,
RunResult,
Site,
StoredNode,
toCreateOrUpdateNode,
@ -144,7 +140,7 @@ async function insertNodeInformation(
async function updateNodeInformation(
nodeData: ParsedNode,
node: StoredNode,
row: any
row: NodeStateRow
): Promise<void> {
Logger.tag("monitoring", "informacallbacktion-retrieval").debug(
"Node is known in monitoring: %s",
@ -201,9 +197,10 @@ async function storeNodeInformation(
nodeData.mac
);
const row = await db.get("SELECT * FROM node_state WHERE mac = ?", [
node.mac,
]);
const row = await db.get<NodeStateRow>(
"SELECT * FROM node_state WHERE mac = ?",
[node.mac]
);
if (isUndefined(row)) {
return await insertNodeInformation(nodeData, node);
@ -365,8 +362,8 @@ export function parseNodesJson(body: string): NodesParsingResult {
async function updateSkippedNode(
id: NodeStateId,
node?: StoredNode
): Promise<RunResult> {
return await db.run(
): Promise<void> {
await db.run(
"UPDATE node_state " +
"SET hostname = ?, monitoring_state = ?, modified_at = ?" +
"WHERE id = ?",
@ -384,19 +381,35 @@ async function sendMonitoringMailsBatched(
name
);
while (true) {
Logger.tag("monitoring", "mail-sending").debug("Sending next batch...");
let nodeStates = await findBatchFun();
while (nodeStates.length > 0) {
await sendMonitoringMailsBatch(name, mailType, nodeStates);
nodeStates = await findBatchFun();
}
const nodeStates = await findBatchFun();
if (_.isEmpty(nodeStates)) {
Logger.tag("monitoring", "mail-sending").debug(
'Done sending "%s" mails.',
name
);
return;
}
}
async function sendMonitoringMailsBatch(
name: string,
mailType: MailType,
nodeStates: NodeStateRow[]
) {
Logger.tag("monitoring", "mail-sending").debug("Sending next batch...");
for (const nodeState of nodeStates) {
await sendMonitoringMail(name, mailType, nodeState);
}
}
async function sendMonitoringMail(
name: string,
mailType: MailType,
nodeState: NodeStateRow
) {
const mac = nodeState.mac;
Logger.tag("monitoring", "mail-sending").debug(
"Loading node data for: %s",
@ -406,13 +419,10 @@ async function sendMonitoringMailsBatched(
const result = await NodeService.findNodeDataWithSecretsByMac(mac);
if (!result) {
Logger.tag("monitoring", "mail-sending").debug(
'Node not found. Skipping sending of "' +
name +
'" mail: ' +
mac
`Node not found. Skipping sending of "${name}" mail: ${mac}`
);
await updateSkippedNode(nodeState.id);
continue;
return;
}
const { node, nodeSecrets } = result;
@ -424,7 +434,7 @@ async function sendMonitoringMailsBatched(
mac
);
await updateSkippedNode(nodeState.id, node);
continue;
return;
}
const monitoringToken = nodeSecrets.monitoringToken;
@ -435,13 +445,24 @@ async function sendMonitoringMailsBatched(
mac
);
await updateSkippedNode(nodeState.id, node);
continue;
return;
}
await enqueMail(name, mailType, node, nodeState, monitoringToken);
await updateNodeForSentEmail(mailType, node, nodeState);
}
async function enqueMail(
name: string,
mailType: MailType,
node: StoredNode,
nodeState: NodeStateRow,
monitoringToken: MonitoringToken
) {
Logger.tag("monitoring", "mail-sending").info(
'Sending "%s" mail for: %s',
name,
mac
node.mac
);
await MailService.enqueue(
@ -454,10 +475,16 @@ async function sendMonitoringMailsBatched(
disableUrl: monitoringDisableUrl(monitoringToken),
}
);
}
async function updateNodeForSentEmail(
mailType: MailType,
node: StoredNode,
nodeState: NodeStateRow
) {
Logger.tag("monitoring", "mail-sending").debug(
"Updating node state: ",
mac
node.mac
);
const timestamp = now();
@ -474,8 +501,6 @@ async function sendMonitoringMailsBatched(
nodeState.id,
]
);
}
}
}
async function sendOnlineAgainMails(