• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / yamcs-cfs / #130

27 Sep 2023 05:48PM UTC coverage: 0.0%. Remained the same
#130

push

web-flow
Merge pull request #44 from WindhoverLabs/csv_plugin

Csv plugin

302 of 302 new or added lines in 2 files covered. (100.0%)

0 of 6578 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/windhoverlabs/yamcs/archive/DeltaCountedParameterFormatter.java
1
package com.windhoverlabs.yamcs.archive;
2

3
import com.csvreader.CsvWriter;
4
import java.io.Closeable;
5
import java.io.IOException;
6
import java.io.OutputStream;
7
import java.io.Writer;
8
import java.nio.charset.Charset;
9
import java.time.Duration;
10
import java.time.Instant;
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.LinkedHashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Map.Entry;
17
import org.yamcs.client.Helpers;
18
import org.yamcs.parameter.ParameterValue;
19
import org.yamcs.parameter.ParameterValueWithId;
20
import org.yamcs.parameter.Value;
21
import org.yamcs.protobuf.Pvalue.MonitoringResult;
22
import org.yamcs.protobuf.Yamcs.NamedObjectId;
23
import org.yamcs.utils.TimeEncoding;
24

25
/**
26
 * Formats in tab separated format parameters. The list of the possible parameters has to be known
27
 * in advance.
28
 *
29
 * @author nm
30
 */
