Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor models to records #172

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ dotnet_diagnostic.CA1825.severity = warning

# CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly
dotnet_diagnostic.CA1826.severity = warning
dotnet_code_quality.CA1826.exclude_ordefault_methods = true

# CA1827: Do not use Count() or LongCount() when Any() can be used
dotnet_diagnostic.CA1827.severity = warning
Expand Down
14 changes: 7 additions & 7 deletions src/SIL.Machine.AspNetCore/Models/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public enum BuildJobRunner
ClearML
}

public class Build
public record Build
{
public string BuildId { get; set; } = default!;
public BuildJobState JobState { get; set; }
public string JobId { get; set; } = default!;
public BuildJobRunner JobRunner { get; set; }
public string Stage { get; set; } = default!;
public string? Options { get; set; } = default;
public required string BuildId { get; init; }
public required BuildJobState JobState { get; init; }
public required string JobId { get; init; }
public required BuildJobRunner JobRunner { get; init; }
public required string Stage { get; init; }
public string? Options { get; set; }
}
16 changes: 8 additions & 8 deletions src/SIL.Machine.AspNetCore/Models/ClearMLMetricsEvent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace SIL.Machine.AspNetCore.Models;

public class ClearMLMetricsEvent
public record ClearMLMetricsEvent
{
public string Metric { get; set; } = default!;
public string Variant { get; set; } = default!;
public double Value { get; set; }
public double MinValue { get; set; }
public int MinValueIteration { get; set; }
public double MaxValue { get; set; }
public int MaxValueIteration { get; set; }
public required string Metric { get; init; }
public required string Variant { get; init; }
public required double Value { get; init; }
public required double MinValue { get; init; }
public required int MinValueIteration { get; init; }
public required double MaxValue { get; init; }
public required int MaxValueIteration { get; init; }
}
4 changes: 2 additions & 2 deletions src/SIL.Machine.AspNetCore/Models/ClearMLProject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SIL.Machine.AspNetCore.Models;

public class ClearMLProject
public record ClearMLProject
{
public string Id { get; set; } = default!;
public required string Id { get; init; }
}
25 changes: 14 additions & 11 deletions src/SIL.Machine.AspNetCore/Models/ClearMLTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ public enum ClearMLTaskStatus
Unknown
}

public class ClearMLTask
public record ClearMLTask
{
public string Id { get; set; } = default!;
public string Name { get; set; } = default!;
public ClearMLProject Project { get; set; } = default!;
public ClearMLTaskStatus Status { get; set; }
public string StatusReason { get; set; } = default!;
public string StatusMessage { get; set; } = default!;
public DateTime Created { get; set; }
public int LastIteration { get; set; }
public int ActiveDuration { get; set; }
public Dictionary<string, Dictionary<string, ClearMLMetricsEvent>> LastMetrics { get; set; } = default!;
public required string Id { get; init; }
public required string Name { get; init; }
public required ClearMLProject Project { get; init; }
public required ClearMLTaskStatus Status { get; init; }
public required string StatusReason { get; init; }
public required string StatusMessage { get; init; }
public required DateTime Created { get; init; }
public required int LastIteration { get; init; }
public required int ActiveDuration { get; init; }
public required IReadOnlyDictionary<
string,
IReadOnlyDictionary<string, ClearMLMetricsEvent>
> LastMetrics { get; init; }
}
24 changes: 12 additions & 12 deletions src/SIL.Machine.AspNetCore/Models/Corpus.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace SIL.Machine.AspNetCore.Models;

