check point

This commit is contained in:
meelstorm
2025-07-14 12:03:59 +02:00
commit d3cb790bd9
431 changed files with 44078 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
public class GroupIndex:IIndex
{
private readonly int _groupPathPart;
private readonly int _goodBadPathPart;
public GroupIndex(int groupPathPart,int goodBadPathPart)
{
_groupPathPart = groupPathPart;
_goodBadPathPart = goodBadPathPart;
}
private readonly object _lock = new object();
private readonly Dictionary<string, ProductResults> _groups = new Dictionary<string, ProductResults>();
public void Track(string filePath)
{
var parts = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar);
string groupKey;
try
{
groupKey = Path.Combine(Enumerable.Range(0, _groupPathPart + 1).Select(x => parts[x]).ToArray());
}
catch (Exception e)
{
Console.WriteLine($"Tracking {filePath}");
Console.WriteLine(e);
return;
}
string badGoodPart;
if (_goodBadPathPart == -1) badGoodPart = RingbufferPath.GOOD_DIR;
else
badGoodPart = parts[_goodBadPathPart];
lock (_lock)
{
if (!_groups.ContainsKey(groupKey))
{
_groups[groupKey] = new ProductResults();
}
if (badGoodPart == RingbufferPath.GOOD_DIR)
{
_groups[groupKey].Good.Insert(0,filePath);
}
else
{
_groups[groupKey].Bad.Insert(0, filePath);
}
}
}
public List<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
{
lock (_lock)
{
var filesToDelete = new List<string>();
foreach (var p in _groups)
{
var goodToDelete = p.Value.Good.Skip(allowedAmountGood).ToList();
var badToDelete = p.Value.Bad.Skip(allowedAmountBad).ToList();
goodToDelete.ForEach(x => p.Value.Good.Remove(x));
badToDelete.ForEach(x => p.Value.Bad.Remove(x));
filesToDelete.AddRange(goodToDelete);
filesToDelete.AddRange(badToDelete);
}
return filesToDelete;
}
}
private class ProductResults
{
public List<string> Good { get; set; }=new List<string>();
public List<string> Bad { get; set; }=new List<string>();
}
}
}

View File

