This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOutputFormatter.java
268 lines (239 loc) · 8.37 KB
/
OutputFormatter.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package utility;
import flink_dsp.query1.AverageDelayOutcome;
import flink_dsp.query2.ReasonRankingOutcome;
import flink_dsp.query3.CompanyRankingOutcome;
import org.apache.commons.io.FileUtils;
import scala.Tuple2;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import static utility.DataCommonTransformation.formatDate;
/**
* Class used to format the queries' outcomes into string
*/
@SuppressWarnings({"ResultOfMethodCallIgnored", "DuplicatedCode"})
public class OutputFormatter {
private static final boolean CONSOLE_RESULTS_PRINT = false;
private static final String RESULTS_DIRECTORY = "Results";
private static final String CSV_SEP = ";";
private static final String NEW_LINE = "\n";
private static final String AM = "05:00-11:59";
private static final String PM = "12:00-19:00";
private static final String DAILY_HEADER = "DAILY: \t";
private static final String WEEKLY_HEADER = "WEEKLY: \t";
private static final String MONTHLY_HEADER = "MONTHLY: \t";
private static final String QUERY1_HEADER = "QUERY 1 - ";
private static final String QUERY2_HEADER = "QUERY 2 - ";
private static final String QUERY3_HEADER = "QUERY 3 - ";
/**
* Scope: Flink - Query 1
*/
public static final String QUERY1_DAILY_CSV_FILE_PATH = "Results/query1_daily.csv";
public static final String QUERY1_WEEKLY_CSV_FILE_PATH = "Results/query1_weekly.csv";
public static final String QUERY1_MONTHLY_CSV_FILE_PATH = "Results/query1_monthly.csv";
/**
* Scope: Flink - Query 2
*/
public static final String QUERY2_DAILY_CSV_FILE_PATH = "Results/query2_daily.csv";
public static final String QUERY2_WEEKLY_CSV_FILE_PATH = "Results/query2_weekly.csv";
/**
* Scope: Flink - Query 3
*/
public static final String QUERY3_DAILY_CSV_FILE_PATH = "Results/query3_daily.csv";
public static final String QUERY3_WEEKLY_CSV_FILE_PATH = "Results/query3_weekly.csv";
public static final String[] FLINK_OUTPUT_FILES = {QUERY1_DAILY_CSV_FILE_PATH, QUERY1_WEEKLY_CSV_FILE_PATH,
QUERY1_MONTHLY_CSV_FILE_PATH, QUERY2_DAILY_CSV_FILE_PATH, QUERY2_WEEKLY_CSV_FILE_PATH,
QUERY3_DAILY_CSV_FILE_PATH, QUERY3_WEEKLY_CSV_FILE_PATH};
/**
* Scope: Global
* Used to remove old files in Results directory
*/
public static void cleanResultsFolder() {
try {
FileUtils.cleanDirectory(new File(RESULTS_DIRECTORY));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Could not clean Results directory");
}
}
/**
* Scope: Flink - Query 1
* Formats the processing outcome as a string to be published to Kafka
* @param outcome to format
* @return formatted string
*/
public static String query1OutcomeFormatter(AverageDelayOutcome outcome) {
StringBuilder builder = new StringBuilder();
builder.append(formatDate(outcome.getStartDate().getTime()));
outcome.getBoroMeans().forEach((k, v) -> builder.append(CSV_SEP).append(k).append(CSV_SEP).append(v));
return builder.toString();
}
/**
* Scope: Flink - Query 2
* Formats the processing outcome as a string to be published to Kafka
* @param outcome to format
* @return formatted string
*/
public static String query2OutcomeFormatter(ReasonRankingOutcome outcome) {
return formatDate(outcome.getStartDate().getTime()) +
CSV_SEP + AM + CSV_SEP +
outcome.getAmRanking().toString() +
CSV_SEP + PM + CSV_SEP +
outcome.getPmRanking().toString();
}
/**
* Scope: Flink - Query 3
* Formats the processing outcome as a string to be published to Kafka
* @param outcome to format
* @return formatted string
*/
public static String query3OutcomeFormatter(CompanyRankingOutcome outcome) {
StringBuilder builder = new StringBuilder();
builder.append(formatDate(outcome.getStartDate().getTime()));
for (Tuple2<String, Double> score : outcome.getCompanyRanking()) {
builder.append(CSV_SEP)
.append(score._1())
.append(CSV_SEP)
.append(score._2());
}
return builder.toString();
}
/**
* @deprecated - needed before Kafka Flink output topic creation
* Scope: Flink - Query 1
* Save outcome to csv file and print result
* @param path where to store csv
* @param outcome to be stored
*/
@Deprecated
public static void writeOutputQuery1(String path, AverageDelayOutcome outcome) {
try {
// output structures
File file = new File(path);
if (!file.exists()) {
// creates the file if it does not exist
file.createNewFile();
}
// append to existing version of the same file
FileWriter writer = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(writer);
StringBuilder builder = new StringBuilder();
builder.append(formatDate(outcome.getStartDate().getTime()));
outcome.getBoroMeans().forEach((k, v) -> builder.append(CSV_SEP).append(k).append(CSV_SEP).append(v));
builder.append(NEW_LINE);
bw.append(builder.toString());
bw.close();
writer.close();
// prints formatted output
if (CONSOLE_RESULTS_PRINT) {
switch (path) {
case QUERY1_DAILY_CSV_FILE_PATH:
System.out.println(QUERY1_HEADER + DAILY_HEADER + builder.toString());
break;
case QUERY1_WEEKLY_CSV_FILE_PATH:
System.out.println(QUERY1_HEADER + WEEKLY_HEADER + builder.toString());
break;
case QUERY1_MONTHLY_CSV_FILE_PATH:
System.out.println(QUERY1_HEADER + MONTHLY_HEADER + builder.toString());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Could not export query 1 result to CSV file");
}
}
/**
* @deprecated - needed before Kafka Flink output topic creation
* Scope: Flink - Query 2
* Save outcome to csv file and print result
* @param path where to store csv
* @param outcome to be stored
*/
@Deprecated
public static void writeOutputQuery2(String path, ReasonRankingOutcome outcome) {
try {
// output structures
File file = new File(path);
if (!file.exists()) {
// creates the file if it does not exist
file.createNewFile();
}
// append to existing version of the same file
FileWriter writer = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(writer);
StringBuilder builder = new StringBuilder();
builder.append(formatDate(outcome.getStartDate().getTime()));
builder.append(CSV_SEP + AM + CSV_SEP);
builder.append(outcome.getAmRanking().toString());
builder.append(CSV_SEP + PM + CSV_SEP);
builder.append(outcome.getPmRanking().toString());
builder.append(NEW_LINE);
bw.append(builder.toString());
bw.close();
writer.close();
// prints formatted output
if (CONSOLE_RESULTS_PRINT) {
switch (path) {
case QUERY2_DAILY_CSV_FILE_PATH:
System.out.println(QUERY2_HEADER + DAILY_HEADER + builder.toString());
break;
case QUERY2_WEEKLY_CSV_FILE_PATH:
System.out.println(QUERY2_HEADER + WEEKLY_HEADER + builder.toString());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Could not export query 2 result to CSV file");
}
}
/**
* @deprecated - needed before Kafka Flink output topic creation
* Scope: Flink - Query 3
* Save outcome to csv file and print result
* @param path where to store csv
* @param outcome to be stored
*/
@Deprecated
public static void writeOutputQuery3(String path, CompanyRankingOutcome outcome) {
try {
// output structures
File file = new File(path);
if (!file.exists()) {
// creates the file if it does not exist
file.createNewFile();
}
// append to existing version of the same file
FileWriter writer = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(writer);
StringBuilder builder = new StringBuilder();
builder.append(formatDate(outcome.getStartDate().getTime()));
for (Tuple2<String, Double> score : outcome.getCompanyRanking()) {
builder.append(CSV_SEP)
.append(score._1())
.append(CSV_SEP)
.append(score._2());
}
builder.append(NEW_LINE);
bw.append(builder.toString());
bw.close();
writer.close();
// prints formatted output
if (CONSOLE_RESULTS_PRINT) {
switch (path) {
case QUERY3_DAILY_CSV_FILE_PATH:
System.out.println(QUERY3_HEADER + DAILY_HEADER + builder.toString());
break;
case QUERY3_WEEKLY_CSV_FILE_PATH:
System.out.println(QUERY3_HEADER + WEEKLY_HEADER + builder.toString());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Could not export query 3 result to CSV file");
}
}
}