Skip to content

Commit 41042a4

Browse files
committed
format and remove copy method
1 parent ee55abe commit 41042a4

File tree

2 files changed

+120
-132
lines changed

2 files changed

+120
-132
lines changed

src/main/java/opendota/CreateParsedDataBlob.java

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -147,34 +147,34 @@ public ParsedData createParsedDataBlob(List<Entry> entries) {
147147
Metadata meta = processMetadata(entries);
148148
long tEnd = System.currentTimeMillis();
149149
System.err.format("metadata: %sms\n", tEnd - tStart);
150-
150+
151151
tStart = System.currentTimeMillis();
152152
List<Entry> expanded = processExpand(entries, meta);
153153
tEnd = System.currentTimeMillis();
154154
System.err.format("expand: %sms\n", tEnd - tStart);
155-
155+
156156
tStart = System.currentTimeMillis();
157157
ParsedData parsedData = processParsedData(expanded, new ParsedData(), meta);
158158
tEnd = System.currentTimeMillis();
159159
System.err.format("populate: %sms\n", tEnd - tStart);
160-
160+
161161
tStart = System.currentTimeMillis();
162162
parsedData.teamfights = processTeamfights(expanded, meta);
163163
tEnd = System.currentTimeMillis();
164164
System.err.format("teamfights: %sms\n", tEnd - tStart);
165-
165+
166166
tStart = System.currentTimeMillis();
167167
parsedData.pauses = processPauses(entries);
168168
tEnd = System.currentTimeMillis();
169169
System.err.format("pauses: %sms\n", tEnd - tStart);
170-
170+
171171
tStart = System.currentTimeMillis();
172172
AllPlayersResult ap = processAllPlayers(entries, meta);
173173
parsedData.radiant_gold_adv = ap.radiantGoldAdv;
174174
parsedData.radiant_xp_adv = ap.radiantXpAdv;
175175
tEnd = System.currentTimeMillis();
176176
System.err.format("processAllPlayers: %sms\n", tEnd - tStart);
177-
177+
178178
return parsedData;
179179
}
180180

