forked from maradine/ps2bot
/
PlaytimeCollector.java
333 lines (269 loc) · 11.5 KB
/
PlaytimeCollector.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.Connection;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.parser.Parser;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.LinkedList;
import java.io.IOException;
import java.util.Properties;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.BatchUpdateException;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.DriverManager;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.BinaryConnectionFactory;
import net.spy.memcached.AddrUtil;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.ExecutionException;
/*
do a big weapon_play_time pull
for each entry:
create key from character_id+item_id
get key from cache
if key exists and last_save < this last_save
compute diff of value field
insert time record into db
write new key to cache
else if key exists and last_save = this last_save
increment dupe count
else
query SOE for prior entry
compute diff of value field
insert time record into db
write new key to cache
*/
public class PlaytimeCollector {
public static HashMap<String, Integer> collectPlaytime(int timeout, long interval, Properties props) throws TimeoutException, IOException, SQLException, InterruptedException, ExecutionException {
Document doc = null;
org.jsoup.Connection jcon = null;
String soeapikey = props.getProperty("soeapikey");
String querystring;
MemcachedClient mcc = null;
String memcachedServer = props.getProperty("memcached_server", "");
mcc = new MemcachedClient(new BinaryConnectionFactory(),AddrUtil.getAddresses(memcachedServer));
if (soeapikey == null || soeapikey.equals("")) {
querystring = "http://census.soe.com/xml/get/ps2:v2/characters_weapon_stat?stat_name=weapon_play_time&c:sort=last_save_date:-1&c:limit=5000&c:join=type:character^on:character_id^to:character_id^show:faction_id'battle_rank.value";
} else {
querystring = "http://census.soe.com/s:"+soeapikey+"/xml/get/ps2:v2/characters_weapon_stat?stat_name=weapon_play_time&c:sort=last_save_date:-1&c:limit=5000&c:join=type:character^on:character_id^to:character_id^show:faction_id'battle_rank.value";
}
System.out.println("running "+ querystring);
try {
jcon = Jsoup.connect(querystring);
jcon.timeout(timeout);
jcon.maxBodySize(0);
jcon.parser(Parser.xmlParser());
doc = jcon.get();
} catch (IOException ioe) {
System.out.println("***connection error to SOE***");
System.out.println(ioe);
throw ioe;
}
Elements playtimeEvents = doc.select("characters_weapon_stat_list > characters_weapon_stat");
//System.out.println("GOT "+playtimeEvents.size()+" ROWS FROM DOC.SELECT");
int hitCounter = 0;
int dupeCounter = 0;
int updateCounter = 0;
ArrayList<PlaytimeRow> alPtr = new ArrayList<PlaytimeRow>();
int pullTimestamp = 2000000000;
for (Element el : playtimeEvents) {
String characterId = el.attr("character_id");
String itemId = el.attr("item_id");
String lastSave = el.attr("last_save");
String value = el.attr("value");
String vehicleId = el.attr("vehicle_id");
int factionId=-1;
if (el.childNodeSize()>0) {
factionId = Integer.parseInt(el.childNode(0).attr("faction_id"));
}
int brValue=-1;
if (el.childNodeSize()>0) {
if (el.childNode(0).childNodeSize()>0) {
brValue = Integer.parseInt(el.childNode(0).childNode(0).attr("value"));
}
}
//System.out.println("ELEMENT: "+characterId+" "+itemId+" "+lastSave+" "+value+" "+vehicleId);
if (Integer.parseInt(lastSave) < pullTimestamp) {
pullTimestamp = Integer.parseInt(lastSave);
}
PlaytimeRow ptr = new PlaytimeRow(Long.parseLong(characterId), Integer.parseInt(itemId), Integer.parseInt(lastSave), Integer.parseInt(value), Integer.parseInt(vehicleId), factionId, brValue);
String cacheKey = characterId+"+"+itemId+"+"+vehicleId;
Future<Object> future = mcc.asyncGet(cacheKey);
Object cachedObject = null;
try {
cachedObject = future.get(5, TimeUnit.SECONDS); //make this variable
} catch (TimeoutException e) {
future.cancel(false);
System.out.println("Timeout to memcached exceeded.");
throw e;
} catch (Exception e) {
future.cancel(false);
System.out.println("UNHANDLED CONCURRENCY EXCPETION");
throw e;
}
if (cachedObject == null) {
// System.out.println("KEY "+cacheKey+" IS NOT IN CACHE");
Future<Boolean> setter = mcc.set(cacheKey, 0, ptr);
try {
Boolean success = setter.get(5, TimeUnit.SECONDS);
if (success) {
// System.out.println("WROTE NEW RECORD TO CACHE");
}
} catch (TimeoutException e) {
setter.cancel(false);
System.out.println("Timeout to memcached exceeded.");
throw e;
} catch (Exception e) {
setter.cancel(false);
System.out.println("UNHANDLED CONCURRENCY EXCPETION");
throw e;
}
} else {
// System.out.println("KEY "+cacheKey+" CACHE HIT");
hitCounter++;
PlaytimeRow cachedPtr = (PlaytimeRow)cachedObject;
if (ptr.getLastSave() > cachedPtr.getLastSave()) {
// System.out.println("PULLED RECORD IS NEWER THAN CACHE.");
Future<Boolean> setter = mcc.set(cacheKey, 0, ptr);
try {
Boolean success = setter.get(5, TimeUnit.SECONDS);
if (success) {
// System.out.println("WROTE UPDATED RECORD TO CACHE");
updateCounter++;
int diff = ptr.getValue() - cachedPtr.getValue();
ptr.setValue(diff);
alPtr.add(ptr);
}
} catch (TimeoutException e) {
setter.cancel(false);
System.out.println("Timeout to memcached exceeded.");
throw e;
} catch (Exception e) {
setter.cancel(false);
System.out.println("UNHANDLED CONCURRENCY EXCPETION");
throw e;
}
} else {
// System.out.println("DUPLICATE RECORD.");
dupeCounter++;
}
}
}
mcc.shutdown();
System.out.println(playtimeEvents.size() + " playtime events processed.");
System.out.println(hitCounter + " cache hits.");
System.out.println(dupeCounter + " duplicate events.");
System.out.println(updateCounter + " events updated.");
int newRows = playtimeEvents.size() - dupeCounter;
float percentWarm = (float)updateCounter / (float)newRows * 100;
if (Float.isNaN(percentWarm)) {
percentWarm = 0f;
}
System.out.println("Cache is "+percentWarm+"% warm.");
HashMap<String, Integer> results = new HashMap<String,Integer>();
results.put("playtimeEvents", playtimeEvents.size());
results.put("newRows", newRows);
results.put("updateCounter", updateCounter);
results.put("dupeCounter", dupeCounter);
results.put("hitCounter", hitCounter);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
System.out.println("SOMETHING BLEW THE FUCK UP WHILE LOADING THE DRIVER");
}
java.sql.Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String insertString = "insert into v2_playtimes (character_id, item_id, last_save, vehicle_id, diff, faction_id, br_value) values (?,?,?,?,?,?,?)";
String logString = "insert into v2_api_pulls (timestamp, dupes, first_timestamp_pulled, last_timestamp_pulled, write_time, `interval`, stat, cache_heat, updated_rows) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
String dbServer = props.getProperty("db_server");
String database = props.getProperty("database");
String dbUsername = props.getProperty("db_username");
String dbPassword = props.getProperty("db_password");
int minTimestamp=2000000000;
int maxTimestamp=0;
long startTimeMillis = System.currentTimeMillis();
int duration = 0;
int[] updateCounts=new int[0];
try {
conn = DriverManager.getConnection("jdbc:mysql://"+dbServer+"/"+database+"?user="+dbUsername+"&password="+dbPassword);
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(insertString);
for (PlaytimeRow ptr : alPtr) {
pstmt.setLong(1,ptr.getCharacterId());
pstmt.setInt(2,ptr.getItemId());
pstmt.setLong(3,ptr.getLastSave());
pstmt.setInt(4,ptr.getVehicleId());
pstmt.setInt(5,ptr.getValue());
pstmt.setInt(6,ptr.getFactionId());
pstmt.setInt(7,ptr.getBrValue());
pstmt.addBatch();
//System.out.println("for shits, last save is "+ptr.getLastSave());
if (ptr.getLastSave() < minTimestamp) {
minTimestamp = ptr.getLastSave();
}
if (ptr.getLastSave() > maxTimestamp) {
maxTimestamp = ptr.getLastSave();
}
}
updateCounts = pstmt.executeBatch();
} catch (BatchUpdateException ex) {
System.out.println("Batch Update Exception - dupes in load stream.");
updateCounts = ex.getUpdateCounts();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
}
conn.commit();
long endTimeMillis = System.currentTimeMillis();
duration = (int)(endTimeMillis-startTimeMillis);
try {
pstmt = conn.prepareStatement(logString);
pstmt.setInt(1, pullTimestamp);
pstmt.setInt(2, dupeCounter);
pstmt.setInt(3, minTimestamp);
pstmt.setInt(4, maxTimestamp);
pstmt.setInt(5, duration);
pstmt.setLong(6, interval);
pstmt.setString(7, "playtimes");
pstmt.setFloat(8, percentWarm);
pstmt.setInt(9, updateCounter);
pstmt.executeUpdate();
conn.commit();
} catch (BatchUpdateException ex) {
System.out.println("Batch Update Exception: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
throw ex;
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException sqlEx) { } // ignore
pstmt = null;
}
}
return results;
}
}