public class Corpus
public record Corpus
{
public string Id { get; set; } = default!;
public string SourceLanguage { get; set; } = default!;
public string TargetLanguage { get; set; } = default!;
public bool TrainOnAll { get; set; }
public bool PretranslateAll { get; set; }
public Dictionary<string, HashSet<int>>? TrainOnChapters { get; set; }
public Dictionary<string, HashSet<int>>? PretranslateChapters { get; set; }
public HashSet<string> TrainOnTextIds { get; set; } = default!;
public HashSet<string> PretranslateTextIds { get; set; } = default!;
public List<CorpusFile> SourceFiles { get; set; } = default!;
public List<CorpusFile> TargetFiles { get; set; } = default!;
public required string Id { get; init; }
public required string SourceLanguage { get; init; }
public required string TargetLanguage { get; init; }
public required bool TrainOnAll { get; init; }
public required bool PretranslateAll { get; init; }
public IReadOnlyDictionary<string, IReadOnlySet<int>>? TrainOnChapters { get; init; }
public IReadOnlyDictionary<string, IReadOnlySet<int>>? PretranslateChapters { get; init; }
public required IReadOnlySet<string> TrainOnTextIds { get; init; }
public required IReadOnlySet<string> PretranslateTextIds { get; init; }
public required IReadOnlyList<CorpusFile> SourceFiles { get; init; }
public required IReadOnlyList<CorpusFile> TargetFiles { get; init; }
}
8 changes: 4 additions & 4 deletions src/SIL.Machine.AspNetCore/Models/CorpusFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public enum FileFormat
Paratext = 1
}

public class CorpusFile
public record CorpusFile
{
public string Location { get; set; } = default!;
public FileFormat Format { get; set; }
public string TextId { get; set; } = default!;
public required string Location { get; init; }
public required FileFormat Format { get; init; }
public required string TextId { get; init; }
}
8 changes: 4 additions & 4 deletions src/SIL.Machine.AspNetCore/Models/Lock.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace SIL.Machine.AspNetCore.Models;

public class Lock
public record Lock
{
public string Id { get; set; } = default!;
public DateTime? ExpiresAt { get; set; }
public string HostId { get; set; } = default!;
public required string Id { get; init; }
public DateTime? ExpiresAt { get; init; }
public required string HostId { get; init; }
}
10 changes: 5 additions & 5 deletions src/SIL.Machine.AspNetCore/Models/ModelDownloadUrl.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace SIL.Machine.AspNetCore.Models;
namespace SIL.Machine.AspNetCore.Models;

public class ModelDownloadUrl
public record ModelDownloadUrl
{
public string Url { get; set; } = default!;
public int ModelRevision { get; set; } = default!;
public DateTime ExpiresAt { get; set; } = default!;
public required string Url { get; init; }
public required int ModelRevision { get; init; }
public required DateTime ExpiresAt { get; init; }
}
10 changes: 5 additions & 5 deletions src/SIL.Machine.AspNetCore/Models/Pretranslation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace SIL.Machine.AspNetCore.Models;

public class Pretranslation
public record Pretranslation
{
public string CorpusId { get; set; } = default!;
public string TextId { get; set; } = default!;
public List<string> Refs { get; set; } = default!;
public string Translation { get; set; } = default!;
public required string CorpusId { get; init; }
public required string TextId { get; init; }
public required IReadOnlyList<string> Refs { get; init; }
public required string Translation { get; init; }
}
12 changes: 6 additions & 6 deletions src/SIL.Machine.AspNetCore/Models/RWLock.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace SIL.Machine.AspNetCore.Models;

