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));
}
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();
}
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;
});
}
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));
});
}
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();
}));
}
Aggregations