-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppRoutingFunction.java
170 lines (127 loc) · 4.85 KB
/
AppRoutingFunction.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.takipi.udf.app;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.takipi.api.client.ApiClient;
import com.takipi.api.client.data.category.Category;
import com.takipi.api.client.data.view.SummarizedView;
import com.takipi.api.client.data.view.ViewFilters;
import com.takipi.api.client.data.view.ViewInfo;
import com.takipi.api.client.util.category.CategoryUtil;
import com.takipi.api.client.util.client.ClientUtil;
import com.takipi.api.client.util.view.ViewUtil;
import com.takipi.common.util.CollectionUtil;
import com.takipi.udf.ContextArgs;
import com.takipi.udf.input.Input;
import com.takipi.udf.util.TestUtil;
public class AppRoutingFunction {
private static final boolean SHARED = true;
private static final boolean IMMUTABLE_VIEWS = true;
public static String validateInput(String rawInput) {
return parseAppRoutingInput(rawInput).toString();
}
static AppRoutingInput parseAppRoutingInput(String rawInput) {
System.out.println("validateInput rawInput: " + rawInput);
if (Strings.isNullOrEmpty(rawInput)) {
throw new IllegalArgumentException("Input is empty");
}
AppRoutingInput input;
try {
input = AppRoutingInput.of(rawInput);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
if (input.category_name == null) {
throw new IllegalArgumentException("'category_name' must exist");
}
if (input.max_views <= 0) {
throw new IllegalArgumentException("'max_views' must be greater than zero");
}
return input;
}
public static void execute(String rawContextArgs, String rawInput) {
System.out.println("execute: " + rawContextArgs);
ContextArgs args = (new Gson()).fromJson(rawContextArgs, ContextArgs.class);
if (!args.viewValidate()) {
throw new IllegalArgumentException("Invalid context args - " + rawContextArgs);
}
AppRoutingInput input = parseAppRoutingInput(rawInput);
buildAppRoutingViews(args, input);
}
private static void buildAppRoutingViews(ContextArgs args, AppRoutingInput input) {
ApiClient apiClient = args.apiClient();
String serviceId = args.serviceId;
List<String> apps = ClientUtil.getApplications(apiClient, serviceId, true);
if (CollectionUtil.safeIsEmpty(apps)) {
System.out.println("Could not acquire apps for service " + serviceId);
return;
}
Map<String, SummarizedView> views = ViewUtil.getServiceViewsByName(apiClient, serviceId);
if (CollectionUtil.safeIsEmpty(views)) {
System.out.println("Could not acquire view for service " + serviceId);
return;
}
String categoryId = createAppCategory(apiClient, serviceId, input);
int appsWithViewsSize = 0;
Set<String> appsWithoutViews = Sets.newHashSet();
for (String app : apps) {
if (!views.containsKey(app)) {
appsWithoutViews.add(app);
} else {
appsWithViewsSize++;
}
}
if (appsWithViewsSize >= input.max_views) {
System.out.println("Found " + appsWithViewsSize + " app views, exceeding max for " + serviceId);
return;
}
List<ViewInfo> viewInfos = Lists.newArrayList();
for (String app : appsWithoutViews) {
ViewInfo viewInfo = new ViewInfo();
viewInfo.name = app;
viewInfo.filters = new ViewFilters();
viewInfo.filters.apps = Collections.singletonList(app);
viewInfo.shared = SHARED;
viewInfo.immutable = IMMUTABLE_VIEWS;
viewInfos.add(viewInfo);
}
ViewUtil.createFilteredViews(apiClient, serviceId, viewInfos, views, categoryId);
}
private static String createAppCategory(ApiClient apiClient, String serviceId, AppRoutingInput input) {
Category category = CategoryUtil.getServiceCategoryByName(apiClient, serviceId, input.category_name);
String result;
if (category == null) {
result = CategoryUtil.createCategory(input.category_name, serviceId, apiClient, SHARED);
System.out.println("Created category " + result + " for " + input.category_name);
} else {
result = category.id;
}
return result;
}
static class AppRoutingInput extends Input {
public String category_name; // The category in which to place views
public int max_views; // Max number of views to create in the category
private AppRoutingInput(String raw) {
super(raw);
}
static AppRoutingInput of(String raw) {
return new AppRoutingInput(raw);
}
@Override
public String toString() {
return "AppRouting: " + category_name + " " + max_views;
}
}
// A sample program on how to programmatically activate AppRoutingFunction
public static void main(String[] args) {
String rawContextArgs = TestUtil.getViewContextArgs(args, "All Events");
// some test values
String[] sampleValues = new String[] { "category_name=Apps", "max_views=50" };
AppRoutingFunction.execute(rawContextArgs, String.join("\n", sampleValues));
}
}