/** * Same as {@code getAsString(path, null)} */ default String getAsString(String path) { return getAsString(path, null); }
/** * Same as {@code getAsString(path, null)} */ default String getAsString(String path) { return getAsString(path, null); }
@Override public Set<String> fetchIds() { try { String json = jsondb.getAsString(getCollectionPath(), new GetOptions().depth(1)); if (json != null) { Map<String,Boolean> map = Json.reader().forType(new TypeReference<Map<String,Boolean>>() {}).readValue(json); return map.keySet() .stream().map(path -> path.substring(path.indexOf(':') + 1)).collect(Collectors.toSet()); } else { return Collections.<String>emptySet(); } } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
/** * Obtains all RawMetrics entries in the DB for the current integration * * @param integrationId - the integrationId for which we are obtaining the metrics * @return a Map containing all RawMetrics entries for the current integration, * the key is either HISTORY or the podName. * @throws IOException */ @Override public Map<String,RawMetrics> getRawMetrics(String integrationId) throws IOException { //try to obtain all raw metrics in this integration Map<String,RawMetrics> metrics = new HashMap<>(); String path = path(integrationId); String json = jsonDB.getAsString(path); if (json != null) { metrics = Json.reader().forType(VALUE_TYPE_REF).readValue(json); } return metrics; }
/** * If Integrations get deleted we should also delete their metrics * * @param activeIntegrationIds * @throws IOException * @throws JsonMappingException */ @Override public void curate(Set<String> activeIntegrationIds) throws IOException, JsonMappingException { //1. Loop over all RawMetrics String json = jsonDB.getAsString(path(), new GetOptions().depth(1)); if (json != null) { Map<String,Boolean> metricsMap = Json.reader().forType(TYPE_REFERENCE).readValue(json); Set<String> rawIntegrationIds = metricsMap.keySet(); for (String rawIntId : rawIntegrationIds) { if (! activeIntegrationIds.contains(rawIntId)) { jsonDB.delete(path(rawIntId)); } } } }
/** * Persists the latest metrics of a live pod to the database. */ @Override public void persist(RawMetrics rawMetrics) { try { //persist the latest rawMetrics String path = path(rawMetrics.getIntegrationId(), rawMetrics.getPod()); String json = Json.writer().writeValueAsString(rawMetrics); if (jsonDB.exists(path)) { //only update if not the same (don't cause unnecessary and expensive writes) if (! jsonDB.getAsString(path).equals(json)) { jsonDB.update(path, json); } } else { jsonDB.set(path, json); } } catch (JsonProcessingException e) { LOGGER.error("Error persisting metrics!", e); } }