-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolRepository.cs
More file actions
131 lines (110 loc) · 6.07 KB
/
Copy pathToolRepository.cs
File metadata and controls
131 lines (110 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace WebToolsDataMonitor
{
public class ToolRepository
{
private static readonly Lazy<ToolRepository> _instance = new Lazy<ToolRepository>(() => new ToolRepository());
public static ToolRepository Instance => _instance.Value;
private readonly object _lock = new object();
private Dictionary<string, ToolConfig> _registeredTools = new Dictionary<string, ToolConfig>(StringComparer.OrdinalIgnoreCase);
private ToolRepository()
{
LoadAllConfiguredTools();
}
// Re-reads every tool/page config file from disk, replacing the in-memory set. Lets an edit to
// tools/*.yaml or tools/pages/*.yaml (a fixed destinationUrl, a new tool, an addr change) take effect
// on the next run without restarting the whole application - see Form1.RunSelectedToolsOnceAsync,
// which calls this at the start of every single run and every continuous-loop cycle.
public void Reload()
{
var freshTools = new Dictionary<string, ToolConfig>(StringComparer.OrdinalIgnoreCase);
LoadAllConfiguredTools(freshTools);
lock (_lock)
{
_registeredTools = freshTools;
}
}
private void LoadAllConfiguredTools() => LoadAllConfiguredTools(_registeredTools);
private void LoadAllConfiguredTools(Dictionary<string, ToolConfig> targetTools)
{
string toolsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tools");
string pagesDir = Path.Combine(toolsDir, "pages");
Logger.Instance.Info("TOOL_REPO", $"Initializing engine configuration path mapping fields under: {toolsDir}");
if (!Directory.Exists(toolsDir)) Directory.CreateDirectory(toolsDir);
if (!Directory.Exists(pagesDir)) Directory.CreateDirectory(pagesDir);
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).IgnoreUnmatchedProperties().Build();
try
{
string[] toolManifestFiles = Directory.GetFiles(toolsDir, "*.yaml");
Logger.Instance.Debug("TOOL_REPO", $"Discovered ({toolManifestFiles.Length}) base tool profiles.");
foreach (string toolFilePath in toolManifestFiles)
{
if (Path.GetFileName(toolFilePath).Equals("logger_config.yaml", StringComparison.OrdinalIgnoreCase)) continue;
try
{
string toolYaml = File.ReadAllText(toolFilePath);
var tool = deserializer.Deserialize<ToolConfig>(toolYaml);
if (tool == null || string.IsNullOrWhiteSpace(tool.ToolName)) continue;
Logger.Instance.Info("TOOL_REPO", $"Processing profile: '{tool.ToolName}' targeting endpoint -> {tool.Addr}");
foreach (string pattern in tool.PageFilePatterns)
{
string[] matchedPageFiles = Directory.GetFiles(pagesDir, pattern);
foreach (string pageFilePath in matchedPageFiles)
{
try
{
string pageYaml = File.ReadAllText(pageFilePath);
var pageConfigsList = deserializer.Deserialize<List<ScrapPageConfig>>(pageYaml);
if (pageConfigsList != null)
{
// destinationUrl may be a relative path ("/status/acts") - resolve it
// against this tool's addr, same convention as pageLogin/BaseLoginUrl.
// Already-absolute URLs (older configs) pass through unchanged.
foreach (var pageConfig in pageConfigsList)
{
pageConfig.DestinationUrl = tool.ResolveUrl(pageConfig.DestinationUrl);
}
tool.ScrapPages.AddRange(pageConfigsList);
Logger.Instance.Debug("TOOL_REPO", $" -> File '{Path.GetFileName(pageFilePath)}' mapped {pageConfigsList.Count} destination page URLs to '{tool.ToolName}'.");
}
}
catch (Exception pEx)
{
Logger.Instance.Error("TOOL_REPO", $"Failed compiling pages block inside file {Path.GetFileName(pageFilePath)}: {pEx.Message}");
}
}
}
targetTools[tool.ToolName] = tool;
}
catch (Exception tEx)
{
Logger.Instance.Error("TOOL_REPO", $"Corrupt master manifest layout on {Path.GetFileName(toolFilePath)}: {tEx.Message}");
}
}
Logger.Instance.Info("TOOL_REPO", $"Cache fully built. System profile orchestrators ready: {targetTools.Count}");
}
catch (Exception ex)
{
Logger.Instance.Critical("TOOL_REPO", $"Internal tool mapping layer faulted: {ex.Message}");
}
}
public ToolConfig GetTool(string toolName)
{
lock (_lock)
{
return _registeredTools.TryGetValue(toolName, out var config) ? config : null;
}
}
public IEnumerable<ToolConfig> GetAllTools()
{
lock (_lock)
{
return new List<ToolConfig>(_registeredTools.Values);
}
}
} // Closes class ToolRepository
} // Closes namespace WebToolsDataMonitor