31
public class DeltaCountedParameterFormatter implements Closeable {
32
  protected Writer writer;
33
  protected boolean printTime = true;
×
34
  protected boolean printRaw = false;
×
35
  protected boolean printMonitoring = false;
×
36
  protected boolean printUnique = false;
×
37
  protected boolean keepValues =
×
38
      false; // if set to true print the latest known value of a parameter even for
39
  // parameters not retrieved in a packet
40
  protected boolean writeHeader = true;
×
41
  protected boolean allParametersPresent =
×
42
      false; // true = print only those lines that contain all parameters' values
43
  protected int timewindow = -1; // [ms], -1 = no window at all
×
44

45
  String previousLine;
46
  long lastLineInstant;
47
  Instant lastLineInstantObj;
48
  protected int unsavedLineCount;
49
  private Instant firstInstant;
50

51
  public Instant getFirstInstant() {
52
    return firstInstant;
×
53
  }
54

55
  public void setFirstInstant(Instant firstInstant) {
56
    this.firstInstant = firstInstant;
×
57
  }
×
58

59
  Map<NamedObjectId, ParameterValue> subscribedParameters = new LinkedHashMap<>();
×
60
  protected int linesSaved, linesReceived;
61
  protected boolean first = true;
×
62
  static char DEFAULT_COLUMN_SEPARATOR = '\t';
×
63
  CsvWriter csvWriter;
64
  char columnSeparator = DEFAULT_COLUMN_SEPARATOR;
×
65

66
  public DeltaCountedParameterFormatter(Writer writer, Collection<NamedObjectId> paramList) {
67
    this(writer, paramList, DEFAULT_COLUMN_SEPARATOR);
×
68
  }
×
69

70
  public DeltaCountedParameterFormatter(
71
      Writer writer, Collection<NamedObjectId> paramList, char columnSeparator) {
×
72
    this.writer = writer;
×
73
    for (NamedObjectId id : paramList) {
×
74
      subscribedParameters.put(id, null);
×
75
    }
×
76
    this.columnSeparator = columnSeparator;
×
77
    if (writer != null) {
×
78
      csvWriter = new CsvWriter(writer, this.columnSeparator);
×
79
    }
80
  }
×
81

82
  public void updateWriter(OutputStream outputStream, Charset charset) {
83
    if (csvWriter != null) {
×
84
      csvWriter.close();
×
85
    }
86
    csvWriter = new CsvWriter(outputStream, columnSeparator, charset);
×
87
  }
×
88

89
  public void setPrintRaw(boolean printRaw) {
90
    this.printRaw = printRaw;
×
91
  }
×
92

93
  public void setPrintMonitoring(boolean printMonitoring) {
94
    this.printMonitoring = printMonitoring;
×
95
  }
×
96

97
  public void setPrintTime(boolean printTime) {
98
    this.printTime = printTime;
×
99
  }
×
100

101
  public void setPrintUnique(boolean printUnique) {
102
    this.printUnique = printUnique;
×
103
  }
×
104

105
  public void setWriteHeader(boolean writeHeader) {
106
    this.writeHeader = writeHeader;
×
107
  }
×
108

109
  public void setAllParametersPresent(boolean allParametersPresent) {
110
    this.allParametersPresent = allParametersPresent;
×
111
  }
×
112

113
  public void setKeepValues(boolean keepValues) {
114
    this.keepValues = keepValues;
×
115
  }
×
116

117
  public void setTimeWindow(int timewindow) {
118
    this.timewindow = timewindow;
×
119
  }
×
120

121
  public void resetTimeWindow() {
122
    this.timewindow = -1;
×
123
  }
×
124

125
  private void writeHeader() throws IOException {
126
    // print header line with ops names
127
    List<String> h = new ArrayList<>();
×
128
    if (printTime) {
×
129
      h.add("Time");
×
130
      h.add("DeltaTime");
×
131
    }
132
    for (NamedObjectId noid : subscribedParameters.keySet()) {
×
133
      h.add(noid.getName());
×
134
      if (printRaw) {
×
135
        h.add(noid.getName() + "_RAW");
×
136
      }
137
      if (printMonitoring) {
×
138
        h.add(noid.getName() + "_MONITORING");
×
139
      }
140
    }
×
141
    csvWriter.writeRecord(h.toArray(new String[0]));
×
142
  }
×
143

144
  /**
145
   * adds new parameters - if they are written to the output buffer or not depends on the settings
146
   *
147
   * @param params
148
   * @throws IOException
149
   */
150
  public void writeParameters(List<ParameterValueWithId> params) throws IOException {
151
    long t = params.get(0).getParameterValue().getGenerationTime();
×
152
    if ((timewindow == -1) || (t - lastLineInstant > timewindow)) {
×
153
      writeParameters();
×
154
      lastLineInstant = t;
×
155
      lastLineInstantObj =
×
156
          Helpers.toInstant(params.get(0).getParameterValue().toGpb().getGenerationTime());
×
157

158
      if (!keepValues) {
×
159
        for (Entry<NamedObjectId, ParameterValue> entry : subscribedParameters.entrySet()) {
×
160
          entry.setValue(null);
×
161
        }
×
162
      }
163
    }
164

165
    for (int i = 0; i < params.size(); i++) {
×
166
      ParameterValue pv = params.get(i).getParameterValue();
×
167
      subscribedParameters.put(params.get(i).getId(), pv);
×
168
    }
169
    linesReceived++;
×
170
    ++unsavedLineCount;
×
171
  }
×
172

173
  protected void writeParameters() throws IOException {
174
    if (first) {
×
175
      if (writeHeader) {
×
176
        writeHeader();
×
177
      }
178
      first = false;
×
179
    }
180
    if (unsavedLineCount == 0) {
×
181
      return;
×
182
    }
183
    List<String> l = new ArrayList<>();
×
184
    StringBuilder sb = new StringBuilder();
×
185
    boolean skip = false;
×
186
    for (Entry<NamedObjectId, ParameterValue> entry : subscribedParameters.entrySet()) {
×
187
      ParameterValue pv = entry.getValue();
×
188
      if (pv != null) {
×
189
        Value ev = pv.getEngValue();
×
190
        if (ev != null) {
×
191
          sb.append(ev.toString());
×
192
          l.add(ev.toString());
×
193
        } else {
194
          System.err.println("got parameter without an engineering value for " + entry.getKey());
×
195
          // skip=true;
196
        }
197
        if (printRaw) {
×
198
          Value rv = pv.getRawValue();
×
199
          if (rv != null) {
×
200
            sb.append(rv.toString());
×
201
            l.add(rv.toString());
×
202
          } else {
203
            l.add("");
×
204
          }
205
        }
206
        if (printMonitoring) {
×
207
          MonitoringResult mr = pv.getMonitoringResult();
×
208
          if (mr != null) {
×
209
            sb.append(mr.name());
×
210
            l.add(mr.name());
×
211
          } else {
212
            l.add("");
×
213
          }
214
        }
215
      } else {
×
216
        if (allParametersPresent) {
×
217
          skip = true;
×
218
          break;
×
219
        } else {
220
          l.add("");
×
221
          if (printRaw) {
×
222
            l.add("");
×
223
          }
224
        }
225
      }
226
    }
×
227

228
    if (!skip) {
×
229
      final String line = sb.toString();
×
230
      if (!printUnique || !line.equals(previousLine)) {
×
231
        if (printTime) {
×
232
          l.add(0, TimeEncoding.toString(lastLineInstant));
×
233
          if (firstInstant != null && lastLineInstantObj != null) {
×
234
            Duration delta = Duration.between(firstInstant, lastLineInstantObj);
×
235
            long deltaCount = delta.toMillis();
×
236
            l.add(1, Long.toString(deltaCount));
×
237
          }
238
          ;
239
        }
240
        csvWriter.writeRecord(l.toArray(new String[0]));
×
241
        previousLine = line;
×
242
        linesSaved++;
×
243
      } else {
244
        skip = true;
×
245
      }
246
    }
247
    unsavedLineCount = 0;
×
248
  }
×
249

250
  public void flush() {
251
    csvWriter.flush();
×
252
  }
×
253

254
  @Override
255
  public void close() throws IOException {
256
    writeParameters(); // write the remaining parameters
×
257
    csvWriter.close();
×
258
  }
×
259

260
  public int getLinesSaved() {
261
    return linesSaved;
×
262
  }
263

264
  public int getLinesReceived() {
265
    return linesReceived;
×
266
  }
267
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc