-
Notifications
You must be signed in to change notification settings - Fork 230
/
EnvDashboardView.java
531 lines (477 loc) · 14.8 KB
/
EnvDashboardView.java
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package org.jenkinsci.plugins.environmentdashboard;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.TopLevelItem;
import hudson.model.Descriptor.FormException;
import hudson.model.Hudson;
import hudson.model.View;
import hudson.model.ViewDescriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.environmentdashboard.utils.DBConnection;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.interceptor.RequirePOST;
/**
* Class to provide build wrapper for Dashboard.
*
* author vipin
* date 15/10/2014
*/
public class EnvDashboardView extends View {
private String envOrder = null;
private String compOrder = null;
private String deployHistory = null;
@DataBoundConstructor
public EnvDashboardView(final String name, final String envOrder, final String compOrder,
final String deployHistory) {
super(name, Hudson.getInstance());
this.envOrder = envOrder;
this.compOrder = compOrder;
this.deployHistory = deployHistory;
}
static {
ensureCorrectDBSchema();
}
private static void ensureCorrectDBSchema() {
String returnComment = "";
Connection conn = null;
Statement stat = null;
conn = DBConnection.getConnection();
try {
assert conn != null;
stat = conn.createStatement();
} catch (SQLException e) {
System.out.println("E13" + e.getMessage());
}
try {
stat.execute("ALTER TABLE env_dashboard ADD IF NOT EXISTS packageName VARCHAR(255);");
} catch (SQLException e) {
System.out.println(
"E14: Could not alter table to add package column to table env_dashboard.\n" + e.getMessage());
} finally {
DBConnection.closeConnection();
}
return;
}
@Override
protected void submit(final StaplerRequest req) throws IOException, ServletException, FormException {
req.bindJSON(this, req.getSubmittedForm());
}
@RequirePOST
public void doPurgeSubmit(final StaplerRequest req, StaplerResponse res)
throws IOException, ServletException, FormException {
checkPermission(Jenkins.ADMINISTER);
Connection conn = null;
Statement stat = null;
conn = DBConnection.getConnection();
try {
assert conn != null;
stat = conn.createStatement();
stat.execute("TRUNCATE TABLE env_dashboard");
} catch (SQLException e) {
System.out.println("E15: Could not truncate table env_dashboard.\n" + e.getMessage());
} finally {
DBConnection.closeConnection();
}
res.forwardToPreviousPage(req);
}
@Override
public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
return Hudson.getInstance().doCreateItem(req, rsp);
}
@Extension
public static final class DescriptorImpl extends ViewDescriptor {
private String envOrder;
private String compOrder;
private String deployHistory;
/**
* descriptor impl constructor This empty constructor is required for
* stapler. If you remove this constructor, text name of
* "Build Pipeline View" will be not displayed in the "NewView" page
*/
public DescriptorImpl() {
load();
}
public static ArrayList<String> getCustomColumns() {
Connection conn = null;
Statement stat = null;
ArrayList<String> columns;
columns = new ArrayList<String>();
String queryString = "SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='ENV_DASHBOARD';";
String[] fields = { "envComp", "compName", "envName", "buildstatus", "buildJobUrl", "jobUrl", "buildNum",
"created_at", "packageName" };
boolean columnFound = false;
try {
ResultSet rs = null;
conn = DBConnection.getConnection();
try {
assert conn != null;
stat = conn.createStatement();
} catch (SQLException e) {
System.out.println("E3" + e.getMessage());
}
try {
assert stat != null;
rs = stat.executeQuery(queryString);
} catch (SQLException e) {
System.out.println("E4" + e.getMessage());
}
String col = "";
while (rs.next()) {
columnFound = false;
col = rs.getString("COLUMN_NAME");
for (String presetColumn : fields) {
if (col.toLowerCase().equals(presetColumn.toLowerCase())) {
columnFound = true;
break;
}
}
if (!columnFound) {
columns.add(col.toLowerCase());
}
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E11" + e.getMessage());
return null;
}
return columns;
}
public ListBoxModel doFillColumnItems() {
ListBoxModel m = new ListBoxModel();
ArrayList<String> columns = getCustomColumns();
int position = 0;
m.add("Select column to remove", "");
for (String col : columns) {
m.add(col, col);
}
return m;
}
@SuppressWarnings("unused")
public FormValidation doDropColumn(@QueryParameter("column") final String column) {
Hudson.getInstance().checkPermission(Jenkins.ADMINISTER);
Connection conn = null;
Statement stat = null;
if ("".equals(column)) {
return FormValidation.ok();
}
String queryString = "ALTER TABLE ENV_DASHBOARD DROP COLUMN " + column + ";";
// Get DB connection
conn = DBConnection.getConnection();
try {
assert conn != null;
stat = conn.createStatement();
} catch (SQLException e) {
return FormValidation.error("Failed to create statement.");
}
try {
assert stat != null;
stat.execute(queryString);
} catch (SQLException e) {
DBConnection.closeConnection();
return FormValidation.error("Failed to remove column: " + column
+ "\nThis column may have already been removed. Refresh to update the list of columns to remove.");
}
DBConnection.closeConnection();
return FormValidation.ok("Successfully removed column " + column + ".");
}
/**
* get the display name
*
* @return display name
*/
@Override
public String getDisplayName() {
return "Environment Dashboard";
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
envOrder = formData.getString("envOrder");
compOrder = formData.getString("compOrder");
deployHistory = formData.getString("deployHistory");
save();
return super.configure(req, formData);
}
}
public ArrayList<String> splitEnvOrder(String envOrder) {
ArrayList<String> orderOfEnvs = new ArrayList<String>();
if (!"".equals(envOrder)) {
orderOfEnvs = new ArrayList<String>(Arrays.asList(envOrder.split("\\s*,\\s*")));
}
return orderOfEnvs;
}
public ArrayList<String> splitCompOrder(String compOrder) {
ArrayList<String> orderOfComps = new ArrayList<String>();
if (!"".equals(compOrder)) {
orderOfComps = new ArrayList<String>(Arrays.asList(compOrder.split("\\s*,\\s*")));
}
return orderOfComps;
}
public ResultSet runQuery(String queryString) {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
// Get DB connection
conn = DBConnection.getConnection();
try {
assert conn != null;
stat = conn.createStatement();
} catch (SQLException e) {
System.out.println("E3" + e.getMessage());
}
try {
assert stat != null;
rs = stat.executeQuery(queryString);
} catch (SQLException e) {
System.out.println("E4" + e.getMessage());
}
return rs;
}
public ArrayList<String> getOrderOfEnvs() {
ArrayList<String> orderOfEnvs;
orderOfEnvs = splitEnvOrder(envOrder);
if (orderOfEnvs == null || orderOfEnvs.isEmpty()) {
String queryString = "select distinct envname from env_dashboard order by envname;";
try {
ResultSet rs = runQuery(queryString);
if (rs == null) {
return null;
}
while (rs.next()) {
if (orderOfEnvs != null) {
orderOfEnvs.add(rs.getString("envName"));
}
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E6" + e.getMessage());
return null;
}
}
return orderOfEnvs;
}
public ArrayList<String> getOrderOfComps() {
ArrayList<String> orderOfComps;
orderOfComps = splitCompOrder(compOrder);
if (orderOfComps == null || orderOfComps.isEmpty()) {
String queryString = "select distinct compname from env_dashboard order by compname;";
try {
ResultSet rs = runQuery(queryString);
while (rs.next()) {
if (orderOfComps != null) {
orderOfComps.add(rs.getString("compName"));
}
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E8" + e.getMessage());
return null;
}
}
return orderOfComps;
}
public Integer getLimitDeployHistory() {
Integer lastDeploy;
if (deployHistory == null || deployHistory.equals("")) {
return 10;
} else {
try {
lastDeploy = Integer.parseInt(deployHistory);
} catch (NumberFormatException e) {
return 10;
}
}
return lastDeploy;
}
public ArrayList<String> getDeployments(String env, Integer lastDeploy) {
if (lastDeploy <= 0) {
lastDeploy = 10;
}
ArrayList<String> deployments;
deployments = new ArrayList<String>();
String queryString = "select top " + lastDeploy + " created_at from env_dashboard where envName ='" + env
+ "' order by created_at desc;";
try {
ResultSet rs = runQuery(queryString);
while (rs.next()) {
deployments.add(rs.getString("created_at"));
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E11" + e.getMessage());
return null;
}
return deployments;
}
public String anyJobsConfigured() {
ArrayList<String> orderOfEnvs;
orderOfEnvs = getOrderOfEnvs();
if (orderOfEnvs == null || orderOfEnvs.isEmpty()) {
return "NONE";
} else {
return "ENVS";
}
}
public String getNiceTimeStamp(String timeStamp) {
return timeStamp.substring(0, 19);
}
public HashMap getCompDeployed(String env, String time) {
HashMap<String, String> deployment;
deployment = new HashMap<String, String>();
String[] fields = { "buildstatus", "compName", "buildJobUrl", "jobUrl", "buildNum", "packageName" };
String queryString = "select " + StringUtils.join(fields, ", ").replace(".$", "")
+ " from env_dashboard where envName = '" + env + "' and created_at = '" + time + "';";
try {
ResultSet rs = runQuery(queryString);
rs.next();
for (String field : fields) {
deployment.put(field, rs.getString(field));
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E10" + e.getMessage());
System.out.println("Error executing: " + queryString);
}
return deployment;
}
public ArrayList<String> getCustomDBColumns() {
return DescriptorImpl.getCustomColumns();
}
public ArrayList<HashMap<String, String>> getDeploymentsByComp(String comp, Integer lastDeploy) {
if (lastDeploy <= 0) {
lastDeploy = 10;
}
ArrayList<HashMap<String, String>> deployments;
deployments = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hash;
String[] fields = { "envName", "buildstatus", "buildJobUrl", "jobUrl", "buildNum", "created_at",
"packageName" };
ArrayList<String> allDBFields = getCustomDBColumns();
for (String field : fields) {
allDBFields.add(field);
}
String queryString = "select top " + lastDeploy + " * from env_dashboard where compName='" + comp
+ "' order by created_at desc;";
try {
ResultSet rs = runQuery(queryString);
while (rs.next()) {
hash = new HashMap<String, String>();
for (String field : allDBFields) {
hash.put(field, rs.getString(field));
}
deployments.add(hash);
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E11" + e.getMessage());
return null;
}
return deployments;
}
public ArrayList<HashMap<String, String>> getDeploymentsByCompEnv(String comp, String env, Integer lastDeploy) {
if (lastDeploy <= 0) {
lastDeploy = 10;
}
ArrayList<HashMap<String, String>> deployments;
deployments = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hash;
String[] fields = { "envName", "buildstatus", "buildJobUrl", "jobUrl", "buildNum", "created_at",
"packageName" };
ArrayList<String> allDBFields = getCustomDBColumns();
for (String field : fields) {
allDBFields.add(field);
}
String queryString = "select top " + lastDeploy + " " + StringUtils.join(allDBFields, ", ").replace(".$", "")
+ " from env_dashboard where compName='" + comp + "' and envName='" + env
+ "' order by created_at desc;";
try {
ResultSet rs = runQuery(queryString);
while (rs.next()) {
hash = new HashMap<String, String>();
for (String field : allDBFields) {
hash.put(field, rs.getString(field));
}
deployments.add(hash);
}
DBConnection.closeConnection();
} catch (SQLException e) {
System.out.println("E11" + e.getMessage());
return null;
}
return deployments;
}
public HashMap getCompLastDeployed(String env, String comp) {
HashMap<String, String> deployment;
deployment = new HashMap<String, String>();
String[] fields = { "buildstatus", "buildJobUrl", "jobUrl", "buildNum", "created_at", "packageName" };
ArrayList<String> allDBFields = getCustomDBColumns();
for (String field : fields) {
allDBFields.add(field);
}
String queryString = "select top 1 " + StringUtils.join(allDBFields, ", ").replace(".$", "")
+ " from env_dashboard where envName = '" + env + "' and compName = '" + comp
+ "' order by created_at desc;";
try {
ResultSet rs = runQuery(queryString);
rs.next();
for (String field : allDBFields) {
deployment.put(field, rs.getString(field));
}
DBConnection.closeConnection();
} catch (SQLException e) {
if (e.getErrorCode() == 2000) {
// We'll assume this comp has never been deployed to this env }
} else {
System.out.println("E12" + e.getMessage());
System.out.println("Error executing: " + queryString);
}
}
return deployment;
}
@Override
public Collection<TopLevelItem> getItems() {
return Collections.EMPTY_LIST;
}
public String getEnvOrder() {
return envOrder;
}
public void setEnvOrder(final String envOrder) {
this.envOrder = envOrder;
}
public String getCompOrder() {
return compOrder;
}
public void setCompOrder(final String compOrder) {
this.compOrder = compOrder;
}
public String getDeployHistory() {
return deployHistory;
}
public void setDeployHistory(final String deployHistory) {
this.deployHistory = deployHistory;
}
@Override
public boolean contains(TopLevelItem topLevelItem) {
return false;
}
@Override
public void onJobRenamed(Item item, String s, String s2) {
}
}