Updated code to save results from query reading updates table to file while saving file from previous 5 runs.
This commit is contained in:
+76
-14
@@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Npgsql;
|
||||
using Npgsql;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TvSyncApp
|
||||
{
|
||||
@@ -43,7 +44,12 @@ namespace TvSyncApp
|
||||
|
||||
// --- Fetch series IDs from PostgreSQL ---
|
||||
string connectionString = $"Host={settings.DbSettings.Hostname};Port={settings.DbSettings.PgsqlPort};Username={settings.DbSettings.PgsqlUsername};Password={settings.DbSettings.PgsqlPassword};Database={settings.DbSettings.DbName}";
|
||||
List<int> seriesIds = GetSeriesIdsFromDb(connectionString);
|
||||
List<SeriesUpdate> seriesUpdates = GetSeriesIdsFromDb(connectionString);
|
||||
|
||||
List<string> seriesIds = seriesUpdates
|
||||
.Select(x => x.SeriesId.ToString())
|
||||
.ToList();
|
||||
|
||||
|
||||
Log.Information("Fetched {Count} series IDs from tvupdates table.", seriesIds.Count);
|
||||
|
||||
@@ -62,35 +68,84 @@ namespace TvSyncApp
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static List<int> GetSeriesIdsFromDb(string connectionString)
|
||||
private static List<SeriesUpdate> GetSeriesIdsFromDb(string connectionString)
|
||||
{
|
||||
var seriesIds = new List<int>();
|
||||
var results = new List<SeriesUpdate>();
|
||||
|
||||
try
|
||||
{
|
||||
using var conn = new NpgsqlConnection(connectionString);
|
||||
conn.Open();
|
||||
|
||||
string query = "SELECT seriesid FROM updates.tvupdates ORDER BY seriesid";
|
||||
const string query = @"
|
||||
SELECT seriesid, timestamp
|
||||
FROM updates.tvupdates
|
||||
ORDER BY seriesid";
|
||||
|
||||
using var cmd = new NpgsqlCommand(query, conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (!reader.IsDBNull(0))
|
||||
seriesIds.Add(reader.GetInt32(0));
|
||||
if (!reader.IsDBNull(0) && !reader.IsDBNull(1))
|
||||
{
|
||||
results.Add(new SeriesUpdate
|
||||
{
|
||||
SeriesId = reader.GetInt32(0),
|
||||
Timestamp = long.Parse(reader.GetString(1))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SaveSeriesUpdatesToFile(results);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to fetch series IDs from PostgreSQL.");
|
||||
Log.Error(ex, "Failed to fetch series updates or manage JSON output.");
|
||||
}
|
||||
|
||||
return seriesIds;
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void SaveSeriesUpdatesToFile(List<SeriesUpdate> data)
|
||||
{
|
||||
string configDir = Path.Combine(AppContext.BaseDirectory, "config");
|
||||
Directory.CreateDirectory(configDir);
|
||||
|
||||
string fileName = "previous_tvmaze_updates.json";
|
||||
string filePath = Path.Combine(configDir, fileName);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
|
||||
string archiveName = $"previous_tvmaze_updates_{timestamp}.json";
|
||||
string archivePath = Path.Combine(configDir, archiveName);
|
||||
|
||||
File.Move(filePath, archivePath);
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
string json = JsonSerializer.Serialize(data, options);
|
||||
File.WriteAllText(filePath, json);
|
||||
|
||||
var archives = Directory.GetFiles(configDir, "previous_tvmaze_updates_*.json")
|
||||
.Select(f => new FileInfo(f))
|
||||
.OrderByDescending(f => f.CreationTimeUtc)
|
||||
.ToList();
|
||||
|
||||
foreach (var oldFile in archives.Skip(5))
|
||||
{
|
||||
oldFile.Delete();
|
||||
}
|
||||
|
||||
Log.Information("Saved {Count} series updates to {FilePath}", data.Count, filePath);
|
||||
}
|
||||
|
||||
|
||||
private static void CreateDirectories(string rootDir)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(rootDir, "log"));
|
||||
@@ -102,4 +157,11 @@ namespace TvSyncApp
|
||||
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "crew"));
|
||||
}
|
||||
}
|
||||
|
||||
public class SeriesUpdate
|
||||
{
|
||||
public int SeriesId { get; set; }
|
||||
public long Timestamp { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user