public class RWLock : IEntity
public record RWLock : IEntity
{
public string Id { get; set; } = default!;
public int Revision { get; set; }
public Lock? WriterLock { get; set; }
public List<Lock> ReaderLocks { get; set; } = new List<Lock>();
public List<Lock> WriterQueue { get; set; } = new List<Lock>();
public string Id { get; set; } = "";
public int Revision { get; set; } = 1;
public Lock? WriterLock { get; init; }
public required IReadOnlyList<Lock> ReaderLocks { get; init; }
public required IReadOnlyList<Lock> WriterQueue { get; init; }

public bool IsAvailableForReading()
{
Expand Down
12 changes: 6 additions & 6 deletions src/SIL.Machine.AspNetCore/Models/TrainSegmentPair.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace SIL.Machine.AspNetCore.Models;

public class TrainSegmentPair : IEntity
public record TrainSegmentPair : IEntity
{
public string Id { get; set; } = default!;
public string Id { get; set; } = "";
public int Revision { get; set; } = 1;
public string TranslationEngineRef { get; set; } = default!;
public string Source { get; set; } = default!;
public string Target { get; set; } = default!;
public bool SentenceStart { get; set; }
public required string TranslationEngineRef { get; init; }
public required string Source { get; init; }
public required string Target { get; init; }
public required bool SentenceStart { get; init; }
}
16 changes: 8 additions & 8 deletions src/SIL.Machine.AspNetCore/Models/TranslationEngine.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace SIL.Machine.AspNetCore.Models;

public class TranslationEngine : IEntity
public record TranslationEngine : IEntity
{
public string Id { get; set; } = default!;
public string Id { get; set; } = "";
public int Revision { get; set; } = 1;
public string EngineId { get; set; } = default!;
public string SourceLanguage { get; set; } = default!;
public string TargetLanguage { get; set; } = default!;
public bool IsModelPersisted { get; set; }
public int BuildRevision { get; set; }
public Build? CurrentBuild { get; set; }
public required string EngineId { get; init; }
public required string SourceLanguage { get; init; }
public required string TargetLanguage { get; init; }
public required bool IsModelPersisted { get; init; }
public int BuildRevision { get; init; }
public Build? CurrentBuild { get; init; }
}
2 changes: 1 addition & 1 deletion src/SIL.Machine.AspNetCore/SIL.Machine.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.14" />
<PackageReference Include="Python.Included" Version="3.11.4" />
<PackageReference Include="Serval.Grpc" Version="0.16.0" Condition="!Exists('..\..\..\serval\src\Serval.Grpc\Serval.Grpc.csproj')" />
<PackageReference Include="SIL.DataAccess" Version="0.5.2" Condition="!Exists('..\..\..\serval\src\SIL.DataAccess\SIL.DataAccess.csproj')" />
<PackageReference Include="SIL.DataAccess" Version="0.6.0" Condition="!Exists('..\..\..\serval\src\SIL.DataAccess\SIL.DataAccess.csproj')" />
<PackageReference Include="SIL.WritingSystems" Version="12.0.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private async Task UpdateTrainJobStatus(

private static double GetMetric(ClearMLTask task, string metric, string variant)
{
if (!task.LastMetrics.TryGetValue(metric, out Dictionary<string, ClearMLMetricsEvent>? metricVariants))
if (!task.LastMetrics.TryGetValue(metric, out IReadOnlyDictionary<string, ClearMLMetricsEvent>? metricVariants))
return 0;

if (!metricVariants.TryGetValue(variant, out ClearMLMetricsEvent? metricEvent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ public async Task<IDistributedReaderWriterLock> CreateAsync(
{
try
{
await _locks.InsertAsync(new RWLock { Id = id }, cancellationToken);
await _locks.InsertAsync(
new RWLock
{
Id = id,
ReaderLocks = [],
WriterQueue = []
},
cancellationToken
);
}
catch (DuplicateKeyException)
{
Expand Down
1 change: 1 addition & 0 deletions src/SIL.Machine.AspNetCore/Services/InMemoryStorage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SIL.ObjectModel;
using static SIL.Machine.AspNetCore.Utils.SharedFileUtils;

namespace SIL.Machine.AspNetCore.Services;
Expand Down
6 changes: 2 additions & 4 deletions src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class LanguageTagService : ILanguageTagService

private readonly Dictionary<string, string> _flores200Languages;

private static readonly Regex LangTagPattern = new Regex(
"(?'language'[a-zA-Z]{2,8})([_-](?'script'[a-zA-Z]{4}))?",
RegexOptions.ExplicitCapture
);
private static readonly Regex LangTagPattern =
new("(?'language'[a-zA-Z]{2,8})([_-](?'script'[a-zA-Z]{4}))?", RegexOptions.ExplicitCapture);

public LanguageTagService()
{
Expand Down
1 change: 1 addition & 0 deletions src/SIL.Machine.AspNetCore/Services/LocalStorage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SIL.ObjectModel;
using static SIL.Machine.AspNetCore.Utils.SharedFileUtils;

namespace SIL.Machine.AspNetCore.Services;
Expand Down
Loading
Loading