@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
/// <summary>
/// Ringbuffer\Product\Good\File
/// </summary>
public class ProductIndex : IIndex
{
private readonly object _lock = new object();
private readonly Dictionary<string, ProductResults> _products = new Dictionary<string, ProductResults>();
public void Track(string filePath)
{
var product = Path.GetDirectoryName(Path.GetDirectoryName(filePath)).Split(Path.DirectorySeparatorChar)
.Last();
var result = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar).Last();
lock (_lock)
{
if (!_products.ContainsKey(product))
_products[product] = new ProductResults
{
Good = new List<string>(),
Bad = new List<string>()
};
if (result == RingbufferFolder.GOOD_DIR)
{
if (!_products[product].Good.Contains(filePath))
_products[product].Good.Insert(0, filePath);
}
else
{
if (!_products[product].Bad.Contains(filePath))
_products[product].Bad.Insert(0, filePath);
}
}
}
public List<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
{
lock (_lock)
{
var filesToDelete = new List<string>();
foreach (var p in _products)
{
var goodToDelete = p.Value.Good.Skip(allowedAmountGood).ToList();
var badToDelete = p.Value.Bad.Skip(allowedAmountGood).ToList();
goodToDelete.ForEach(x => p.Value.Good.Remove(x));
badToDelete.ForEach(x => p.Value.Bad.Remove(x));
filesToDelete.AddRange(goodToDelete);
filesToDelete.AddRange(badToDelete);
}
return filesToDelete;
}
}
private class ProductResults
{
public List<string> Good { get; set; }
public List<string> Bad { get; set; }
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
/// <summary>
/// Ringbuffer\Good\Product\File
/// </summary>
public class RootIndex : IIndex
{
private readonly List<string> _trackedGoodFiles = new List<string>();
private readonly List<string> _trackedBadFiles = new List<string>();
private readonly object _lock = new object();
public void Track(string filePath)
{
lock (_lock)
{
if (IsGood(filePath))
{
if (!_trackedGoodFiles.Contains(filePath))
_trackedGoodFiles.Insert(0, filePath);
}
else
{
if (!_trackedBadFiles.Contains(filePath))
_trackedBadFiles.Insert(0, filePath);
}
}
}
public List<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
{
lock (_lock)
{
var resGood = _trackedGoodFiles.Skip(allowedAmountGood).ToList();
var resBad = _trackedBadFiles.Skip(allowedAmountBad).ToList();
resGood.ForEach(x => { _trackedGoodFiles.Remove(x); });
resBad.ForEach(x => { _trackedBadFiles.Remove(x); });
return resGood.Concat(resBad).ToList();
}
}
private bool IsGood(string filePath)
{
var folder = Path.GetDirectoryName(Path.GetDirectoryName(filePath)).Split(Path.DirectorySeparatorChar)
.Last();
return folder == RingbufferFolder.GOOD_DIR;
}
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Configurations>Debug;Release;CPU</Configurations>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Factories\**" />
<EmbeddedResource Remove="Factories\**" />
<None Remove="Factories\**" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace Inspectron.Ringbuffer.Interfaces
{
public interface IFileIdentity
{
string WriteTo(string directoryPath);
}
}

View File

@@ -0,0 +1,8 @@
namespace Inspectron.Ringbuffer.Interfaces
{
public interface IFileIdentityFactory<T>
{
IFileIdentity CreateFrom(T image);
IFileVerifier CreateVerifier();
}
}

View File

@@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace Inspectron.Ringbuffer.Interfaces
{
public interface IFileVerifier
{
bool IsMainFile(string filePath);
List<string> LinkedFiles(string filePath);
}
}

View File

@@ -0,0 +1,8 @@
namespace Inspectron.Ringbuffer.Interfaces
{
public interface IFolderWritter
{
void WriteGood(IFileIdentity fileIdentity, string product);
void WriteBad(IFileIdentity fileIdentity, string product);
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace Inspectron.Ringbuffer.Interfaces
{
public interface IIndex
{
void Track(string filePath);
List<string> GetFilesToDelete(int allowedAmountGood,int allowedAmountBad);
}
}

View File

@@ -0,0 +1,25 @@
namespace Inspectron.Ringbuffer
{
public class RingbufferConfiguration
{
/// <summary>
/// How many images should be present in Good folder
/// After reaching this amount older images will be deleted
/// </summary>
public int MaxAmountOfGoodImages { get; set; }
/// <summary>
/// How many images should be present in Bad folder
/// After reaching this amount older images will be deleted
/// </summary>
public int MaxAmountOfBadImages { get; set; }
/// <summary>
/// If enabled folder structure will be like "Good\MyProduct\001.bmp
/// If disabled folder structure will be like "MyProduct\Good\001.bmp
/// </summary>
public bool ProductFirst { get; set; }
}
}

View File

@@ -0,0 +1,150 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
[Obsolete]
public class RingbufferFolder:IFolderWritter
{
private string _ringbufferFolder;
private RingbufferConfiguration _configuration;
private IFileVerifier _fileVerifier;
private IIndex _index;
public RingbufferFolder(string ringbufferFolder, RingbufferConfiguration configuration, IFileVerifier fileVerifier)
{
_ringbufferFolder = ringbufferFolder;
_configuration = configuration;
_fileVerifier = fileVerifier;
EnsureDirectory(_ringbufferFolder);
InitIndex();
var th1 = new Thread(RemoveLoop);
th1.Priority = ThreadPriority.Normal;
th1.Start();
var th2 = new Thread(SaveLoop);
th2.Priority = ThreadPriority.Normal;
th2.Start();
}
private void RemoveLoop()
{
while (true)
{
var filesToDelete=_index.GetFilesToDelete(_configuration.MaxAmountOfGoodImages, _configuration.MaxAmountOfBadImages);
filesToDelete.ForEach(x=>
{
DeleteFile(x);
_fileVerifier.LinkedFiles(x).ForEach(DeleteFile);
});
Thread.Sleep(1000);
}
}
ConcurrentQueue<SaveCandidate> _saveQueue = new ConcurrentQueue<SaveCandidate>();
object _lock=new object();
private void EnsureDirectory(string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
private void SaveLoop()
{
while (true)
{
if (_saveQueue.TryDequeue(out var candidate))
{
lock (_lock)
{
EnsureDirectory(candidate.TargetDirectory);
var written=candidate.FileIdentity.WriteTo(candidate.TargetDirectory);
_index.Track(written);
}
}
else
{
Thread.Sleep(1);
}
}
}
private void InitIndex()
{
if (_configuration.ProductFirst)
{
_index=new ProductIndex();
}
else
{
_index=new RootIndex();
}
IndexFiles();
}
public void DeleteFile(string fileToDelete)
{
var fi = new System.IO.FileInfo(fileToDelete);
if (fi.Exists)
{
lock (_lock)
{
fi.Delete();
}
fi.Refresh();
while (fi.Exists)
{
System.Threading.Thread.Sleep(10);
fi.Refresh();
}
}
}
private void IndexFiles()
{
var allFiles=Directory.GetFiles(_ringbufferFolder, "*.*", SearchOption.AllDirectories);
var verifiedFiles = allFiles.Where(x => _fileVerifier.IsMainFile(x)).ToList();
var orderedFiles = verifiedFiles.OrderByDescending(x => new FileInfo(x).CreationTimeUtc).ToList();
orderedFiles.ForEach(_index.Track);
}
public const string GOOD_DIR = "Good";
public const string BAD_DIR = "Bad";
public void WriteGood(IFileIdentity fileIdentity,string product)
{
var path = _configuration.ProductFirst
? Path.Combine(_ringbufferFolder, product, GOOD_DIR)
: Path.Combine(_ringbufferFolder, GOOD_DIR, product);
_saveQueue.Enqueue(new SaveCandidate(fileIdentity, path));
}
public void WriteBad(IFileIdentity fileIdentity, string product)
{
var path = _configuration.ProductFirst
? Path.Combine(_ringbufferFolder, product, BAD_DIR)
: Path.Combine(_ringbufferFolder, BAD_DIR, product);
_saveQueue.Enqueue(new SaveCandidate(fileIdentity, path));
}
}
}

View File

@@ -0,0 +1,182 @@
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
public class RingbufferPath : IFolderWritter,IDisposable
{
private readonly string _pathFormat;
private readonly RingbufferPathConfiguration _configuration;
private readonly IFileVerifier _fileVerifier;
public const string PRODUCT = "$PRODUCT$";
public const string GOODBAD = "$GOODBAD$";
public const string DATE = "$date$";
public const string MONTH = "$month$";
public const string YEAR = "$year$";
private IIndex _index;
ConcurrentQueue<SaveCandidate> _saveQueue = new ConcurrentQueue<SaveCandidate>();
object _lock = new object();
private string _ringbufferFolder;
private int _productPartId;
private int _goodBadPartId;
private int _subPartId;
public RingbufferPath(RingbufferPathConfiguration configuration, IFileVerifier fileVerifier)
{
_pathFormat = configuration.PathFormat;
_configuration = configuration;
_fileVerifier = fileVerifier;
var pathFormat = configuration.PathFormat;
var pathParts = Path.Combine(configuration.RootDirectory,pathFormat).Split(new char[]{ Path.DirectorySeparatorChar },StringSplitOptions.RemoveEmptyEntries);
_ringbufferFolder = configuration.RootDirectory;
_productPartId = -1;
_goodBadPartId = -1;
for (int i = 1; i < pathParts.Length; i++)
{
if (pathParts[i] == PRODUCT) _productPartId = i;
if (pathParts[i] == GOODBAD) _goodBadPartId = i;
}
//if(_productPartId==-1||_goodBadPartId==-1)throw new Exception("one of the markers is missing");
EnsureDirectory(_ringbufferFolder);
InitIndex();
var th1 = new Thread(RemoveLoop);
th1.IsBackground = true;
th1.Priority = ThreadPriority.Normal;
th1.Start();
var th2 = new Thread(SaveLoop);
th2.IsBackground = true;
th2.Priority = ThreadPriority.Normal;
th2.Start();
}
private void InitIndex()
{
_index = new GroupIndex(_configuration.GroupPartId,_goodBadPartId);
IndexFiles();
}
private void IndexFiles()
{
var allFiles = Directory.GetFiles(_ringbufferFolder, "*.*", SearchOption.AllDirectories);
var verifiedFiles = allFiles.Where(x => _fileVerifier.IsMainFile(x)).ToList();
var orderedFiles = verifiedFiles.OrderByDescending(x => new FileInfo(x).CreationTimeUtc).ToList();
orderedFiles.ForEach(_index.Track);
}
private void RemoveLoop()
{
while (!_stop)
{
var filesToDelete = _index.GetFilesToDelete(_configuration.MaxAmountOfGoodImages, _configuration.MaxAmountOfBadImages);
filesToDelete.ForEach(x =>
{
DeleteFile(x);
_fileVerifier.LinkedFiles(x).ForEach(DeleteFile);
});
Thread.Sleep(1000);
}
}
public void DeleteFile(string fileToDelete)
{
var fi = new System.IO.FileInfo(fileToDelete);
if (fi.Exists)
{
lock (_lock)
{
fi.Delete();
}
fi.Refresh();
while (fi.Exists)
{
System.Threading.Thread.Sleep(10);
fi.Refresh();
}
}
}
private bool _stop = false;
private void SaveLoop()
{
while (!_stop)
{
if (_saveQueue.TryDequeue(out var candidate))
{
lock (_lock)
{
EnsureDirectory(candidate.TargetDirectory);
var written = candidate.FileIdentity.WriteTo(candidate.TargetDirectory);
_index.Track(written);
}
}
else
{
Thread.Sleep(1);
}
}
}
private void EnsureDirectory(string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
public static string GOOD_DIR = "Good";
public static string BAD_DIR = "Bad";
string TransformPath(string originalPath,bool isGood,string product)
{
var time = DateTime.Now;
var res= originalPath.Replace(PRODUCT, product)
.Replace(GOODBAD, isGood ? GOOD_DIR : BAD_DIR)
.Replace(MONTH,time.ToString("MMMM"))
.Replace(YEAR,time.Year.ToString())
.Replace(DATE,time.ToString("dd.MM.yyyy"));
return Path.Combine(_configuration.RootDirectory, res);
}
public void WriteGood(IFileIdentity fileIdentity, string product)
{
var path = TransformPath(_configuration.PathFormat, true, product);
_saveQueue.Enqueue(new SaveCandidate(fileIdentity, path));
}
public void WriteBad(IFileIdentity fileIdentity, string product)
{
var path = TransformPath(_configuration.PathFormat, false, product);
_saveQueue.Enqueue(new SaveCandidate(fileIdentity, path));
}
public void Dispose()
{
_stop = true;
}
}
}

View File

@@ -0,0 +1,35 @@
namespace Inspectron.Ringbuffer
{
public class RingbufferPathConfiguration
{
/// <summary>
/// How many images should be present in Good folder
/// After reaching this amount older images will be deleted
/// </summary>
public int MaxAmountOfGoodImages { get; set; }
/// <summary>
/// How many images should be present in Bad folder
/// After reaching this amount older images will be deleted
/// </summary>
public int MaxAmountOfBadImages { get; set; }
/// <summary>
/// Where to store images
/// Supports $PRODUCT$ mark to put product name into its place
/// Supports $GOODBAD$ mark to put product quality into its place
/// </summary>
public string PathFormat { get; set; }
/// <summary>
/// Specifies which part of the path should be checked for maximum files
/// </summary>
public int GroupPartId { get; set; }
/// <summary>
/// Ringbuffer's root directory
/// </summary>
public string RootDirectory { get; set; } = "./";
}
}

View File

@@ -0,0 +1,16 @@
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
public class SaveCandidate
{
public SaveCandidate(IFileIdentity fileIdentity, string targetDirectory)
{
FileIdentity = fileIdentity;
TargetDirectory = targetDirectory;
}
public IFileIdentity FileIdentity { get; set; }
public string TargetDirectory { get; set; }
}
}