Pavlin Radoslavov

Added CLI command to print summary of the intents:

"intents -s" or "intents --summary"

Change-Id: If6ec11b1425cb2a4c93fdd910808065419af14b5
...@@ -45,14 +45,32 @@ import java.util.Set; ...@@ -45,14 +45,32 @@ import java.util.Set;
45 description = "Lists the inventory of intents and their states") 45 description = "Lists the inventory of intents and their states")
46 public class IntentsListCommand extends AbstractShellCommand { 46 public class IntentsListCommand extends AbstractShellCommand {
47 47
48 - @Option(name = "-i", aliases = "--installable", description = "Output Installable Intents", 48 + @Option(name = "-i", aliases = "--installable",
49 + description = "Output Installable Intents",
49 required = false, multiValued = false) 50 required = false, multiValued = false)
50 private boolean showInstallable = false; 51 private boolean showInstallable = false;
51 52
53 + @Option(name = "-s", aliases = "--summary",
54 + description = "Intents summary",
55 + required = false, multiValued = false)
56 + private boolean intentsSummary = false;
52 57
53 @Override 58 @Override
54 protected void execute() { 59 protected void execute() {
55 IntentService service = get(IntentService.class); 60 IntentService service = get(IntentService.class);
61 +
62 + if (intentsSummary) {
63 + IntentSummaries intentSummaries = new IntentSummaries();
64 + intentSummaries.collectIntentSummary(service,
65 + service.getIntents());
66 + if (outputJson()) {
67 + print("%s", intentSummaries.json());
68 + } else {
69 + intentSummaries.printSummary();
70 + }
71 + return;
72 + }
73 +
56 if (outputJson()) { 74 if (outputJson()) {
57 print("%s", json(service, service.getIntents())); 75 print("%s", json(service, service.getIntents()));
58 } else { 76 } else {
...@@ -66,6 +84,230 @@ public class IntentsListCommand extends AbstractShellCommand { ...@@ -66,6 +84,230 @@ public class IntentsListCommand extends AbstractShellCommand {
66 } 84 }
67 } 85 }
68 86
87 + /**
88 + * Internal local class to keep track of all intent summaries.
89 + */
90 + private class IntentSummaries {
91 + private IntentSummary summaryAll;
92 + private IntentSummary summaryConnectivity;
93 + private IntentSummary summaryPointToPoint;
94 + private IntentSummary summaryMultiPointToSinglePoint;
95 + private IntentSummary summarySinglePointToMultiPoint;
96 + private IntentSummary summaryPath;
97 + private IntentSummary summaryLinkCollection;
98 + private IntentSummary summaryUnknownType;
99 +
100 + /**
101 + * Initializes the internal state.
102 + */
103 + private void init() {
104 + summaryAll = new IntentSummary("All");
105 + summaryConnectivity = new IntentSummary("Connectivity");
106 + summaryPointToPoint = new IntentSummary("PointToPoint");
107 + summaryMultiPointToSinglePoint =
108 + new IntentSummary("MultiPointToSinglePoint");
109 + summarySinglePointToMultiPoint =
110 + new IntentSummary("SinglePointToMultiPoint");
111 + summaryPath = new IntentSummary("Path");
112 + summaryLinkCollection = new IntentSummary("LinkCollection");
113 + summaryUnknownType = new IntentSummary("UnknownType");
114 + }
115 +
116 + /**
117 + * Collects summary of all intents.
118 + *
119 + * @param service the Intent Service to use
120 + * @param intents the intents
121 + */
122 + private void collectIntentSummary(IntentService service,
123 + Iterable<Intent> intents) {
124 + init();
125 +
126 + // Collect the summary for each intent type intents
127 + for (Intent intent : intents) {
128 + IntentState intentState = service.getIntentState(intent.id());
129 +
130 + // Update the summary for all Intents
131 + summaryAll.update(intentState);
132 +
133 + if (intent instanceof ConnectivityIntent) {
134 + summaryConnectivity.update(intentState);
135 + // NOTE: ConnectivityIntent is a base type Intent
136 + // continue;
137 + }
138 + if (intent instanceof PointToPointIntent) {
139 + summaryPointToPoint.update(intentState);
140 + continue;
141 + }
142 + if (intent instanceof MultiPointToSinglePointIntent) {
143 + summaryMultiPointToSinglePoint.update(intentState);
144 + continue;
145 + }
146 + if (intent instanceof SinglePointToMultiPointIntent) {
147 + summarySinglePointToMultiPoint.update(intentState);
148 + continue;
149 + }
150 + if (intent instanceof PathIntent) {
151 + summaryPath.update(intentState);
152 + continue;
153 + }
154 + if (intent instanceof LinkCollectionIntent) {
155 + summaryLinkCollection.update(intentState);
156 + continue;
157 + }
158 +
159 + summaryUnknownType.update(intentState);
160 + }
161 + }
162 +
163 + /**
164 + * Gets JSON representation of all Intents summary.
165 + *
166 + * @return JSON representation of all Intents summary
167 + */
168 + ObjectNode json() {
169 + ObjectMapper mapper = new ObjectMapper();
170 + ObjectNode result = mapper.createObjectNode();
171 + result.put("connectivity", summaryConnectivity.json(mapper));
172 + result.put("pointToPoint", summaryPointToPoint.json(mapper));
173 + result.put("multiPointToSinglePoint",
174 + summaryMultiPointToSinglePoint.json(mapper));
175 + result.put("singlePointToMultiPoint",
176 + summarySinglePointToMultiPoint.json(mapper));
177 + result.put("path", summaryPath.json(mapper));
178 + result.put("linkCollection", summaryLinkCollection.json(mapper));
179 + result.put("unknownType", summaryUnknownType.json(mapper));
180 + result.put("all", summaryAll.json(mapper));
181 + return result;
182 + }
183 +
184 + /**
185 + * Prints summary of the intents.
186 + */
187 + private void printSummary() {
188 + summaryConnectivity.printState();
189 + summaryPointToPoint.printState();
190 + summaryMultiPointToSinglePoint.printState();
191 + summarySinglePointToMultiPoint.printState();
192 + summaryPath.printState();
193 + summaryLinkCollection.printState();
194 + summaryUnknownType.printState();
195 + summaryAll.printState();
196 + }
197 +
198 + /**
199 + * Internal local class to keep track of a single type Intent summary.
200 + */
201 + private class IntentSummary {
202 + private final String intentType;
203 + private int total = 0;
204 + private int submitted = 0;
205 + private int compiling = 0;
206 + private int installing = 0;
207 + private int installed = 0;
208 + private int recompiling = 0;
209 + private int withdrawing = 0;
210 + private int withdrawn = 0;
211 + private int failed = 0;
212 + private int unknownState = 0;
213 +
214 + private static final String FORMAT_SUMMARY_LINE1 =
215 + "%-23s total= %7d installed= %7d";
216 + private static final String FORMAT_SUMMARY_LINE2 =
217 + "%-23s withdrawn= %7d failed= %7d";
218 + private static final String FORMAT_SUMMARY_LINE3 =
219 + "%-23s submitted= %7d compiling= %7d";
220 + private static final String FORMAT_SUMMARY_LINE4 =
221 + "%-23s installing= %7d recompiling= %7d";
222 + private static final String FORMAT_SUMMARY_LINE5 =
223 + "%-23s withdrawing= %7d";
224 + private static final String FORMAT_SUMMARY_LINE6 =
225 + "%-23s unknownState= %7d";
226 +
227 + /**
228 + * Constructor.
229 + *
230 + * @param intentType the scring describing the Intent type
231 + */
232 + IntentSummary(String intentType) {
233 + this.intentType = intentType;
234 + }
235 +
236 + /**
237 + * Updates the Intent Summary.
238 + *
239 + * @param intentState the state of the Intent
240 + */
241 + void update(IntentState intentState) {
242 + total++;
243 + switch (intentState) {
244 + case SUBMITTED:
245 + submitted++;
246 + break;
247 + case COMPILING:
248 + compiling++;
249 + break;
250 + case INSTALLING:
251 + installing++;
252 + break;
253 + case INSTALLED:
254 + installed++;
255 + break;
256 + case RECOMPILING:
257 + recompiling++;
258 + break;
259 + case WITHDRAWING:
260 + withdrawing++;
261 + break;
262 + case WITHDRAWN:
263 + withdrawn++;
264 + break;
265 + case FAILED:
266 + failed++;
267 + break;
268 + default:
269 + unknownState++;
270 + break;
271 + }
272 + }
273 +
274 + /**
275 + * Prints the Intent Summary.
276 + */
277 + void printState() {
278 + print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
279 + print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
280 + print(FORMAT_SUMMARY_LINE3, intentType, submitted, compiling);
281 + print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
282 + print(FORMAT_SUMMARY_LINE5, intentType, withdrawing);
283 + if (unknownState != 0) {
284 + print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
285 + }
286 + }
287 +
288 + /**
289 + * Gets the JSON representation of the Intent Summary.
290 + *
291 + * @return the JSON representation of the Intent Summary
292 + */
293 + JsonNode json(ObjectMapper mapper) {
294 + ObjectNode result = mapper.createObjectNode()
295 + .put("total", total)
296 + .put("installed", installed)
297 + .put("failed", failed)
298 + .put("submitted", submitted)
299 + .put("compiling", compiling)
300 + .put("installing", installing)
301 + .put("recompiling", recompiling)
302 + .put("withdrawing", withdrawing)
303 + .put("withdrawn", withdrawn)
304 + .put("unknownState", unknownState);
305 +
306 + return result;
307 + }
308 + }
309 + }
310 +
69 private void printDetails(IntentService service, Intent intent) { 311 private void printDetails(IntentService service, Intent intent) {
70 if (intent.resources() != null && !intent.resources().isEmpty()) { 312 if (intent.resources() != null && !intent.resources().isEmpty()) {
71 print(" resources=%s", intent.resources()); 313 print(" resources=%s", intent.resources());
...@@ -188,5 +430,4 @@ public class IntentsListCommand extends AbstractShellCommand { ...@@ -188,5 +430,4 @@ public class IntentsListCommand extends AbstractShellCommand {
188 } 430 }
189 return result; 431 return result;
190 } 432 }
191 -
192 } 433 }
......