Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

8.2 KB
Raw Blame
/**
 * Shared helpers for the log panel component.
 *
 * Extracted from components/logpanel.js so they can be unit-tested
 * (the logpanel.js IIFE is otherwise unreachable from tests).
 *
 * Exposes window.LdrLogHelpers. Consumers should destructure at the top
 * of their IIFE, e.g.
 *     const { checkLogVisibility, hashString } = window.LdrLogHelpers;
 */
(function() {
    'use strict';

    /**
     * Fresh per-category counts object. Single source of truth for the
     * shape of window._logPanelState.counts — logpanel.js state init,
     * the batch-load reset, and the unit-test beforeEach all use this
     * helper, so adding a new category is a one-line change here.
     * @returns {{info: number, milestone: number, warning: number, error: number}}
     */
    function emptyCounts() {
        return { info: 0, milestone: 0, warning: 0, error: 0 };
    }

    /**
     * Adjust one tracked per-category count in place. Categories the
     * counts object does not track are ignored (the hasOwnProperty guard
     * also keeps hostile keys like "__proto__" inert), and the result is
     * floored at zero so a decrement can never drive a bucket negative.
     * @param {{info: number, milestone: number, warning: number, error: number}} counts
     *     - Counts object from emptyCounts(), mutated in place.
     * @param {string} key - Display category to adjust.
     * @param {number} delta - Signed adjustment, e.g. +1 or -1.
     */
    function bumpCount(counts, key, delta) {
        if (Object.prototype.hasOwnProperty.call(counts, key)) {
            counts[key] = Math.max(0, counts[key] + delta);
        }
    }

    /**
     * Map a raw log severity to the existing UI category that should count
     * and filter it. The raw severity remains on the rendered row so its
     * dataset, CSS class, and badge label are preserved.
     *
     * @param {string} logType - Raw log severity.
     * @returns {string} Display category or custom type, normalized to lowercase.
     */
    function getDisplayLogCategory(logType) {
        const normalizedType = typeof logType === 'string'
            ? logType.toLowerCase()
            : '';
        switch (normalizedType) {
            case 'critical':
            case 'fatal':
                return 'error';
            case 'success':
                return 'milestone';
            default:
                return normalizedType;
        }
    }

    /**
     * Decide whether a log entry of the given type should be visible
     * under a filter selection. Matches the plural/singular variants the
     * UI may emit (e.g. "milestones" vs "milestone").
     * @param {string} logType - Raw rendered log severity.
     * @param {string} filterType - selected filter ('all', 'info', 'milestone', ...)
     * @returns {boolean}
     */
    function checkLogVisibility(logType, filterType) {
        const displayCategory = getDisplayLogCategory(logType);
        switch (filterType) {
            case 'all':
                return true;
            case 'info':
                // 'info' must show ONLY info entries. The old implementation
                // returned true for every type, which made the 'Info'
                // filter button a no-op (clicking it did nothing visible).
                return displayCategory === 'info';
            case 'milestone':
            case 'milestones': // Handle plural form too
                return displayCategory === 'milestone';
            case 'warning':
            case 'warnings':
                return displayCategory === 'warning' ||
                    displayCategory === 'error';
            case 'error':
            case 'errors': // Handle plural form too
                return displayCategory === 'error';
            default:
                return true; // Default to showing everything
        }
    }

    /**
     * Deterministic 32-bit string hash, used to build stable IDs for
     * deduping log entries.
     * @param {string} str
     * @returns {string} The hash as a decimal string.
     */
    function hashString(str) {
        if (!str) return '0';
        let hash = 0;
        for (let i = 0; i < str.length; i++) {
            const char = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
            hash &= hash; // Convert to 32bit integer
        }
        return hash.toString();
    }

    /**
     * Trim and lowercase a log message for deduplication comparison.
     * Null/undefined become the empty string.
     * @param {string} message
     * @returns {string}
     */
    function normalizeMessage(message) {
        if (!message) return '';
        return message.trim().toLowerCase();
    }

    /**
     * Normalize timestamps across a batch of logs so stray entries from
     * a different calendar day (clock skew, retries) are re-anchored to
     * the date most logs already share. Mutates `logs` in place.
     *
     * Uses a frequency count: the most common YYYY-MM-DD wins; logs on
     * other dates have their date re-stamped while preserving the time.
     * @param {Array<{time: string, message?: string, id?: string}>} logs
     */
    function normalizeTimestamps(logs) {
        // Find the most common date in the logs (ignoring the time)
        const dateFrequency = new Map();

        logs.forEach(log => {
            try {
                const date = new Date(log.time);
                // Extract just the date part (YYYY-MM-DD)
                const dateStr = date.toISOString().split('T')[0];
                dateFrequency.set(dateStr, (dateFrequency.get(dateStr) || 0) + 1);
            } catch {
                SafeLogger.error('Error parsing date:', log.time);
            }
        });

        // Find the most frequent date
        let mostCommonDate = null;
        let highestFrequency = 0;

        dateFrequency.forEach((count, date) => {
            if (count > highestFrequency) {
                highestFrequency = count;
                mostCommonDate = date;
            }
        });

        SafeLogger.log(`Most common date: ${mostCommonDate} with ${highestFrequency} occurrences`);

        if (!mostCommonDate) return; // Can't normalize without a common date

        // Normalize all logs to the most common date
        logs.forEach(log => {
            try {
                const date = new Date(log.time);
                const dateStr = date.toISOString().split('T')[0];

                // If this log is from a different date, adjust it to the most common date
                // while preserving the time portion
                if (dateStr !== mostCommonDate) {
                    const [year, month, day] = mostCommonDate.split('-');
                    date.setFullYear(parseInt(year, 10));
                    date.setMonth(parseInt(month, 10) - 1); // Months are 0-indexed
                    date.setDate(parseInt(day, 10));

                    // Update the log time
                    log.time = date.toISOString();
                    // Only derive a fallback id when the entry does not
                    // already carry one. The standard-array loader preserves
                    // the server-provided database id for distinct persisted
                    // rows; re-deriving from `time + hash` here would collide
                    // two rows that share (timestamp, message) but differ
                    // only by id, and the downstream `uniqueLogsMap` would
                    // then collapse one of them before render. Mirror the
                    // parser's "absent" check (null / undefined / '') so the
                    // two layers stay in agreement on what counts as a real
                    // id.
                    if (log.id == null || log.id === '') {
                        log.id = `${log.time}-${hashString(log.message)}`;
                    }
                    SafeLogger.log(`Normalized timestamp for "${log.message.substring(0, 30)}..." from ${dateStr} to ${mostCommonDate}`);
                }
            } catch {
                SafeLogger.error('Error normalizing date:', log.time);
            }
        });
    }

    window.LdrLogHelpers = {
        bumpCount,
        checkLogVisibility,
        emptyCounts,
        getDisplayLogCategory,
        hashString,
        normalizeMessage,
        normalizeTimestamps,
    };
})();