feat(sentiment): dynamic settings, IFinlyticLogger, live log streaming, and EF migration
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Dtos;
|
||||
using FinlyticCore.Dtos.News;
|
||||
using FinlyticCore.Dtos.Sentiment;
|
||||
using FinlyticCore.Dtos.Settings;
|
||||
using FinlyticCore.Models;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticCore.Util;
|
||||
using FinlyticSentiment.Services;
|
||||
using FinlyticSentiment.Util;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -21,9 +28,6 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SentimentMqttClient"/> class.
|
||||
/// </summary>
|
||||
public SentimentMqttClient(
|
||||
ILogger<SentimentMqttClient> logger,
|
||||
IConfiguration configuration,
|
||||
@@ -43,12 +47,10 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
{
|
||||
Host = _configuration["MQTT:Host"] ?? _configuration["MQTT__Host"] ?? "localhost",
|
||||
Port = Convert.ToInt32(_configuration["MQTT:Port"] ?? _configuration["MQTT__Port"] ?? "1883"),
|
||||
ClientId =
|
||||
$"{(_configuration["MQTT:ClientId"] ?? _configuration["MQTT__ClientId"] ?? "FinlyticSentiment")}_{Guid.NewGuid()}"
|
||||
ClientId = $"{(_configuration["MQTT:ClientId"] ?? _configuration["MQTT__ClientId"] ?? "FinlyticSentiment")}_{Guid.NewGuid()}"
|
||||
};
|
||||
|
||||
_logger.LogInformation("[{Channel}] Starting Sentiment MQTT client. Host: {Host}, ClientId: {ClientId}",
|
||||
"SentimentChannel", config.Host, config.ClientId);
|
||||
_logger.LogInformation("Starting Sentiment MQTT client. Host: {Host}, ClientId: {ClientId}", config.Host, config.ClientId);
|
||||
await ConnectAsync(config);
|
||||
}
|
||||
|
||||
@@ -57,7 +59,7 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
/// </summary>
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Stopping Sentiment MQTT client and disconnecting.", "SentimentChannel");
|
||||
_logger.LogInformation("Stopping Sentiment MQTT client and disconnecting.");
|
||||
await DisconnectAsync();
|
||||
}
|
||||
|
||||
@@ -69,16 +71,24 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnConnectedAsync()
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] Sentiment MQTT client connected. Subscribing to RPC response and broadcast topics...",
|
||||
"SentimentChannel");
|
||||
_logger.LogInformation("Sentiment MQTT client connected. Subscribing to RPC response and broadcast topics...");
|
||||
await SubscribeAsync("services/response/#");
|
||||
await SubscribeAsync("services/news/completed");
|
||||
await SubscribeAsync("services/request/sentiment_GetArticle/#");
|
||||
await SubscribeAsync("services/request/sentiment_GetIsin/#");
|
||||
await SubscribeAsync("services/request/sentiment_Analyze/#");
|
||||
await SubscribeAsync("services/request/sentiment_settings_GetAll/#");
|
||||
await SubscribeAsync("services/request/sentiment_settings_Update/#");
|
||||
await SubscribeAsync("services/request/health_Ping/#");
|
||||
await SubscribeAsync("services/config/updated/#");
|
||||
|
||||
FinlyticCore.Services.FinlyticLogBroadcaster.OnLogPublished = async (logDto) =>
|
||||
{
|
||||
if (IsConnected && string.Equals(logDto.ServiceName, "FinlyticSentiment", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await PublishAsync("finlytic/logs/FinlyticSentiment", logDto);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -86,14 +96,12 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(topic)) return;
|
||||
|
||||
// 1. Config update events
|
||||
if (topic.StartsWith("services/config/updated", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (topic.EndsWith("FinlyticSentiment", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await OnConfigUpdatedAsync(payload);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -103,13 +111,11 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract correlationId from topic suffix (e.g. services/request/sentiment_GetArticle/{correlationId})
|
||||
var lastSlash = topic.LastIndexOf('/');
|
||||
if (lastSlash < 0 || lastSlash >= topic.Length - 1) return;
|
||||
|
||||
var correlationId = topic.Substring(lastSlash + 1);
|
||||
|
||||
// 2. Dispatch to specific channel handlers
|
||||
if (topic.StartsWith("services/request/sentiment_GetArticle", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await OnSentimentGetArticleAsync(payload, correlationId);
|
||||
@@ -122,178 +128,205 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
{
|
||||
await OnSentimentAnalyzeAsync(payload, correlationId);
|
||||
}
|
||||
else if (topic.StartsWith("services/request/sentiment_settings_GetAll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await OnSettingsGetAllAsync(correlationId);
|
||||
}
|
||||
else if (topic.StartsWith("services/request/sentiment_settings_Update", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await OnSettingsUpdateAsync(payload, correlationId);
|
||||
}
|
||||
else if (topic.StartsWith("services/request/health_Ping", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await OnHealthPingAsync(topic, correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles dynamic service config update events.
|
||||
/// </summary>
|
||||
private async Task OnSettingsGetAllAsync(string correlationId)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
var settingsService = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.MqttChannel, "[FinlyticSentiment] [Settings_GetAll] Retrieving all dynamic settings via reflection [CorrelationId: {CorrelationId}]", correlationId);
|
||||
try
|
||||
{
|
||||
var settings = await settingsService.GetAllRegisteredSettingsAsync(new[] { typeof(SettingKeys) });
|
||||
var responseTopic = $"services/response/sentiment_settings_GetAll/{correlationId}";
|
||||
|
||||
await PublishAsync(responseTopic, settings);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.MqttChannel, "[FinlyticSentiment] [Settings_GetAll] Published {Count} settings to '{ResponseTopic}'", settings.Count, responseTopic);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await finlyticLogger.LogErrorAsync(SettingKeys.MqttChannel, ex, "[FinlyticSentiment] [Settings_GetAll] Failed to retrieve settings.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnSettingsUpdateAsync(string payload, string correlationId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(payload)) return;
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
var settingsService = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.MqttChannel, "[FinlyticSentiment] [Settings_Update] Processing settings update RPC [CorrelationId: {CorrelationId}]", correlationId);
|
||||
try
|
||||
{
|
||||
Dictionary<string, object?>? updates = null;
|
||||
try
|
||||
{
|
||||
updates = JsonSerializer.Deserialize<Dictionary<string, object?>>(payload);
|
||||
}
|
||||
catch
|
||||
{
|
||||
var list = JsonSerializer.Deserialize<List<DynamicSettingDto>>(payload);
|
||||
if (list != null)
|
||||
{
|
||||
updates = new Dictionary<string, object?>();
|
||||
foreach (var item in list) updates[item.Key] = item.Value;
|
||||
}
|
||||
}
|
||||
|
||||
if (updates != null && updates.Count > 0)
|
||||
{
|
||||
await settingsService.UpdateSettingsAsync(updates);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.MqttChannel, "[FinlyticSentiment] [Settings_Update] Successfully updated {Count} settings in database and cache.", updates.Count);
|
||||
}
|
||||
|
||||
var currentSettings = await settingsService.GetAllRegisteredSettingsAsync(new[] { typeof(SettingKeys) });
|
||||
var responseTopic = $"services/response/sentiment_settings_Update/{correlationId}";
|
||||
await PublishAsync(responseTopic, currentSettings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await finlyticLogger.LogErrorAsync(SettingKeys.MqttChannel, ex, "[FinlyticSentiment] [Settings_Update] Failed to update settings.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnConfigUpdatedAsync(string payload)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] [SentimentMqttClient] Received config update event for FinlyticSentiment.",
|
||||
"SentimentChannel");
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(payload);
|
||||
if (doc.RootElement.TryGetProperty("settings", out var settingsProp))
|
||||
{
|
||||
var dict = JsonSerializer.Deserialize(settingsProp.GetRawText(), typeof(Dictionary<string, string>),
|
||||
FinlyticJsonSerializerContext.Default) as Dictionary<string, string>;
|
||||
var dict = JsonSerializer.Deserialize<Dictionary<string, object?>>(settingsProp.GetRawText());
|
||||
if (dict != null && dict.Count > 0)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var settingsDb = scope.ServiceProvider.GetRequiredService<ISettingsDbService>();
|
||||
await settingsDb.UpdateSettingsFromDictionaryAsync(dict);
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Successfully persisted {Count} updated settings for FinlyticSentiment.",
|
||||
"SentimentChannel", dict.Count);
|
||||
var settings = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
await settings.UpdateSettingsAsync(dict);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] [SentimentMqttClient] Error processing MQTT config update event.",
|
||||
"SentimentChannel");
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles health_Ping RPC requests.
|
||||
/// </summary>
|
||||
private async Task OnHealthPingAsync(string topic, string correlationId)
|
||||
{
|
||||
if (topic.Contains("FinlyticSentiment", StringComparison.OrdinalIgnoreCase) ||
|
||||
!topic.Contains("/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string respTopic = $"services/response/health_Ping/{correlationId}";
|
||||
await PublishAsync(respTopic,
|
||||
new ServiceHealthResponse("FinlyticSentiment", "Online", DateTime.UtcNow, "Connected"));
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Responded to live health_Ping RPC request [CorrelationId: {CorrelationId}].",
|
||||
"SentimentChannel", correlationId);
|
||||
await PublishAsync(respTopic, new ServiceHealthResponse("FinlyticSentiment", "Online", DateTime.UtcNow, "Connected"));
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.HealthPingChannel, "[FinlyticSentiment] Responded to live health_Ping RPC request [CorrelationId: {CorrelationId}].", correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles broadcasted articles on services/news/completed.
|
||||
/// </summary>
|
||||
private async Task OnNewsCompletedAsync(string payload)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(payload)) return;
|
||||
try
|
||||
{
|
||||
var article =
|
||||
JsonSerializer.Deserialize(payload, typeof(NewsArticleDto), FinlyticJsonSerializerContext.Default) as
|
||||
NewsArticleDto;
|
||||
var article = JsonSerializer.Deserialize(payload, typeof(NewsArticleDto), FinlyticJsonSerializerContext.Default) as NewsArticleDto;
|
||||
if (article != null && article.Id != Guid.Empty && OnArticleReceived != null)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] Received real-time article broadcast on services/news/completed: {Title} (ID: {Id})",
|
||||
"SentimentChannel", article.Title, article.Id);
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "Received real-time article broadcast on services/news/completed: {Title} (ID: {Id})", article.Title, article.Id);
|
||||
await OnArticleReceived.Invoke(article);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Error parsing broadcasted article on services/news/completed.",
|
||||
"SentimentChannel");
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles sentiment_GetArticle RPC requests using source-generated DTO deserialization.
|
||||
/// </summary>
|
||||
private async Task OnSentimentGetArticleAsync(string payload, string correlationId)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Processing RPC sentiment_GetArticle request [CorrelationId: {CorrelationId}]",
|
||||
"SentimentChannel", correlationId);
|
||||
if (string.IsNullOrWhiteSpace(payload)) return;
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Processing RPC sentiment_GetArticle request [CorrelationId: {CorrelationId}]", correlationId);
|
||||
|
||||
try
|
||||
{
|
||||
var request =
|
||||
JsonSerializer.Deserialize(payload, typeof(ArticleRequest), FinlyticJsonSerializerContext.Default) as
|
||||
ArticleRequest;
|
||||
var request = JsonSerializer.Deserialize(payload, typeof(ArticleRequest), FinlyticJsonSerializerContext.Default) as ArticleRequest;
|
||||
var articleId = request?.ArticleId ?? request?.Id;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(articleId))
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] [SentimentMqttClient] Missing articleId in request payload.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogWarningAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Missing articleId in request payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var storageService = scope.ServiceProvider.GetRequiredService<ISentimentStorageService>();
|
||||
var sentimentEntry = await storageService.GetArticleSentimentAsync(articleId);
|
||||
|
||||
string responseTopic = $"services/response/sentiment_GetArticle/{correlationId}";
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Publishing RPC sentiment_GetArticle response for article {ArticleId} to {ResponseTopic}",
|
||||
"SentimentChannel", articleId, responseTopic);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Publishing RPC sentiment_GetArticle response for article {ArticleId} to {ResponseTopic}", articleId, responseTopic);
|
||||
await PublishAsync(responseTopic, sentimentEntry);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"[{Channel}] [SentimentMqttClient] Failed to process sentiment_GetArticle RPC request.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogErrorAsync(SettingKeys.SentimentChannel, ex, "[SentimentMqttClient] Failed to process sentiment_GetArticle RPC request.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles sentiment_GetIsin RPC requests using source-generated DTO deserialization.
|
||||
/// </summary>
|
||||
private async Task OnSentimentGetIsinAsync(string payload, string correlationId)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Processing RPC sentiment_GetIsin request [CorrelationId: {CorrelationId}]",
|
||||
"SentimentChannel", correlationId);
|
||||
if (string.IsNullOrWhiteSpace(payload)) return;
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Processing RPC sentiment_GetIsin request [CorrelationId: {CorrelationId}]", correlationId);
|
||||
|
||||
try
|
||||
{
|
||||
var request =
|
||||
JsonSerializer.Deserialize(payload, typeof(IsinRequest), FinlyticJsonSerializerContext.Default) as
|
||||
IsinRequest;
|
||||
var request = JsonSerializer.Deserialize(payload, typeof(IsinRequest), FinlyticJsonSerializerContext.Default) as IsinRequest;
|
||||
var isin = request?.Isin;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(isin))
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] [SentimentMqttClient] Missing ISIN in request payload.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogWarningAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Missing ISIN in request payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var storageService = scope.ServiceProvider.GetRequiredService<ISentimentStorageService>();
|
||||
var isinSummary = await storageService.GetIsinSummaryAsync(isin);
|
||||
|
||||
string responseTopic = $"services/response/sentiment_GetIsin/{correlationId}";
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Publishing RPC sentiment_GetIsin response for ISIN {Isin} to {ResponseTopic}",
|
||||
"SentimentChannel", isin, responseTopic);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Publishing RPC sentiment_GetIsin response for ISIN {Isin} to {ResponseTopic}", isin, responseTopic);
|
||||
await PublishAsync(responseTopic, isinSummary);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] [SentimentMqttClient] Failed to process sentiment_GetIsin RPC request.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogErrorAsync(SettingKeys.SentimentChannel, ex, "[SentimentMqttClient] Failed to process sentiment_GetIsin RPC request.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles manual/forced sentiment_Analyze RPC requests using existing analyzer and storage services.
|
||||
/// </summary>
|
||||
private async Task OnSentimentAnalyzeAsync(string payload, string correlationId)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Processing RPC sentiment_Analyze request [CorrelationId: {CorrelationId}]",
|
||||
"SentimentChannel", correlationId);
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var finlyticLogger = scope.ServiceProvider.GetRequiredService<IFinlyticLogger<SentimentMqttClient>>();
|
||||
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Processing RPC sentiment_Analyze request [CorrelationId: {CorrelationId}]", correlationId);
|
||||
string responseTopic = $"services/response/sentiment_Analyze/{correlationId}";
|
||||
|
||||
if (string.IsNullOrWhiteSpace(payload))
|
||||
@@ -304,33 +337,24 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
|
||||
try
|
||||
{
|
||||
var request =
|
||||
JsonSerializer.Deserialize(payload, typeof(AnalyzeSentimentRequest),
|
||||
FinlyticJsonSerializerContext.Default) as AnalyzeSentimentRequest;
|
||||
var request = JsonSerializer.Deserialize(payload, typeof(AnalyzeSentimentRequest), FinlyticJsonSerializerContext.Default) as AnalyzeSentimentRequest;
|
||||
|
||||
if (request == null ||
|
||||
(string.IsNullOrWhiteSpace(request.ArticleId) && string.IsNullOrWhiteSpace(request.Isin)))
|
||||
if (request == null || (string.IsNullOrWhiteSpace(request.ArticleId) && string.IsNullOrWhiteSpace(request.Isin)))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"[{Channel}] [SentimentMqttClient] Missing ArticleId or Isin in sentiment_Analyze request payload.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogWarningAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Missing ArticleId or Isin in sentiment_Analyze request payload.");
|
||||
await PublishAsync(responseTopic, (object?)null);
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var storageService = scope.ServiceProvider.GetRequiredService<ISentimentStorageService>();
|
||||
var analyzerService = scope.ServiceProvider.GetRequiredService<IFinBertAnalyzerService>();
|
||||
|
||||
object? result = null;
|
||||
|
||||
// Fall 1: Manuelle Analyse für einen einzelnen Artikel
|
||||
if (!string.IsNullOrWhiteSpace(request.ArticleId))
|
||||
{
|
||||
var cleanArticleId = request.ArticleId.Trim();
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Processing article analysis for ArticleId: {ArticleId} (ForceReload: {ForceReload})",
|
||||
"SentimentChannel", cleanArticleId, request.ForceReload);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Processing article analysis for ArticleId: {ArticleId} (ForceReload: {ForceReload})", cleanArticleId, request.ForceReload);
|
||||
|
||||
IsinAnalysisEntry? existingEntry = null;
|
||||
|
||||
@@ -345,7 +369,6 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
}
|
||||
else
|
||||
{
|
||||
// Artikel per RPC von FinlyticNews abfragen
|
||||
if (Guid.TryParse(cleanArticleId, out var articleGuid))
|
||||
{
|
||||
var article = await SendRpcRequestAsync<NewsArticleDto, ArticleRequest>(
|
||||
@@ -357,17 +380,13 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
{
|
||||
var finbertResult = await analyzerService.AnalyzeArticleAsync(article);
|
||||
|
||||
// 🎯 NULL-CHECK: Falls Analyse fehlschlägt/null liefert -> abbrechen
|
||||
if (finbertResult == null)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"[{Channel}] [SentimentMqttClient] FinBERT analysis returned NULL for article {ArticleId}. Aborting manual analysis.",
|
||||
"SentimentChannel", cleanArticleId);
|
||||
await finlyticLogger.LogWarningAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] FinBERT analysis returned NULL for article {ArticleId}. Aborting manual analysis.", cleanArticleId);
|
||||
await PublishAsync(responseTopic, (object?)null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Speichern & Summaries aktualisieren
|
||||
await storageService.SaveArticleSentimentAsync(article, finbertResult);
|
||||
|
||||
if (article.MatchedAssets != null && article.MatchedAssets.Count > 0)
|
||||
@@ -391,86 +410,62 @@ public class SentimentMqttClient : ManagedMqttClient, IHostedService
|
||||
}
|
||||
}
|
||||
|
||||
// FinlyticNews über Re-Analyse informieren
|
||||
await UpdateArticleStatusAsync(article.Id, "Analyzed");
|
||||
|
||||
result = await storageService.GetArticleSentimentAsync(cleanArticleId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"[{Channel}] [SentimentMqttClient] Could not retrieve article {ArticleId} from FinlyticNews for re-analysis.",
|
||||
"SentimentChannel", cleanArticleId);
|
||||
await finlyticLogger.LogWarningAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Could not retrieve article {ArticleId} from FinlyticNews for re-analysis.", cleanArticleId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fall 2: ISIN Gesamtsummary anfordern
|
||||
else if (!string.IsNullOrWhiteSpace(request.Isin))
|
||||
{
|
||||
var cleanIsin = request.Isin.Trim();
|
||||
_logger.LogInformation("[{Channel}] [SentimentMqttClient] Fetching sentiment summary for ISIN: {Isin}",
|
||||
"SentimentChannel", cleanIsin);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Fetching sentiment summary for ISIN: {Isin}", cleanIsin);
|
||||
|
||||
result = await storageService.GetIsinSummaryAsync(cleanIsin);
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"[{Channel}] [SentimentMqttClient] Publishing RPC sentiment_Analyze response to {ResponseTopic}",
|
||||
"SentimentChannel", responseTopic);
|
||||
await finlyticLogger.LogInfoAsync(SettingKeys.SentimentChannel, "[SentimentMqttClient] Publishing RPC sentiment_Analyze response to {ResponseTopic}", responseTopic);
|
||||
await PublishAsync(responseTopic, result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] [SentimentMqttClient] Failed to process sentiment_Analyze RPC request.",
|
||||
"SentimentChannel");
|
||||
await finlyticLogger.LogErrorAsync(SettingKeys.SentimentChannel, ex, "[SentimentMqttClient] Failed to process sentiment_Analyze RPC request.");
|
||||
await PublishAsync(responseTopic, (object?)null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requests pending news articles from FinlyticNews via MQTT RPC.
|
||||
/// </summary>
|
||||
/// <param name="limit">The maximum number of articles to request (capped at 10).</param>
|
||||
/// <returns>A list of pending news article DTOs.</returns>
|
||||
public async Task<List<NewsArticleDto>> GetPendingArticlesAsync(int limit = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = new LimitRequest(Math.Min(limit, 10));
|
||||
var articles =
|
||||
await SendRpcRequestAsync<List<NewsArticleDto>, LimitRequest>("news_GetPending", payload,
|
||||
TimeSpan.FromSeconds(10));
|
||||
var articles = await SendRpcRequestAsync<List<NewsArticleDto>, LimitRequest>("news_GetPending", payload, TimeSpan.FromSeconds(10));
|
||||
return articles ?? [];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Error executing MQTT RPC for news_GetPending.", "SentimentChannel");
|
||||
_logger.LogError(ex, "Error executing MQTT RPC for news_GetPending.");
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches an RPC request to update the article status in FinlyticNews (e.g. to "Analyzed").
|
||||
/// </summary>
|
||||
/// <param name="id">The article identifier.</param>
|
||||
/// <param name="status">The target status string (default "Analyzed").</param>
|
||||
/// <returns>True if the status update succeeded; otherwise, false.</returns>
|
||||
public async Task<bool> UpdateArticleStatusAsync(Guid id, string status = "Analyzed")
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new UpdateNewsStatusRequest(id, status);
|
||||
var response =
|
||||
await SendRpcRequestAsync<UpdateNewsStatusResponse, UpdateNewsStatusRequest>("news_UpdateStatus",
|
||||
request, TimeSpan.FromSeconds(8));
|
||||
var response = await SendRpcRequestAsync<UpdateNewsStatusResponse, UpdateNewsStatusRequest>("news_UpdateStatus", request, TimeSpan.FromSeconds(8));
|
||||
return response?.Success ?? false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Error executing MQTT RPC for news_UpdateStatus (ID: {Id}).",
|
||||
"SentimentChannel", id);
|
||||
_logger.LogError(ex, "Error executing MQTT RPC for news_UpdateStatus (ID: {Id}).", id);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using FinlyticCore.Models.Settings;
|
||||
|
||||
namespace FinlyticSentiment.Util;
|
||||
|
||||
public static class SettingKeys
|
||||
{
|
||||
// --- Logging-Kanäle ---
|
||||
public static readonly SettingKey<bool> SentimentChannel = new("Logging.Channel.Sentiment", true);
|
||||
public static readonly SettingKey<bool> MqttChannel = new("Logging.Channel.MQTT", true);
|
||||
public static readonly SettingKey<bool> HealthPingChannel = new("Logging.Channel.Health", true);
|
||||
|
||||
// --- FinBERT & Modell-Parameter ---
|
||||
public static readonly SettingKey<int> MaxBatchSize = new("FinBert.MaxBatchSize", 10);
|
||||
public static readonly SettingKey<double> MinimumConfidenceThreshold = new("FinBert.MinConfidenceThreshold", 0.60);
|
||||
public static readonly SettingKey<int> TimeoutSeconds = new("FinBert.TimeoutSeconds", 30);
|
||||
public static readonly SettingKey<int> SentimentWindowDays = new("Sentiment.WindowDays", 14);
|
||||
public static readonly SettingKey<double> DecayFactorPerDay = new("Sentiment.DecayFactorPerDay", 0.90);
|
||||
public static readonly SettingKey<bool> EnableAutoSummarization = new("Feature.EnableAutoSummarization", true);
|
||||
}
|
||||
Reference in New Issue
Block a user