org.yamcs.client.archive.ArchiveClient Maven / Gradle / Ivy
package org.yamcs.client.archive;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.yamcs.api.MethodHandler;
import org.yamcs.api.Observer;
import org.yamcs.client.Command;
import org.yamcs.client.Helpers;
import org.yamcs.client.Page;
import org.yamcs.client.StreamReceiver;
import org.yamcs.client.StreamSender;
import org.yamcs.client.archive.ArchiveClient.ListOptions.AscendingOption;
import org.yamcs.client.archive.ArchiveClient.ListOptions.LimitOption;
import org.yamcs.client.archive.ArchiveClient.ListOptions.ListOption;
import org.yamcs.client.archive.ArchiveClient.ListOptions.NoRealtimeOption;
import org.yamcs.client.archive.ArchiveClient.ListOptions.NoRepeatOption;
import org.yamcs.client.archive.ArchiveClient.ListOptions.SourceOption;
import org.yamcs.client.archive.ArchiveClient.RangeOptions.MinimumGapOption;
import org.yamcs.client.archive.ArchiveClient.RangeOptions.MinimumRangeOption;
import org.yamcs.client.archive.ArchiveClient.RangeOptions.RangeOption;
import org.yamcs.client.archive.ArchiveClient.StreamOptions.CommandOption;
import org.yamcs.client.archive.ArchiveClient.StreamOptions.MergeTimeOption;
import org.yamcs.client.archive.ArchiveClient.StreamOptions.PacketOption;
import org.yamcs.client.archive.ArchiveClient.StreamOptions.StreamOption;
import org.yamcs.client.base.AbstractPage;
import org.yamcs.client.base.ResponseObserver;
import org.yamcs.protobuf.AlarmData;
import org.yamcs.protobuf.Archive.GetParameterSamplesRequest;
import org.yamcs.protobuf.Archive.ListParameterHistoryRequest;
import org.yamcs.protobuf.Archive.ListParameterHistoryResponse;
import org.yamcs.protobuf.Archive.StreamParameterValuesRequest;
import org.yamcs.protobuf.Commanding.CommandHistoryEntry;
import org.yamcs.protobuf.CommandsApiClient;
import org.yamcs.protobuf.Event;
import org.yamcs.protobuf.EventsApiClient;
import org.yamcs.protobuf.GetCommandRequest;
import org.yamcs.protobuf.GetParameterRangesRequest;
import org.yamcs.protobuf.IndexGroup;
import org.yamcs.protobuf.IndexResponse;
import org.yamcs.protobuf.IndexesApiClient;
import org.yamcs.protobuf.ListCommandHistoryIndexRequest;
import org.yamcs.protobuf.ListCommandsRequest;
import org.yamcs.protobuf.ListCommandsResponse;
import org.yamcs.protobuf.ListCompletenessIndexRequest;
import org.yamcs.protobuf.ListEventIndexRequest;
import org.yamcs.protobuf.ListEventSourcesRequest;
import org.yamcs.protobuf.ListEventSourcesResponse;
import org.yamcs.protobuf.ListEventsRequest;
import org.yamcs.protobuf.ListEventsResponse;
import org.yamcs.protobuf.ListPacketIndexRequest;
import org.yamcs.protobuf.ListParameterIndexRequest;
import org.yamcs.protobuf.PacketsApiClient;
import org.yamcs.protobuf.ParameterArchiveApiClient;
import org.yamcs.protobuf.Pvalue.ParameterData;
import org.yamcs.protobuf.Pvalue.ParameterValue;
import org.yamcs.protobuf.Pvalue.Ranges;
import org.yamcs.protobuf.Pvalue.Ranges.Range;
import org.yamcs.protobuf.Pvalue.TimeSeries;
import org.yamcs.protobuf.Pvalue.TimeSeries.Sample;
import org.yamcs.protobuf.StreamArchiveApiClient;
import org.yamcs.protobuf.StreamCommandIndexRequest;
import org.yamcs.protobuf.StreamCommandsRequest;
import org.yamcs.protobuf.StreamCompletenessIndexRequest;
import org.yamcs.protobuf.StreamEventIndexRequest;
import org.yamcs.protobuf.StreamEventsRequest;
import org.yamcs.protobuf.StreamPacketIndexRequest;
import org.yamcs.protobuf.StreamPacketsRequest;
import org.yamcs.protobuf.StreamParameterIndexRequest;
import org.yamcs.protobuf.Table.GetTableDataRequest;
import org.yamcs.protobuf.Table.ReadRowsRequest;
import org.yamcs.protobuf.Table.Row;
import org.yamcs.protobuf.Table.TableData;
import org.yamcs.protobuf.Table.TableData.TableRecord;
import org.yamcs.protobuf.Table.WriteRowsRequest;
import org.yamcs.protobuf.Table.WriteRowsResponse;
import org.yamcs.protobuf.TableApiClient;
import org.yamcs.protobuf.TmPacketData;
import org.yamcs.protobuf.Yamcs.ArchiveRecord;
import org.yamcs.protobuf.alarms.AlarmsApiClient;
import org.yamcs.protobuf.alarms.ListAlarmsRequest;
import org.yamcs.protobuf.alarms.ListAlarmsResponse;
import com.google.protobuf.Timestamp;
public class ArchiveClient {
private String instance;
private IndexesApiClient indexService;
private CommandsApiClient commandService;
private ParameterArchiveApiClient parameterArchiveService;
private StreamArchiveApiClient streamArchiveService;
private AlarmsApiClient alarmService;
private TableApiClient tableService;
private EventsApiClient eventService;
private PacketsApiClient packetService;
public ArchiveClient(MethodHandler handler, String instance) {
this.instance = instance;
indexService = new IndexesApiClient(handler);
commandService = new CommandsApiClient(handler);
parameterArchiveService = new ParameterArchiveApiClient(handler);
streamArchiveService = new StreamArchiveApiClient(handler);
alarmService = new AlarmsApiClient(handler);
tableService = new TableApiClient(handler);
eventService = new EventsApiClient(handler);
packetService = new PacketsApiClient(handler);
}
public String getInstance() {
return instance;
}
public CompletableFuture> listCommandIndex(Instant start, Instant stop, ListOption... options) {
ListCommandHistoryIndexRequest.Builder requestb = ListCommandHistoryIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (ListOption option : options) {
if (option instanceof LimitOption) {
requestb.setLimit(((LimitOption) option).limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new CommandIndexPage(requestb.build()).future();
}
public CompletableFuture> listPacketIndex(Instant start, Instant stop, ListOption... options) {
ListPacketIndexRequest.Builder requestb = ListPacketIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (ListOption option : options) {
if (option instanceof LimitOption) {
requestb.setLimit(((LimitOption) option).limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new PacketIndexPage(requestb.build()).future();
}
public CompletableFuture> listParameterIndex(Instant start, Instant stop, ListOption... options) {
ListParameterIndexRequest.Builder requestb = ListParameterIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (ListOption option : options) {
if (option instanceof LimitOption) {
requestb.setLimit(((LimitOption) option).limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new ParameterIndexPage(requestb.build()).future();
}
public CompletableFuture> listEventIndex(Instant start, Instant stop, ListOption... options) {
ListEventIndexRequest.Builder requestb = ListEventIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (ListOption option : options) {
if (option instanceof LimitOption) {
requestb.setLimit(((LimitOption) option).limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new EventIndexPage(requestb.build()).future();
}
public CompletableFuture> listCompletenessIndex(Instant start, Instant stop,
ListOption... options) {
ListCompletenessIndexRequest.Builder requestb = ListCompletenessIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (ListOption option : options) {
if (option instanceof LimitOption) {
requestb.setLimit(((LimitOption) option).limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new CompletenessIndexPage(requestb.build()).future();
}
public CompletableFuture streamPacketIndex(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamPacketIndexRequest.Builder requestb = StreamPacketIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof PacketOption) {
for (String packet : ((PacketOption) option).packets) {
requestb.addNames(packet);
}
} else if (option instanceof MergeTimeOption) {
requestb.setMergeTime(((MergeTimeOption) option).mergeTime);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
indexService.streamPacketIndex(null, requestb.build(), new Observer() {
@Override
public void next(ArchiveRecord message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamParameterIndex(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamParameterIndexRequest.Builder requestb = StreamParameterIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof MergeTimeOption) {
requestb.setMergeTime(((MergeTimeOption) option).mergeTime);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
indexService.streamParameterIndex(null, requestb.build(), new Observer() {
@Override
public void next(ArchiveRecord message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamCommandIndex(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamCommandIndexRequest.Builder requestb = StreamCommandIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof MergeTimeOption) {
requestb.setMergeTime(((MergeTimeOption) option).mergeTime);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
indexService.streamCommandIndex(null, requestb.build(), new Observer() {
@Override
public void next(ArchiveRecord message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamEventIndex(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamEventIndexRequest.Builder requestb = StreamEventIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof MergeTimeOption) {
requestb.setMergeTime(((MergeTimeOption) option).mergeTime);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
indexService.streamEventIndex(null, requestb.build(), new Observer() {
@Override
public void next(ArchiveRecord message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamCompletenessIndex(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamCompletenessIndexRequest.Builder requestb = StreamCompletenessIndexRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof MergeTimeOption) {
requestb.setMergeTime(((MergeTimeOption) option).mergeTime);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
indexService.streamCompletenessIndex(null, requestb.build(), new Observer() {
@Override
public void next(ArchiveRecord message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture> listCommands() {
return listCommands(null, null);
}
public CompletableFuture> listCommands(Instant start, Instant stop) {
ListCommandsRequest.Builder requestb = ListCommandsRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
return new CommandPage(requestb.build()).future();
}
public CompletableFuture getCommand(String id) {
GetCommandRequest.Builder requestb = GetCommandRequest.newBuilder()
.setInstance(instance)
.setId(id);
CompletableFuture f = new CompletableFuture<>();
commandService.getCommand(null, requestb.build(), new ResponseObserver<>(f));
return f.thenApply(response -> new Command(response));
}
public CompletableFuture streamCommands(StreamReceiver consumer, Instant start,
Instant stop, StreamOption... options) {
StreamCommandsRequest.Builder requestb = StreamCommandsRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof CommandOption) {
for (String command : ((CommandOption) option).commands) {
requestb.addName(command);
}
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
commandService.streamCommands(null, requestb.build(), new Observer() {
@Override
public void next(CommandHistoryEntry message) {
Command command = new Command(message.getId(), message.getCommandName(), message.getAliasesMap(),
message.getAssignmentsList(), message.getOrigin(), message.getSequenceNumber(),
Helpers.toInstant(message.getGenerationTime()));
command.merge(message);
consumer.accept(command);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture> listEvents(ListOption... options) {
return listEvents(null, null, options);
}
public CompletableFuture> listEvents(Instant start, Instant stop, ListOption... options) {
var requestb = ListEventsRequest.newBuilder().setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (var option : options) {
if (option instanceof ListOptions.FilterOption o) {
requestb.setFilter(o.filter);
} else if (option instanceof ListOptions.AscendingOption o) {
requestb.setOrder(o.ascending ? "asc" : "desc");
} else if (option instanceof ListOptions.LimitOption o) {
requestb.setLimit(o.limit);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
return new EventPage(requestb.build()).future();
}
public CompletableFuture streamEvents(StreamReceiver consumer, Instant start, Instant stop,
StreamOption... options) {
var requestb = StreamEventsRequest.newBuilder().setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof StreamOptions.EventSourceOption o) {
for (String source : o.eventSources) {
requestb.addSource(source);
}
} else if (option instanceof StreamOptions.FilterOption o) {
requestb.setFilter(o.filter);
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
eventService.streamEvents(null, requestb.build(), new Observer() {
@Override
public void next(Event message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamPackets(StreamReceiver consumer, Instant start, Instant stop,
StreamOption... options) {
StreamPacketsRequest.Builder requestb = StreamPacketsRequest.newBuilder()
.setInstance(instance);
if (start != null) {
requestb.setStart(Timestamp.newBuilder().setSeconds(start.getEpochSecond()).setNanos(start.getNano()));
}
if (stop != null) {
requestb.setStop(Timestamp.newBuilder().setSeconds(stop.getEpochSecond()).setNanos(stop.getNano()));
}
for (StreamOption option : options) {
if (option instanceof StreamOptions.PacketOption) {
for (String packet : ((StreamOptions.PacketOption) option).packets) {
requestb.addName(packet);
}
} else {
throw new IllegalArgumentException("Unsupported option " + option.getClass());
}
}
CompletableFuture f = new CompletableFuture<>();
packetService.streamPackets(null, requestb.build(), new Observer() {
@Override
public void next(TmPacketData message) {
consumer.accept(message);
}
@Override
public void completeExceptionally(Throwable t) {
f.completeExceptionally(t);
}
@Override
public void complete() {
f.complete(null);
}
});
return f;
}
public CompletableFuture streamValues(List parameters,
StreamReceiver
© 2015 - 2024 Weber Informatics LLC | Privacy Policy