1// Copyright 2023 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15import { LogSource } from '../log-source'; 16import { LogEntry, Level } from '../shared/interfaces'; 17import { timeFormat } from '../shared/time-format'; 18 19export class MockLogSource extends LogSource { 20 private intervalId: NodeJS.Timeout | null = null; 21 22 constructor(sourceName = 'Mock Log Source') { 23 super(sourceName); 24 } 25 26 start(): void { 27 const getRandomInterval = () => { 28 return Math.floor(Math.random() * (200 - 50 + 1) + 50); 29 }; 30 31 const readLogEntry = () => { 32 const logEntry = this.readLogEntryFromHost(); 33 this.publishLogEntry(logEntry); 34 35 const nextInterval = getRandomInterval(); 36 setTimeout(readLogEntry, nextInterval); 37 }; 38 39 readLogEntry(); 40 } 41 42 stop(): void { 43 if (this.intervalId) { 44 clearInterval(this.intervalId); 45 this.intervalId = null; 46 } 47 } 48 49 getLevel(): Level { 50 interface ValueWeightPair { 51 level: Level; 52 weight: number; 53 } 54 55 const valueWeightPairs: ValueWeightPair[] = [ 56 { level: Level.INFO, weight: 7.45 }, 57 { level: Level.DEBUG, weight: 0.25 }, 58 { level: Level.WARNING, weight: 1.5 }, 59 { level: Level.ERROR, weight: 0.5 }, 60 { level: Level.CRITICAL, weight: 0.05 }, 61 ]; 62 63 const totalWeight = valueWeightPairs.reduce( 64 (acc, pair) => acc + pair.weight, 65 0, 66 ); 67 let randomValue = Level.INFO; 68 let randomNum = Math.random() * totalWeight; 69 70 for (let i = 0; i < valueWeightPairs.length; i++) { 71 randomNum -= valueWeightPairs[i].weight; 72 if (randomNum <= 0) { 73 randomValue = valueWeightPairs[i].level; 74 break; 75 } 76 } 77 78 return randomValue; 79 } 80 81 readLogEntryFromHost(): LogEntry { 82 // Emulate reading log data from a host device 83 const sources = ['application', 'server', 'database', 'network']; 84 const messages = [ 85 'Request processed successfully!', 86 'An unexpected error occurred while performing the operation.', 87 'Connection timed out. Please check your network settings.', 88 'Invalid input detected. Please provide valid data.', 89 'Database connection lost. Attempting to reconnect.', 90 'User authentication failed. Invalid credentials provided.', 91 'System reboot initiated. Please wait for the system to come back online.', 92 'File not found. (The requested file does not exist).', 93 'Data corruption detected. Initiating recovery process.', 94 'Network congestion detected. Traffic is high, please try again later.', 95 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam condimentum auctor justo, sit amet condimentum nibh facilisis non. Quisque in quam a urna dignissim cursus. Suspendisse egestas nisl sed massa dictum dictum. In tincidunt arcu nec odio eleifend, vel pharetra justo iaculis. Vivamus quis tellus ac velit vehicula consequat. Nam eu felis sed risus hendrerit faucibus ac id lacus. Vestibulum tincidunt tellus in ex feugiat interdum. Nulla sit amet luctus neque. Mauris et aliquet nunc, vel finibus massa. Curabitur laoreet eleifend nibh eget luctus. Fusce sodales augue nec purus faucibus, vel tristique enim vehicula. Aenean eu magna eros. Fusce accumsan dignissim dui auctor scelerisque. Proin ultricies nunc vel tincidunt facilisis.', 96 ]; 97 const level = this.getLevel(); 98 const timestamp: Date = new Date(); 99 const getRandomValue = (values: string[]) => { 100 const randomIndex = Math.floor(Math.random() * values.length); 101 return values[randomIndex]; 102 }; 103 104 const logEntry: LogEntry = { 105 level: level, 106 timestamp: timestamp, 107 fields: [ 108 { key: 'level', value: level }, 109 { key: 'time', value: timeFormat.format(timestamp) }, 110 { key: 'source', value: getRandomValue(sources) }, 111 { key: 'message', value: getRandomValue(messages) }, 112 ], 113 }; 114 115 return logEntry; 116 } 117} 118