This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TrustedAdvisorStats.java
128 lines (107 loc) · 4.61 KB
/
TrustedAdvisorStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright 2013-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
package com.amazon.aws.supportapi.samples;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.support.AWSSupport;
import com.amazonaws.services.support.AWSSupportClient;
import com.amazonaws.services.support.model.*;
import java.util.*;
/**
* Sample class to get Trusted Advisor Stats.
*/
public class TrustedAdvisorStats {
public static void main(String[] args) {
AWSSupportClient support = new AWSSupportClient(new DefaultAWSCredentialsProviderChain());
/**
* Get a map of checkId to which category it is in.
*/
final Map<String, String> checkId2CategoryMap = getChecks(support);
/**
* Get the Trusted Advisor Summaries for all checks.
*/
DescribeTrustedAdvisorCheckSummariesRequest request = new DescribeTrustedAdvisorCheckSummariesRequest();
request.setCheckIds(new LinkedList<String>(checkId2CategoryMap.keySet()));
DescribeTrustedAdvisorCheckSummariesResult result = support.describeTrustedAdvisorCheckSummaries(request);
List<TrustedAdvisorCheckSummary> summaries = result.getSummaries();
/**
* Create a map of category to category totals.
*/
Map<String, TrustedAdvisorAggregatedCheckSummary> category2TotalMap = new HashMap<String, TrustedAdvisorAggregatedCheckSummary>() {{
Set<String> categories = new HashSet<String>(checkId2CategoryMap.values());
for (String category : categories) {
put(category, new TrustedAdvisorAggregatedCheckSummary());
}
}};
/**
* Cycle through all summaries and update the total of the respective category.
*/
for (TrustedAdvisorCheckSummary summary : summaries) {
String checkId = summary.getCheckId();
String category = checkId2CategoryMap.get(checkId);
TrustedAdvisorAggregatedCheckSummary aggregatedTotal = category2TotalMap.get(category);
aggregatedTotal.add(summary);
}
/**
* Print out the totals.
*/
for (Map.Entry<String, TrustedAdvisorAggregatedCheckSummary> entry : category2TotalMap.entrySet()) {
String category = entry.getKey();
TrustedAdvisorAggregatedCheckSummary aggregatedTotal = entry.getValue();
System.out.println(category + " (" + aggregatedTotal.getCategoryStatus() + "): " + aggregatedTotal);
}
}
private static Map<String, String> getChecks(AWSSupport support) {
DescribeTrustedAdvisorChecksRequest request = new DescribeTrustedAdvisorChecksRequest().withLanguage("en");
DescribeTrustedAdvisorChecksResult checks = support.describeTrustedAdvisorChecks(request);
Map<String, String> map = new HashMap<String, String>();
for (TrustedAdvisorCheckDescription check : checks.getChecks()) {
map.put(check.getId(), check.getCategory());
}
return map;
}
/**
* An enumerated list of all the different category statuses.
*/
private static enum CategoryStatus {
not_available, ok, warning, error;
CategoryStatus getWorst(CategoryStatus status) {
if (status.ordinal() < ordinal()) {
return this;
} else {
return status;
}
}
}
private final static class TrustedAdvisorAggregatedCheckSummary extends TrustedAdvisorResourcesSummary {
private CategoryStatus categoryStatus = CategoryStatus.not_available;
public void add(TrustedAdvisorCheckSummary checkSummary) {
TrustedAdvisorResourcesSummary resourcesSummary = checkSummary.getResourcesSummary();
CategoryStatus categoryStatus = CategoryStatus.valueOf(checkSummary.getStatus());
this.categoryStatus = this.categoryStatus.getWorst(categoryStatus);
setResourcesFlagged(safeAdd(getResourcesFlagged(), resourcesSummary.getResourcesFlagged()));
setResourcesIgnored(safeAdd(getResourcesIgnored(), resourcesSummary.getResourcesIgnored()));
setResourcesProcessed(safeAdd(getResourcesProcessed(), resourcesSummary.getResourcesProcessed()));
setResourcesSuppressed(safeAdd(getResourcesSuppressed(), resourcesSummary.getResourcesSuppressed()));
}
private long safeAdd(Long l1, Long l2) {
return safeGet(l1) + safeGet(l2);
}
private long safeGet(Long l) {
if (l == null) {
return 0;
} else {
return l;
}
}
public CategoryStatus getCategoryStatus() {
return categoryStatus;
}
}
}