-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FieldTypesResource.java
104 lines (94 loc) · 4.59 KB
/
FieldTypesResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.graylog.plugins.enterprise.search.rest;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.graylog2.database.NotFoundException;
import org.graylog2.indexer.fieldtypes.FieldTypeDTO;
import org.graylog2.indexer.fieldtypes.FieldTypeMapper;
import org.graylog2.indexer.fieldtypes.FieldTypes;
import org.graylog2.indexer.fieldtypes.IndexFieldTypesDTO;
import org.graylog2.indexer.fieldtypes.IndexFieldTypesService;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.shared.rest.resources.RestResource;
import org.graylog2.streams.StreamService;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.google.common.collect.ImmutableSet.of;
import static org.graylog2.indexer.fieldtypes.FieldTypes.Type.createType;
@Api(value = "Enterprise/Field Types", description = "Field Types")
@Path("/fields")
@Produces(MediaType.APPLICATION_JSON)
@RequiresAuthentication
public class FieldTypesResource extends RestResource implements PluginRestResource {
private final IndexFieldTypesService indexFieldTypesService;
private final StreamService streamService;
private final FieldTypeMapper fieldTypeMapper;
private static final FieldTypes.Type UNKNOWN_TYPE = createType("unknown", of());
@Inject
public FieldTypesResource(IndexFieldTypesService indexFieldTypesService, StreamService streamService, FieldTypeMapper fieldTypeMapper) {
this.indexFieldTypesService = indexFieldTypesService;
this.streamService = streamService;
this.fieldTypeMapper = fieldTypeMapper;
}
private Set<MappedFieldTypeDTO> mergeCompoundFieldTypes(Stream<MappedFieldTypeDTO> stream) {
return stream.collect(Collectors.groupingBy(MappedFieldTypeDTO::name, Collectors.toSet()))
.entrySet()
.stream()
.map(entry -> {
final Set<MappedFieldTypeDTO> fieldTypes = entry.getValue();
final String fieldName = entry.getKey();
if (fieldTypes.size() == 1) {
return fieldTypes.iterator().next();
}
final String compoundFieldType = "compound(" + fieldTypes.stream()
.map(mappedFieldTypeDTO -> mappedFieldTypeDTO.type().type())
.collect(Collectors.joining(",")) + ")";
final ImmutableSet<String> commonProperties = fieldTypes.stream()
.map(mappedFieldTypeDTO -> mappedFieldTypeDTO.type().properties())
.reduce((s1, s2) -> Sets.intersection(s1, s2).immutableCopy())
.orElse(ImmutableSet.of());
return MappedFieldTypeDTO.create(fieldName, createType(compoundFieldType, commonProperties));
})
.collect(Collectors.toSet());
}
private MappedFieldTypeDTO mapPhysicalFieldType(FieldTypeDTO fieldType) {
final FieldTypes.Type mappedFieldType = fieldTypeMapper.mapType(fieldType.physicalType()).orElse(UNKNOWN_TYPE);
return MappedFieldTypeDTO.create(fieldType.fieldName(), mappedFieldType);
}
@GET
public Set<MappedFieldTypeDTO> allFieldTypes() {
return mergeCompoundFieldTypes(indexFieldTypesService.findAll()
.stream()
.map(IndexFieldTypesDTO::fields)
.flatMap(Collection::stream)
.map(this::mapPhysicalFieldType));
}
@POST
public Set<MappedFieldTypeDTO> byStreams(FieldTypesForStreamsRequest request) {
return mergeCompoundFieldTypes(request.streams()
.stream()
.map(streamId -> {
try {
return streamService.load(streamId);
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
})
.filter(Objects::nonNull)
.map(indexSet -> indexSet.getIndexSet().getConfig().id())
.flatMap(indexSetId -> this.indexFieldTypesService.findForIndexSet(indexSetId).stream())
.map(IndexFieldTypesDTO::fields)
.flatMap(Collection::stream)
.map(this::mapPhysicalFieldType));
}
}