Home | History | Annotate | Download | only in afe
      1 package autotest.afe;
      2 
      3 import autotest.common.StaticDataRepository;
      4 import autotest.common.Utils;
      5 import autotest.common.table.RpcDataSource;
      6 import autotest.common.ui.NotifyManager;
      7 
      8 import com.google.gwt.json.client.JSONArray;
      9 import com.google.gwt.json.client.JSONObject;
     10 import com.google.gwt.json.client.JSONString;
     11 import com.google.gwt.json.client.JSONValue;
     12 
     13 import java.util.ArrayList;
     14 import java.util.Arrays;
     15 import java.util.HashMap;
     16 import java.util.List;
     17 import java.util.Map;
     18 
     19 /**
     20  * Custom RpcDataSource to process the list of host queue entries for a job and
     21  * consolidate metahosts of the same label.
     22  */
     23 class JobStatusDataSource extends RpcDataSource {
     24     private JSONObject dictionary;
     25 
     26     public JobStatusDataSource() {
     27         super("get_host_queue_entries", "get_num_host_queue_entries");
     28 
     29         // retrieve the dictionary from static data
     30         StaticDataRepository staticData = StaticDataRepository.getRepository();
     31         dictionary = staticData.getData("status_dictionary").isObject();
     32     }
     33 
     34     private String translateStatus(String status)  {
     35         if (dictionary.containsKey(status)) {
     36             return dictionary.get(status).isString().stringValue();
     37         }
     38         else {
     39             NotifyManager.getInstance().showError("Unknown status", "Can not find status " +
     40                                                   status);
     41             return status;
     42         }
     43     }
     44 
     45     @Override
     46     protected List<JSONObject> handleJsonResult(JSONValue result) {
     47         List<JSONObject> queueEntries = super.handleJsonResult(result);
     48         List<JSONObject> rows = new ArrayList<JSONObject>();
     49         Map<List<String>, JSONObject> metaHostEntries= new HashMap<List<String>, JSONObject>();
     50         for(JSONObject queueEntry : queueEntries) {
     51             // translate status
     52             String status = queueEntry.get("status").isString().stringValue();
     53             String translation = translateStatus(status);
     54             queueEntry.put("status", new JSONString(translation));
     55 
     56             boolean hasHost = (queueEntry.get("host").isNull() == null);
     57             boolean hasMetaHost = (queueEntry.get("meta_host").isNull() == null);
     58 
     59             if (!hasHost && !hasMetaHost) {
     60                 queueEntry.put("hostname", new JSONString("(hostless)"));
     61                 rows.add(queueEntry);
     62 
     63             } else if (!hasHost && hasMetaHost) {
     64                 // metahost
     65                 incrementMetaHostCount(metaHostEntries, queueEntry);
     66             } else {
     67                 // non-metahost
     68                 processHostData(queueEntry);
     69                 rows.add(queueEntry);
     70             }
     71         }
     72 
     73         addMetaHostRows(metaHostEntries, rows);
     74 
     75         return rows;
     76     }
     77 
     78     protected void processHostData(JSONObject queueEntry) {
     79         JSONObject host = queueEntry.get("host").isObject();
     80         queueEntry.put("hostname", host.get("hostname"));
     81         // don't show host details if the job is complete - it'll only confuse
     82         // the user
     83         boolean complete = queueEntry.get("complete").isBoolean().booleanValue();
     84         if (!complete) {
     85             queueEntry.put("host_status", host.get("status"));
     86             queueEntry.put("host_locked", AfeUtils.getLockedText(host));
     87         }
     88     }
     89 
     90     private void incrementMetaHostCount(Map<List<String>, JSONObject> metaHostEntries,
     91                                         JSONObject queueEntry) {
     92         String label = queueEntry.get("meta_host").isString().stringValue();
     93         String status = queueEntry.get("status").isString().stringValue();
     94         if (status.equals("Queued")) {
     95             status = "Unassigned";
     96         }
     97         List<String> key = getMetaHostKey(label, status);
     98 
     99         if (!metaHostEntries.containsKey(key)) {
    100             queueEntry.put("id_list", new JSONArray());
    101             metaHostEntries.put(key, queueEntry);
    102         }
    103 
    104         JSONObject metaHostEntry = metaHostEntries.get(key).isObject();
    105         JSONArray idList = metaHostEntry.get("id_list").isArray();
    106         idList.set(idList.size(), queueEntry.get("id"));
    107     }
    108 
    109     private List<String> getMetaHostKey(String label, String status) {
    110         // arrays don't hash correctly, so use a list instead
    111         return Arrays.asList(new String[] {label, status});
    112     }
    113 
    114     private void addMetaHostRows(Map<List<String>, JSONObject> metaHostEntries,
    115                                  List<JSONObject> rows) {
    116         for (JSONObject entry : metaHostEntries.values()) {
    117             String label = Utils.jsonToString(entry.get("meta_host"));
    118             String status = Utils.jsonToString(entry.get("status"));
    119             int count = entry.get("id_list").isArray().size();
    120 
    121             entry.put("hostname", new JSONString(label + " (label)"));
    122             entry.put("status", new JSONString(Integer.toString(count) + " " + status));
    123             rows.add(entry);
    124         }
    125     }
    126 }
    127