1+ import moment from 'moment' ;
12import { Api } from "./stores/Main" ;
23
34const logsEndpoint = '/instance/logs' ;
45
6+ const LOGS_TIME_LIMIT = 20
7+ const LOGS_LINE_LIMIT = 1000
8+
59export const establishConnection = async ( api : Api ) => {
610 const logElement = document . getElementById ( "logs-container" ) ;
711
@@ -10,13 +14,33 @@ export const establishConnection = async (api: Api) => {
1014 return ;
1115 }
1216
13- const appendLogElement = ( logEntry : string ) => {
14- const tag = document . createElement ( "p" ) ;
15-
16- tag . appendChild ( document . createTextNode ( logEntry ) ) ;
17- logElement . appendChild ( tag ) ;
18- logElement . scrollIntoView ( false ) ;
19- } ;
17+ const appendLogElement = ( logEntry : string , logType ?: string ) => {
18+ const tag = document . createElement ( 'p' )
19+ tag . appendChild ( document . createTextNode ( logEntry ) )
20+ logElement . appendChild ( tag )
21+ logElement . scrollIntoView ( false )
22+
23+ if ( logType === 'message' ) {
24+ const logEntryTime = moment . utc (
25+ logElement . children [ 0 ] . innerHTML . split ( ' ' ) . slice ( 0 , 2 ) . join ( ' ' ) ,
26+ )
27+
28+ const timeDifference =
29+ moment ( logEntryTime ) . isValid ( ) &&
30+ moment . duration ( moment . utc ( Date . now ( ) ) . diff ( logEntryTime ) ) . asMinutes ( )
31+
32+ if (
33+ logElement . childElementCount > LOGS_LINE_LIMIT &&
34+ timeDifference > LOGS_TIME_LIMIT
35+ ) {
36+ logElement . removeChild ( logElement . children [ 0 ] )
37+ }
38+ }
39+
40+ if ( logEntry . split ( ' ' ) [ 3 ] === '[ERROR]' ) {
41+ tag . classList . add ( 'error-log' )
42+ }
43+ }
2044
2145 const { response, error } = await api . getWSToken ( {
2246 instanceId : "" ,
@@ -53,8 +77,7 @@ export const establishConnection = async (api: Api) => {
5377 } ;
5478
5579 socket . onmessage = function ( event ) {
56- const logEntry = decodeURIComponent ( atob ( event . data ) ) ;
57-
58- appendLogElement ( logEntry )
59- } ;
80+ const logEntry = decodeURIComponent ( atob ( event . data ) )
81+ appendLogElement ( logEntry , "message" )
82+ }
6083} ;
0 commit comments