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