Home | History | Annotate | Download | only in tko
      1 package autotest.tko;
      2 
      3 import autotest.common.JsonRpcCallback;
      4 import autotest.common.SimpleCallback;
      5 import autotest.common.StaticDataRepository;
      6 import autotest.common.Utils;
      7 import autotest.common.ui.TabView;
      8 
      9 import com.google.gwt.event.dom.client.BlurEvent;
     10 import com.google.gwt.event.dom.client.BlurHandler;
     11 import com.google.gwt.event.dom.client.ClickEvent;
     12 import com.google.gwt.event.dom.client.ClickHandler;
     13 import com.google.gwt.event.logical.shared.SelectionEvent;
     14 import com.google.gwt.event.logical.shared.SelectionHandler;
     15 import com.google.gwt.json.client.JSONArray;
     16 import com.google.gwt.json.client.JSONObject;
     17 import com.google.gwt.json.client.JSONString;
     18 import com.google.gwt.json.client.JSONValue;
     19 import com.google.gwt.user.client.ui.Button;
     20 import com.google.gwt.user.client.ui.CheckBox;
     21 import com.google.gwt.user.client.ui.HorizontalPanel;
     22 import com.google.gwt.user.client.ui.ListBox;
     23 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
     24 import com.google.gwt.user.client.ui.Panel;
     25 import com.google.gwt.user.client.ui.SuggestBox;
     26 import com.google.gwt.user.client.ui.SuggestOracle;
     27 import com.google.gwt.user.client.ui.TextBox;
     28 
     29 import java.util.Arrays;
     30 import java.util.HashMap;
     31 import java.util.HashSet;
     32 import java.util.Map;
     33 import java.util.Set;
     34 
     35 public class ExistingGraphsFrontend extends GraphingFrontend {
     36 
     37     private CheckBox normalize = new CheckBox("Normalize Performance (allows multiple benchmarks" +
     38                                               " on one graph)");
     39     private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
     40     private TextBox hostname = new TextBox();
     41     private SuggestBox hostnameSuggest = new SuggestBox(oracle, hostname);
     42     private Panel benchmarkWrapper = new HorizontalPanel();
     43     private TextBox kernel = new TextBox();
     44     private JSONObject hostsAndTests = null;
     45     private Button graphButton = new Button("Graph");
     46 
     47     private ListBox singleBenchmark = new ListBox(false);
     48     private ListBox multiBenchmark = new ListBox(true);
     49 
     50     public ExistingGraphsFrontend(final TabView parent) {
     51         normalize.addClickHandler(new ClickHandler() {
     52             public void onClick(ClickEvent event) {
     53                 normalizeClicked();
     54             }
     55         });
     56 
     57         hostnameSuggest.getTextBox().addBlurHandler(new BlurHandler() {
     58             public void onBlur(BlurEvent event) {
     59                 refreshTests();
     60             }
     61         });
     62         hostnameSuggest.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
     63             public void onSelection(SelectionEvent<SuggestOracle.Suggestion> event) {
     64                 refreshTests();
     65             }
     66         });
     67 
     68         addBenchmarkItem("(Please select a hostname first)");
     69 
     70         graphButton.addClickHandler(new ClickHandler() {
     71             public void onClick(ClickEvent event) {
     72                 parent.updateHistory();
     73                 showGraph();
     74             }
     75         });
     76 
     77         kernel.setText("all");
     78 
     79         table.setWidget(0, 0, normalize);
     80         table.getFlexCellFormatter().setColSpan(0, 0, 2);
     81         benchmarkWrapper.add(singleBenchmark);
     82         benchmarkWrapper.add(multiBenchmark);
     83         multiBenchmark.setVisible(false);
     84 
     85         addControl("Hostname:", hostnameSuggest);
     86         addControl("Benchmark:", benchmarkWrapper);
     87         addControl("Kernel:", kernel);
     88         table.setWidget(table.getRowCount(), 1, graphButton);
     89 
     90         table.getColumnFormatter().setWidth(0, "1px");
     91 
     92         initWidget(table);
     93     }
     94 
     95     private void addBenchmarkItem(String item) {
     96         singleBenchmark.addItem(item);
     97         multiBenchmark.addItem(item);
     98     }
     99 
    100     private void getHostsAndTests(final SimpleCallback onFinished) {
    101         setEnabled(false);
    102         rpcProxy.rpcCall("get_hosts_and_tests", new JSONObject(), new JsonRpcCallback() {
    103             @Override
    104             public void onSuccess(JSONValue result) {
    105                 hostsAndTests = result.isObject();
    106                 onFinished.doCallback(null);
    107                 setEnabled(true);
    108             }
    109         });
    110     }
    111 
    112     @Override
    113     public void refresh() {
    114         getHostsAndTests(new SimpleCallback() {
    115             public void doCallback(Object source) {
    116                 oracle.clear();
    117                 for (String host : hostsAndTests.keySet()) {
    118                     oracle.add(host);
    119                 }
    120             }
    121         });
    122     }
    123 
    124     @Override
    125     public void addToHistory(Map<String, String> args) {
    126         args.put("normalize", String.valueOf(normalize.getValue()));
    127         args.put("hostname", hostname.getText());
    128 
    129         // Add the selected benchmarks
    130         StringBuilder benchmarks = new StringBuilder();
    131         ListBox benchmark = getVisibleBenchmark();
    132         for (int i = 0; i < benchmark.getItemCount(); i++) {
    133             if (benchmark.isItemSelected(i)) {
    134                 benchmarks.append(benchmark.getValue(i));
    135                 benchmarks.append(",");
    136             }
    137         }
    138 
    139         args.put("benchmark", benchmarks.toString());
    140         args.put("kernel", kernel.getText());
    141     }
    142 
    143     @Override
    144     public void handleHistoryArguments(final Map<String, String> args) {
    145         hostname.setText(args.get("hostname"));
    146         normalize.setValue(Boolean.parseBoolean(args.get("normalize")));
    147         normalizeClicked();
    148         kernel.setText(args.get("kernel"));
    149 
    150         getHostsAndTests(new SimpleCallback() {
    151             public void doCallback(Object source) {
    152                 refreshTests();
    153 
    154                 ListBox benchmark = getVisibleBenchmark();
    155                 Set<String> benchmarks =
    156                     new HashSet<String>(Arrays.asList(args.get("benchmark").split(",")));
    157                 for (int i = 0; i < benchmark.getItemCount(); i++) {
    158                     benchmark.setItemSelected(i, benchmarks.contains(benchmark.getValue(i)));
    159                 }
    160             }
    161         });
    162     }
    163 
    164     private ListBox getVisibleBenchmark() {
    165         boolean multiVisible = normalize.getValue();
    166         if (multiVisible) {
    167             assert multiBenchmark.isVisible();
    168             return multiBenchmark;
    169         } else {
    170             assert singleBenchmark.isVisible();
    171             return singleBenchmark;
    172         }
    173     }
    174 
    175     @Override
    176     protected void addAdditionalEmbeddingParams(JSONObject params) {
    177         // No embedding
    178     }
    179 
    180     // Change the state of the page based on the status of the "normalize" checkbox
    181     private void normalizeClicked() {
    182         boolean multiVisible = normalize.getValue();
    183         ListBox dest;
    184         ListBox src;
    185 
    186         if (multiVisible) {
    187             dest = multiBenchmark;
    188             src = singleBenchmark;
    189         } else {
    190             dest = singleBenchmark;
    191             src = multiBenchmark;
    192         }
    193 
    194         dest.setVisible(true);
    195         src.setVisible(false);
    196 
    197         dest.setSelectedIndex(src.getSelectedIndex());
    198         src.setSelectedIndex(-1);
    199     }
    200 
    201     private void setEnabled(boolean enabled) {
    202         normalize.setEnabled(enabled);
    203         hostname.setEnabled(enabled);
    204         singleBenchmark.setEnabled(enabled);
    205         multiBenchmark.setEnabled(enabled);
    206         kernel.setEnabled(enabled);
    207         graphButton.setEnabled(enabled);
    208     }
    209 
    210     private void refreshTests() {
    211         JSONValue value = hostsAndTests.get(hostnameSuggest.getText());
    212         if (value == null) {
    213             return;
    214         }
    215 
    216         HashSet<String> selectedTests = new HashSet<String>();
    217         ListBox benchmark = getVisibleBenchmark();
    218         for (int i = 0; i < benchmark.getItemCount(); i++) {
    219             if (benchmark.isItemSelected(i)) {
    220                 selectedTests.add(benchmark.getValue(i));
    221             }
    222         }
    223 
    224         JSONArray tests = value.isObject().get("tests").isArray();
    225         singleBenchmark.clear();
    226         multiBenchmark.clear();
    227         for (int i = 0; i < tests.size(); i++) {
    228             String test = Utils.jsonToString(tests.get(i));
    229             addBenchmarkItem(test);
    230             if (selectedTests.contains(test)) {
    231                 benchmark.setItemSelected(i, true);
    232             }
    233         }
    234     }
    235 
    236     private void showGraph() {
    237         String hostnameStr = hostnameSuggest.getText();
    238 
    239         JSONValue value = hostsAndTests.get(hostnameStr);
    240         if (value == null) {
    241             return;
    242         }
    243 
    244         String url;
    245         HashMap<String, String> args = new HashMap<String, String>();
    246         args.put("kernel", kernel.getText());
    247 
    248         if (normalize.getValue()) {
    249             url = "/tko/machine_aggr.cgi?";
    250             final JSONArray tests = new JSONArray();
    251             for (int i = 0; i < multiBenchmark.getItemCount(); i++) {
    252                 if (multiBenchmark.isItemSelected(i)) {
    253                     tests.set(tests.size(), new JSONString(multiBenchmark.getValue(i)));
    254                 }
    255             }
    256 
    257             args.put("machine", hostnameStr);
    258 
    259             StringBuilder arg = new StringBuilder();
    260             for (int i = 0; i < tests.size(); i++) {
    261                 String test = Utils.jsonToString(tests.get(i));
    262                 String key = getKey(test);
    263                 if (i != 0) {
    264                     arg.append(",");
    265                 }
    266                 arg.append(test);
    267                 arg.append(":");
    268                 arg.append(key);
    269             }
    270             args.put("benchmark_key", arg.toString());
    271         } else {
    272             int benchmarkIndex = singleBenchmark.getSelectedIndex();
    273             if (benchmarkIndex == -1) {
    274                 return;
    275             }
    276 
    277             url = "/tko/machine_test_attribute_graph.cgi?";
    278 
    279             JSONObject hostObject = value.isObject();
    280             String machine = Utils.jsonToString(hostObject.get("id"));
    281             String benchmarkStr = singleBenchmark.getValue(benchmarkIndex);
    282 
    283             args.put("machine", machine);
    284             args.put("benchmark", benchmarkStr);
    285             args.put("key", getKey(benchmarkStr));
    286         }
    287         Utils.openUrlInNewWindow(url + Utils.encodeUrlArguments(args));
    288     }
    289 
    290     private String getKey(String benchmark) {
    291         JSONObject benchmarkKey =
    292             StaticDataRepository.getRepository().getData("benchmark_key").isObject();
    293         return Utils.jsonToString(benchmarkKey.get(benchmark.replaceAll("\\..*", "")));
    294     }
    295 
    296     @Override
    297     public String getFrontendId() {
    298         return "existing_graphs";
    299     }
    300 }
    301