Search in sources :

Example 11 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class MappingsDocumentTypeFactoryCustomizer method apply.

@Override
public Mono<Void> apply(Map.Entry<String, List<DocumentType>> entry) {
    List<Field> collect = entry.getValue().stream().flatMap(documentType -> {
        List<Field> sourceFields = documentType.getSourceFields();
        if (sourceFields == null) {
            return Stream.empty();
        }
        if (sourceFields.size() == 1) {
            Field field = sourceFields.get(0);
            if (field instanceof Field.FieldMappings) {
                return Stream.of(field);
            }
        }
        return sourceFields.stream().map(child -> Field.of(documentType.getName(), child));
    }).collect(Collectors.toList());
    if (collect.stream().allMatch(f -> f instanceof Field.FieldMappings)) {
        Map<String, Object> accumulator = new HashMap<>();
        for (Field field : collect) {
            deepMerge(accumulator, field.getExtra());
        }
        String pluginDriverName = entry.getKey();
        return _indexWriterEventPublisher.publishCreateIndexTemplate(IndexTemplateDTO.of(pluginDriverName + "_template", null, List.of("*-" + pluginDriverName + "-data"), _jsonFactory.toJson(accumulator), List.of("data"), 10));
    }
    Map<String, Object> objectNode = new HashMap<>();
    for (Field parent : collect) {
        Map<String, Object> fieldNode = _createFieldNode(parent);
        Map<String, Object> parentNodeWithName = fieldNode;
        Field child = parent.getChild();
        while (child != Field.FieldObj.NIL) {
            Map<String, Object> parentNode = (Map<String, Object>) parentNodeWithName.get(parent.getName());
            Map<String, Object> childNode = _createFieldNode(child);
            parentNode.put("properties", childNode);
            parentNodeWithName = childNode;
            parent = child;
            child = child.getChild();
        }
        objectNode = _merge(objectNode, fieldNode);
    }
    String pluginDriverName = entry.getKey();
    return _indexWriterEventPublisher.publishCreateIndexTemplate(IndexTemplateDTO.of(pluginDriverName + "_template", null, List.of("*-" + pluginDriverName + "-data"), _jsonFactory.toJson(Map.of("properties", objectNode)), List.of("data"), 10));
}
Also used : Field(io.openk9.plugin.driver.manager.api.Field) FieldType(io.openk9.plugin.driver.manager.api.FieldType) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HashMap(java.util.HashMap) IndexTemplateDTO(io.openk9.index.writer.model.IndexTemplateDTO) DocumentType(io.openk9.plugin.driver.manager.api.DocumentType) Collectors(java.util.stream.Collectors) JsonFactory(io.openk9.json.api.JsonFactory) DocumentTypeFactoryCustomizer(io.openk9.plugin.driver.manager.api.DocumentTypeFactoryCustomizer) Component(org.osgi.service.component.annotations.Component) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) IndexWriterEventPublisher(io.openk9.index.writer.mappings.publisher.api.IndexWriterEventPublisher) Reference(org.osgi.service.component.annotations.Reference) Field(io.openk9.plugin.driver.manager.api.Field) HashMap(java.util.HashMap) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class BasePluginDriver method invokeDataParser.

@Override
public Mono<Void> invokeDataParser(Datasource datasource, Date fromDate, Date toDate) {
    String jsonConfig = datasource.getJsonConfig();
    JsonNode jsonNode = getJsonFactory().fromJsonToJsonNode(jsonConfig);
    ObjectNode objectNode = jsonNode.toObjectNode();
    ObjectNode requestJson = getJsonFactory().createObjectNode();
    objectNode.stream().filter(e -> _containsKey(e.getKey())).forEach(e -> requestJson.set(e.getKey(), e.getValue()));
    requestJson.put("timestamp", fromDate.getTime());
    requestJson.put("datasourceId", datasource.getDatasourceId());
    Map<String, Object> headers = headersObject();
    Publisher<byte[]> request = getHttpClient().request(method(), path(), requestJson.toString(), headers);
    return Mono.from(request).then();
}
Also used : Arrays(java.util.Arrays) Datasource(io.openk9.model.Datasource) Date(java.util.Date) JsonNode(io.openk9.json.api.JsonNode) Publisher(org.reactivestreams.Publisher) Map(java.util.Map) ObjectNode(io.openk9.json.api.ObjectNode) Mono(reactor.core.publisher.Mono) HttpClient(io.openk9.http.client.HttpClient) Collectors(java.util.stream.Collectors) JsonFactory(io.openk9.json.api.JsonFactory) ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Example 13 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class BaseNerEnrichProcessor method _getEntityOrCreate.

