-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (79 loc) · 3.62 KB
/
Program.cs
File metadata and controls
95 lines (79 loc) · 3.62 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
using System;
using Microsoft.EntityFrameworkCore;
class Program
{
static void Main(string[] args)
{
// Parse the configuration file
var config = ConfigParser.ParseConfig("data.config");
// Build the connection string
var connectionString = ConnectionStringBuilder.BuildConnectionString(config.Database);
// Set up the DbContext based on the database type
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
switch (config.Database.DbType.ToLower())
{
case "psql": // PostgreSQL
optionsBuilder.UseNpgsql(connectionString);
break;
case "sql": // MSSQL
optionsBuilder.UseSqlServer(connectionString);
break;
case "oracle": // Oracle
optionsBuilder.UseOracle(connectionString);
break;
default:
throw new NotSupportedException($"Unsupported database type: {config.Database.DbType}");
}
var options = optionsBuilder.Options;
using (var context = new YourDbContext(options))
{
var monitoringService = new MonitoringService(context, config.Smtp);
var email_body = "<html><head><style>table, th, td { border: 1px solid black; border-collapse: collapse; padding: 5px; }</style></head><body>";
// Store query results in a dictionary
var queryResults = new Dictionary<string, List<dynamic>>();
// Execute each query and store the results
foreach (var queryName in config.Query.QueryData)
{
if (config.Query.Queries.TryGetValue(queryName, out var sql))
{
Console.WriteLine($"Executing query: {queryName}");
var results = monitoringService.ExecuteQuery(sql);
queryResults[queryName] = results;
email_body += "<h2>" + queryName + "</h2>";
if (results.Any())
{
email_body += "<table>";
// Add table headers
var firstRow = (IDictionary<string, object>)results[0];
email_body += "<tr>";
foreach (var column in firstRow)
{
email_body += "<th>" + column.Key + "</th>";
}
email_body += "</tr>";
// Add table rows
foreach (var row in results)
{
var rowDict = (IDictionary<string, object>)row;
email_body += "<tr>";
foreach (var column in rowDict)
{
email_body += "<td>" + column.Value + "</td>";
}
email_body += "</tr>";
}
email_body += "</table><br>";
}
else
{
email_body += "<p>No results found.</p>";
}
}
}
email_body += "</body></html>";
// Send the results via email
var subject = config.Smtp.EmailSubject.Replace("(yyyy-mm-dd_now)", DateTime.Now.ToString("yyyy-MM-dd"));
monitoringService.SendEmail(subject, email_body, queryResults);
}
}
}