Продолжение работы

This commit is contained in:
2025-05-13 19:22:41 +04:00
parent aad2081912
commit 83ee914991
64 changed files with 1252 additions and 1229 deletions
+13 -31
View File
@@ -8,6 +8,16 @@ namespace Pf2eDataScrapper.Helpers;
/// </summary>
public class DatabaseHelper
{
/// <summary>
/// Context для доступа к базе данных.
/// </summary>
public Pf2eDbContext Context { get; }
/// <summary>
/// Сервис для работы с URL-Slug. Используется для преобразования названий в URL-friendly формат.
/// </summary>
public SlugHelper Slugger { get; }
/// <summary>
/// Последний найденный идентификатор сущности.
/// Используется для генерации новых ID сущностей.
@@ -19,18 +29,10 @@ public class DatabaseHelper
/// </summary>
public int NextEntityId => ++_id;
/// <summary>
/// Сервис для работы с URL-Slug. Используется для преобразования названий в URL-friendly формат.
/// </summary>
public readonly SlugHelper Slugger = new();
/// <summary>
/// Context для доступа к базе данных.
/// </summary>
public readonly Pf2eDbContext Context = new();
private DatabaseHelper()
private DatabaseHelper(Pf2eDbContext context, SlugHelper slugHelper)
{
Context = context;
Slugger = slugHelper;
_id = Context.Entities.Any() ? Context.Entities.Max(e => e.Id) : 0;
}
@@ -41,24 +43,4 @@ public class DatabaseHelper
{
return Slugger.GenerateSlug(name);
}
/// <summary>
/// Возвращает singleton-инстанцию класса <see cref="DatabaseHelper"/>.
/// </summary>
public static readonly DatabaseHelper Instance = new();
/// <summary>
/// Контекст для доступа к базе данных.
/// </summary>
public static Pf2eDbContext DbContext => Instance.Context;
/// <summary>
/// Обновляет контекст и сохраняет изменения в базе данных.
/// </summary>
public static void ResetContext()
{
Instance.Context.SaveChanges();
// Instance.Context.Dispose();
// Instance.Context = new();
}
}
+156
View File
@@ -0,0 +1,156 @@
namespace Pf2eDataScrapper.Processors;
internal class ProcessProgress
{
/// <summary>
/// Прогресс обработки обработчиков <see cref="IProcessor"/>.
/// </summary>
public ProgressItem Processors { get; } = new();
/// <summary>
/// Прогресс работы конкретного обработчика <see cref="IProcessor"/>.
/// </summary>
public ProgressItem Local { get; } = new();
/// <summary>
/// Конструктор класса <see cref="ProcessProgress"/>.
/// </summary>
public ProcessProgress()
{
Processors.Update += Update;
Local.Update += Update;
}
/// <summary>
/// Событие обновления
/// </summary>
private void Update()
{
ProgressUpdate?.Invoke(this);
}
public void Message(string message)
{
ProgressMessage?.Invoke(this, message);
}
public delegate void ProgressUpdateDelegate(ProcessProgress sender);
public event ProgressUpdateDelegate? ProgressUpdate;
public delegate void ProgressMessageDelegate(ProcessProgress sender, string message);
public event ProgressMessageDelegate? ProgressMessage;
/// <summary>
/// Класс прогресса.
/// </summary>
public class ProgressItem
{
/// <summary>
/// Количество обрабатываемых процессов.
/// </summary>
public int Count
{
get => _count;
set
{
_count = value;
Update?.Invoke();
}
}
private int _count = 0;
/// <summary>
/// Индекс текущего обрабатываемого процесса.
/// </summary>
public int Current
{
get => _current;
set
{
_current = value;
Update?.Invoke();
}
}
private int _current = 0;
/// <summary>
/// Название текущего обрабатываемого процесса.
/// </summary>
public string Text
{
get => _text;
set
{
_text = value;
Update?.Invoke();
}
}
private string _text = "";
/// <summary>
/// Значение прогресса.
/// </summary>
public double Percent => Count > 0 ? 1.0 * Current / Count : 1.0;
/// <summary>
/// Сброс прогресса до нуля и установка нового максимального значения.
/// </summary>
/// <param name="max"></param>
public void Reset(int max = 0)
{
_count = max;
_current = 0;
_text = "";
Update?.Invoke();
}
/// <summary>
/// Отмечает начало работы над процессом, описываемым строкой <paramref name="text"/>.
/// </summary>
/// <param name="text">Строка, описывающая процесс.</param>
public void Start(string text)
{
_text = text;
Update?.Invoke();
}
/// <summary>
/// Отмечает конец работы над процессом и увеличивает значение прогресса.
/// </summary>
public void End()
{
_text = "";
_current++;
Update?.Invoke();
}
/// <summary>
/// Отмечает прогресс завершённым.
/// </summary>
public void Complete()
{
_current = _count;
_text = "Готово!";
Update?.Invoke();
}
/// <summary>
/// Получение строкового представления прогресса.
/// </summary>
/// <returns>Строковое представление прогресса.</returns>
public override string ToString()
{
return $"{Text} [{Current} / {Count}; {Percent:P}]";
}
/// <summary>
/// Делегат события обновления.
/// </summary>
public delegate void UpdateDelegate();
/// <summary>
/// Событие обновление.
/// </summary>
public event UpdateDelegate? Update;
}
}