Commit 120c8a3574ea8d8473ca6803937d44adc73ef139
1 parent
2b4a33975c
Exists in
master
debugging cekstatus
Showing 2 changed files with 47 additions and 27 deletions Side-by-side Diff
cekstatus.js
... | ... | @@ -8,15 +8,25 @@ var argv = require('minimist')(process.argv.slice(2), {string: ['requestid', 'de |
8 | 8 | var strftime = require('strftime'); |
9 | 9 | var fs = require('fs'); |
10 | 10 | var ini = require('ini'); |
11 | +var winston = require('winston'); | |
11 | 12 | |
12 | 13 | var config; |
14 | +var logger = new winston.Logger({ | |
15 | + transports: [ | |
16 | + new (winston.transports.Console)() | |
17 | + ] | |
18 | +}); | |
19 | + | |
20 | +function setLogger(_logger) { | |
21 | + logger = _logger; | |
22 | +} | |
13 | 23 | |
14 | 24 | function advice(data, callback) { |
15 | 25 | if (!data.trxid && !data.destination) { |
16 | - console.log('Webreport advice request without data. Canceling'); | |
26 | + logger.warn('Webreport advice request without data. Canceling', {data: data}); | |
17 | 27 | return; |
18 | 28 | } |
19 | - | |
29 | + | |
20 | 30 | if (data.config) { |
21 | 31 | config = data.config; |
22 | 32 | } else { |
... | ... | @@ -24,19 +34,19 @@ function advice(data, callback) { |
24 | 34 | config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); |
25 | 35 | } |
26 | 36 | catch(err) { |
27 | - console.log('Error loading config file'); | |
37 | + logger.warn('Error loading config file'); | |
28 | 38 | return; |
29 | 39 | } |
30 | 40 | } |
31 | - | |
41 | + | |
32 | 42 | login(data, callback); |
33 | 43 | }; |
34 | 44 | |
35 | 45 | function login(data, callback) { |
36 | 46 | var jar = request.jar(); |
37 | - | |
47 | + | |
38 | 48 | var options = { |
39 | - url: 'http://103.11.75.142:9009/dealer/index.php/admin/login', | |
49 | + url: 'http://103.11.75.142:9009/dealer/index.php/admin/login', | |
40 | 50 | jar: jar, |
41 | 51 | followAllRedirects: true, |
42 | 52 | form: { |
... | ... | @@ -45,21 +55,21 @@ function login(data, callback) { |
45 | 55 | Submit: 'Login', |
46 | 56 | }, |
47 | 57 | }; |
48 | - | |
58 | + | |
49 | 59 | //console.log('Requesting', options); |
50 | 60 | request.post(options, function(error, httpResponse, body) { |
51 | 61 | if (error) { |
52 | - console.log('Error retrieving login'); | |
62 | + logger.warn('Error retrieving login', {options: options, error: error, body: body}); | |
53 | 63 | return; |
54 | 64 | } |
55 | - | |
65 | + | |
56 | 66 | if (body.search('Incorrect username') >= 0) { |
57 | - console.log('Salah username / password'); | |
67 | + logger.warn('Salah username / password', {options = options}); | |
58 | 68 | return; |
59 | 69 | } |
60 | 70 | |
61 | 71 | getTrxStatusPage(data, jar, callback); |
62 | - | |
72 | + | |
63 | 73 | }); |
64 | 74 | } |
65 | 75 | |
... | ... | @@ -79,29 +89,29 @@ function getTrxStatusPage(data, jar, callback) { |
79 | 89 | submit: 'Find', |
80 | 90 | }, |
81 | 91 | }; |
82 | - | |
92 | + | |
83 | 93 | if (data.trxid) { |
84 | 94 | options.form.trxid = data.trxid; |
85 | 95 | } |
86 | - | |
96 | + | |
87 | 97 | if (data.destination) { |
88 | 98 | options.form.destmisdn = data.destination; |
89 | 99 | } |
90 | - | |
100 | + | |
91 | 101 | //console.log(options); |
92 | 102 | request.post(options, function(error, httpResponse, body) { |
93 | 103 | if (error) { |
94 | - console.log('Error retrieving trx status page'); | |
104 | + logger.warn('Error retrieving trx status page', {data: data, options: options, err: error}); | |
95 | 105 | return; |
96 | 106 | } |
97 | - | |
107 | + | |
98 | 108 | body = body.replace(/<\/thead>\s<\/tbody>/, "</thead>\n<tbody>"); |
99 | - | |
109 | + | |
100 | 110 | var document = parse5.parse(body); |
101 | 111 | var xhtml = xmlser.serializeToString(document); |
102 | 112 | var doc = new dom().parseFromString(xhtml); |
103 | 113 | var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"}); |
104 | - | |
114 | + | |
105 | 115 | var nodes = select('//x:*[@id="dirlist"]/x:tbody/x:tr/x:td', doc); |
106 | 116 | var status = {}; |
107 | 117 | |
... | ... | @@ -119,39 +129,46 @@ function getTrxStatusPage(data, jar, callback) { |
119 | 129 | } |
120 | 130 | } |
121 | 131 | catch(errStatus) { |
122 | - console.log('Error parsing status'); | |
132 | + logger.warn('Error parsing status', {data: data, status: status}); | |
123 | 133 | if (callback) { |
124 | 134 | callback(); |
125 | 135 | } |
126 | 136 | return; |
127 | 137 | } |
128 | - | |
138 | + | |
129 | 139 | status.response = status.response.replace('<td>', ''); |
130 | 140 | status.response = status.response.replace('</td>', ''); |
131 | 141 | status.response = status.response.replace(/\n/g, ''); |
132 | 142 | status.response = status.response.trim(); |
133 | 143 | //status.response_raw = status.response; |
134 | - | |
144 | + | |
135 | 145 | xml2js(status.response, function (err, result) { |
136 | 146 | if (err) { |
137 | - console.log('Gagal parsing XML', status.response); | |
147 | + logger.warn('Gagal parsing XML', {err: err, status: status, doc: doc}); | |
138 | 148 | if (callback) { |
139 | 149 | status.response = {}; |
140 | 150 | callback(status); |
141 | 151 | } |
142 | 152 | return; |
143 | 153 | } |
144 | - | |
145 | - status.response = result.result; | |
146 | - | |
154 | + | |
155 | + try { | |
156 | + status.response = result.result; | |
157 | + } | |
158 | + catch(e) { | |
159 | + logger.warn('Exception on get status.response', {e: e, result: result}); | |
160 | + } | |
161 | + | |
162 | + | |
163 | + | |
147 | 164 | if (!callback) { |
148 | - console.log(status); | |
165 | + logger.warn('Invalid callback on checkstatus', {status: status}); | |
149 | 166 | } else { |
150 | 167 | callback(status); |
151 | 168 | } |
152 | 169 | }); |
153 | 170 | }); |
154 | - | |
171 | + | |
155 | 172 | } |
156 | 173 | |
157 | 174 | if (require.main === module) { |
... | ... | @@ -171,4 +188,5 @@ if (require.main === module) { |
171 | 188 | advice(data); |
172 | 189 | } |
173 | 190 | |
191 | +exports.setLogger = setLogger; | |
174 | 192 | exports.advice = advice; |