Refactor repo with ponytail cleanup (#31)
This commit is contained in:
@@ -1,160 +1,123 @@
|
||||
/*
|
||||
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
||||
* Copyright (C) 2025 Prime Build
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3 only.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace ThreadPilot.Models
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration model for process monitoring and power plan associations.
|
||||
/// </summary>
|
||||
public partial class ProcessMonitorConfiguration : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string defaultPowerPlanGuid = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string defaultPowerPlanName = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isEventBasedMonitoringEnabled = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isFallbackPollingEnabled = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private int pollingIntervalSeconds = 5;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool preventDuplicatePowerPlanChanges = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private int powerPlanChangeDelayMs = 250;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool enableLogging = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private List<ProcessPowerPlanAssociation> associations = new();
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime lastSavedDate = DateTime.Now;
|
||||
|
||||
[ObservableProperty]
|
||||
private string configurationVersion = "1.0";
|
||||
|
||||
public ProcessMonitorConfiguration()
|
||||
{
|
||||
this.Associations = new List<ProcessPowerPlanAssociation>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all enabled associations sorted by priority (descending).
|
||||
/// </summary>
|
||||
public IEnumerable<ProcessPowerPlanAssociation> GetEnabledAssociations()
|
||||
{
|
||||
return this.Associations
|
||||
.Where(a => a.IsEnabled)
|
||||
.OrderByDescending(a => a.Priority)
|
||||
.ThenBy(a => a.ExecutableName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the best matching association for a process.
|
||||
/// </summary>
|
||||
public ProcessPowerPlanAssociation? FindMatchingAssociation(ProcessModel process)
|
||||
{
|
||||
return this.GetEnabledAssociations()
|
||||
.FirstOrDefault(a => a.MatchesProcess(process));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds association by executable name.
|
||||
/// </summary>
|
||||
public ProcessPowerPlanAssociation? FindAssociationByExecutable(string executableName)
|
||||
{
|
||||
return this.Associations
|
||||
.FirstOrDefault(a => a.MatchesExecutable(executableName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds or updates an association.
|
||||
/// </summary>
|
||||
public void AddOrUpdateAssociation(ProcessPowerPlanAssociation association)
|
||||
{
|
||||
var existing = this.Associations.FirstOrDefault(a => a.Id == association.Id);
|
||||
if (existing != null)
|
||||
{
|
||||
var index = this.Associations.IndexOf(existing);
|
||||
this.Associations[index] = association;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Associations.Add(association);
|
||||
}
|
||||
this.LastSavedDate = DateTime.Now;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an association.
|
||||
/// </summary>
|
||||
public bool RemoveAssociation(string associationId)
|
||||
{
|
||||
var association = this.Associations.FirstOrDefault(a => a.Id == associationId);
|
||||
if (association != null)
|
||||
{
|
||||
this.Associations.Remove(association);
|
||||
this.LastSavedDate = DateTime.Now;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the configuration.
|
||||
/// </summary>
|
||||
public List<string> Validate()
|
||||
{
|
||||
var errors = new List<string>();
|
||||
|
||||
if (this.PollingIntervalSeconds < 1)
|
||||
{
|
||||
errors.Add("Polling interval must be at least 1 second");
|
||||
}
|
||||
|
||||
if (this.PowerPlanChangeDelayMs < 0)
|
||||
{
|
||||
errors.Add("Power plan change delay cannot be negative");
|
||||
}
|
||||
|
||||
// Check for duplicate associations
|
||||
var duplicates = this.Associations
|
||||
.GroupBy(a => new { a.ExecutableName, a.MatchByPath })
|
||||
.Where(g => g.Count() > 1)
|
||||
.Select(g => g.Key.ExecutableName);
|
||||
|
||||
foreach (var duplicate in duplicates)
|
||||
{
|
||||
errors.Add($"Duplicate association found for executable: {duplicate}");
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace ThreadPilot.Models
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
public partial class ProcessMonitorConfiguration : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string defaultPowerPlanGuid = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string defaultPowerPlanName = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isEventBasedMonitoringEnabled = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isFallbackPollingEnabled = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private int pollingIntervalSeconds = 5;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool preventDuplicatePowerPlanChanges = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private int powerPlanChangeDelayMs = 250;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool enableLogging = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private List<ProcessPowerPlanAssociation> associations = new();
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime lastSavedDate = DateTime.Now;
|
||||
|
||||
[ObservableProperty]
|
||||
private string configurationVersion = "1.0";
|
||||
|
||||
public ProcessMonitorConfiguration()
|
||||
{
|
||||
this.Associations = new List<ProcessPowerPlanAssociation>();
|
||||
}
|
||||
|
||||
public IEnumerable<ProcessPowerPlanAssociation> GetEnabledAssociations()
|
||||
{
|
||||
return this.Associations
|
||||
.Where(a => a.IsEnabled)
|
||||
.OrderByDescending(a => a.Priority)
|
||||
.ThenBy(a => a.ExecutableName);
|
||||
}
|
||||
|
||||
public ProcessPowerPlanAssociation? FindMatchingAssociation(ProcessModel process)
|
||||
{
|
||||
return this.GetEnabledAssociations()
|
||||
.FirstOrDefault(a => a.MatchesProcess(process));
|
||||
}
|
||||
|
||||
public ProcessPowerPlanAssociation? FindAssociationByExecutable(string executableName)
|
||||
{
|
||||
return this.Associations
|
||||
.FirstOrDefault(a => a.MatchesExecutable(executableName));
|
||||
}
|
||||
|
||||
public void AddOrUpdateAssociation(ProcessPowerPlanAssociation association)
|
||||
{
|
||||
var existing = this.Associations.FirstOrDefault(a => a.Id == association.Id);
|
||||
if (existing != null)
|
||||
{
|
||||
var index = this.Associations.IndexOf(existing);
|
||||
this.Associations[index] = association;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Associations.Add(association);
|
||||
}
|
||||
this.LastSavedDate = DateTime.Now;
|
||||
}
|
||||
|
||||
public bool RemoveAssociation(string associationId)
|
||||
{
|
||||
var association = this.Associations.FirstOrDefault(a => a.Id == associationId);
|
||||
if (association != null)
|
||||
{
|
||||
this.Associations.Remove(association);
|
||||
this.LastSavedDate = DateTime.Now;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<string> Validate()
|
||||
{
|
||||
var errors = new List<string>();
|
||||
|
||||
if (this.PollingIntervalSeconds < 1)
|
||||
{
|
||||
errors.Add("Polling interval must be at least 1 second");
|
||||
}
|
||||
|
||||
if (this.PowerPlanChangeDelayMs < 0)
|
||||
{
|
||||
errors.Add("Power plan change delay cannot be negative");
|
||||
}
|
||||
|
||||
// Check for duplicate associations
|
||||
var duplicates = this.Associations
|
||||
.GroupBy(a => new { a.ExecutableName, a.MatchByPath })
|
||||
.Where(g => g.Count() > 1)
|
||||
.Select(g => g.Key.ExecutableName);
|
||||
|
||||
foreach (var duplicate in duplicates)
|
||||
{
|
||||
errors.Add($"Duplicate association found for executable: {duplicate}");
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user