Fix: Use seconds for unix timestamps to not compare seconds and milliseconds.

This commit is contained in:
baldo 2021-08-09 21:54:13 +02:00
parent d06c732cfe
commit 3a253ba72e
3 changed files with 9 additions and 8 deletions

View file

@ -16,7 +16,7 @@ import {normalizeMac} from "../utils/strings";
import {monitoringDisableUrl} from "../utils/urlBuilder"; import {monitoringDisableUrl} from "../utils/urlBuilder";
import CONSTRAINTS from "../validation/constraints"; import CONSTRAINTS from "../validation/constraints";
import {forConstraint} from "../validation/validator"; import {forConstraint} from "../validation/validator";
import {MAC, MailType, Node, NodeId, NodeState, NodeStateData, UnixTimestamp} from "../types"; import {MAC, MailType, Node, NodeId, NodeState, NodeStateData, UnixTimestampSeconds} from "../types";
const MONITORING_STATE_MACS_CHUNK_SIZE = 100; const MONITORING_STATE_MACS_CHUNK_SIZE = 100;
const NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE = 20; const NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE = 20;
@ -688,7 +688,7 @@ export async function deleteOfflineNodes(): Promise<void> {
await deleteNodesOfflineSinceBefore(deleteBefore); await deleteNodesOfflineSinceBefore(deleteBefore);
} }
async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestamp): Promise<void> { async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds): Promise<void> {
Logger Logger
.tag('nodes', 'delete-never-online') .tag('nodes', 'delete-never-online')
.info( .info(
@ -765,7 +765,7 @@ async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestamp): Promis
} }
} }
async function deleteNodesOfflineSinceBefore(deleteBefore: UnixTimestamp): Promise<void> { async function deleteNodesOfflineSinceBefore(deleteBefore: UnixTimestampSeconds): Promise<void> {
const rows = await db.all( const rows = await db.all(
'SELECT * FROM node_state WHERE state = ? AND last_seen < ?', 'SELECT * FROM node_state WHERE state = ? AND last_seen < ?',
[ [

View file

@ -10,7 +10,7 @@ import Logger from "../logger";
import * as MailService from "../services/mailService"; import * as MailService from "../services/mailService";
import {normalizeString} from "../utils/strings"; import {normalizeString} from "../utils/strings";
import {monitoringConfirmUrl, monitoringDisableUrl} from "../utils/urlBuilder"; import {monitoringConfirmUrl, monitoringDisableUrl} from "../utils/urlBuilder";
import {FastdKey, MonitoringState, MonitoringToken, Node, NodeSecrets, NodeStatistics, UnixTimestamp, Token} from "../types"; import {FastdKey, MonitoringState, MonitoringToken, Node, NodeSecrets, NodeStatistics, UnixTimestampSeconds, Token} from "../types";
import util from "util"; import util from "util";
const pglob = util.promisify(glob); const pglob = util.promisify(glob);
@ -235,7 +235,7 @@ async function deleteNodeFile(token: Token): Promise<void> {
async function parseNodeFile(file: string): Promise<{node: Node, nodeSecrets: NodeSecrets}> { async function parseNodeFile(file: string): Promise<{node: Node, nodeSecrets: NodeSecrets}> {
const contents = await fs.readFile(file); const contents = await fs.readFile(file);
const stats = await fs.lstat(file); const stats = await fs.lstat(file);
const modifiedAt = stats.mtimeMs; const modifiedAt = Math.floor(stats.mtimeMs / 1000);
const lines = contents.toString(); const lines = contents.toString();
@ -491,7 +491,7 @@ export async function fixNodeFilenames(): Promise<void> {
} }
} }
export async function findNodesModifiedBefore(timestamp: UnixTimestamp): Promise<Node[]> { export async function findNodesModifiedBefore(timestamp: UnixTimestampSeconds): Promise<Node[]> {
const nodes = await getAllNodes(); const nodes = await getAllNodes();
return _.filter(nodes, node => node.modifiedAt < timestamp); return _.filter(nodes, node => node.modifiedAt < timestamp);
} }

View file

@ -6,7 +6,8 @@ export type Token = string;
export type FastdKey = string; export type FastdKey = string;
export type MAC = string; export type MAC = string;
export type UnixTimestamp = number; export type UnixTimestampSeconds = number;
export type UnixTimestampMilliseconds = number;
export type MonitoringToken = string; export type MonitoringToken = string;
export enum MonitoringState { export enum MonitoringState {
@ -39,7 +40,7 @@ export type Node = {
monitoring: boolean; monitoring: boolean;
monitoringConfirmed: boolean; monitoringConfirmed: boolean;
monitoringState: MonitoringState; monitoringState: MonitoringState;
modifiedAt: UnixTimestamp; modifiedAt: UnixTimestampSeconds;
}; };
// TODO: Complete interface / class declaration. // TODO: Complete interface / class declaration.