-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cpp
257 lines (215 loc) · 8.91 KB
/
main.cpp
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
#include "main.hpp"
int min(int a, int b){
return a < b ? a : b;
}
int example = 0;
QString escape(QString arg){
arg = arg.replace(">", ">");
arg = arg.replace(">", "<");
return arg;
}
QString print_queries(QList<Query*> queries, int top){
QString output_string;
QTextStream output(&output_string, QIODevice::WriteOnly);
output <<"<table class=\"queryList\">"
"<tr>"
"<th>Total duration</th>"
"<th>Executions</th>"
"<th>Average duration</th>"
"<th>User</th>"
"<th>Database</th>"
"<th>Query</th>"
"</tr>";
for(int i=0; i<min(queries.size(), top); i++){
Query *q = queries.at(i);
output << QString("<tr class=\"row%1\">"
"<td class=\"relevantInformation top center\">%3 ms</td>"
"<td class=\"top center\">%4</td>"
"<td class=\"top center\">%5 ms</td>"
"<td class=\"top center\">%6</td>"
"<td class=\"top center\">%7</td>"
"<td><pre onclick=\"highlight(this);\">%8</pre></td></tr>"
"<tr><td colspan=\"6\">"
"<input type=\"button\" class=\"examplesButton\" value=\"Show examples\" "
"onclick=\"javascript:toggleExamples(this, %9);\" />"
"<div id=\"example_%9\" \"class=\"examples c1\" style=\"display: none;\">") \
.arg(i%2) \
.arg(q->getTotalDuration() / 1000) \
.arg(q->getExecutions()) \
.arg(q->getAverageDuration() / 1000) \
.arg(q->getUser()) \
.arg(q->getDatabase()) \
.arg(escape(q->getStatement())) \
.arg(example);
QStringList examples = q->getExamples();
QList<uint> durations = q->getDurations();
for(int j=0; j<examples.count(); j++){
output << QString("<div class=\"example%1 sql\">%2ms | <pre onclick=\"highlight(this);\">%3</pre></div>") \
.arg(j % 2) \
.arg(durations.at(j) / 1000) \
.arg(escape(examples.at(j)));
}
output << "</div></td></tr>";
example++;
}
output << "</table>";
return output_string;
}
int main(int argc, char **argv){
QTextStream qout(stdout, QIODevice::WriteOnly);
QTextStream qerr(stderr, QIODevice::WriteOnly);
QTextStream qin(stdin, QIODevice::ReadOnly);
Args args;
args.add(new Arg("h", "help", Arg::setTrue, QVariant(false)));
args.add(new Arg("v", "verbose", Arg::setTrue, QVariant(false)));
//args.add(new Arg("i", "input-file", Arg::readableFile, QVariant("/var/log/postgresql.log")));
args.add(new Arg("i", "input-file", Arg::readableFile,
QVariant("/var/log/postgresql/postgresql-9.1-main.log")));
args.add(new Arg("o", "output-file", Arg::writableFile,
QVariant("report.html")));
args.add(new Arg("u", "users", Arg::toString, QVariant()));
args.add(new Arg("d", "databases", Arg::toString, QVariant()));
args.add(new Arg("top", Arg::toInt, QVariant(20)));
args.add(new Arg("t", "query-types", Arg::toString,
QVariant("SELECT,UPDATE,INSERT,DELETE")));
if(!args.parse(argc, argv)){
args.help();
return -1;
}
QStringList users = args.getStringList("users");
QStringList databases = args.getStringList("databases");
QStringList query_types = args.getStringList("query-types");
int ret;
QString line;
int start_index;
int stop_index;
int old_query_id;
int new_query_id;
int old_line_id;
int new_line_id;
QString database;
QString user;
QString statement;
uint duration;
QFile input_file;
ret = args.getFile(&input_file, "input-file", QIODevice::ReadOnly | QIODevice::Text);
if(ret)return ret;
QFile output_file;
ret = args.getFile(&output_file, "output-file", QIODevice::WriteOnly | QIODevice::Text);
if(ret)return ret;
QTextStream output(&output_file);
/* display the top N queries */
int top = args.getInt("top");
Queries queries;
old_query_id = -1;
old_line_id = -0;
statement = "";
duration = 0;
QTime timer;
timer.start();
uint lines = 0;
if(input_file.atEnd()){
qerr << "The input file (" << input_file.fileName();
qerr << ") seems to be empty" << endl;
}
while (!input_file.atEnd()) {
line = input_file.readLine(4096);
lines++;
if(lines % 1000 == 0){
qout << "Read " << lines << " lines." << endl;
qout.flush();
}
if(line[0] == '\t'){
statement.append(line);
continue;
}
start_index = line.indexOf("[", 15);
start_index = line.indexOf("[", start_index + 3) + 1;
stop_index = line.indexOf("-", start_index);
new_query_id = line.mid(start_index, stop_index - start_index).toInt();
start_index = stop_index + 1;
stop_index = line.indexOf("]", start_index);
new_line_id = line.mid(start_index, stop_index - start_index).toInt();
if(new_query_id != old_query_id || old_line_id < new_line_id){
old_query_id = new_query_id;
QString hashStatement = Query::normalize(statement);
QString upperStatement = hashStatement.toUpper();
statement = Query::format(statement);
if((
upperStatement.startsWith("INSERT") ||
upperStatement.startsWith("DELETE") ||
upperStatement.startsWith("UPDATE") ||
upperStatement.startsWith("SELECT")
)
&& (!users.length() || users.contains(user))
&& (!databases.length() || databases.contains(database))){
uint hash = qHash(hashStatement);
if(queries.contains(hash)){
queries[hash]->addStatement(duration, statement);
}else{
queries.insert(hash, new Query(hashStatement, statement, user, database, duration));
}
}
user = "";
database = "";
duration = 0;
statement = "";
start_index = line.indexOf("user=", stop_index) + 5;
stop_index = line.indexOf(",", start_index);
user = line.mid(start_index, stop_index - start_index);
start_index = line.indexOf("db=", stop_index) + 3;
stop_index = line.indexOf(" ", start_index);
if(start_index == -1 || stop_index == -1)continue;
database = line.mid(start_index, stop_index - start_index);
start_index = line.indexOf("duration: ", stop_index) + 10;
stop_index = line.indexOf(" ", start_index);
duration = line.mid(start_index, stop_index - start_index).toDouble() * 1000;
start_index = line.indexOf("statement: ", stop_index) + 11;
if(start_index < stop_index)continue;
stop_index = line.length();
statement = line.mid(start_index, stop_index - start_index);
}else{
start_index = line.indexOf("] ", stop_index);
if(start_index != -1){
start_index += 2;
stop_index = line.length() - start_index;
statement.append(line.mid(start_index, line.length() - start_index));
}
}
}
QFile header(":templates/header.html");
if (!header.open(QIODevice::ReadOnly | QIODevice::Text)){
qout << "Unable to open templates/header.html" << endl;
return -4;
}else{
output << header.readAll();
header.close();
}
QList<Query*> queries_sorted;
output << QString("<div class=\"information\">"
"<ul>"
"<li>Generated on %1</li>"
"<li>Parsed %2 (%3 lines) in %4 ms</li>"
"</ul>"
"</div>") \
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) \
.arg(args.getString("input_file")) \
.arg(lines) \
.arg(timer.elapsed());
output << "<div class=\"reports\">";
output << "<h2 id=\"NormalizedQueriesMostTimeReport\">Queries that took up the most time (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
output << print_queries(queries.sortedQueries(Queries::mostTotalDuration), top);
output << "<h2 id=\"NormalizedQueriesMostFrequentReport\">Most frequent queries (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
output << print_queries(queries.sortedQueries(Queries::mostExecutions), top);
output << "<h2 id=\"NormalizedQueriesSlowestAverageReport\">Slowest queries (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
output << print_queries(queries.sortedQueries(Queries::mostAverageDuration), top);
QFile footer(":templates/footer.html");
if (!footer.open(QIODevice::ReadOnly | QIODevice::Text)){
qout << "Unable to open templates/footer.html" << endl;
return -5;
}else{
output << footer.readAll();
footer.close();
}
qout << "Wrote to file " << args.getString("output-file") << endl;
}