All files / app/web-ui/scripts log-script.ts

100% Statements 163/163
100% Branches 1/1
100% Functions 1/1
100% Lines 163/163

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 1631x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x
export const logScript = (basePath: string) => `
// The currently selected log filter
let currentLogFilter = 'none';
// Stores the reference to the timeout for the log refresh loop
let currentLogLoop;
// Stores the paused state
let isPaused = true
 
// Listener for the pause button - will toggle the pause button UI and stop or continue the log refresh loop
function togglePause() {
    isPaused = !isPaused;
    const pauseBtn = document.getElementById('pauseBtn');
    const pauseIcon = document.getElementById('pauseIcon');
    const pauseText = document.getElementById('pauseText');
    
    if (isPaused) {
        pauseBtn.classList.add('paused');
        pauseIcon.textContent = '▶';
        pauseText.textContent = 'Resume';
 
        document.querySelectorAll('.filter-btn').forEach(btn => {
            btn.setAttribute('disabled', 'disabled');;
        });
 
        // Stop auto-refresh
        clearTimeout(currentLogLoop);
    } else {
        pauseBtn.classList.remove('paused');
        pauseIcon.textContent = '⏸';
        pauseText.textContent = 'Pause';
 
        document.querySelectorAll('.filter-btn').forEach(btn => {
            btn.removeAttribute('disabled');;
        });
 
        // Resume auto-refresh
        setLogLoading()
        refreshLog();
    }
}
 
// Listener for the Toggle Log button
function toggleLog() {
    const viewer = document.getElementById('logViewer');
    viewer.classList.toggle('collapsed');
    let visible = viewer.classList.toggle('expanded');
    if(visible) {
        document.getElementById('logContent').style.display = "block"
        currentLogFilter = 'info'
        isPaused = false
        setLogLoading()
        refreshLog()
    } else {
        document.getElementById('logContent').style.display = "none"
        currentLogFilter = 'none'
        isPaused = true
        clearTimeout(currentLogLoop)
    }
}
 
// This function refreshes the log by fetching it from the server and updating the DOM
async function refreshLog() {
    const logs = await fetchLog()
    setLog(logs)
    currentLogLoop = setTimeout(() => refreshLog(), 500);
}
 
// This function returns the fetched log from the server
async function fetchLog() {
    try{
        const fetchedState = await fetch("${basePath}/api/log?loglevel=" + currentLogFilter, { 
            headers: {
                "Accept": "application/json"
            }
        })
 
        if(!fetchedState.ok) {
            throw new Error('Response not ok!')
        }
        
        return fetchedState.json();
    } catch (err) {
        return [{
            level: 'error',
            time: Date.now(),
            source: "",
            message: "Unable to fetch logs: " + err.message
        }];
    }
}
 
// Listener for the filter buttons
function selectFilter(level) {
    
    // Remove active class from all buttons
    document.querySelectorAll('.filter-btn').forEach(btn => {
        btn.classList.remove('active');
    });
    
    // Add active class to clicked button
    event.target.classList.add('active');
    
    // Update current filter and reload logs from server
    currentLogFilter = level;
    setLogLoading()
}
 
// Function removes all logs from screen and puts new log lines onto the screen
function setLog(log) {
    if(!log) {
        return
    }
    if(log.length === 0) {
        document.getElementById('logContent').innerHTML = '<div style="color: #888; text-align: center; padding: 20px;">No logs to display</div>';
        return
    }
    if(log.length === document.getElementById('logContent').childElementCount) {
        //console.log('Not refreshing view because log count is equal')
        return
    }
    document.getElementById('logContent').innerHTML = '';
    for (const logLine of log) {
        addLogLine(logLine.level, logLine.source, logLine.message, logLine.time)
    }
}
 
// Adds a single log line to the screen
function addLogLine(level, source, message, time) {
    const logContent = document.getElementById('logContent');
    
    const logEntry = document.createElement('div');
    logEntry.className = 'log-entry';
    logEntry.dataset.level = level.toLowerCase();
    
    const logEntryTime = document.createElement('span');
    logEntryTime.className = 'log-timestamp';
    logEntryTime.innerHTML = formatDate(time);
    logEntry.appendChild(logEntryTime);
 
    const logLevel = document.createElement('span');
    logLevel.className = 'log-level ' + level;
    logLevel.innerHTML = level.toUpperCase();
    logEntry.appendChild(logLevel);
 
    const logSource = document.createElement('span');
    logSource.className = 'log-source';
    logSource.innerHTML = source
    logEntry.appendChild(logSource)
 
    const logMessage = document.createElement('span');
    logMessage.className = 'log-message';
    logMessage.innerHTML = message
    logEntry.appendChild(logMessage)
    
    logContent.appendChild(logEntry);
    logContent.scrollTop = logContent.scrollHeight;
}
 
// Replaces the log content with a loading indicator
function setLogLoading() {
    document.getElementById('logContent').innerHTML = '<div style="color: #888; text-align: center; padding: 20px;">Loading logs...</div>';
}
`