Skip to content

Commit

Permalink
Merge pull request #207 from LykkeBusiness/LT-5012-eod-borders
Browse files Browse the repository at this point in the history
Lt 5012 eod borders
  • Loading branch information
gponomarev-lykke committed Mar 26, 2024
2 parents 0621cfc + 0da7d82 commit 1db63cc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions MarginTrading.AccountsManagement.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InconsistentNaming/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue">&lt;Policy Inspect="False" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright (c) 2019 Lykke Corp.
See the LICENSE file in the project root for more information.</s:String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public interface IAccountBalanceHistoryApi
[CanBeNull] [Query] DateTime? to = null,
[CanBeNull] [Query] AccountBalanceChangeReasonTypeContract? reasonType = null,
[Query] bool filterByTradingDay = false);

/// <summary>
/// Get account balance change history by account Id and TradingDay
/// </summary>
[Get("/api/balance-history/by-account/{accountId}/trading-day")]
Task<Dictionary<string, AccountBalanceChangeContract[]>> ByAccount([NotNull] string accountId,
[Query] DateTime tradingDay,
[CanBeNull] [Query] AccountBalanceChangeReasonTypeContract? reasonType = null);

/// <summary>
/// Get account balance change history by account Id and eventSourceId (like Withdraw or Deposit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ public class AccountBalanceHistoryController : Controller, IAccountBalanceHistor
return data.GroupBy(i => i.AccountId).ToDictionary(g => g.Key, g => g.Select(Convert).ToArray());
}

/// <inheritdoc />
[Route("by-account/{accountId}/trading-day")]
[HttpGet]
public async Task<Dictionary<string, AccountBalanceChangeContract[]>> ByAccount(
string accountId,
[FromQuery] DateTime tradingDay,
[FromQuery] AccountBalanceChangeReasonTypeContract? reasonType = null)
{
var data = await _accountBalanceChangesRepository.GetAsync(
accountId,
tradingDay.AssumeUtcIfUnspecified(),
reasonType?.ToType<AccountBalanceChangeReasonType>());

return data
.GroupBy(i => i.AccountId)
.ToDictionary(
g => g.Key,
g => g.Select(Convert).ToArray());
}

/// <summary>
/// Get account balance change history by account Id and eventSourceId (like Withdraw or Deposit)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public interface IAccountBalanceChangesRepository
DateTime? @from = null, DateTime? to = null, AccountBalanceChangeReasonType? reasonType = null,
bool filterByTradingDay = false);

Task<IReadOnlyList<IAccountBalanceChange>> GetAsync(string accountId,
DateTime tradingDay,
AccountBalanceChangeReasonType? reasonType = null);

Task<IReadOnlyList<IAccountBalanceChange>> GetAsync(string accountId, string eventSourceId);

Task<decimal> GetCompensationsProfit(string accountId, DateTime[] days);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Data;

using Microsoft.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,6 +42,17 @@ public class AccountBalanceChangesRepository : IAccountBalanceChangesRepository
"[TradingDate] [datetime] NULL, " +
"INDEX IX_{0}_Base (Id, AccountId, ChangeTimestamp, EventSourceId, ReasonType)" +
");";

private const string CreateTradingDateIndexScript = @"IF NOT EXISTS(
SELECT * FROM sys.indexes
WHERE name = 'IX_{0}_TradingDate'
AND object_id = OBJECT_ID('dbo.{0}'))
BEGIN
CREATE INDEX [IX_{0}_TradingDate] ON [dbo].[{0}]
(
[TradingDate]
)
END";

private static Type DataTypeLight => typeof(IAccountBalanceChangeLight);
private static readonly string GetColumnsLight = string.Join(",", DataTypeLight.GetProperties().Select(x => x.Name));
Expand All @@ -62,7 +75,13 @@ public class AccountBalanceChangesRepository : IAccountBalanceChangesRepository
_logger = logger;

using var conn = new SqlConnection(_settings.Db.ConnectionString);
try { conn.CreateTableIfDoesntExists(CreateTableScript, TableName); }
try
{
conn.CreateTableIfDoesntExists(CreateTableScript, TableName);

var indexScript = string.Format(CreateTradingDateIndexScript, TableName);
conn.Execute(indexScript, commandType: CommandType.Text);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create table {TableName}", TableName);
Expand Down Expand Up @@ -148,6 +167,26 @@ public async Task<IReadOnlyList<IAccountBalanceChangeLight>> GetLightAsync(DateT

return data.ToList();
}

public async Task<IReadOnlyList<IAccountBalanceChange>> GetAsync(string accountId,
DateTime tradingDay,
AccountBalanceChangeReasonType? reasonType = null)
{
var whereClause = "WHERE 1=1 " + (!string.IsNullOrWhiteSpace(accountId) ? " AND AccountId=@accountId" : "")
+ " AND TradingDate = @tradingDay"
+ (reasonType != null ? " AND ReasonType = @reasonType" : "");

await using var conn = new SqlConnection(_settings.Db.ConnectionString);
var data = await conn.QueryAsync<AccountBalanceChangeEntity>(
$"SELECT {GetColumns} FROM {TableName} WITH (NOLOCK) {whereClause}", new
{
accountId,
tradingDay,
reasonType = reasonType.ToString(),
});

return data.ToList();
}

public async Task<IReadOnlyList<IAccountBalanceChange>> GetAsync(string accountId, string eventSourceId)
{
Expand Down

0 comments on commit 1db63cc

Please sign in to comment.