Skip to content

Commit

Permalink
added AddressMetric used in SourceMetrics and TargetMetrics
Browse files Browse the repository at this point in the history
* those contain metrics about a single sourceAddress or targetAddress
* gathered the information when ConnectionMetrics are requested

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Mar 23, 2018
1 parent c15cd60 commit 780cc86
Show file tree
Hide file tree
Showing 17 changed files with 663 additions and 266 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.model.connectivity;

import java.util.Optional;

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.model.base.json.Jsonifiable;

/**
* TODO doc
*/
public interface AddressMetric extends Jsonifiable.WithFieldSelectorAndPredicate<JsonField> {

/**
*
* @return
*/
ConnectionStatus getStatus();

/**
*
* @return
*/
Optional<String> getStatusDetails();

/**
*
* @return
*/
long getMessageCount();

/**
* Returns all non hidden marked fields of this {@code AddressMetric}.
*
* @return a JSON object representation of this Source including only non hidden marked fields.
*/
@Override
default JsonObject toJson() {
return toJson(FieldType.notHidden());
}

@Override
default JsonObject toJson(final JsonSchemaVersion schemaVersion, final JsonFieldSelector fieldSelector) {
return toJson(schemaVersion, FieldType.notHidden()).get(fieldSelector);
}

/**
* An enumeration of the known {@code JsonField}s of an {@code AddressMetric}.
*/
@Immutable
final class JsonFields {

/**
* JSON field containing the {@code JsonSchemaVersion}.
*/
public static final JsonFieldDefinition<Integer> SCHEMA_VERSION =
JsonFactory.newIntFieldDefinition(JsonSchemaVersion.getJsonKey(), FieldType.SPECIAL, FieldType.HIDDEN,
JsonSchemaVersion.V_1, JsonSchemaVersion.V_2);

/**
* JSON field containing the {@code ConnectionStatus} value.
*/
public static final JsonFieldDefinition<String> STATUS =
JsonFactory.newStringFieldDefinition("status", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);

/**
* JSON field containing the {@code ConnectionStatus} details.
*/
public static final JsonFieldDefinition<String> STATUS_DETAILS =
JsonFactory.newStringFieldDefinition("statusDetails", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);

/**
* JSON field containing the amount of consumed/published messages.
*/
public static final JsonFieldDefinition<Long> MESSAGE_COUNT =
JsonFactory.newLongFieldDefinition("messageCount", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);

private JsonFields() {
throw new AssertionError();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,53 @@ public static ConnectionMetrics newConnectionMetrics(final ConnectionStatus conn
/**
* Returns a new {@code SourceMetrics}.
*
* @param addresses the addresses of the source
* @param consumerCount the amount of consumers started for the source
* @param status the ConnectionStatus of the source metrics to create
* @param statusDetails the optional details about the connection status
* @param consumedMessages the current state of the Client performing the connection
* @param addressMetrics the AddressMetrics of all addresses in the source
* @param consumedMessages the amount of consumed messages
* @return a new SourceMetrics which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code connectionStatus} is {@code null}.
*/
public static SourceMetrics newSourceMetrics(final Set<String> addresses, final int consumerCount,
final ConnectionStatus status, @Nullable final String statusDetails, final long consumedMessages) {
return ImmutableSourceMetrics.of(addresses, consumerCount, status, statusDetails, consumedMessages);
public static SourceMetrics newSourceMetrics(final Map<String, AddressMetric> addressMetrics,
final long consumedMessages) {
return ImmutableSourceMetrics.of(addressMetrics, consumedMessages);
}

/**
* Returns a new {@code TargetMetrics}.
*
* @param address the address of the target
* @param topics the topics of the target
* @param status the ConnectionStatus of the source metrics to create
* @param statusDetails the optional details about the connection status
* @param consumedMessages the current state of the Client performing the connection
* @param addressMetrics the AddressMetrics of all addresses in the target
* @param consumedMessages the amount of consumed messages
* @return a new SourceMetrics which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code connectionStatus} is {@code null}.
*/
public static TargetMetrics newTargetMetrics(final String address, final Set<String> topics,
final ConnectionStatus status, @Nullable final String statusDetails, final long consumedMessages) {
return ImmutableTargetMetrics.of(address, topics, status, statusDetails, consumedMessages);
public static TargetMetrics newTargetMetrics(final Map<String, AddressMetric> addressMetrics,
final long consumedMessages) {
return ImmutableTargetMetrics.of(addressMetrics, consumedMessages);
}

/**
* Returns a new {@code AddressMetric}.
*
* @param status the ConnectionStatus of the source metrics to create
* @param statusDetails the optional details about the connection status
* @param messageCount the amount of totally consumed/published messages
* @return a new AddressMetric which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if any parameter is {@code null}.
*/
public static AddressMetric newAddressMetric(final ConnectionStatus status, @Nullable final String statusDetails,
final long messageCount) {
return ImmutableAddressMetric.of(status, statusDetails, messageCount);
}

/**
* Creates a new {@code AddressMetric} object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the Connection to be created.
* @return a new AddressMetric which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object.
*/
public static AddressMetric addressMetricFromJson(final JsonObject jsonObject) {
return ImmutableAddressMetric.fromJson(jsonObject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.model.connectivity;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;

/**
* Immutable implementation of {@link AddressMetric}.
*/
@Immutable
final class ImmutableAddressMetric implements AddressMetric {

private final ConnectionStatus status;
@Nullable private final String statusDetails;
private final long messageCount;

private ImmutableAddressMetric(final ConnectionStatus status, @Nullable final String statusDetails,
final long messageCount) {
this.status = status;
this.statusDetails = statusDetails;
this.messageCount = messageCount;
}

/**
* TODO Doc
*/
public static ImmutableAddressMetric of(final ConnectionStatus status, @Nullable final String statusDetails,
final long consumedMessages) {
return new ImmutableAddressMetric(status, statusDetails, consumedMessages);
}

@Override
public ConnectionStatus getStatus() {
return status;
}

@Override
public Optional<String> getStatusDetails() {
return Optional.ofNullable(statusDetails);
}

@Override
public long getMessageCount() {
return messageCount;
}

@Override
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder();

jsonObjectBuilder.set(JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate);
jsonObjectBuilder.set(JsonFields.STATUS, status.getName(), predicate);
if (statusDetails != null) {
jsonObjectBuilder.set(JsonFields.STATUS_DETAILS, statusDetails, predicate);
}
jsonObjectBuilder.set(JsonFields.MESSAGE_COUNT, messageCount, predicate);
return jsonObjectBuilder.build();
}

/**
* Creates a new {@code AddressMetric} object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the AddressMetric to be created.
* @return a new AddressMetric which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object.
*/
public static AddressMetric fromJson(final JsonObject jsonObject) {
final ConnectionStatus readConnectionStatus = ConnectionStatus.forName(
jsonObject.getValueOrThrow(JsonFields.STATUS)).orElse(ConnectionStatus.UNKNOWN);
final String readConnectionStatusDetails = jsonObject.getValue(JsonFields.STATUS_DETAILS)
.orElse(null);
final long readConsumedMessages = jsonObject.getValueOrThrow(JsonFields.MESSAGE_COUNT);
return ImmutableAddressMetric.of(readConnectionStatus, readConnectionStatusDetails, readConsumedMessages);
}

@Override
public boolean equals(final Object o) {
if (this == o) {return true;}
if (!(o instanceof ImmutableAddressMetric)) {return false;}
final ImmutableAddressMetric that = (ImmutableAddressMetric) o;
return status == that.status &&
Objects.equals(statusDetails, that.statusDetails) &&
messageCount == that.messageCount;
}

@Override
public int hashCode() {
return Objects.hash(status, statusDetails, messageCount);
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"status=" + status +
", statusDetails=" + statusDetails +
", messageCount=" + messageCount +
"]";
}
}
Loading

0 comments on commit 780cc86

Please sign in to comment.