-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
287 lines (223 loc) · 5.5 KB
/
utils.php
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
function je($data)
{
echo json_encode($data,True);
}
/**
* Function checks if the current line follows all the rules
* means: It should have any of the included phrases and it should not have any of the exluded phrases.
* Priority will be given to included phrases.
* If included phrase found, will return true
* If excluded phrase found, WIll return false
* If nothing found, return false.
* @param unknown_type $line
*/
function filter_valid_line($line)
{
global $excluded_phrases, $included_phrases;
foreach ($excluded_phrases as $excluded_phrase)
{
if ( strpos(strtolower($line),strtolower($excluded_phrase)) > 0)
{
//yes...excluded phrase found..Hence returning false
return false;
}
}
foreach ($included_phrases as $included_phrase)
{
if ( strpos(strtolower($line),strtolower($included_phrase)) > 0)
{
//yes...excluded phrase found..Hence skiping the current line
return true;
}
}
//neither inclided not excluded phrase found..hence retuning false
return false;
}
function parse_file($file_name)
{
$myfile = fopen($file_name, "r") or die("Unable to open file!");
while(!feof($myfile))
{
$line = fgets($myfile);
//Checking if the current line contains the excluded phrases. if yes,
// then we will not trace it
if(!filter_valid_line($line))
{
//oops..the current line is not allowed to be in report..
//hence continuing
continue;
}
//echo $line;
$res = get_time_n_query($line);
//Trimming the query to 2000 char
$output_arr[] = $res;
}
fclose($myfile);
$res = sort_file($output_arr);
return $res;
}
function get_time_n_query($line)
{
include 'config.php';
$pos_log = strpos($line, $delimiter_for_query);
$pos_log = $pos_log + strlen($delimiter_for_query);
$trimmed_line = substr($line, $pos_log);
$time = substr($trimmed_line,0, strpos($trimmed_line, ' '));
//first occurence of select
$query = substr($trimmed_line, strpos($trimmed_line, 'SELECT'));
$query = substr($query, 0,$max_query_len);
$sql_token = tokenize_query($query) ;
$where_tokens = count_where_str_token($sql_token['where']);
$assigned_grp = get_group_name($where_tokens);
$hash = create_hash($sql_token['select'].$sql_token['table'].$assigned_grp);
$response = array(
'group' => $assigned_grp,
'time' => $time,
'hash' => $hash,
'query' => $query,
'token' => $sql_token,
'where_tokens' => $where_tokens,
);
return $response;
}
function create_group_wise_file($output)
{
$t = time();
foreach ($output as $key => $val)
{
$fp = fopen($key.'_'.$t.'.csv', 'w');
foreach ( $val as $row)
{
$r = array();
$r['group'] = $row['group'];
$r['hash'] = $row['hash'];
$r['time'] = $row['time'];
$r['query'] = $row['query'];
$output = '';
fputcsv($fp, $r);
}
fclose($fp);
}
}
function sort_file($data)
{
foreach ($data as $key => $row)
{
$time[$key] = $row['time'];
$query[$key] = $row['query'];
}
array_multisort($time, SORT_DESC, $query, SORT_ASC, $data);
return $data;
}
function tokenize_query($query)
{
$select = get_string_between($query, "SELECT", "FROM");
$table = get_string_between($query, "FROM", "WHERE");
$where = get_string_between($query,"WHERE");
return array('select' => $select,'table' => $table , 'where' => $where );
}
function get_string_between($string, $start, $end = '')
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = $end != '' ? strpos($string,$end,$ini) - $ini : -1;
return trim(substr($string,$ini,$len));
}
function download_file($output_arr, $filename = '')
{
include 'config.php';
/*
$output_str = '';
foreach ($output_arr as $key => $val)
{
if($val['time'] === false or $val['query'] === false ) continue;
$output_str .= $val['hash'].$file_del.$val['group'].$file_del.$val['time'].$file_del.'"'.$val['query'].'"'.$new_line;
}
if ($filename == '')
{
$file = 'Data_report';
$filename = $file . "_" . date("Y-m-d_H-i", time());
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=" . $filename . ".csv");
print $output_str;
*/
$fp = fopen($filename.'.csv', 'w');
foreach ($output_arr as $fields)
{
$r = array();
foreach ($output_file_columns as $columns)
{
if (array_key_exists($columns, $fields))
{
$r[$columns] = $fields[$columns];
}
}
fputcsv($fp, $r);
}
fclose($fp);
}
/**
* COunting the no of where operator in where line
* Enter description here ...
* @param unknown_type $where
*/
function count_where_str_token($where)
{
global $sql_where_operator;
$total_count = 0;
$output = array();
foreach ($sql_where_operator as $val)
{
$arr = explode($val, $where);
$len = count($arr) - 1;
$output[$val] = count($arr) - 1;
$total_count += $len;
}
$output['total'] = $total_count;
return $output;
}
function get_group_name($where_tokens)
{
$str = '';
foreach ($where_tokens as $key => $val)
{
$str .= $key.'_'.$val.'_';
}
return substr($str,0,-1);
}
function create_hash($str)
{
return md5($str);
}
function get_hash_count($output)
{
$hash_arr = array();
foreach ($output as $key => $val)
{
$hash = $val['hash'];
$hash_arr[$hash] = $hash_arr[$hash] +1 ;
}
return $hash_arr;
}
function get_column_group_by($data,$column)
{
if($column == '')
{
return 'Invalid Column';
}
$new_arr = array();
foreach ($data as $val)
{
if ($val[$column] == '')
{
continue;
}
$new_arr[$val[$column]][] = $val;
}
return $new_arr;
}