Skip to content

Commit

Permalink
Change name Location to DataCenter
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dębowski committed Dec 9, 2024
1 parent ee40721 commit 1dc242d
Show file tree
Hide file tree
Showing 53 changed files with 2,989 additions and 197 deletions.
4 changes: 2 additions & 2 deletions src/hiPower.Abstracts/IDataCenterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public interface IDataCenterService
{
Task<ErrorOr<DataCenter>> CreateAsync(DataCenter location);
Task<ErrorOr<DataCenter>> UpdateAsync(string id, DataCenter location);
Task<ErrorOr<DataCenter>> CreateAsync(DataCenter dataCenter);
Task<ErrorOr<DataCenter>> UpdateAsync(string id, DataCenter dataCenter);
Task<ErrorOr<bool>> DeleteAsync (string id);
Task<ErrorOr<DataCenter>> GetAsync (string id);
Task<ErrorOr<IEnumerable<DataCenter>>> GetAsync ();
Expand Down
2 changes: 1 addition & 1 deletion src/hiPower.Abstracts/IRemoteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public interface IRemoteService
{
Task<ErrorOr<IEnumerable<ConfigSetting>>> GetConfigurationAsync (RemoteServiceOptions options);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetStatisticsAsync (RemoteServiceOptions options);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetStatisticsAsync (RemoteServiceOptions options, IEnumerable<string> statItemsList);
Task<ErrorOr<ServerInfo>> GetInfoAsync (RemoteServiceOptions options);
}
1 change: 1 addition & 0 deletions src/hiPower.Abstracts/IServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IServerService

Task<ErrorOr<IEnumerable<ConfigSetting>>> GetRemoteConfigurationAsync(string id);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteStatisticsAsync (string id);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteUptimeAsync (string id);
Task<ErrorOr<ServerInfo>> GetRemoteServerInfoAsync(string id);
}
4 changes: 2 additions & 2 deletions src/hiPower.Abstracts/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace hiPower.Abstracts;

public interface IUnitOfWork : IDisposable
{
IGenericRepository<ServerLocation> DataCenterRepository { get; }
IGenericRepository<Entity.ServerDetails> ServerRepository { get; }
IGenericRepository<Entity.DataCenter> DataCenterRepository { get; }
IGenericRepository<Entity.ServiceDetails> ServerRepository { get; }
Task SaveAsync ();
}
9 changes: 9 additions & 0 deletions src/hiPower.Common.Type/MonitorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace hiPower.Common.Type;

