private Map<String, Object> marshalMetricInfo(MetricInfo metricInfo) throws ParseException { Map<String, Object> unmarshalledMetricInfo = new HashMap<>(); unmarshalledMetricInfo.put(MetricInfoSchema.METRIC_SCOPE_ID, metricInfo.getScopeId().toStringId()); unmarshalledMetricInfo.put(MetricInfoSchema.METRIC_CLIENT_ID, metricInfo.getClientId()); unmarshalledMetricInfo.put(MetricInfoSchema.METRIC_CHANNEL, metricInfo.getChannel()); Map<String, Object> unmarshalledMetricValue = new HashMap<>(); unmarshalledMetricValue.put(MetricInfoSchema.METRIC_MTR_NAME, metricInfo.getName()); unmarshalledMetricValue.put(MetricInfoSchema.METRIC_MTR_TYPE, DatastoreUtils.convertToClientMetricType(metricInfo.getMetricType())); unmarshalledMetricValue.put(MetricInfoSchema.METRIC_MTR_TIMESTAMP, KapuaDateUtils.formatDate(metricInfo.getFirstMessageOn())); unmarshalledMetricValue.put(MetricInfoSchema.METRIC_MTR_MSG_ID, metricInfo.getFirstMessageId().toString()); unmarshalledMetricInfo.put(MetricInfoSchema.METRIC_MTR, unmarshalledMetricValue); return unmarshalledMetricInfo; }
@Then("^The metric \"(.+)\" was last published in the list \"(.+)\" on \"(.+)\"$") public void checkLastPublishDateOfMetricInList(String metric, String lstKey, String date) throws Exception { MetricInfoListResult metList = (MetricInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (MetricInfo tmpMet : metList.getItems()) { if (tmpMet.getName().equals(metric)) { assertEquals(tmpMet.getLastMessageOn(), tmpCaptured); return; } } fail(String.format("There is no metric [%s]", metric)); }
private MetricInfo unmarshalMetricInfo(Map<String, Object> metricInfoMap) throws DatamodelMappingException, JsonParseException, JsonMappingException, IOException, ParseException { KapuaId scopeId = new KapuaEid(new BigInteger((String) metricInfoMap.get(MetricInfoSchema.METRIC_SCOPE_ID))); MetricInfo metricInfo = new MetricInfoImpl(scopeId); String id = (String) metricInfoMap.get(ModelContext.DATASTORE_ID_KEY); metricInfo.setId(new StorableIdImpl(id)); @SuppressWarnings("unchecked") Map<String, Object> metricMap = (Map<String, Object>) metricInfoMap.get(MetricInfoSchema.METRIC_MTR); String name = (String) metricMap.get(MetricInfoSchema.METRIC_MTR_NAME); String type = (String) metricMap.get(MetricInfoSchema.METRIC_MTR_TYPE); String lastMsgTimestamp = (String) metricMap.get(MetricInfoSchema.METRIC_MTR_TIMESTAMP); String lastMsgId = (String) metricMap.get(MetricInfoSchema.METRIC_MTR_MSG_ID); String clientId = (String) metricInfoMap.get(MetricInfoSchema.METRIC_CLIENT_ID); String channel = (String) metricInfoMap.get(MetricInfoSchema.METRIC_CHANNEL); metricInfo.setClientId(clientId); metricInfo.setChannel(channel); metricInfo.setFirstMessageId(new StorableIdImpl(lastMsgId)); String metricName = DatastoreUtils.restoreMetricName(name); metricInfo.setName(metricName); Date timestamp = KapuaDateUtils.parseDate(lastMsgTimestamp); metricInfo.setFirstMessageOn(timestamp); metricInfo.setMetricType(DatastoreUtils.convertToKapuaType(type)); return metricInfo; }
/** * Get the metric identifier getting parameters from the metricInfo. Then it calls {@link MetricInfoField#getOrDeriveId(StorableId id, KapuaId scopeId, String clientId, String channel, String * metricName, Class metricType)} * * @param id * @param metricInfo * @return */ public static String getOrDeriveId(StorableId id, MetricInfo metricInfo) { return getOrDeriveId(id, metricInfo.getScopeId(), metricInfo.getClientId(), metricInfo.getChannel(), metricInfo.getName(), metricInfo.getMetricType()); }
sort.add(SortField.descending(MessageSchema.MESSAGE_TIMESTAMP)); MessageQuery messageQuery = new MessageQueryImpl(metricInfo.getScopeId()); messageQuery.setAskTotalCount(true); messageQuery.setFetchStyle(StorableFetchStyle.FIELDS); messageQuery.setSortFields(sort); RangePredicate messageIdPredicate = new RangePredicateImpl(new StorableFieldImpl(MetricInfoSchema.METRIC_MTR_TIMESTAMP), metricInfo.getFirstMessageOn(), null); TermPredicate clientIdPredicate = storablePredicateFactory.newTermPredicate(MessageField.CLIENT_ID, metricInfo.getClientId()); ExistsPredicate metricPredicate = new ExistsPredicateImpl(MessageField.METRICS.field(), metricInfo.getName()); } else if (messageList.isEmpty()) { LOG.warn("Cannot find last timestamp for the specified client id '{}' - account '{}'", metricInfo.getClientId(), metricInfo.getScopeId()); } else { LOG.error("Cannot find last timestamp for the specified client id '{}' - account '{}'. More than one result returned by the query!", metricInfo.getClientId(), metricInfo.getScopeId()); metricInfo.setLastMessageId(lastPublishedMessageId); metricInfo.setLastMessageOn(lastPublishedMessageTimestamp);
ConfigurationException, ClientException { ArgumentValidator.notNull(metricInfo, "metricInfo"); ArgumentValidator.notNull(metricInfo.getScopeId(), "metricInfo.scopeId"); ArgumentValidator.notNull(metricInfo.getFirstMessageId(), "metricInfoCreator.firstPublishedMessageId"); ArgumentValidator.notNull(metricInfo.getFirstMessageOn(), "metricInfoCreator.firstPublishedMessageTimestamp"); String metricInfoId = MetricInfoField.getOrDeriveId(metricInfo.getId(), metricInfo); StorableId storableId = new StorableIdImpl(metricInfoId); MetricInfo storedField = find(metricInfo.getScopeId(), storableId); if (storedField == null) { Metadata metadata = mediator.getMetadata(metricInfo.getScopeId(), metricInfo.getFirstMessageOn().getTime()); String kapuaIndexName = metadata.getRegistryIndexName(); UpdateRequest request = new UpdateRequest(metricInfo.getId().toString(), new TypeDescriptor(metadata.getRegistryIndexName(), MetricInfoSchema.METRIC_TYPE_NAME), metricInfo); response = client.upsert(request);
String metricInfoId = MetricInfoField.getOrDeriveId(metricInfo.getId(), metricInfo); MetricInfo storedField = find(metricInfo.getScopeId(), storableId); if (storedField != null) { DatastoreCacheManager.getInstance().getMetricsCache().put(metricInfoId, true); Metadata metadata = mediator.getMetadata(metricInfo.getScopeId(), metricInfo.getFirstMessageOn().getTime()); bulkRequest.add( new UpdateRequest(metricInfo.getId().toString(), new TypeDescriptor(metadata.getRegistryIndexName(), MetricInfoSchema.METRIC_TYPE_NAME), metricInfo));
@Then("^The metric \"(.+)\" was first published in the list \"(.+)\" on \"(.+)\"$") public void checkFirstPublishDateOfMetricInList(String metric, String lstKey, String date) throws Exception { MetricInfoListResult metList = (MetricInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (MetricInfo tmpMet : metList.getItems()) { if (tmpMet.getName().equals(metric)) { assertEquals(tmpMet.getFirstMessageOn(), tmpCaptured); return; } } fail(String.format("There is no metric [%s]", metric)); }
@Then("^Client \"(.+)\" first published a metric in the list \"(.+)\" on \"(.+)\"$") public void checkFirstPublishDateOfClientMetric(String clientId, String lstKey, String date) throws Exception { MetricInfoListResult metList = (MetricInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (MetricInfo tmpMet : metList.getItems()) { if (tmpMet.getClientId().equals(clientId)) { assertEquals(tmpMet.getFirstMessageOn(), tmpCaptured); return; } } fail(String.format("No metric matches the client id [%s]", clientId)); }
private void checkMetricInfoAgainstPreparedMessages(MetricInfoListResult metInfo, List<KapuaDataMessage> msgLst) { Set<String> msgMetrics = new HashSet<>(); Set<String> msgClients = new HashSet<>(); Set<String> infoMetrics = new HashSet<>(); Set<String> infoClients = new HashSet<>(); assertNotNull("No channel info data!", metInfo); assertNotNull("No messages to compare to!", msgLst); for (KapuaDataMessage tmpMsg : msgLst) { msgClients.add(tmpMsg.getClientId()); for (String tmpMet : tmpMsg.getPayload().getMetrics().keySet()) { msgMetrics.add(tmpMet); } } for (MetricInfo tmpMet : metInfo.getItems()) { infoClients.add(tmpMet.getClientId()); infoMetrics.add(tmpMet.getName()); } assertEquals("The number of clients does not match!", msgClients.size(), infoClients.size()); assertEquals("The number of topics does not match!", msgMetrics.size(), infoMetrics.size()); for (String tmpMetric : msgMetrics) { assertTrue(String.format("The topic [%s] is not found in the info list!", tmpMetric), infoMetrics.contains(tmpMetric)); } for (String tmpClient : msgClients) { assertTrue(String.format("The client id [%s] is not found in the info list!", tmpClient), infoClients.contains(tmpClient)); } }
private void isMetricForFirstMessageInStoreOK(StorableId msgId, Date storedOn) throws KapuaException { KapuaId tmpAccId = ((Account) stepData.get("LastAccount")).getId(); String tmpClId = ((Device) stepData.get("LastDevice")).getClientId(); AndPredicate andPredicate = new AndPredicateImpl(); andPredicate.getPredicates().add(new TermPredicateImpl(MetricInfoField.CLIENT_ID, tmpClId)); MetricInfoQuery metricInfoQuery = DatastoreQueryFactory.createBaseMetricInfoQuery(tmpAccId, 100); metricInfoQuery.setPredicate(andPredicate); metricInfoQuery.addFetchAttributes(MetricInfoField.TIMESTAMP_FULL.field()); MetricInfoListResult metricInfoList = metricInfoRegistryService.query(metricInfoQuery); assertNotNull("Cannot find the metric info registry!", metricInfoList); assertNotEquals("Cannot find the metric info registry!", metricInfoList.getSize(), 0); assertNotNull("Cannot find the metric info registry!", metricInfoList.getFirstItem()); assertEquals("Wrong metric info message id!", metricInfoList.getFirstItem().getFirstMessageId(), msgId); assertEquals("Wrong metric info message on!", metricInfoList.getFirstItem().getFirstMessageOn(), storedOn); }
@Then("^Client \"(.+)\" last published a metric in the list \"(.+)\" on \"(.+)\"$") public void checkLastPublishDateOfClientMetric(String clientId, String lstKey, String date) throws Exception { MetricInfoListResult metList = (MetricInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (MetricInfo tmpMet : metList.getItems()) { if (tmpMet.getClientId().equals(clientId)) { assertEquals(tmpMet.getLastMessageOn(), tmpCaptured); return; } } fail(String.format("No metric matches the client id [%s]", clientId)); }
@When("^I delete all metrics from the list \"(.*)\"$") public void deleteAllMetricsFromList(String lstKey) throws KapuaException { Account account = (Account) stepData.get("LastAccount"); MetricInfoListResult tmpList = (MetricInfoListResult) stepData.get(lstKey); for (MetricInfo tmpItem : tmpList.getItems()) { metricInfoRegistryServiceProxy.delete(account.getId(), tmpItem.getId()); } }
String id = metrics.getResult().get(i).getId().toString(); if (metricsCache.get(id)) { metricsCache.remove(id);