Search in sources :

Example 1 with Position

use of org.eclipse.kapua.service.datastore.model.Position in project kapua by eclipse.

the class EsDocumentBuilder method getMessageBuilder.

private XContentBuilder getMessageBuilder(String accountName, Message message, String asset, String topicFull, String semTopic, String[] topicParts, String messageId, Date indexedOn, Date receivedOn) throws IOException, ParseException {
    XContentBuilder messageBuilder = XContentFactory.jsonBuilder().startObject().field(EsSchema.MESSAGE_TIMESTAMP, indexedOn).field(EsSchema.MESSAGE_RECEIVED_ON, // TODO Which field ??
    receivedOn).field(EsSchema.MESSAGE_IP_ADDRESS, "127.0.0.1").field(EsSchema.MESSAGE_ACCOUNT, accountName).field(EsSchema.MESSAGE_AS_NAME, asset).field(EsSchema.MESSAGE_SEM_TOPIC, semTopic).field(EsSchema.MESSAGE_TOPIC_PARTS, topicParts);
    Payload payload = message.getPayload();
    if (payload == null) {
        messageBuilder.endObject();
        return messageBuilder;
    }
    messageBuilder.field(EsSchema.MESSAGE_COLLECTED_ON, payload.getCollectedOn());
    Position kapuaPosition = payload.getPosition();
    if (kapuaPosition != null) {
        Map<String, Object> location = new HashMap<String, Object>();
        location.put("lon", kapuaPosition.getLongitude());
        location.put("lat", kapuaPosition.getLatitude());
        Map<String, Object> position = new HashMap<String, Object>();
        position.put(EsSchema.MESSAGE_POS_LOCATION, location);
        position.put(EsSchema.MESSAGE_POS_ALT, kapuaPosition.getAltitude());
        position.put(EsSchema.MESSAGE_POS_PRECISION, kapuaPosition.getPrecision());
        position.put(EsSchema.MESSAGE_POS_HEADING, kapuaPosition.getHeading());
        position.put(EsSchema.MESSAGE_POS_SPEED, kapuaPosition.getSpeed());
        position.put(EsSchema.MESSAGE_POS_TIMESTAMP, kapuaPosition.getTimestamp());
        position.put(EsSchema.MESSAGE_POS_SATELLITES, kapuaPosition.getSatellites());
        position.put(EsSchema.MESSAGE_POS_STATUS, kapuaPosition.getStatus());
        messageBuilder.field(EsSchema.MESSAGE_POS, position);
    }
    messageBuilder.field(EsSchema.MESSAGE_BODY, payload.getBody());
    Map<String, EsMetric> metricMappings = new HashMap<String, EsMetric>();
    List<EsMetricDocumentBuilder> metricBuilders = new ArrayList<EsMetricDocumentBuilder>();
    Map<String, Object> kapuaMetrics = payload.getMetrics();
    if (kapuaMetrics != null) {
        Map<String, Object> metrics = new HashMap<String, Object>();
        String[] metricNames = kapuaMetrics.keySet().toArray(new String[] {});
        for (String kapuaMetricName : metricNames) {
            Object metricValue = kapuaMetrics.get(kapuaMetricName);
            // ////////////////////
            // Sanitize field names. '.' is not allowed
            String esMetricName = EsUtils.normalizeMetricName(kapuaMetricName);
            String esType = EsUtils.getEsTypeFromValue(metricValue);
            String esTypeAcronim = EsUtils.getEsTypeAcronym(esType);
            EsMetric esMetric = new EsMetric();
            esMetric.setName(esMetricName);
            esMetric.setType(esType);
            // ////////////////////
            Map<String, Object> field = new HashMap<String, Object>();
            field.put(esTypeAcronim, metricValue);
            metrics.put(esMetricName, field);
            // each metric is potentially a dynamic field so report it a new mapping
            String mappedName = EsUtils.getMetricValueQualifier(esMetricName, esType);
            metricMappings.put(mappedName, esMetric);
            EsMetricDocumentBuilder metricBuilder = new EsMetricDocumentBuilder();
            String metric = getTopicMetricKey(topicFull, mappedName);
            metricBuilder.setId(metric);
            // TODO retrieve the uuid field
            metricBuilder.setContent(this.getMetricBuilder(accountName, asset, semTopic, mappedName, metricValue, indexedOn, messageId));
            metricBuilders.add(metricBuilder);
        }
        messageBuilder.field(EsSchema.MESSAGE_MTR, metrics);
    }
    messageBuilder.endObject();
    this.setMessageMetrics(metricMappings);
    this.setMetricBuilders(metricBuilders);
    return messageBuilder;
}
Also used : Position(org.eclipse.kapua.service.datastore.model.Position) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Payload(org.eclipse.kapua.service.datastore.model.Payload) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with Position

use of org.eclipse.kapua.service.datastore.model.Position in project kapua by eclipse.

the class MessageStoreServiceTest method testStore.

@Test
public void testStore() throws Exception {
    KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
    long accountSerial = (new Date()).getTime();
    AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
    MessageStoreService messageStoreService = locator.getService(MessageStoreService.class);
    DatastoreObjectFactory dsObjectFactory = locator.getFactory(DatastoreObjectFactory.class);
    MessageCreator messageCreator = dsObjectFactory.newMessageCreator();
    Payload messagePayload = dsObjectFactory.newPayload();
    Position messagePosition = dsObjectFactory.newPosition();
    Map<String, Object> metrics = new HashMap<String, Object>();
    Date now = new Date();
    messageCreator.setTimestamp(now);
    messageCreator.setReceivedOn(now);
    String topicName = String.format("%s/CLIENT001/APP01", accountCreator.getName());
    messageCreator.setTopic(topicName);
    metrics.put("metric_long", 1L);
    metrics.put("metric_string", "pippo");
    messagePayload.setMetrics(metrics);
    messagePayload.setCollectedOn(now);
    messagePosition.setAltitude(1.0);
    messagePosition.setTimestamp(now);
    messagePayload.setPosition(messagePosition);
    messagePayload.setMetrics(metrics);
    messageCreator.setPayload(messagePayload);
// FIXME store fails fix it !!
// StorableId messageId = messageStoreService.store(scopeId, messageCreator);
// 
// Message asserts
// assertNotNull(messageId);
// assertTrue(!messageId.toString().isEmpty());
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) DatastoreObjectFactory(org.eclipse.kapua.service.datastore.DatastoreObjectFactory) Position(org.eclipse.kapua.service.datastore.model.Position) HashMap(java.util.HashMap) MessageStoreService(org.eclipse.kapua.service.datastore.MessageStoreService) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) Date(java.util.Date) MessageCreator(org.eclipse.kapua.service.datastore.model.MessageCreator) Payload(org.eclipse.kapua.service.datastore.model.Payload) KapuaId(org.eclipse.kapua.model.id.KapuaId) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Payload (org.eclipse.kapua.service.datastore.model.Payload)2 Position (org.eclipse.kapua.service.datastore.model.Position)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 AccountCreator (org.eclipse.kapua.service.account.AccountCreator)1 DatastoreObjectFactory (org.eclipse.kapua.service.datastore.DatastoreObjectFactory)1 MessageStoreService (org.eclipse.kapua.service.datastore.MessageStoreService)1 MessageCreator (org.eclipse.kapua.service.datastore.model.MessageCreator)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Test (org.junit.Test)1