public enum MonitorState
{
None,
Ready,
Stop,
Suspend
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace hiPower.Common.Type;

public enum ServerState
public enum ServiceState
{
Unknown = 0,
On = 10,
Expand Down
24 changes: 12 additions & 12 deletions src/hiPower.Core/DataCenterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace hiPower.Core;
public class DataCenterService(IUnitOfWork unit,
IMapper mapper) : IDataCenterService
{
public async Task<ErrorOr<DataCenter>> CreateAsync (DataCenter location)
public async Task<ErrorOr<Dto.Manager.DataCenter>> CreateAsync (Dto.Manager.DataCenter dataCenter)
{
var result = await unit.DataCenterRepository.CreateAsync(location.Adapt<ServerLocation>());
var result = await unit.DataCenterRepository.CreateAsync(dataCenter.Adapt<Entity.DataCenter>());
await unit.SaveAsync ();
return mapper.Map<DataCenter> (result);
return mapper.Map<Dto.Manager.DataCenter> (result);
}

public async Task<ErrorOr<bool>> DeleteAsync (string id)
{
var servers = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(id.ToUpper()), null, null);
var servers = await unit.ServerRepository.GetAll(x => x.DataCenterId.Equals(id.ToUpper()), null, null);
if (servers.Any())
{
return DataCenterErrors.DeleteError;
Expand All @@ -26,39 +26,39 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)
return true;
}

public async Task<ErrorOr<DataCenter>> GetAsync (string id)
public async Task<ErrorOr<Dto.Manager.DataCenter>> GetAsync (string id)
{
var result = await unit.DataCenterRepository.GetAsync(x => x.Id.Equals(id.ToUpperInvariant()), null);
if (result is null)
{
return Error.NotFound();
}
return result.Adapt<DataCenter> ();
return result.Adapt<Dto.Manager.DataCenter> ();
}

public async Task<ErrorOr<IEnumerable<DataCenter>>> GetAsync ()
public async Task<ErrorOr<IEnumerable<Dto.Manager.DataCenter>>> GetAsync ()
{
var result = await unit.DataCenterRepository.GetAll(null, null,null);
return result.Adapt<IEnumerable<DataCenter>> ().ToErrorOr ();
return result.Adapt<IEnumerable<Dto.Manager.DataCenter>> ().ToErrorOr ();
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetServers (string id)
{
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(id.ToUpper()), null, null);
var result = await unit.ServerRepository.GetAll(x => x.DataCenterId.Equals(id.ToUpper()), null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
.ToErrorOr();
}

public async Task<ErrorOr<DataCenter>> UpdateAsync (string id, DataCenter dataCenter)
public async Task<ErrorOr<Dto.Manager.DataCenter>> UpdateAsync (string id, Dto.Manager.DataCenter dataCenter)
{
if (!id.Equals(dataCenter.Id, StringComparison.OrdinalIgnoreCase))
{
return Error.NotFound (dataCenter.Id);
}
var result = unit.DataCenterRepository
.Update(dataCenter.Adapt<ServerLocation>())
.Update(dataCenter.Adapt<Entity.DataCenter>())
.ToErrorOr();
await unit.SaveAsync ();
return await Task.FromResult(result.Adapt<DataCenter> ());
return await Task.FromResult(result.Adapt<Dto.Manager.DataCenter> ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace hiPower.Core.Mappings;

internal class LocationMapping : IRegister
internal class DataCenterMapping : IRegister
{
public void Register (TypeAdapterConfig config)
{
config.NewConfig<DataCenter, ServerLocation> ()
config.NewConfig<Dto.Manager.DataCenter, Entity.DataCenter> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.Name, src => src.Name)
.Map (dest => dest.Address, src => src.Address)
Expand All @@ -16,7 +16,7 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.Country, src => src.Country)
.Map (dest => dest.Description, src => src.Description);

config.NewConfig<ServerLocation, DataCenter> ()
config.NewConfig<Entity.DataCenter, Dto.Manager.DataCenter> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.Name, src => src.Name)
.Map (dest => dest.Address, src => src.Address)
Expand All @@ -26,7 +26,7 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.Country, src => src.Country)
.Map( dest => dest.Description, src => src.Description);

config.NewConfig<DataCenter, HintItem>()
config.NewConfig<Dto.Manager.DataCenter, HintItem>()
.Map(dest => dest.Id, src => src.Id)
.Map(dest => dest.Name, src => src.Name);
}
Expand Down
10 changes: 5 additions & 5 deletions src/hiPower.Core/Mappings/ServerMappring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ internal class ServerMappring : IRegister
{
public void Register (TypeAdapterConfig config)
{
config.NewConfig<Server, ServerDetails> ()
config.NewConfig<Server, ServiceDetails> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.LocalId, src => src.LocalId)
.Map (dest => dest.LocationId, src => src.LocationId)
.Map (dest => dest.DataCenterId, src => src.DataCenterId)
.Map (dest => dest.Name, src => src.Name)
.Map (dest => dest.Description, src => src.Description)
.Map (dest => dest.Proto, src => src.Proto)
Expand All @@ -22,10 +22,10 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.OS, src => src.OS)
.Map (dest => dest.Configuration, src => src.Configuration);

config.NewConfig<ServerDetails, Server> ()
config.NewConfig<ServiceDetails, Server> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.LocalId, src => src.LocalId)
.Map (dest => dest.LocationId, src => src.LocationId)
.Map (dest => dest.DataCenterId, src => src.DataCenterId)
.Map (dest => dest.Name, src => src.Name)
.Map (dest => dest.Description, src => src.Description)
.Map (dest => dest.Proto, src => src.Proto)
Expand All @@ -37,7 +37,7 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.OS, src => src.OS)
.Map (dest => dest.Configuration, src => src.Configuration);

