Skip to content

Commit

Permalink
Initial work on obtaining units
Browse files Browse the repository at this point in the history
  • Loading branch information
luk-kaminski committed Apr 26, 2024
1 parent 861a098 commit a93faa7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.plugins.formatting.units.fields;

import org.graylog.plugins.formatting.units.model.UnitView;

import java.util.List;
import java.util.Optional;

public class FieldUnitObtainer {

private final List<FieldUnitObtainingMethod> prioritizedMethodList = List.of(new HardcodedFieldUnitObtainingMethod());

public Optional<UnitView> obtainUnit(final String fieldName) {
return prioritizedMethodList.stream()
.map(fieldUnitObtainingMethod -> fieldUnitObtainingMethod.obtainUnit(fieldName))
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.plugins.formatting.units.fields;

import org.graylog.plugins.formatting.units.model.UnitView;

import java.util.Optional;

public interface FieldUnitObtainingMethod {

Optional<UnitView> obtainUnit(final String fieldName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.plugins.formatting.units.fields;

import org.graylog.plugins.formatting.units.model.BaseUnitView;
import org.graylog.plugins.formatting.units.model.Conversion;
import org.graylog.plugins.formatting.units.model.DerivedUnit;
import org.graylog.plugins.formatting.units.model.UnitView;

import java.util.Optional;

import static org.graylog.plugins.formatting.units.model.Conversion.ConversionAction.DIVIDE;
import static org.graylog2.plugin.Message.FIELD_GL2_ACCOUNTED_MESSAGE_SIZE;
import static org.graylog2.plugin.Message.FIELD_GL2_PROCESSING_DURATION_MS;

public class HardcodedFieldUnitObtainingMethod implements FieldUnitObtainingMethod {

@Override
public Optional<UnitView> obtainUnit(final String fieldName) {
//TODO: complete list
//TODO: take units from DB instead of creating from scratch
return switch (fieldName) {
case FIELD_GL2_PROCESSING_DURATION_MS ->
Optional.of(new DerivedUnit("ms", "millisecond", "time", new Conversion(1000, DIVIDE)));
case FIELD_GL2_ACCOUNTED_MESSAGE_SIZE -> Optional.of(new BaseUnitView("b", "byte", "size"));
default -> Optional.empty();
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import jakarta.inject.Inject;
import org.graylog.plugins.formatting.units.fields.FieldUnitObtainer;
import org.graylog.plugins.views.search.elasticsearch.IndexLookup;
import org.graylog.plugins.views.search.rest.MappedFieldTypeDTO;
import org.graylog2.Configuration;
Expand All @@ -43,19 +44,22 @@ public class MappedFieldTypesServiceImpl implements MappedFieldTypesService {
private final FieldTypeMapper fieldTypeMapper;
private final IndexLookup indexLookup;
private final boolean streamAwareFieldTypes;
private final FieldUnitObtainer fieldUnitObtainer;


@Inject
public MappedFieldTypesServiceImpl(final Configuration configuration,
final StreamService streamService,
final IndexFieldTypesService indexFieldTypesService,
final FieldTypeMapper fieldTypeMapper,
final IndexLookup indexLookup) {
final IndexLookup indexLookup,
final FieldUnitObtainer fieldUnitObtainer) {
this.streamService = streamService;
this.indexFieldTypesService = indexFieldTypesService;
this.fieldTypeMapper = fieldTypeMapper;
this.indexLookup = indexLookup;
this.streamAwareFieldTypes = configuration.maintainsStreamAwareFieldTypes();
this.fieldUnitObtainer = fieldUnitObtainer;
}

@Override
Expand All @@ -75,7 +79,9 @@ public Set<MappedFieldTypeDTO> fieldTypesByStreamIds(Collection<String> streamId

private MappedFieldTypeDTO mapPhysicalFieldType(FieldTypeDTO fieldType) {
final FieldTypes.Type mappedFieldType = fieldTypeMapper.mapType(fieldType).orElse(UNKNOWN_TYPE);
return new MappedFieldTypeDTO(fieldType.fieldName(), mappedFieldType);
return new MappedFieldTypeDTO(fieldType.fieldName(),
mappedFieldType,
fieldUnitObtainer.obtainUnit(fieldType.fieldName()).orElse(null));
}

private Set<MappedFieldTypeDTO> mergeCompoundFieldTypes(java.util.stream.Stream<MappedFieldTypeDTO> stream) {
Expand Down Expand Up @@ -104,7 +110,9 @@ private Set<MappedFieldTypeDTO> mergeCompoundFieldTypes(java.util.stream.Stream<
final Set<String> properties = distinctTypes.size() > 1
? Sets.union(commonProperties, Collections.singleton(PROP_COMPOUND_TYPE))
: commonProperties;
return new MappedFieldTypeDTO(fieldName, createType(resultingFieldType, properties));
return new MappedFieldTypeDTO(fieldName,
createType(resultingFieldType, properties),
fieldUnitObtainer.obtainUnit(fieldName).orElse(null));

})
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.graylog.plugins.formatting.units.fields.FieldUnitObtainer;
import org.graylog.plugins.views.search.elasticsearch.IndexLookup;
import org.graylog.plugins.views.search.rest.MappedFieldTypeDTO;
import org.graylog2.Configuration;
Expand Down Expand Up @@ -56,6 +57,9 @@ public class MappedFieldTypesServiceImplTest {
@Mock
private IndexFieldTypesService indexFieldTypesService;

@Mock
private FieldUnitObtainer fieldUnitObtainer;

@Mock
private IndexLookup indexLookup;

Expand All @@ -71,7 +75,7 @@ public class MappedFieldTypesServiceImplTest {
public void setUp() throws Exception {
final Configuration withStreamAwarenessOff = spy(new Configuration());
doReturn(false).when(withStreamAwarenessOff).maintainsStreamAwareFieldTypes();
this.mappedFieldTypesService = new MappedFieldTypesServiceImpl(withStreamAwarenessOff, streamService, indexFieldTypesService, new FieldTypeMapper(), indexLookup);
this.mappedFieldTypesService = new MappedFieldTypesServiceImpl(withStreamAwarenessOff, streamService, indexFieldTypesService, new FieldTypeMapper(), indexLookup, fieldUnitObtainer);
when(streamService.indexSetIdsByIds(Collections.singleton("stream1"))).thenReturn(Collections.singleton("indexSetId"));
when(streamService.indexSetIdsByIds(Collections.singleton("stream2"))).thenReturn(Collections.singleton("indexSetId"));
}
Expand All @@ -80,7 +84,7 @@ public void setUp() throws Exception {
public void testDifferenceBetweenStreamAwareAndUnawareFieldTypeRetrieval() {
final Configuration withStreamAwarenessOn = spy(new Configuration());
doReturn(true).when(withStreamAwarenessOn).maintainsStreamAwareFieldTypes();
MappedFieldTypesServiceImpl streamAwareMappedFieldTypesService = new MappedFieldTypesServiceImpl(withStreamAwarenessOn, streamService, indexFieldTypesService, new FieldTypeMapper(), indexLookup);
MappedFieldTypesServiceImpl streamAwareMappedFieldTypesService = new MappedFieldTypesServiceImpl(withStreamAwarenessOn, streamService, indexFieldTypesService, new FieldTypeMapper(), indexLookup, fieldUnitObtainer);

final List<IndexFieldTypesDTO> fieldTypes = ImmutableList.of(
createIndexTypes(
Expand Down

0 comments on commit a93faa7

Please sign in to comment.