@@ -213,7 +213,8 @@ private void populate(Entry e, ParsedData container, Metadata meta) {
213213
container.chat.add(deepCopy(e, Entry.class));
214214
break;
215215
case "cosmetics":
216-
Type mapType = new TypeToken<Map<String, Integer>>() {}.getType();
216+
Type mapType = new TypeToken<Map<String, Integer>>() {
217+
}.getType();
217218
container.cosmetics = g.fromJson(e.key, mapType);
218219
break;
219220
case "CHAT_MESSAGE_FIRSTBLOOD":
@@ -396,7 +397,6 @@ private void handleDamageTargets(Entry e, PlayerData player) {
396397
targets.put(target, targets.getOrDefault(target, 0) + e.value);
397398
}
398399

399-
400400
private void handleMapField(Entry e, PlayerData player, ParsedData container, Metadata meta) {
401401
Map<String, Integer> targetMap = getPlayerMap(player, e.type);
402402
int value = e.value != null ? e.value : 1;
@@ -1495,7 +1495,7 @@ private Map<String, Integer> getPlayerMap(PlayerData player, String type) {
14951495

14961496
public Entry shallowCopy(Entry entry) {
14971497
try {
1498-
return (Entry)entry.clone();
1498+
return (Entry) entry.clone();
14991499
} catch (CloneNotSupportedException ex) {
15001500
throw new RuntimeException(ex);
15011501
}
@@ -1519,41 +1519,40 @@ public <T> T deepCopy(T anObject, Class<T> classInfo) {
15191519
}
15201520
}
15211521

1522-
1523-
// public UploadProps processUploadProps(List<Entry> entries) {
1524-
// UploadProps container = new UploadProps();
1525-
// container.playerMap = new HashMap<>();
1526-
1527-
// for (Entry e : entries) {
1528-
// switch (e.type) {
1529-
// case "epilogue":
1530-
// try {
1531-
// JsonNode json = mapper.readTree(e.key);
1532-
// JsonNode dota = json.get("gameInfo_").get("dota_");
1533-
// container.matchId = dota.get("matchId_").asLong();
1534-
// container.gameMode = dota.get("gameMode_").asInt();
1535-
// container.radiantWin = dota.get("gameWinner_").asInt() == 2;
1536-
// } catch (Exception ex) {
1537-
// ex.printStackTrace();
1538-
// }
1539-
// break;
1540-
// case "interval":
1541-
// if (!container.playerMap.containsKey(e.playerSlot)) {
1542-
// container.playerMap.put(e.playerSlot, new UploadPlayerData());
1543-
// }
1544-
// UploadPlayerData playerData = container.playerMap.get(e.playerSlot);
1545-
// playerData.heroId = e.heroId;
1546-
// playerData.level = e.level;
1547-
// playerData.kills = e.kills;
1548-
// playerData.deaths = e.deaths;
1549-
// playerData.assists = e.assists;
1550-
// playerData.denies = e.denies;
1551-
// playerData.lastHits = e.lh;
1552-
// playerData.gold = e.gold;
1553-
// playerData.xp = e.xp;
1554-
// break;
1555-
// }
1556-
// }
1557-
1558-
// return container;
1559-
// }
1522+
// public UploadProps processUploadProps(List<Entry> entries) {
1523+
// UploadProps container = new UploadProps();
1524+
// container.playerMap = new HashMap<>();
1525+
1526+
// for (Entry e : entries) {
1527+
// switch (e.type) {
1528+
// case "epilogue":
1529+
// try {
1530+
// JsonNode json = mapper.readTree(e.key);
1531+
// JsonNode dota = json.get("gameInfo_").get("dota_");
1532+
// container.matchId = dota.get("matchId_").asLong();
1533+
// container.gameMode = dota.get("gameMode_").asInt();
1534+
// container.radiantWin = dota.get("gameWinner_").asInt() == 2;
1535+
// } catch (Exception ex) {
1536+
// ex.printStackTrace();
1537+
// }
1538+
// break;
1539+
// case "interval":
1540+
// if (!container.playerMap.containsKey(e.playerSlot)) {
1541+
// container.playerMap.put(e.playerSlot, new UploadPlayerData());
1542+
// }
1543+
// UploadPlayerData playerData = container.playerMap.get(e.playerSlot);
1544+
// playerData.heroId = e.heroId;
1545+
// playerData.level = e.level;
1546+
// playerData.kills = e.kills;
1547+
// playerData.deaths = e.deaths;
1548+
// playerData.assists = e.assists;
1549+
// playerData.denies = e.denies;
1550+
// playerData.lastHits = e.lh;
1551+
// playerData.gold = e.gold;
1552+
// playerData.xp = e.xp;
1553+
// break;
1554+
// }
1555+
// }
1556+
1557+
// return container;
1558+
// }

src/main/java/opendota/Main.java

Lines changed: 74 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.sun.net.httpserver.HttpExchange;
2424
import com.sun.net.httpserver.HttpHandler;
2525
import com.sun.net.httpserver.HttpServer;
26-
26+
2727
public class Main {
2828

2929
public static void main(String[] args) throws Exception {
@@ -35,11 +35,11 @@ public static void main(String[] args) throws Exception {
3535
server.start();
3636

3737
// Re-register ourselves
38-
Timer timer = new Timer();
39-
TimerTask task = new RegisterTask();
38+
Timer timer = new Timer();
39+
TimerTask task = new RegisterTask();
4040
timer.schedule(task, 0, 5000);
4141
}
42-
42+
4343
static class MyHandler implements HttpHandler {
4444
@Override
4545
public void handle(HttpExchange t) throws IOException {
@@ -71,7 +71,8 @@ public void handle(HttpExchange t) throws IOException {
7171
try {
7272
Map<String, String> query = splitQuery(t.getRequestURI());
7373
URI replayUrl = URI.create(query.get("replay_url"));
74-
// boolean v2 = t.getRequestURI().getRawQuery() != null ? t.getRequestURI().getRawQuery().contains("v2") : false;
74+
// boolean v2 = t.getRequestURI().getRawQuery() != null ?
75+
// t.getRequestURI().getRawQuery().contains("v2") : false;
7576
// boolean v2 = true;
7677

7778
// Get the replay as a byte[]
@@ -87,12 +88,12 @@ public void handle(HttpExchange t) throws IOException {
8788

8889
if (replayUrl.toString().endsWith(".bz2")) {
8990
// Write byte[] to bunzip, get back decompressed byte[]
90-
Process bz = new ProcessBuilder(new String[] {"bunzip2"}).start();
91+
Process bz = new ProcessBuilder(new String[] { "bunzip2" }).start();
9192

9293
// Start separate thread so we can consume output while sending input
9394
Thread thread = new Thread(() -> {
9495
try {
95-
copy(new ByteArrayInputStream(bzIn), bz.getOutputStream());
96+
bz.getOutputStream().write(bzIn);
9697
bz.getOutputStream().close();
9798
} catch (IOException ex) {
9899
ex.printStackTrace();
@@ -119,57 +120,62 @@ public void handle(HttpExchange t) throws IOException {
119120
t.getResponseBody().close();
120121
}
121122

122-
// String cmd = String.format("""
123-
// curl --max-time 145 --fail -L %s | %s | curl -X POST -T - "localhost:5600%s" %s
124-
// """,
125-
// replayUrl,
126-
// replayUrl.toString().endsWith(".bz2") ? "bunzip2" : "cat",
127-
// v2 ? "?blob" : "",
128-
// v2 ? "" : " | node processors/createParsedDataBlob.mjs"
129-
// );
130-
// System.err.println(cmd);
131-
// // Download, unzip, parse, aggregate
132-
// Process proc = new ProcessBuilder(new String[] {"bash", "-c", cmd})
133-
// .start();
134-
// ByteArrayOutputStream output = new ByteArrayOutputStream();
135-
// ByteArrayOutputStream error = new ByteArrayOutputStream();
136-
// copy(proc.getInputStream(), output);
137-
// // Write error to console
138-
// copy(proc.getErrorStream(), error);
139-
// System.err.println(error.toString());
140-
// int exitCode = proc.waitFor();
141-
// if (exitCode != 0) {
142-
// // We can send 200 status here and no response if expected error (read the error string)
143-
// // Maybe we can pass the specific error info in the response headers
144-
// int status = 500;
145-
// if (error.toString().contains("curl: (28) Operation timed out")) {
146-
// // Parse took too long, maybe China replay?
147-
// status = 200;
148-
// }
149-
// if (error.toString().contains("curl: (22) The requested URL returned error: 502")) {
150-
// // Google-Edge-Cache: origin retries exhausted Error: 2010
151-
// // Server error, don't retry
152-
// status = 200;
153-
// }
154-
// if (error.toString().contains("bunzip2: Data integrity error when decompressing")) {
155-
// // Corrupted replay, don't retry
156-
// status = 200;
157-
// }
158-
// if (error.toString().contains("bunzip2: Compressed file ends unexpectedly")) {
159-
// // Corrupted replay, don't retry
160-
// status = 200;
161-
// }
162-
// if (error.toString().contains("bunzip2: (stdin) is not a bzip2 file.")) {
163-
// // Tried to unzip a non-bz2 file
164-
// status = 200;
165-
// }
166-
// t.sendResponseHeaders(status, 0);
167-
// t.getResponseBody().close();
168-
// } else {
169-
// t.sendResponseHeaders(200, output.size());
170-
// output.writeTo(t.getResponseBody());
171-
// t.getResponseBody().close();
172-
// }
123+
// String cmd = String.format("""
124+
// curl --max-time 145 --fail -L %s | %s | curl -X POST -T - "localhost:5600%s"
125+
// %s
126+
// """,
127+
// replayUrl,
128+
// replayUrl.toString().endsWith(".bz2") ? "bunzip2" : "cat",
129+
// v2 ? "?blob" : "",
130+
// v2 ? "" : " | node processors/createParsedDataBlob.mjs"
131+
// );
132+
// System.err.println(cmd);
133+
// // Download, unzip, parse, aggregate
134+
// Process proc = new ProcessBuilder(new String[] {"bash", "-c", cmd})
135+
// .start();
136+
// ByteArrayOutputStream output = new ByteArrayOutputStream();
137+
// ByteArrayOutputStream error = new ByteArrayOutputStream();
138+
// copy(proc.getInputStream(), output);
139+
// // Write error to console
140+
// copy(proc.getErrorStream(), error);
141+
// System.err.println(error.toString());
142+
// int exitCode = proc.waitFor();
143+
// if (exitCode != 0) {
144+
// // We can send 200 status here and no response if expected error (read the
145+
// error string)
146+
// // Maybe we can pass the specific error info in the response headers
147+
// int status = 500;
148+
// if (error.toString().contains("curl: (28) Operation timed out")) {
149+
// // Parse took too long, maybe China replay?
150+
// status = 200;
151+
// }
152+
// if (error.toString().contains("curl: (22) The requested URL returned error:
153+
// 502")) {
154+
// // Google-Edge-Cache: origin retries exhausted Error: 2010
155+
// // Server error, don't retry
156+
// status = 200;
157+
// }
158+
// if (error.toString().contains("bunzip2: Data integrity error when
159+
// decompressing")) {
160+
// // Corrupted replay, don't retry
161+
// status = 200;
162+
// }
163+
// if (error.toString().contains("bunzip2: Compressed file ends unexpectedly"))
164+
// {
165+
// // Corrupted replay, don't retry
166+
// status = 200;
167+
// }
168+
// if (error.toString().contains("bunzip2: (stdin) is not a bzip2 file.")) {
169+
// // Tried to unzip a non-bz2 file
170+
// status = 200;
171+
// }
172+
// t.sendResponseHeaders(status, 0);
173+
// t.getResponseBody().close();
174+
// } else {
175+
// t.sendResponseHeaders(200, output.size());
176+
// output.writeTo(t.getResponseBody());
177+
// t.getResponseBody().close();
178+
// }
173179
}
174180
}
175181

@@ -179,33 +185,15 @@ public static Map<String, String> splitQuery(URI uri) throws UnsupportedEncoding
179185
String[] pairs = query.split("&");
180186
for (String pair : pairs) {
181187
int idx = pair.indexOf("=");
182-
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
188+
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"),
189+
URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
183190
}
184191
return query_pairs;
185192
}
186-
187-
// buffer size used for reading and writing
188-
private static final int BUFFER_SIZE = 8192;
189-
190-
/**
191-
* Reads all bytes from an input stream and writes them to an output stream.
192-
*/
193-
private static long copy(InputStream source, OutputStream sink) throws IOException {
194-
long nread = 0L;
195-
byte[] buf = new byte[BUFFER_SIZE];
196-
int n;
197-
while ((n = source.read(buf)) > 0) {
198-
sink.write(buf, 0, n);
199-
nread += n;
200-
}
201-
return nread;
202-
}
203193
}
204194

205-
class RegisterTask extends TimerTask
206-
{
207-
public void run()
208-
{
195+
class RegisterTask extends TimerTask {
196+
public void run() {
209197
if (System.getenv().containsKey("SERVICE_REGISTRY_HOST")) {
210198
try {
211199
String ip = "";
@@ -217,16 +205,18 @@ public void run()
217205
ip = RegisterTask.shellExec("hostname -i");
218206
}
219207
long nproc = Runtime.getRuntime().availableProcessors();
220-
String postCmd = "curl -X POST --max-time 60 -L " + System.getenv().get("SERVICE_REGISTRY_HOST") + "/register/parser/" + ip + ":5600" + "?size=" + nproc + "&key=" + System.getenv().get("RETRIEVER_SECRET");
208+
String postCmd = "curl -X POST --max-time 60 -L " + System.getenv().get("SERVICE_REGISTRY_HOST")
209+
+ "/register/parser/" + ip + ":5600" + "?size=" + nproc + "&key="
210+
+ System.getenv().get("RETRIEVER_SECRET");
221211
System.err.println(postCmd);
222212
RegisterTask.shellExec(postCmd);
223213
} catch (Exception e) {
224214
System.err.println(e);
225215
}
226216
}
227-
}
217+
}
228218

229-
public static String shellExec(String cmdCommand) throws IOException {
219+
public static String shellExec(String cmdCommand) throws IOException {
230220
final StringBuilder stringBuilder = new StringBuilder();
231221
String[] cmdArr = cmdCommand.split(" ");
232222
final Process process = Runtime.getRuntime().exec(cmdArr, null, null);
@@ -237,5 +227,4 @@ public static String shellExec(String cmdCommand) throws IOException {
237227
}
238228
return stringBuilder.toString();
239229
}
240-
}
241-
230+
}

0 commit comments

Comments
 (0)