1
+ const { Pool, Client } = require ( 'pg' )
2
+ const pool = new Pool ( {
3
+ user : 'postgres' ,
4
+ password : '8ed89611-dff5-444f-867c-c66098a349cd' ,
5
+ host : '172.17.0.1' ,
6
+ database : 'ws_trail' ,
7
+ port : 5432 ,
8
+ max : 4
9
+ } )
10
+
11
+ module . exports . SaveEvent = async function ( data , sourceIP , country ) {
12
+
13
+ var userID = data . uid ;
14
+ var context = data . ctx ;
15
+ var eventType = data . type ;
16
+ var eventTitle = data . title ;
17
+ var eventValue = data . val ;
18
+ var eventData = data . data ;
19
+ var wlid = data . wlid ;
20
+
21
+ if ( country != null && country != "" ) {
22
+ if ( eventData == null ) {
23
+ eventData = { } ;
24
+ }
25
+ eventData . country = country ;
26
+ }
27
+
28
+ var SQL = `INSERT INTO Logs (time, userID, context, eventType, eventTitle, eventValue, eventData, sourceIP, wlid)
29
+ VALUES (NOW(), '${ Clean ( userID ) } ', '${ Clean ( context ) } ', '${ Clean ( eventType ) } ', '${ Clean ( eventTitle ) } ',
30
+ '${ Clean ( eventValue ) } ', '${ Clean ( JSON . stringify ( eventData ) ) } ', '${ sourceIP } ', '${ Clean ( wlid ) } ' ) RETURNING time;`
31
+
32
+ //console.log(SQL);
33
+
34
+ var rez = null ;
35
+ try {
36
+ rez = await pool . query ( SQL ) ;
37
+ }
38
+ catch ( ex ) {
39
+ console . log ( "Crash in SQL Query for SaveEvent: " + SQL ) ;
40
+ var obj = { count : 0 , command : "INVALID_QUERY" , time : 0 } ;
41
+ return obj
42
+ }
43
+ //console.log(rez);
44
+ var obj = { count : rez . rowCount , command : rez . command , time : rez . rows [ 0 ] . time } ;
45
+ return obj
46
+ }
47
+
48
+ module . exports . Query = async function ( SQL , sourceIP ) {
49
+ var rez = await pool . query ( SQL ) ;
50
+ //console.log(rez);
51
+ var obj = { count : rez . rowCount , rows : rez . rows } ;
52
+ return obj
53
+ }
54
+
55
+ module . exports . StartOrUpdateSession = async function ( data , sourceIP , userAgent ) {
56
+
57
+ var userID = data . uid ;
58
+
59
+ var context = data . ctx ;
60
+ if ( context == null ) {
61
+ context = "" ;
62
+ }
63
+
64
+ var wlid = data . wlid ;
65
+
66
+ if ( wlid == null || wlid == "" ) {
67
+ wlid = - 1 ;
68
+ }
69
+ else {
70
+ try {
71
+ wlid = parseInt ( wlid ) ;
72
+ } catch ( ex ) { }
73
+ }
74
+
75
+
76
+ var SQLSearch = `SELECT * FROM sessions WHERE lastupdate > ( now()::timestamp - INTERVAL '1 min' )::timestamp ORDER BY id desc LIMIT 1` ;
77
+ var rezSearch = await pool . query ( SQLSearch ) ;
78
+
79
+ if ( rezSearch . rowCount == 0 ) {
80
+ var SQL = `INSERT INTO sessions (start_time, is_active, wlid, userid, context, total_time, active_time, idle_time, user_agent, sourceip, lastupdate)
81
+ VALUES (NOW(), '1', '${ Clean ( wlid ) } ', '${ Clean ( userID ) } ', '${ Clean ( context ) } ', 0, 0, 0, '${ Clean ( userAgent ) } ', '${ sourceIP } ', NOW() ) RETURNING id;`
82
+ var rez = await pool . query ( SQL ) ;
83
+ //console.log(rez);
84
+ var obj = { count : rez . rowCount , command : rez . command , sessionID : rez . rows [ 0 ] . id } ;
85
+ return obj
86
+ }
87
+ else {
88
+ var timeDiffInMinutes = `extract(epoch from (NOW()::timestamp - start_time::timestamp))::integer` ;
89
+ var SQL = `UPDATE sessions SET is_active = '1', total_time = ${ timeDiffInMinutes } , lastupdate = NOW() WHERE id = '${ rezSearch . rows [ 0 ] . id } ';`
90
+ var rez = await pool . query ( SQL ) ;
91
+ //console.log(rez);
92
+ var obj = { count : rez . rowCount , command : rez . command , sessionID : rezSearch . rows [ 0 ] . id } ;
93
+ return obj
94
+ }
95
+
96
+ }
97
+
98
+ module . exports . UpdateSession = async function ( sessionID , active_time , is_active ) {
99
+
100
+ var timeDiffInMinutes = `extract(epoch from (NOW() - start_time::timestamp))::integer` ;
101
+
102
+ var SQL = `UPDATE sessions SET is_active = '${ Clean ( is_active ) } ', total_time = ${ timeDiffInMinutes } , active_time = active_time + ${ active_time } , idle_time = (${ timeDiffInMinutes } ) - (active_time + ${ active_time } ), lastupdate = NOW() WHERE id = '${ sessionID } ';`
103
+ //console.log(SQL);
104
+
105
+ var rez = await pool . query ( SQL ) ;
106
+
107
+ var obj = { count : rez . rowCount , command : rez . command , sessionID : sessionID } ;
108
+ return obj
109
+
110
+ }
111
+
112
+ module . exports . CloseSession = async function ( sessionID ) {
113
+
114
+ /*
115
+ var timeDiffInMinutes = `(DATE_PART('day', end_time::timestamp - start_time::timestamp) * 24 +
116
+ DATE_PART('hour', end_time::timestamp - start_time::timestamp)) * 60 +
117
+ DATE_PART('minute', end_time::timestamp - start_time::timestamp)`;
118
+ */
119
+
120
+ var timeDiffInMinutes = `extract(epoch from (NOW()::timestamp - start_time::timestamp))::integer` ;
121
+
122
+ var SQL = `UPDATE sessions SET end_time = NOW(), is_active = '0', total_time = ${ timeDiffInMinutes } , idle_time = (${ timeDiffInMinutes } ) - (active_time), lastupdate = NOW() WHERE id = '${ sessionID } ';`
123
+ var rez = await pool . query ( SQL ) ;
124
+ //console.log(rez);
125
+ var obj = { count : rez . rowCount , command : rez . command , sessionID : sessionID } ;
126
+ return obj
127
+
128
+ }
129
+
130
+ function Clean ( txt ) {
131
+ if ( txt == null ) { return "" ; }
132
+ else { return ( txt + "" ) . replace ( / \' / g, "''" ) ; }
133
+ }
0 commit comments