private async Task<AccountInfoResponse> SaveAccountAndTankInformation(AccountAndTanksContextRow accountAndTanksInfo) { var contextData = new AccountInformationPipelineContextData { AccountInfo = accountAndTanksInfo.AccountInfo, AccountInfoHistory = accountAndTanksInfo.AccountInfoHistory, Tanks = accountAndTanksInfo.Tanks, TanksHistory = accountAndTanksInfo.TanksHistory }; var context = new OperationContext(new AccountRequest(contextData.AccountInfo?.AccountId ?? -1, RealmType.Ru, RequestLanguage.En)); context.AddOrReplace(contextData); var pipeline = new Pipeline<IOperationContext>(_operationFactory); pipeline .AddOperation<CalculateStatisticsOperation>() .AddOperation<SaveAccountAndTanksOperation>() .AddOperation<BuildAccountInfoResponseOperation>() ; var firstOperation = pipeline.Build(); if (firstOperation != null) { await firstOperation .Invoke(context, null) .ConfigureAwait(false); } return contextData?.Response ?? new AccountInfoResponse(); }
public async Task StartImport( string mongoConnectionString, string sqlConnectionString, long[] accountsToImport, IProgress<ImportProgress> progress) { // Get summary history count - for percentage calculation // Here I'm using simple query in the loop. But there is the way to pass an array as SQL parameter - using DataTable int historyCount = 0; using (var context = await CreateDbContext(sqlConnectionString)) { var connection = context.Database.GetDbConnection(); foreach (long accountId in accountsToImport) { historyCount += await GetAccountHistoryCount(accountId, connection); } if (connection.State.Equals(ConnectionState.Open)) { await connection.CloseAsync(); } } var allOldData = new AccountAndTanksContextRow[historyCount]; await GetAllDataFromOldDatabase(sqlConnectionString, accountsToImport, progress, allOldData, historyCount); SetupOperations(mongoConnectionString); int historyCounter = historyCount; for (int i = 0; i < historyCount; i++) { var saved = await SaveAccountAndTankInformation(allOldData[i]); var p = new ImportProgress { Progress = 100 * (historyCounter + 1) / (2 * historyCount), AccountName = saved.Nickname, LastBattle = saved.LastBattleTime, BattlesCount = saved.Battles, TanksCount = saved.Tanks?.Count ?? 0 }; progress.Report(p); historyCounter++; } }
private async Task GetAllDataFromOldDatabase(string sqlConnectionString, long[] accountsToImport, IProgress<ImportProgress> progress, AccountAndTanksContextRow[] allOldData, int historyCount) { int historyCounter = 0; // In loop, get account info, history rows and tank history rows and collect to List using (var context = await CreateDbContext(sqlConnectionString)) { var connection = context.Database.GetDbConnection(); foreach (long accountId in accountsToImport) { var accountInfo = await context.AccountInfos.FromSqlRaw( SelectAccountInfoQuery, new SqlParameter("AccountId", SqlDbType.Int) {Value = accountId}).FirstOrDefaultAsync(); var allHistory = await GetAccountInfoHistory(accountInfo.AccountId, connection); foreach (var accountInfoHistory in allHistory) { allOldData[historyCounter] = new AccountAndTanksContextRow { AccountInfo = new AccountInfo { AccountId = accountId, CreatedAt = accountInfo.CreatedAt, LastBattleTime = accountInfoHistory.AccountInfoHistory.LastBattleTime, Nickname = accountInfo.Nickname, Realm = RealmType.Ru, UpdatedAt = accountInfoHistory.UpdatedAt }, AccountInfoHistory = accountInfoHistory.AccountInfoHistory, Tanks = new List<TankInfo>(), TanksHistory = new Dictionary<long, TankInfoHistory>() }; var oldTanksListByUpdateDate = await GetTanksHistory(accountId, allOldData[historyCounter].AccountInfo.LastBattleTime, connection); foreach (var tankInfo in oldTanksListByUpdateDate) { allOldData[historyCounter].Tanks.Add(new TankInfo(accountId, tankInfo.history.TankId) { LastBattleTime = tankInfo.history.LastBattleTime, BattleLifeTimeInSeconds = tankInfo.BattleLifetime, MarkOfMastery = tankInfo.Mastery }); allOldData[historyCounter].TanksHistory[tankInfo.history.TankId] = tankInfo.history; } var p = new ImportProgress { Progress = 100 * (historyCounter + 1) / (2 * historyCount), AccountName = accountInfo.Nickname, LastBattle = accountInfoHistory.AccountInfoHistory.LastBattleTime.ToDateTime(), BattlesCount = accountInfoHistory.AccountInfoHistory.Battles ?? 0, TanksCount = oldTanksListByUpdateDate.Count }; progress.Report(p); historyCounter++; } } if (connection.State.Equals(ConnectionState.Open)) { await connection.CloseAsync(); } } }