|
1 | 1 | using System.Diagnostics; |
| 2 | +using System.Dynamic; |
2 | 3 | using System.Net.Http.Json; |
3 | 4 |
|
4 | 5 | using CodeProject.AI.SDK.API; |
@@ -28,8 +29,11 @@ public abstract class ModuleWorkerBase : BackgroundService |
28 | 29 | private readonly ILogger _logger; |
29 | 30 | private readonly IHostApplicationLifetime _appLifetime; |
30 | 31 |
|
31 | | - private bool _cancelled = false; |
32 | | - private bool _performSelfTest = false; |
| 32 | + private bool _cancelled = false; |
| 33 | + private bool _performSelfTest = false; |
| 34 | + private int _successInferences; |
| 35 | + private long _totalSuccessInf_ms; |
| 36 | + private int _failedInferences; |
33 | 37 |
|
34 | 38 | /// <summary> |
35 | 39 | /// Gets or sets the name of this Module |
@@ -93,7 +97,7 @@ public ModuleWorkerBase(ILogger logger, IConfiguration configuration, |
93 | 97 | EnableGPU = configuration.GetValue<bool>("CPAI_MODULE_ENABLE_GPU", true); |
94 | 98 | _accelDeviceName = configuration.GetValue<string?>("CPAI_ACCEL_DEVICE_NAME", null); |
95 | 99 | _halfPrecision = configuration.GetValue<string?>("CPAI_HALF_PRECISION", null) ?? "enable"; // Can be enable, disable or force |
96 | | - _logVerbosity = configuration.GetValue<string?>("CPAI_LOG_VERBOSITY", null) ?? "info"; // Can be Quiet, Info or Loud |
| 100 | + _logVerbosity = configuration.GetValue<string?>("CPAI_LOG_VERBOSITY", null) ?? "quiet"; // Can be Quiet, Info or Loud |
97 | 101 |
|
98 | 102 | _performSelfTest = configuration.GetValue<bool>("CPAI_MODULE_DO_SELFTEST", false); |
99 | 103 |
|
@@ -127,6 +131,22 @@ protected virtual void InitModule() |
127 | 131 | /// <returns>An object to serialize back to the server.</returns> |
128 | 132 | protected abstract ModuleResponse ProcessRequest(BackendRequest request); |
129 | 133 |
|
| 134 | + /// <summary> |
| 135 | + /// Returns an object containing current stats for this module |
| 136 | + /// </summary> |
| 137 | + /// <returns>An object</returns> |
| 138 | + protected virtual dynamic? Status() |
| 139 | + { |
| 140 | + dynamic status = new ExpandoObject(); |
| 141 | + status.SuccessfulInferences = _successInferences; |
| 142 | + status.FailedInferences = _failedInferences; |
| 143 | + status.NumInferences = _successInferences + _failedInferences; |
| 144 | + status.AverageInferenceMs = _successInferences > 0 |
| 145 | + ? _totalSuccessInf_ms / _successInferences : 0; |
| 146 | + |
| 147 | + return status; |
| 148 | + } |
| 149 | + |
130 | 150 | /// <summary> |
131 | 151 | /// Called when the module is asked to execute a self-test to ensure it install and runs |
132 | 152 | /// correctly |
@@ -188,13 +208,22 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber) |
188 | 208 | ModuleResponse response = ProcessRequest(request); |
189 | 209 | stopWatch.Stop(); |
190 | 210 |
|
| 211 | + if (response.Success) |
| 212 | + { |
| 213 | + _successInferences++; |
| 214 | + _totalSuccessInf_ms += response.InferenceMs; |
| 215 | + } |
| 216 | + else |
| 217 | + _failedInferences++; |
| 218 | + |
191 | 219 | long processMs = stopWatch.ElapsedMilliseconds; |
192 | 220 | response.ModuleName = ModuleName; |
193 | 221 | response.ModuleId = _moduleId; |
194 | 222 | response.ProcessMs = processMs; |
195 | 223 | response.ExecutionProvider = ExecutionProvider ?? string.Empty; |
196 | 224 | response.Command = request.payload?.command ?? string.Empty; |
197 | 225 | response.CanUseGPU = CanUseGPU; |
| 226 | + response.StatusData = Status(); |
198 | 227 |
|
199 | 228 | HttpContent content = JsonContent.Create(response, response.GetType()); |
200 | 229 |
|
|
0 commit comments