Skip to content

Commit ba90e0e

Browse files
authored
Merge pull request #174 from coinapi/SDKConsoleApp
Generating output.csv file for further processing
2 parents 8450bd4 + e04b469 commit ba90e0e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

data-api/csharp-ws/CoinAPI.WebSocket.Stats.Console/Program.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public async Task MakeRequest([FromService] IConfiguration configuration, string
165165
};
166166
wsClient.SendHelloMessage(hello);
167167

168-
_ = PrintingTaskLoopAsync(wsClient, endpoint_name, subscribe_data_type, asset, symbol, exchange,latency_type);
168+
_ = PrintingTaskLoopAsync(wsClient, endpoint_name, subscribe_data_type, asset, symbol, exchange, supress_hb, latency_type);
169169

170170
await Task.Run(() => Console.ReadKey());
171171
}
@@ -202,7 +202,7 @@ private void ProcessMsg(DateTime? time_exchange, DateTime? time_coinapi, Latency
202202

203203
private async Task PrintingTaskLoopAsync(CoinApiWsClient wsClient,
204204
string endpoint_name, string subscribe_data_type, string asset,
205-
string symbol, string exchange, string latency_type)
205+
string symbol, string exchange, bool supress_hb, string latency_type)
206206
{
207207
var iterations = 0;
208208
Serilog.Log.Information($"Time: {DateTime.UtcNow}");
@@ -225,7 +225,32 @@ private async Task PrintingTaskLoopAsync(CoinApiWsClient wsClient,
225225

226226
Serilog.Log.Information(strbld.ToString());
227227

228-
var process = Process.GetCurrentProcess();
228+
string basePath = "output";
229+
string extension = ".csv";
230+
231+
string csvFilePath = basePath + extension;
232+
int counter = 1;
233+
234+
while (File.Exists(csvFilePath))
235+
{
236+
csvFilePath = $"{basePath}_{counter}{extension}";
237+
counter++;
238+
}
239+
240+
using (StreamWriter sw = File.CreateText(csvFilePath))
241+
{
242+
sw.WriteLine("Parameters:");
243+
sw.WriteLine($"Endpoint Name: {endpoint_name}");
244+
sw.WriteLine($"Subscribe Data Type: {subscribe_data_type}");
245+
sw.WriteLine($"Asset: {asset}");
246+
sw.WriteLine($"Symbol: {symbol}");
247+
sw.WriteLine($"Exchange: {exchange}");
248+
sw.WriteLine($"Supress heartbeat: {supress_hb}");
249+
sw.WriteLine($"Latency type: {latency_type}");
250+
sw.WriteLine();
251+
sw.WriteLine("Timestamp;Messages;BytesReceived;WaitingCPU%;ParsingCPU%;ProcessingCPU%;LatencyMin(ms);LatencyMax(ms)");
252+
}
253+
229254

230255
while (true)
231256
{
@@ -276,7 +301,7 @@ private async Task PrintingTaskLoopAsync(CoinApiWsClient wsClient,
276301

277302
strbld.AppendFormat($"Messages: {msgCountOnInterval,-8}");
278303
strbld.AppendFormat($"| Recv bytes: {bytesCountOnInterval,-8}");
279-
strbld.Append($"| CPU: wait: {cpuWaitingPercent:F2}% | parse: {cpuParsingPercent:F2}% | process: {cpuHandlingPercent:F2}%");
304+
strbld.Append($"| CPU: wait: {cpuWaitingPercent:P} | parse: {cpuParsingPercent:P} | process: {cpuHandlingPercent:P}");
280305

281306
if (latencies.Any())
282307
{
@@ -285,6 +310,12 @@ private async Task PrintingTaskLoopAsync(CoinApiWsClient wsClient,
285310
}
286311

287312
Serilog.Log.Information(strbld.ToString());
313+
314+
using (StreamWriter sw = File.AppendText(csvFilePath))
315+
{
316+
sw.WriteLine($"\"{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss}\";{msgCountOnInterval};{bytesCountOnInterval};{cpuWaitingPercent:F2};{cpuParsingPercent:F2};{cpuHandlingPercent:F2};{(latencies.Any() ? latencies.Min().TotalMilliseconds : 0):F2};{(latencies.Any() ? latencies.Max().TotalMilliseconds : 0):F2}");
317+
}
318+
288319
}
289320
}
290321

data-api/csharp-ws/CoinAPI.WebSocket.Stats.Console/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ This command subscribes to quote data for the BTC_USD spot symbol in Coinbase ex
103103
## Logging
104104

105105
The application logs detailed information to the console, including subscribed data types, message counts, CPU usage, and latencies.
106+
Furthermore, same data is written in CSV format to the `output.csv` file in the application's root directory.
106107

107108
## Stopping the Application
108109

0 commit comments

Comments
 (0)