private Mono<JsonNode> _getEntityOrCreate(ObjectNode jsonNode) {
    return _entityManagerClient.getOrAddEntities(_jsonFactory.fromJsonNode(jsonNode, Request.class)).map(httpResponse -> {
        List<Response> responseList = httpResponse.getResponse();
        JsonNode entitiesJsonNode = jsonNode.get(entitiesField());
        ArrayNode entitiesArrayNode = entitiesJsonNode.toArrayNode();
        ArrayNode arrayNode = _jsonFactory.createArrayNode();
        for (JsonNode node : entitiesArrayNode) {
            Optional<Response> responseOptional = responseList.stream().filter(response -> node.get("tmpId").asLong() == response.getTmpId()).findFirst();
            if (responseOptional.isPresent()) {
                Entity entity = responseOptional.get().getEntity();
                ObjectNode result = _jsonFactory.createObjectNode();
                result.put("entityType", entity.getType());
                result.put("id", entity.getId());
                result.put("context", node.get("context"));
                arrayNode.add(result);
            }
        }
        return arrayNode;
    });
}
Also used : Response(io.openk9.entity.manager.model.payload.Response) EnrichItem(io.openk9.model.EnrichItem) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) Response(io.openk9.entity.manager.model.payload.Response) JsonNode(io.openk9.json.api.JsonNode) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) ArrayNode(io.openk9.json.api.ArrayNode) JsonFactory(io.openk9.json.api.JsonFactory) DatasourceContext(io.openk9.model.DatasourceContext) List(java.util.List) EntityManagerClient(io.openk9.entity.manager.client.api.EntityManagerClient) Map(java.util.Map) Optional(java.util.Optional) Request(io.openk9.entity.manager.model.payload.Request) ObjectNode(io.openk9.json.api.ObjectNode) HttpClient(io.openk9.http.client.HttpClient) Constants(io.openk9.core.api.constant.Constants) Entity(io.openk9.entity.manager.model.Entity) ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode)

Example 14 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class BaseNerEnrichProcessor method process.

@Override
public Mono<ObjectNode> process(ObjectNode objectNode, DatasourceContext context, EnrichItem enrichItem, PluginDriverDTO pluginDriverName) {
    return Mono.defer(() -> {
        JsonNode datasourceConfiguration = _jsonFactory.fromJsonToJsonNode(enrichItem.getJsonConfig());
        if (!datasourceConfiguration.isObject()) {
            return Mono.error(new RuntimeException("jsonConfig must be an instance of ObjectNode " + datasourceConfiguration.toString()));
        }
        ObjectNode request = prepareRequestRawContent(objectNode, datasourceConfiguration.toObjectNode(), context, pluginDriverName);
        return Mono.from(_httpClient.request(getMethod(), getPath(), request.toString(), getHeaders())).retry(5).map(_jsonFactory::fromJsonToJsonNode).map(JsonNode::toObjectNode).map(jsonNodes -> {
            jsonNodes.put(Constants.TENANT_ID, context.getTenant().getTenantId());
            jsonNodes.put(Constants.DATASOURCE_ID, context.getDatasource().getDatasourceId());
            jsonNodes.put(Constants.CONTENT_ID, objectNode.get(Constants.CONTENT_ID));
            jsonNodes.put(Constants.RAW_CONTENT, objectNode.get(Constants.RAW_CONTENT));
            jsonNodes.put(Constants.INGESTION_ID, objectNode.get(Constants.INGESTION_ID));
            return jsonNodes;
        }).flatMap(this::_getEntityOrCreate).map(entities -> objectNode.set(entitiesField(), entities));
    });
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Example 15 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class SchedulerListHttpHandler method apply.

@Override
public Publisher<Void> apply(HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
    return _httpResponseWriter.write(httpResponse, Mono.defer(() -> {
        Map<String, ScheduleOptions> jobs;
        try {
            jobs = _scheduler.getJobs();
        } catch (SchedulerError schedulerError) {
            throw new SchedulerException(schedulerError);
        }
        Set<String> jobNames = jobs.keySet();
        Function<Datasource, Optional<String>> findJobName = datasource -> jobNames.stream().filter(e -> e.endsWith("-" + datasource.getName())).findFirst();
        return _datasourceRepository.findAll(true).concatMap(datasource -> {
            Optional<String> jobNameOptional = findJobName.apply(datasource);
            if (jobNameOptional.isPresent()) {
                ObjectNode objectNode = _jsonFactory.createObjectNode();
                objectNode.put("datasourceId", datasource.getDatasourceId());
                objectNode.put("scheduling", datasource.getScheduling());
                objectNode.put("datasourceName", datasource.getName());
                objectNode.put("jobName", jobNameOptional.get());
                return Mono.just(objectNode.toMap());
            } else {
                return Mono.empty();
            }
        }).collectList();
    }));
}
Also used : Function(java.util.function.Function) SchedulerError(org.apache.karaf.scheduler.SchedulerError) SchedulerException(io.openk9.sql.scheduler.web.exception.SchedulerException) Set(java.util.Set) ObjectNode(io.openk9.json.api.ObjectNode) Map(java.util.Map)

Aggregations

ObjectNode (io.openk9.json.api.ObjectNode)12 JsonNode (io.openk9.json.api.JsonNode)9 ArrayNode (io.openk9.json.api.ArrayNode)6 JsonFactory (io.openk9.json.api.JsonFactory)5 Map (java.util.Map)5 Mono (reactor.core.publisher.Mono)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Activate (org.osgi.service.component.annotations.Activate)3 Component (org.osgi.service.component.annotations.Component)3 Reference (org.osgi.service.component.annotations.Reference)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Entity (io.openk9.entity.manager.model.Entity)2 Request (io.openk9.entity.manager.model.payload.Request)2 Response (io.openk9.entity.manager.model.payload.Response)2 HttpClient (io.openk9.http.client.HttpClient)2 Binding (io.openk9.ingestion.api.Binding)2 ReceiverReactor (io.openk9.ingestion.api.ReceiverReactor)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2