• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.tko.embedded_spreadsheet;
2 
3 import autotest.common.JsonRpcCallback;
4 import autotest.common.JsonRpcProxy;
5 import autotest.common.spreadsheet.Spreadsheet;
6 import autotest.common.spreadsheet.Spreadsheet.CellInfo;
7 import autotest.common.spreadsheet.Spreadsheet.SpreadsheetListener;
8 import autotest.tko.HeaderField;
9 import autotest.tko.SpreadsheetDataProcessor;
10 import autotest.tko.TestGroupDataSource;
11 import autotest.tko.TestSet;
12 import autotest.tko.TkoSpreadsheetUtils;
13 import autotest.tko.TkoSpreadsheetUtils.DrilldownType;
14 
15 import com.google.gwt.http.client.URL;
16 import com.google.gwt.json.client.JSONNumber;
17 import com.google.gwt.json.client.JSONObject;
18 import com.google.gwt.json.client.JSONValue;
19 import com.google.gwt.user.client.Command;
20 import com.google.gwt.user.client.Window;
21 
22 import java.util.Collections;
23 import java.util.List;
24 
25 public class EmbeddedSpreadsheetPresenter implements SpreadsheetListener {
26     public interface Display {
showNoResults()27         public void showNoResults();
showSpreadsheet()28         public void showSpreadsheet();
getSpreadsheet()29         public Spreadsheet getSpreadsheet();
getOnSpreadsheetRendered()30         public Command getOnSpreadsheetRendered();
31     }
32 
33     private static class SimpleHeaderField extends HeaderField {
SimpleHeaderField(String name)34         protected SimpleHeaderField(String name) {
35             super(name, name);
36         }
37 
38         @Override
getSqlCondition(String value)39         public String getSqlCondition(String value) {
40             return getSimpleSqlCondition(name, value);
41         }
42     }
43 
44     public static final String ROW_HEADER = "hostname";
45     public static final String COLUMN_HEADER = "test_name";
46     public static final String DRILLDOWN_ROW_HEADER = "job_tag";
47     public static final String DRILLDOWN_COLUMN_HEADER = "subdir";
48 
49     private List<HeaderField> rowHeader =
50             Collections.singletonList((HeaderField) new SimpleHeaderField(ROW_HEADER));
51     private List<HeaderField> columnHeader =
52             Collections.singletonList((HeaderField) new SimpleHeaderField(COLUMN_HEADER));
53 
54     private String afeJobIdStr;
55     private JSONObject condition;
56 
57     private Display display;
58 
bindDisplay(Display display)59     public void bindDisplay(Display display) {
60         this.display = display;
61     }
62 
initialize(String afeJobIdStr)63     public void initialize(String afeJobIdStr) {
64         JsonRpcProxy.setDefaultBaseUrl(JsonRpcProxy.TKO_BASE_URL);
65         this.afeJobIdStr = afeJobIdStr;
66 
67         condition = getFilterCondition(afeJobIdStr);
68         if (condition == null) {
69             showNoResults();
70         }
71 
72         JsonRpcProxy.getProxy().rpcCall("get_num_test_views", condition, new JsonRpcCallback() {
73             @Override
74             public void onSuccess(JSONValue result) {
75                 if (result.isNumber().doubleValue() != 0) {
76                     renderSpreadsheet(condition);
77                 } else {
78                     showNoResults();
79                 }
80             }
81         });
82     }
83 
getFilterCondition(String afeJobIdStr)84     private JSONObject getFilterCondition(String afeJobIdStr) {
85         if (afeJobIdStr == null) {
86             return null;
87         }
88 
89         int afeJobId;
90         try {
91             afeJobId = Integer.parseInt(afeJobIdStr);
92         } catch (NumberFormatException e) {
93             return null;
94         }
95 
96         JSONObject condition = new JSONObject();
97         condition.put("afe_job_id", new JSONNumber(afeJobId));
98         return condition;
99     }
100 
showNoResults()101     private void showNoResults() {
102         display.showNoResults();
103     }
104 
renderSpreadsheet(JSONObject condition)105     private void renderSpreadsheet(JSONObject condition) {
106         display.showSpreadsheet();
107         SpreadsheetDataProcessor spreadsheetProcessor =
108                 new SpreadsheetDataProcessor(display.getSpreadsheet());
109         spreadsheetProcessor.setDataSource(TestGroupDataSource.getStatusCountDataSource());
110         spreadsheetProcessor.setHeaders(rowHeader, columnHeader, new JSONObject());
111 
112         display.getSpreadsheet().setListener(this);
113         spreadsheetProcessor.refresh(condition, display.getOnSpreadsheetRendered());
114     }
115 
116     @Override
onCellClicked(CellInfo cellInfo, boolean isRightClick)117     public void onCellClicked(CellInfo cellInfo, boolean isRightClick) {
118         TestSet testSet = TkoSpreadsheetUtils.getTestSet(
119                 cellInfo, condition, rowHeader, columnHeader);
120 
121         if (testSet.isSingleTest()) {
122             openTestDetailView(testSet.getTestIndex());
123         } else {
124             openSpreadsheetView(testSet.getPartialSqlCondition(),
125                     TkoSpreadsheetUtils.getDrilldownType(cellInfo));
126         }
127     }
128 
openTestDetailView(int testIdx)129     private void openTestDetailView(int testIdx) {
130         openUrl("/new_tko/#tab_id=test_detail_view&object_id=" + testIdx);
131     }
132 
openSpreadsheetView(String extraCondition, DrilldownType drilldownType)133     private void openSpreadsheetView(String extraCondition, DrilldownType drilldownType) {
134         String drilldownPath;
135 
136         switch (drilldownType) {
137         case DRILLDOWN_ROW:
138             drilldownPath = generatePath(DRILLDOWN_ROW_HEADER, COLUMN_HEADER, extraCondition);
139             break;
140         case DRILLDOWN_COLUMN:
141             drilldownPath = generatePath(ROW_HEADER, DRILLDOWN_COLUMN_HEADER, extraCondition);
142             break;
143         case DRILLDOWN_BOTH:
144             drilldownPath = generatePath(
145                     DRILLDOWN_ROW_HEADER, DRILLDOWN_COLUMN_HEADER, extraCondition);
146             break;
147         default:
148             throw new UnsupportedOperationException(
149                     "DrilldownType " + drilldownType + " not supported");
150         }
151 
152         openUrl(drilldownPath);
153     }
154 
generatePath(String rowHeader, String columnHeader, String extraCondition)155     private String generatePath(String rowHeader, String columnHeader, String extraCondition) {
156         String condition = "afe_job_id = " + afeJobIdStr + " AND " + extraCondition;
157 
158         return "/new_tko/#tab_id=spreadsheet_view&row=" + rowHeader + "&column=" + columnHeader +
159                 "&condition=" + URL.encodeComponent(condition, true) + "&show_incomplete=true";
160     }
161 
openUrl(String url)162     private void openUrl(String url) {
163         Window.open(url, "_blank", null);
164     }
165 }
166