try {
List<JSONObject> jsonInventoryList = client.checks();
for (Iterator<JSONObject> i = jsonInventoryList.iterator(); i.hasNext();) {
JSONObject jsonInventory = i.next();
InventoryItem inventoryItem = inventoryItemMap.get(jsonInventory.getString("id"));
if (inventoryItem == null) {
continue;
}
// the connection may not have been tested yet from Pingdom
if (!jsonInventory.containsKey("lasttesttime")) {
continue;
}
Date lastTestTime = new DateTime().withMillis(0).plusSeconds(jsonInventory.getInt("lasttesttime")).toDate();
if (lastActivityPoll != null && lastActivityPoll.after(lastTestTime)) {
continue;
}
lastActivityPoll = lastTestTime.after(lastActivityPoll) ? lastTestTime : lastActivityPoll;
Map<String, Object> eventContext = new HashMap<>();
eventContext.put("activityPubDate", lastTestTime);
eventContext.put("activityTitle", String.format("Service check %s (%s) has a response time of %dms.",
jsonInventory.getString("name"),
jsonInventory.getString("type").toUpperCase(),
jsonInventory.getInt("lastresponsetime")));
eventContext.put("payload", jsonInventory);
if (jsonInventory.containsKey("lastresponsetime")) {
eventContext.put("lastResponseTime", jsonInventory.getInt("lastresponsetime"));
}
Event event = eventService.createEvent(EventId.ACTIVITY,
inventoryItem,
eventContext);
PingdomEntryDetails details = new PingdomEntryDetails.Builder()
.lastErrorTime(jsonInventory.containsKey("lasterrortime") ? jsonInventory.getInt("lasterrortime") : 0)
.lastResponseTime(jsonInventory.containsKey("lastresponsetime") ? jsonInventory.getInt("lastresponsetime") : 0)
.lastTestTime(jsonInventory.getInt("lasttesttime"))
.checkCreated(jsonInventory.getInt("created"))
.resolution(jsonInventory.getInt("resolution"))
.status(jsonInventory.getString("status"))
.build();
// Create a new message to be delivered to inboxes
messageService.sendAccountMessage(event,
inventoryItem,
connection,
lastTestTime.getTime(),
MessageType.ACTIVITY,
inventoryItem.getHashtags(),
details);
}
// Update the connection's last polling time
connection.setLastActivityPollDate(new Date(lastActivityPoll.getTime() + 1));