From 533b87118e7df531b93d37415bd4fd408845a1f9 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 1 Mar 2024 10:05:51 +0100 Subject: [PATCH] Apply client settings without reloading the plugin --- Influx/Influx.csproj | 2 +- Influx/Influx/InfluxStatisticsClient.cs | 37 +++++++++++++++++++------ Influx/InfluxPlugin.cs | 3 +- Influx/Windows/ConfigurationWindow.cs | 20 ++++++++----- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Influx/Influx.csproj b/Influx/Influx.csproj index 158dcc2..b00ee1d 100644 --- a/Influx/Influx.csproj +++ b/Influx/Influx.csproj @@ -1,7 +1,7 @@ net7.0-windows - 0.5 + 0.6 11.0 enable true diff --git a/Influx/Influx/InfluxStatisticsClient.cs b/Influx/Influx/InfluxStatisticsClient.cs index b1dc97f..2b686a3 100644 --- a/Influx/Influx/InfluxStatisticsClient.cs +++ b/Influx/Influx/InfluxStatisticsClient.cs @@ -15,21 +15,23 @@ namespace Influx.Influx; internal sealed class InfluxStatisticsClient : IDisposable { - private readonly InfluxDBClient _influxClient; + private InfluxDBClient? _influxClient; private readonly IChatGui _chatGui; private readonly Configuration _configuration; private readonly IClientState _clientState; + private readonly IPluginLog _pluginLog; private readonly IReadOnlyDictionary _classJobToArrayIndex; private readonly IReadOnlyDictionary _classJobNames; private readonly Dictionary _expToJobs; public InfluxStatisticsClient(IChatGui chatGui, Configuration configuration, IDataManager dataManager, - IClientState clientState) + IClientState clientState, IPluginLog pluginLog) { - _influxClient = new InfluxDBClient(configuration.Server.Server, configuration.Server.Token); _chatGui = chatGui; _configuration = configuration; _clientState = clientState; + _pluginLog = pluginLog; + UpdateClient(); _classJobToArrayIndex = dataManager.GetExcelSheet()!.Where(x => x.RowId > 0) .ToDictionary(x => (byte)x.RowId, x => (byte)x.ExpArrayIndex); @@ -41,7 +43,20 @@ internal sealed class InfluxStatisticsClient : IDisposable .ToDictionary(x => x.ExpArrayIndex, x => x.Abbreviation.ToString()); } - public bool Enabled => _configuration.Server.Enabled; + public bool Enabled => _configuration.Server.Enabled && + !string.IsNullOrEmpty(_configuration.Server.Server) && + !string.IsNullOrEmpty(_configuration.Server.Token) && + !string.IsNullOrEmpty(_configuration.Server.Organization) && + !string.IsNullOrEmpty(_configuration.Server.Bucket); + + public void UpdateClient() + { + _influxClient?.Dispose(); + _influxClient = null; + + if (Enabled) + _influxClient = new InfluxDBClient(_configuration.Server.Server, _configuration.Server.Token); + } public void OnStatisticsUpdate(StatisticsUpdate update) { @@ -53,9 +68,13 @@ internal sealed class InfluxStatisticsClient : IDisposable var validFcIds = currencyStats.Keys .Where(x => x.CharacterType == CharacterType.Character) - .Where(x => _configuration.IncludedCharacters.SingleOrDefault(config => config.LocalContentId == x.CharacterId)?.IncludeFreeCompany == true) + .Where(x => _configuration.IncludedCharacters + .SingleOrDefault(config => config.LocalContentId == x.CharacterId)?.IncludeFreeCompany == true) .Select(x => x.FreeCompanyId) .ToList(); + var client = _influxClient; + if (client == null) + return; Task.Run(async () => { try @@ -124,7 +143,8 @@ internal sealed class InfluxStatisticsClient : IDisposable .Tag("id", character.CharacterId.ToString()) .Tag("player_name", character.Name) .Tag("type", character.CharacterType.ToString()) - .Tag("fc_id", character.FreeCompanyId > 0 ? character.FreeCompanyId.ToString() : null) + .Tag("fc_id", + character.FreeCompanyId > 0 ? character.FreeCompanyId.ToString() : null) .Tag("job", abbreviation) .Field("level", level) .Timestamp(date, WritePrecision.S)); @@ -225,7 +245,7 @@ internal sealed class InfluxStatisticsClient : IDisposable } } - var writeApi = _influxClient.GetWriteApiAsync(); + var writeApi = client.GetWriteApiAsync(); await writeApi.WritePointsAsync( values, _configuration.Server.Bucket, _configuration.Server.Organization); @@ -234,6 +254,7 @@ internal sealed class InfluxStatisticsClient : IDisposable } catch (Exception e) { + _pluginLog.Error(e, "Unable to update statistics"); _chatGui.PrintError(e.Message); } }); @@ -241,6 +262,6 @@ internal sealed class InfluxStatisticsClient : IDisposable public void Dispose() { - _influxClient.Dispose(); + _influxClient?.Dispose(); } } diff --git a/Influx/InfluxPlugin.cs b/Influx/InfluxPlugin.cs index f61792c..43ea69a 100644 --- a/Influx/InfluxPlugin.cs +++ b/Influx/InfluxPlugin.cs @@ -58,12 +58,13 @@ public class InfluxPlugin : IDalamudPlugin _submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector); _localStatsCalculator = new LocalStatsCalculator(pluginInterface, clientState, addonLifecycle, pluginLog, dataManager); _fcStatsCalculator = new FcStatsCalculator(this, pluginInterface, clientState, addonLifecycle, gameGui, framework, pluginLog); - _influxStatisticsClient = new InfluxStatisticsClient(chatGui, _configuration, dataManager, clientState); + _influxStatisticsClient = new InfluxStatisticsClient(chatGui, _configuration, dataManager, clientState, _pluginLog); _windowSystem = new WindowSystem(typeof(InfluxPlugin).FullName); _statisticsWindow = new StatisticsWindow(); _windowSystem.AddWindow(_statisticsWindow); _configurationWindow = new ConfigurationWindow(_pluginInterface, clientState, _configuration); + _configurationWindow.ConfigUpdated += (_, _) => _influxStatisticsClient.UpdateClient(); _windowSystem.AddWindow(_configurationWindow); _commandManager.AddHandler("/influx", new CommandInfo(ProcessCommand)); diff --git a/Influx/Windows/ConfigurationWindow.cs b/Influx/Windows/ConfigurationWindow.cs index b93f0de..840fd72 100644 --- a/Influx/Windows/ConfigurationWindow.cs +++ b/Influx/Windows/ConfigurationWindow.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using Dalamud.Interface.Colors; using Dalamud.Interface.Utility.Raii; using Dalamud.Interface.Windowing; @@ -23,6 +24,8 @@ internal sealed class ConfigurationWindow : Window _configuration = configuration; } + public event EventHandler? ConfigUpdated; + public override void Draw() { using var tabBar = ImRaii.TabBar("InfluxConfigTabs"); @@ -43,35 +46,35 @@ internal sealed class ConfigurationWindow : Window if (ImGui.Checkbox("Enable Server Connection", ref enabled)) { _configuration.Server.Enabled = enabled; - Save(); + Save(true); } string server = _configuration.Server.Server; if (ImGui.InputText("InfluxDB URL", ref server, 128)) { _configuration.Server.Server = server; - Save(); + Save(true); } string token = _configuration.Server.Token; if (ImGui.InputText("Token", ref token, 128, ImGuiInputTextFlags.Password)) { _configuration.Server.Token = token; - Save(); + Save(true); } string organization = _configuration.Server.Organization; if (ImGui.InputText("Organization", ref organization, 128)) { _configuration.Server.Organization = organization; - Save(); + Save(true); } string bucket = _configuration.Server.Bucket; if (ImGui.InputText("Bucket", ref bucket, 128)) { _configuration.Server.Bucket = bucket; - Save(); + Save(true); } } @@ -160,8 +163,11 @@ internal sealed class ConfigurationWindow : Window } } - private void Save() + private void Save(bool sendEvent = false) { _pluginInterface.SavePluginConfig(_configuration); + + if (sendEvent) + ConfigUpdated?.Invoke(this, EventArgs.Empty); } }