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, hasOwnProperty,
isLayerConfig, isLayerConfig,
isPlainObject, isPlainObject,
isString,
Url, Url,
Version, Version,
} from "./types"; } from "./types";

View file

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