config.NewConfig<ServerDetails, RemoteServiceOptions> ()
config.NewConfig<ServiceDetails, RemoteServiceOptions> ()
.MapWith (src => new RemoteServiceOptions(src.Proto.ToProtocol(), src.HostAddress, Convert.ToUInt16(src.Port), src.LocalId, src.ApiKey, src.Auth));
}
}
49 changes: 45 additions & 4 deletions src/hiPower.Core/ServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ServerService(IUnitOfWork unit,
{
public async Task<ErrorOr<Server>> CreateAsync (Server server)
{
var result = await unit.ServerRepository.CreateAsync(server.Adapt<ServerDetails>());
var result = await unit.ServerRepository.CreateAsync(server.Adapt<ServiceDetails>());
await unit.SaveAsync ();
return mapper.Map<Server> (result);
}
Expand All @@ -24,7 +24,7 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)

public async Task<ErrorOr<IEnumerable<Server>>> GetAllAsync (string dataCenterId)
{
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(dataCenterId.ToUpper()), null, null);
var result = await unit.ServerRepository.GetAll(x => x.DataCenterId.Equals(dataCenterId.ToUpper()), null, null);
if (result.Any ())
{
return result.Adapt<IEnumerable<Server>> ()
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task<ErrorOr<Server>> UpdateAsync (string id, Server server)
{
if (id.ToUpper ().Equals (server.Id.ToUpper ()))
{
var updateServer = server.Adapt<Entity.ServerDetails> ();
var updateServer = server.Adapt<Entity.ServiceDetails> ();
var result = unit.ServerRepository.Update(updateServer);
await unit.SaveAsync ();
return await Task.FromResult(result.Adapt<Server>());
Expand Down Expand Up @@ -96,6 +96,25 @@ public async Task<ErrorOr<IEnumerable<ConfigSetting>>> GetRemoteConfigurationAsy

public async Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteStatisticsAsync (string id)
{
IEnumerable<string> statiscticsParams = [
"corrupt-packets",
"dnsupdate-answers",
"dnsupdate-changes",
"dnsupdate-queries",
"dnsupdate-refused",
"incoming-notifications",
"noerror-packets",
"nxdomain-packets",
"tcp-answers",
"tcp-queries",
"tcp4-answers",
"tcp4-queries",
"udp-do-queries",
"udp-in-errors",
"udp-queries",
"udp4-answers",
"udp4-queries"];

var service = await GetAsync(id);

if (service.IsError && service.FirstError.Type == ErrorType.NotFound)
Expand All @@ -104,7 +123,29 @@ public async Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteStatisticsAsync
}

var options = service.Value.Adapt<RemoteServiceOptions> ();
var response = await remoteService.GetStatisticsAsync (options);
var response = await remoteService.GetStatisticsAsync (options, statiscticsParams);

if (response.IsError)
{
return response.FirstError;
}

return response;
}

public async Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteUptimeAsync (string id)
{
IEnumerable<string> statiscticsParams = ["uptime"];

var service = await GetAsync(id);

if (service.IsError && service.FirstError.Type == ErrorType.NotFound)
{
return Error.NotFound ();
}

var options = service.Value.Adapt<RemoteServiceOptions> ();
var response = await remoteService.GetStatisticsAsync (options, statiscticsParams);

if (response.IsError)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace hiPower.Database.Configuration;

internal class ServerLocationEntityConfiguration : IEntityTypeConfiguration<ServerLocation>
internal class DataCenterEntityConfiguration : IEntityTypeConfiguration<DataCenter>
{
public void Configure (EntityTypeBuilder<ServerLocation> builder)
public void Configure (EntityTypeBuilder<DataCenter> builder)
{
builder.ToTable($"{Prefix.Table}{nameof(ServerLocation)}");
builder.ToTable($"{Prefix.Table}{nameof(DataCenter)}");

builder.HasKey( x => x.Id );

Expand Down Expand Up @@ -43,14 +43,14 @@ public void Configure (EntityTypeBuilder<ServerLocation> builder)
.HasMaxLength (50);

builder.HasMany(x => x.Servers)
.WithOne(y => y.Location)
.HasForeignKey(x => x.LocationId)
.WithOne(y => y.DataCenter)
.HasForeignKey(x => x.DataCenterId)
.OnDelete(DeleteBehavior.NoAction);

builder.HasData (DefaultLocation);
}

private IEnumerable<ServerLocation> DefaultLocation => [
private IEnumerable<DataCenter> DefaultLocation => [
new (){
Id = "7eb5999f-aef5-11ef-9fd9-47f022e22a50",
Name = "Default",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using hiPower.Common.Type;
using hiPower.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace hiPower.Database.Configuration;

internal class MonitorServiceEntityConfiguration : IEntityTypeConfiguration<MonitorService>
{
public void Configure (EntityTypeBuilder<MonitorService> builder)
{
builder.ToTable ($"{Prefix.Table}{nameof(MonitorService)}");

builder.HasKey ( t => t.ServiceId );

builder.HasIndex (i => new { i.ServiceId, i.MonitorState });

builder.Property (p => p.ServiceId)
.HasMaxLength (36)
.HasConversion (value => value.ToUpperInvariant (),
value => value)
.IsRequired();

builder.Property(p => p.MonitorState)
.HasConversion(value => value.ToString().ToLowerInvariant(),
value => Enum.Parse<MonitorState>(value, true))
.HasMaxLength(10)
.IsRequired();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using hiPower.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace hiPower.Database.Configuration;

internal class MonitorVariablesEntityConfiguration : IEntityTypeConfiguration<MonitorVariables>
{
public void Configure (EntityTypeBuilder<MonitorVariables> builder)
{
builder.ToTable ($"{Prefix.Table}{nameof (MonitorVariables)}");

builder.HasKey (k => k.ServiceId);

builder.Property (p => p.ServiceId)
.HasMaxLength (36)
.HasConversion (value => value.ToUpperInvariant (),
value => value)
.IsRequired ();

builder.Property (p => p.Variable)
.HasMaxLength (30)
.IsRequired ();
}
}
Loading

0 comments on commit 1dc242d

Please sign in to comment.