• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.tko;
2 
3 import autotest.common.ui.NotifyManager;
4 import autotest.common.ui.TabView;
5 
6 import com.google.gwt.json.client.JSONNumber;
7 import com.google.gwt.json.client.JSONObject;
8 import com.google.gwt.json.client.JSONString;
9 import com.google.gwt.user.client.ui.TextBox;
10 
11 import java.util.Map;
12 
13 public class MachineQualHistogramFrontend extends DynamicGraphingFrontend {
14     private static final String DEFAULT_INTERVAL = "10";
15 
16     private FilterSelector globalFilters =
17         new FilterSelector(DBColumnSelector.TEST_VIEW);
18     private FilterSelector testFilters =
19         new FilterSelector(DBColumnSelector.TEST_VIEW);
20     private TextBox interval = new TextBox();
21 
MachineQualHistogramFrontend(final TabView parent)22     public MachineQualHistogramFrontend(final TabView parent) {
23         super(parent, new MachineQualHistogram(), "qual");
24 
25         interval.setText(DEFAULT_INTERVAL);
26 
27         addControl("Preconfigured:", preconfig);
28         addControl("Global filters:", globalFilters);
29         addControl("Test set filters:", testFilters);
30         addControl("Interval:", interval);
31 
32         commonInitialization();
33     }
34 
35     @Override
addAdditionalEmbeddingParams(JSONObject params)36     protected void addAdditionalEmbeddingParams(JSONObject params) {
37         params.put("graph_type", new JSONString("qual"));
38         params.put("params", buildParams());
39     }
40 
buildQuery()41     private JSONString buildQuery() {
42         String gFilterString = globalFilters.getFilterString();
43         String tFilterString = testFilters.getFilterString();
44         boolean hasGFilter = !gFilterString.equals("");
45         boolean hasTFilter = !tFilterString.equals("");
46 
47         StringBuilder sql = new StringBuilder();
48 
49         sql.append("SELECT hostname, COUNT(DISTINCT ");
50         if (hasTFilter) {
51             sql.append("IF(");
52             sql.append(tFilterString);
53             sql.append(", test_idx, NULL)");
54         } else {
55             sql.append("test_idx");
56         }
57         sql.append(") 'total', COUNT(DISTINCT IF(");
58         if (hasTFilter) {
59             sql.append(TkoUtils.wrapWithParens(tFilterString));
60             sql.append(" AND ");
61         }
62 
63         sql.append("status = 'GOOD', test_idx, NULL)) 'good' FROM tko_test_view_outer_joins");
64         if (hasGFilter) {
65             sql.append(" WHERE ");
66             sql.append(gFilterString);
67         }
68         sql.append(" GROUP BY hostname");
69         return new JSONString(sql.toString());
70     }
71 
buildFilterString()72     private JSONString buildFilterString() {
73         StringBuilder filterString = new StringBuilder();
74         String gFilterString = globalFilters.getFilterString();
75         String tFilterString = testFilters.getFilterString();
76         boolean hasGFilter = !gFilterString.equals("");
77         boolean hasTFilter = !tFilterString.equals("");
78         if (hasGFilter) {
79             filterString.append(TkoUtils.wrapWithParens(gFilterString));
80             if (hasTFilter) {
81                 filterString.append(" AND ");
82             }
83         }
84         if (hasTFilter) {
85             filterString.append(TkoUtils.wrapWithParens(tFilterString));
86         }
87         return new JSONString(filterString.toString());
88     }
89 
90     @Override
buildParams()91     protected JSONObject buildParams() {
92         if (interval.getText().equals("")) {
93             NotifyManager.getInstance().showError("You must enter an interval");
94             return null;
95         }
96 
97         int intervalValue;
98         try {
99             intervalValue = Integer.parseInt(interval.getText());
100         } catch (NumberFormatException e) {
101             NotifyManager.getInstance().showError("Interval must be an integer");
102             return null;
103         }
104 
105         JSONObject params = new JSONObject();
106         params.put("query", buildQuery());
107         params.put("filter_string", buildFilterString());
108         params.put("interval", new JSONNumber(intervalValue));
109 
110         return params;
111     }
112 
113     @Override
getFrontendId()114     public String getFrontendId() {
115         return "machine_qual_histogram";
116     }
117 
118     @Override
addToHistory(Map<String, String> args)119     public void addToHistory(Map<String, String> args) {
120         globalFilters.addToHistory(args, "globalFilter");
121         testFilters.addToHistory(args, "testFilter");
122         args.put("interval", interval.getText());
123     }
124 
125     @Override
handleHistoryArguments(Map<String, String> args)126     public void handleHistoryArguments(Map<String, String> args) {
127         setVisible(false);
128         globalFilters.reset();
129         testFilters.reset();
130         globalFilters.handleHistoryArguments(args, "globalFilter");
131         testFilters.handleHistoryArguments(args, "testFilter");
132         interval.setText(args.get("interval"));
133         setVisible(true);
134     }
135 }
136