use of org.phoebus.olog.entity.Property in project phoebus-olog by Olog.
the class LogResource method groupLogEntries.
@SuppressWarnings("unused")
@PostMapping(value = "/group")
public void groupLogEntries(@RequestBody List<Long> logEntryIds) {
logger.log(Level.INFO, "Grouping log entries: " + logEntryIds.stream().map(id -> Long.toString(id)).collect(Collectors.joining(",")));
Property existingLogEntryGroupProperty = null;
List<Log> logs = new ArrayList<>();
// the same group. If not, throw exception.
synchronized (logGroupSyncObject) {
for (Long id : logEntryIds) {
Optional<Log> log = null;
try {
log = logRepository.findById(Long.toString(id));
} catch (ResponseStatusException exception) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Log id " + id + " not found");
}
Property logEntryGroupProperty = LogEntryGroupHelper.getLogEntryGroupProperty(log.get());
if (logEntryGroupProperty != null && existingLogEntryGroupProperty != null && !logEntryGroupProperty.getAttribute(LogEntryGroupHelper.ATTRIBUTE_ID).equals(existingLogEntryGroupProperty.getAttribute(LogEntryGroupHelper.ATTRIBUTE_ID))) {
logger.log(Level.INFO, "Grouping not allowed due to conflicting log entry groups.");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot group: at least two entries already contained in different groups");
}
if (logEntryGroupProperty != null) {
existingLogEntryGroupProperty = logEntryGroupProperty;
}
logs.add(log.get());
}
final Property logEntryGroupProperty;
// If no existing log entry group was found, create a new.
if (existingLogEntryGroupProperty == null) {
logEntryGroupProperty = LogEntryGroupHelper.createNewLogEntryProperty();
} else {
logEntryGroupProperty = existingLogEntryGroupProperty;
}
// Now update the log entries by adding the log group property. Except for those that already have it.
logs.forEach(log -> {
if (LogEntryGroupHelper.getLogEntryGroupProperty(log) == null) {
log.getProperties().add(logEntryGroupProperty);
logRepository.update(log);
}
});
}
}
use of org.phoebus.olog.entity.Property in project phoebus-olog by Olog.
the class PropertyRepository method deleteById.
@Override
public void deleteById(String propertyName) {
try {
UpdateResponse response = client.update(new UpdateRequest(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, propertyName).doc(jsonBuilder().startObject().field("state", State.Inactive).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
if (response.getResult().equals(Result.UPDATED)) {
BytesReference ref = client.get(new GetRequest(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
Property deletedProperty = mapper.readValue(ref.streamInput(), Property.class);
logger.log(Level.INFO, "Deleted property " + deletedProperty.toLogger());
}
} catch (DocumentMissingException e) {
logger.log(Level.SEVERE, "Failed to delete property: " + propertyName + " because it does not exist", e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Failed to delete property: " + propertyName + " because it does not exist");
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to delete property: " + propertyName, e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete property: " + propertyName);
}
}
use of org.phoebus.olog.entity.Property in project phoebus-olog by Olog.
the class PropertyRepository method findAllById.
@Override
public Iterable<Property> findAllById(Iterable<String> propertyNames) {
MultiGetRequest request = new MultiGetRequest();
for (String propertyName : propertyNames) {
request.add(new MultiGetRequest.Item(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, propertyName));
}
try {
List<Property> foundProperties = new ArrayList<Property>();
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
for (MultiGetItemResponse multiGetItemResponse : response) {
if (!multiGetItemResponse.isFailed()) {
GetResponse res = multiGetItemResponse.getResponse();
StreamInput str = res.getSourceAsBytesRef().streamInput();
foundProperties.add(mapper.readValue(str, Property.class));
}
}
return foundProperties;
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to find properties: " + propertyNames, e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Failed to find properties: " + propertyNames);
}
}
use of org.phoebus.olog.entity.Property in project phoebus-olog by Olog.
the class PropertyRepository method deleteAttribute.
public void deleteAttribute(String propertyName, String attributeName) {
try {
Property property = findById(propertyName).get();
if (property != null) {
property.setAttributes(property.getAttributes().stream().map(p -> {
if (p.getName().equals(attributeName)) {
p.setState(State.Inactive);
}
return p;
}).collect(Collectors.toSet()));
}
UpdateResponse response = client.update(new UpdateRequest(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, propertyName).doc(mapper.writeValueAsBytes(property), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
if (response.getResult().equals(Result.UPDATED)) {
BytesReference ref = client.get(new GetRequest(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
Property deletedProperty = mapper.readValue(ref.streamInput(), Property.class);
logger.log(Level.INFO, "Deleted property attribute" + deletedProperty.toLogger());
}
} catch (DocumentMissingException e) {
logger.log(Level.SEVERE, propertyName + " Does not exist and thus cannot be deleted");
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
use of org.phoebus.olog.entity.Property in project phoebus-olog by Olog.
the class PropertiesResourceTest method testFindById.
@Test
public void testFindById() throws Exception {
when(propertyRepository.findById("property1")).thenReturn(Optional.of(property1));
MockHttpServletRequestBuilder request = get("/" + OlogResourceDescriptors.PROPERTY_RESOURCE_URI + "/property1");
MvcResult result = mockMvc.perform(request).andExpect(status().isOk()).andReturn();
Property property = objectMapper.readValue(result.getResponse().getContentAsString(), Property.class);
assertEquals("property1", property.getName());
verify(propertyRepository, times(1)).findById("property1");
reset(propertyRepository);
}
Aggregations