private static OjsDbContext GetData() { var db = new OjsDbContext(); db.Database.CommandTimeout = 10 * 60; return(db); }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; var participants = oldDb.Participants.Select(x => new { x.Id, x.Contest, OldUserId = x.User1.Id, x.RegisteredOn, x.IsOfficial, x.Answer, }).ToList(); var contests = context.Contests.Select(x => new { x.OldId, x.Id, Question = x.Questions.FirstOrDefault() }).ToDictionary(x => x.OldId); var users = context.Users.Select(x => new { x.OldId, x.Id }).ToDictionary(x => x.OldId); foreach (var oldParticipant in participants) { if (!contests.ContainsKey(oldParticipant.Contest)) { continue; } var contest = contests[oldParticipant.Contest]; if (!users.ContainsKey(oldParticipant.OldUserId)) { continue; } var participant = new Participant { OldId = oldParticipant.Id, PreserveCreatedOn = true, CreatedOn = oldParticipant.RegisteredOn, IsOfficial = oldParticipant.IsOfficial, ContestId = contest.Id, UserId = users[oldParticipant.OldUserId].Id, }; if (contest.Question != null) { var answer = new ParticipantAnswer { ContestQuestionId = contest.Question.Id, Answer = oldParticipant.Answer, Participant = participant, }; context.ParticipantAnswers.Add(answer); } context.Participants.Add(participant); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; var users = oldDb.Users.OrderBy(x => x.Id).Select( x => new { x.aspnet_Users.UserName, x.aspnet_Users.aspnet_Membership.Email, Profile = x, HasAnyParticipationWithSubmissions = x.Participants.Any(y => y.Submissions.Any()), CreatedOn = x.aspnet_Users.aspnet_Membership.CreateDate, x.Id, }) .Where(x => x.HasAnyParticipationWithSubmissions); //// .Where(x => !x.UserName.StartsWith("*****@*****.**")) //// .Where(x => !x.Email.StartsWith("*****@*****.**")) //// .Where(x => x.Profile.LastName != "*****@*****.**") //// .Where(x => !x.UserName.Contains("\'") && !x.UserName.Contains("\\") && !x.UserName.Contains("/") && !x.UserName.Contains("%") && !x.UserName.Contains(")")); foreach (var oldUser in users) { var dateOfBirth = oldUser.Profile.DateOfBirth; if (dateOfBirth.HasValue && dateOfBirth.Value.Year < 1900) { dateOfBirth = null; } if (dateOfBirth.HasValue && dateOfBirth.Value.Year > 2006) { dateOfBirth = null; } var user = new UserProfile(oldUser.UserName.Trim(), oldUser.Email.Trim()) { UserSettings = new UserSettings { FirstName = oldUser.Profile.FirstName.Trim().MaxLength(30), LastName = oldUser.Profile.LastName.Trim().MaxLength(30), City = oldUser.Profile.City.MaxLength(30), DateOfBirth = dateOfBirth, EducationalInstitution = oldUser.Profile.EducationalInstitution.MaxLength(50), FacultyNumber = oldUser.Profile.FacultyNumber.MaxLength(30), Company = oldUser.Profile.Company.MaxLength(30), JobTitle = oldUser.Profile.JobTitle.MaxLength(30), }, IsGhostUser = true, OldId = oldUser.Id, PreserveCreatedOn = true, CreatedOn = oldUser.CreatedOn, }; context.Users.Add(user); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; }
internal static void Main() { Console.WriteLine("Starting migration..."); Database.SetInitializer(new MigrateDatabaseToLatestVersion<OjsDbContext, OldDatabaseMigrationConfiguration>()); var context = new OjsDbContext(); var problemsCount = context.Problems.Count(); Console.WriteLine("Done!"); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); }
internal static void Main() { Console.WriteLine("Starting migration..."); Database.SetInitializer(new MigrateDatabaseToLatestVersion <OjsDbContext, OldDatabaseMigrationConfiguration>()); //// Database.SetInitializer(new MigrateDatabaseToLatestVersion<OjsDbContext, DefaultMigrationConfiguration>()); var context = new OjsDbContext(); var problemsCount = context.Problems.Count(); Console.WriteLine("Done!"); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); }
private static OjsDbContext GetData() { var db = new OjsDbContext(); db.Database.CommandTimeout = 10 * 60; db.DbContext.Configuration.AutoDetectChangesEnabled = false; db.DbContext.Configuration.ValidateOnSaveEnabled = false; db.DbContext.Configuration.ProxyCreationEnabled = false; db.DbContext.Configuration.LazyLoadingEnabled = false; return(db); }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { foreach (var oldContestType in oldDb.ContestTypes) { var category = new ContestCategory { Name = oldContestType.Name, IsVisible = oldContestType.IsVisible, OrderBy = oldContestType.Order, }; context.ContestCategories.Add(category); } context.SaveChanges(); }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { foreach (var oldContestType in oldDb.ContestTypes) { var category = new ContestCategory { Name = oldContestType.Name, IsVisible = oldContestType.IsVisible, OrderBy = oldContestType.Order, }; context.ContestCategories.Add(category); } context.SaveChanges(); }
protected override void Seed(OjsDbContext context) { // context.ClearDatabase(); if (context.Problems.Count() > 400) { // The data is already copied. return; } this.SeedCheckers(context); this.SeedRoles(context); this.SeedSubmissionTypes(context); foreach (var copier in this.copiers) { copier.Copy(context, this.oldDb); context.SaveChanges(); // Prevents us from forgetting to call SaveChanges in this.XXXCopier().Copy() methods } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { // Moving tests in iterations to prevent OutOfMemoryException var count = oldDb.Tests.Count(); const int ElementsByIteration = 100; var iterations = Math.Ceiling((decimal)count / ElementsByIteration); for (int i = 0; i < iterations; i++) { GC.Collect(); var newDb = new OjsDbContext(); newDb.Configuration.AutoDetectChangesEnabled = false; newDb.Configuration.ValidateOnSaveEnabled = false; oldDb = new TelerikContestSystemEntities(); oldDb.Configuration.AutoDetectChangesEnabled = false; GC.Collect(); foreach (var oldTest in oldDb.Tests.OrderBy(x => x.Id).Skip(i * ElementsByIteration).Take(ElementsByIteration)) { var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldTest.Task); var test = new Data.Models.Test { InputDataAsString = oldTest.Input, OutputDataAsString = oldTest.Output, Problem = problem, IsTrialTest = oldTest.IsZeroTest, OrderBy = oldTest.Order, }; newDb.Tests.Add(test); } newDb.SaveChanges(); Console.Write("."); } }
protected override void Seed(OjsDbContext context) { // context.ClearDatabase(); if (context.Problems.Count() > 400) { // The data is already copied. return; } this.SeedCheckers(context); this.SeedRoles(context); this.SeedSubmissionTypes(context); var stopwatch = new Stopwatch(); foreach (var copier in this.copiers) { stopwatch.Restart(); Console.Write("{0} started... ", copier.GetType().Name); copier.Copy(context, this.oldDb); Console.WriteLine("Done! Time elapsed: {0}", stopwatch.Elapsed); context.SaveChanges(); // Prevents us from forgetting to call SaveChanges in this.XXXCopier().Copy() methods } }
protected override void Seed(OjsDbContext context) { // context.ClearDatabase(); if (context.Problems.Count() > 400) { // The data is already copied. return; } this.SeedCheckers(context); this.SeedRoles(context); this.SeedSubmissionTypes(context); var stopwatch = new Stopwatch(); foreach (var copier in this.copiers) { stopwatch.Restart(); Console.Write("{0} started... ", copier.GetType().Name); copier.Copy(context, this.oldDb); Console.WriteLine("Done! Time elapsed: {0}", stopwatch.Elapsed); context.SaveChanges(); // Prevents us from forgetting to call SaveChanges in this.XXXCopier().Copy() methods } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { // Moving tests in iterations to prevent OutOfMemoryException var count = oldDb.Tests.Count(); const int ElementsByIteration = 100; var iterations = Math.Ceiling((decimal)count / ElementsByIteration); for (int i = 0; i < iterations; i++) { GC.Collect(); var newDb = new OjsDbContext(); newDb.Configuration.AutoDetectChangesEnabled = false; newDb.Configuration.ValidateOnSaveEnabled = false; oldDb = new TelerikContestSystemEntities(); oldDb.Configuration.AutoDetectChangesEnabled = false; GC.Collect(); foreach (var oldTest in oldDb.Tests.OrderBy(x => x.Id).Skip(i * ElementsByIteration).Take(ElementsByIteration)) { var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldTest.Task); var test = new Data.Models.Test { InputDataAsString = oldTest.Input, OutputDataAsString = oldTest.Output, Problem = problem, IsTrialTest = oldTest.IsZeroTest, OrderBy = oldTest.Order, }; newDb.Tests.Add(test); } newDb.SaveChanges(); Console.Write("."); } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { var tests = context.Tests.Select(x => new { x.Id, x.ProblemId, x.IsTrialTest, x.OrderBy }).ToList(); var count = oldDb.Submissions.Count(); const int ElementsByIteration = 500; var iterations = Math.Ceiling((decimal)count / ElementsByIteration); for (int i = 0; i < iterations; i++) { GC.Collect(); var newDb = new OjsDbContext(); newDb.Configuration.AutoDetectChangesEnabled = false; newDb.Configuration.ValidateOnSaveEnabled = false; var csharpSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code"); var cplusPlusSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code"); var javaScriptSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)"); oldDb = new TelerikContestSystemEntities(); var dataSource = oldDb.Submissions.AsNoTracking() .Where(x => x.Id != 127774) .OrderBy(x => x.Id) .Skip(i * ElementsByIteration) .Take(ElementsByIteration); foreach (var oldSubmission in dataSource) { var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldSubmission.Task); var participant = newDb.Participants.FirstOrDefault(x => x.OldId == oldSubmission.Participant); var submission = new Submission { Content = oldSubmission.File.ToText().Compress(), PreserveCreatedOn = true, CreatedOn = oldSubmission.SubmittedOn, Problem = problem, Participant = participant, Points = oldSubmission.Points, Processed = true, Processing = false, }; switch (oldSubmission.Language) { case "C# Code": submission.SubmissionType = csharpSubmissionType; break; case "C++ Code": submission.SubmissionType = cplusPlusSubmissionType; break; case "JavaScript Code": submission.SubmissionType = javaScriptSubmissionType; break; case "C + + 觐�": submission.SubmissionType = cplusPlusSubmissionType; break; default: submission.SubmissionType = csharpSubmissionType; break; } var reportFragments = oldSubmission.FullReport.Split( new[] { this.contentHeader }, StringSplitOptions.RemoveEmptyEntries); if (string.IsNullOrWhiteSpace(oldSubmission.FullReport) || !reportFragments.Any()) { continue; } if (reportFragments.Count() == 1) { // Some kind of exception (e.g. "not enough disk space") var errorFragments = reportFragments[0].Split( new[] { this.contentFooter }, StringSplitOptions.RemoveEmptyEntries); submission.IsCompiledSuccessfully = false; submission.CompilerComment = errorFragments[0]; } else if (!reportFragments[1].Trim().StartsWith("Compilation successfull!!!")) { submission.IsCompiledSuccessfully = false; var compilerParts = reportFragments[0].Split( new[] { "\r\n" }, 3, StringSplitOptions.RemoveEmptyEntries); submission.CompilerComment = compilerParts.Count() > 2 ? compilerParts[2].Trim(' ', '\n', '\r', '\t', '-', '=') : null; } else { submission.IsCompiledSuccessfully = true; var compilerParts = reportFragments[0].Split( new[] { "\r\n" }, 3, StringSplitOptions.RemoveEmptyEntries); submission.CompilerComment = compilerParts.Count() > 2 ? compilerParts[2].Trim(' ', '\n', '\r', '\t', '-', '=') : null; for (int j = 2; j < reportFragments.Length - 1; j++) { var testRunText = reportFragments[j].Trim(); var testRunTextParts = testRunText.Split(new[] { this.contentFooter }, StringSplitOptions.None); var testRunTitle = testRunTextParts[0].Trim(); var testRunDescription = testRunTextParts[1].Trim() + Environment.NewLine; var isZeroTest = testRunTitle.StartsWith("Zero"); var testOrderAsString = testRunTitle.GetStringBetween("�", " "); var testOrder = int.Parse(testOrderAsString); var test = tests.FirstOrDefault( x => x.ProblemId == problem.Id && x.IsTrialTest == isZeroTest && x.OrderBy == testOrder); if (test == null) { continue; } var testRun = new TestRun { MemoryUsed = 0, TimeUsed = 0, Submission = submission, TestId = test.Id, }; if (testRunDescription.StartsWith("Answer correct!!!")) { testRun.ResultType = TestRunResultType.CorrectAnswer; testRun.ExecutionComment = null; testRun.CheckerComment = null; var timeUsedAsString = testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used"); if (timeUsedAsString != null) { double timeUsed; if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed)) { testRun.TimeUsed = (int)timeUsed; } } var memoryUsedAsString = testRunDescription.GetStringBetween( "Memory used (in bytes): ", Environment.NewLine); if (memoryUsedAsString != null) { testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim()); } } else if (testRunDescription.StartsWith("Answer incorrect!")) { testRun.ResultType = TestRunResultType.WrongAnswer; testRun.ExecutionComment = null; testRun.CheckerComment = testRunDescription.GetStringBetween( "Answer incorrect!", "Time used"); // Won't work with non-zero tests (will return null) if (testRun.CheckerComment != null) { testRun.CheckerComment = testRun.CheckerComment.Trim(); if (string.IsNullOrWhiteSpace(testRun.CheckerComment)) { testRun.CheckerComment = null; } } var timeUsedAsString = testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used"); if (timeUsedAsString != null) { double timeUsed; if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed)) { testRun.TimeUsed = (int)timeUsed; } } var memoryUsedAsString = testRunDescription.GetStringBetween( "Memory used (in bytes): ", Environment.NewLine); if (memoryUsedAsString != null) { testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim()); } } else if (testRunDescription.StartsWith("Runtime error:")) { testRun.ResultType = TestRunResultType.RunTimeError; testRun.ExecutionComment = testRunDescription.Replace("Runtime error:", string.Empty).Trim(); testRun.CheckerComment = null; testRun.TimeUsed = 0; testRun.MemoryUsed = 0; } else if (testRunDescription.StartsWith("Time limit!")) { testRun.ResultType = TestRunResultType.TimeLimit; testRun.ExecutionComment = null; testRun.CheckerComment = null; testRun.TimeUsed = problem.TimeLimit; testRun.MemoryUsed = 0; } else { testRun.ResultType = TestRunResultType.RunTimeError; testRun.ExecutionComment = testRunDescription.Trim(); testRun.CheckerComment = null; testRun.TimeUsed = 0; testRun.MemoryUsed = 0; } newDb.TestRuns.Add(testRun); } } newDb.Submissions.Add(submission); } newDb.SaveChanges(); Console.Write("."); } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; context.Configuration.ValidateOnSaveEnabled = false; var participants = oldDb.Participants.Select(x => new { x.Id, x.Contest, OldUserId = x.User1.Id, x.RegisteredOn, x.IsOfficial, x.Answer, }).ToList(); var contests = context.Contests.Select(x => new { x.OldId, x.Id, Question = x.Questions.FirstOrDefault() }).ToDictionary(x => x.OldId); var users = context.Users.Select(x => new { x.OldId, x.Id }).ToDictionary(x => x.OldId); foreach (var oldParticipant in participants) { if (!contests.ContainsKey(oldParticipant.Contest)) { continue; } var contest = contests[oldParticipant.Contest]; if (!users.ContainsKey(oldParticipant.OldUserId)) { // User is no longer active continue; } var participant = new Participant { OldId = oldParticipant.Id, PreserveCreatedOn = true, CreatedOn = oldParticipant.RegisteredOn, IsOfficial = oldParticipant.IsOfficial, ContestId = contest.Id, UserId = users[oldParticipant.OldUserId].Id, }; if (contest.Question != null) { var answer = new ParticipantAnswer { ContestQuestionId = contest.Question.Id, Answer = oldParticipant.Answer, Participant = participant, }; context.ParticipantAnswers.Add(answer); } context.Participants.Add(participant); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; context.Configuration.ValidateOnSaveEnabled = true; }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; var users = oldDb.Users.OrderBy(x => x.Id).Select( x => new { x.aspnet_Users.UserName, x.aspnet_Users.aspnet_Membership.Email, Profile = x, HasAnyParticipationWithSubmissions = x.Participants.Any(y => y.Submissions.Any()), CreatedOn = x.aspnet_Users.aspnet_Membership.CreateDate, x.Id, }) .Where(x => x.HasAnyParticipationWithSubmissions); //// .Where(x => !x.UserName.StartsWith("*****@*****.**")) //// .Where(x => !x.Email.StartsWith("*****@*****.**")) //// .Where(x => x.Profile.LastName != "*****@*****.**") //// .Where(x => !x.UserName.Contains("\'") && !x.UserName.Contains("\\") && !x.UserName.Contains("/") && !x.UserName.Contains("%") && !x.UserName.Contains(")")); foreach (var oldUser in users) { var dateOfBirth = oldUser.Profile.DateOfBirth; if (dateOfBirth.HasValue && dateOfBirth.Value.Year < 1900) { dateOfBirth = null; } if (dateOfBirth.HasValue && dateOfBirth.Value.Year > 2006) { dateOfBirth = null; } var user = new UserProfile(oldUser.UserName.Trim(), oldUser.Email.Trim()) { UserSettings = new UserSettings { FirstName = oldUser.Profile.FirstName.Trim().MaxLength(30), LastName = oldUser.Profile.LastName.Trim().MaxLength(30), City = oldUser.Profile.City.MaxLength(30), DateOfBirth = dateOfBirth, EducationalInstitution = oldUser.Profile.EducationalInstitution.MaxLength(50), FacultyNumber = oldUser.Profile.FacultyNumber.MaxLength(30), Company = oldUser.Profile.Company.MaxLength(30), JobTitle = oldUser.Profile.JobTitle.MaxLength(30), }, IsGhostUser = true, OldId = oldUser.Id, PreserveCreatedOn = true, CreatedOn = oldUser.CreatedOn, }; context.Users.Add(user); } try { context.SaveChanges(); } catch { // ((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors.First().ValidationErrors.First() throw; } context.Configuration.AutoDetectChangesEnabled = true; }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { var count = oldDb.Submissions.Count(); const int ElementsByIteration = 1000; var iterations = Math.Ceiling((decimal)count / ElementsByIteration); for (int i = 0; i < iterations; i++) { GC.Collect(); var newDb = new OjsDbContext(); newDb.Configuration.AutoDetectChangesEnabled = false; newDb.Configuration.ValidateOnSaveEnabled = false; var cSharpSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code"); var cPlusPlusSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code"); var javaScriptSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)"); oldDb = new TelerikContestSystemEntities(); var dataSource = oldDb.Submissions.AsNoTracking() .Where(x => x.Id != 127774) .OrderBy(x => x.Id) .Skip(i * ElementsByIteration) .Take(ElementsByIteration); foreach (var oldSubmission in dataSource) { var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldSubmission.Task); var participant = newDb.Participants.FirstOrDefault(x => x.OldId == oldSubmission.Participant); var submission = new Submission { Content = oldSubmission.File.ToText().Compress(), PreserveCreatedOn = true, CreatedOn = oldSubmission.SubmittedOn, Problem = problem, Participant = participant, }; switch (oldSubmission.Language) { case "C# Code": submission.SubmissionType = cSharpSubmissionType; break; case "C++ Code": submission.SubmissionType = cPlusPlusSubmissionType; break; case "JavaScript Code": submission.SubmissionType = javaScriptSubmissionType; break; case "C + + код": submission.SubmissionType = cPlusPlusSubmissionType; break; default: submission.SubmissionType = cSharpSubmissionType; break; } var reportFragments = oldSubmission.FullReport.Split( new[] { contentHeader }, StringSplitOptions.RemoveEmptyEntries); if (string.IsNullOrWhiteSpace(oldSubmission.FullReport) || !reportFragments.Any()) { continue; } if (reportFragments.Count() == 1) { // Some kind of exception (e.g. "not enough disk space") var errorFragments = reportFragments[0].Split( new[] { contetnFooter }, StringSplitOptions.RemoveEmptyEntries); submission.IsCompiledSuccessfully = false; submission.CompilerComment = errorFragments[0]; } else if (!reportFragments[1].Trim().StartsWith("Compilation successfull!!!")) { submission.IsCompiledSuccessfully = false; var compilerParts = reportFragments[0].Split( new[] { "\r\n" }, 3, StringSplitOptions.RemoveEmptyEntries); if (compilerParts.Count() > 2) { submission.CompilerComment = compilerParts[2].Trim( new[] { ' ', '\n', '\r', '\t', '-', '=' }); } else { submission.CompilerComment = null; } } else { submission.IsCompiledSuccessfully = true; submission.CompilerComment = null; // TODO: Parse all tests for (int j = 2; j < reportFragments.Length - 1; j++) { var testRunText = reportFragments[j]; bool isZeroTest; if (testRunText.StartsWith("Zero")) { isZeroTest = true; } else { isZeroTest = false; } var testOrderAsString = testRunText.GetStringBetween("№", " "); var testOrder = int.Parse(testOrderAsString); var test = newDb.Tests.FirstOrDefault( x => x.ProblemId == problem.Id && x.IsTrialTest == isZeroTest && x.OrderBy == testOrder); var testRun = new TestRun { CheckerComment = string.Empty, ExecutionComment = string.Empty, MemoryUsed = 0, TimeUsed = 0, ResultType = new TestRunResultType(), Submission = submission, Test = test, }; newDb.TestRuns.Add(testRun); } } newDb.Submissions.Add(submission); } newDb.SaveChanges(); } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; foreach (var task in oldDb.Tasks.OrderBy(x => x.Id).Include(x => x.Contest1).ToList()) { var contest = context.Contests.FirstOrDefault(x => x.OldId == task.Contest); var problem = new Problem { OldId = task.Id, Contest = contest, Name = task.Name, MaximumPoints = (short)task.MaxPoints, TimeLimit = task.TimeLimit, MemoryLimit = (int)task.MemoryLimit * 2, // Multiplying memory limit by 2 because the previous worker didn't respect the memory limit OrderBy = 0, ShowResults = task.Contest1.ShowResults, CreatedOn = task.AddedOn, PreserveCreatedOn = true, }; if (task.DescriptionLink != null || (task.Description != null && task.Description.Length != 0)) { var resource = new ProblemResource { CreatedOn = task.AddedOn, PreserveCreatedOn = true, Name = string.Format("Условие на задачата"), Type = ProblemResourceType.ProblemDescription, }; if (task.DescriptionLink != null) { if (task.Id == 368) { task.DescriptionLink = "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/DocumentSystem-Skeleton.rar"; } if (task.Id == 369) { task.DescriptionLink = "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/AcademyGeometry-Skeleton.zip"; } var web = new WebClient(); var data = web.DownloadData(task.DescriptionLink); resource.File = data; resource.FileExtension = task.DescriptionLink.GetFileExtension(); } else { resource.File = task.Description; resource.FileExtension = task.DescriptionFormat; } problem.Resources.Add(resource); } switch (task.Checker1.Name) { case "Exact": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Exact"); break; case "Trim": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Trim"); break; case "Sort": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Sort lines"); break; default: problem.Checker = null; break; } context.Problems.Add(problem); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; foreach (var task in oldDb.Tasks.OrderBy(x => x.Id).Include(x => x.Contest1).ToList()) { var contest = context.Contests.FirstOrDefault(x => x.OldId == task.Contest); var problem = new Problem { OldId = task.Id, Contest = contest, Name = task.Name, MaximumPoints = (short)task.MaxPoints, TimeLimit = task.TimeLimit, MemoryLimit = task.MemoryLimit, OrderBy = 0, ShowResults = task.Contest1.ShowResults, CreatedOn = task.AddedOn, PreserveCreatedOn = true, }; if (task.DescriptionLink != null || (task.Description != null && task.Description.Length != 0)) { var resource = new ProblemResource { CreatedOn = task.AddedOn, PreserveCreatedOn = true, Name = string.Format("Условие на задачата"), Type = ProblemResourceType.ProblemDescription, }; if (task.DescriptionLink != null) { if (task.Id == 368) { task.DescriptionLink = "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/DocumentSystem-Skeleton.rar"; } if (task.Id == 369) { task.DescriptionLink = "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/AcademyGeometry-Skeleton.zip"; } var web = new WebClient(); var data = web.DownloadData(task.DescriptionLink); resource.File = data; resource.FileExtension = task.DescriptionLink.GetFileExtension(); } else { resource.File = task.Description; resource.FileExtension = task.DescriptionFormat; } problem.Resources.Add(resource); } switch (task.Checker1.Name) { case "Exact": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Exact"); break; case "Trim": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Trim"); break; case "Sort": problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Sort lines"); break; default: problem.Checker = null; break; } context.Problems.Add(problem); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { var cSharpSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code"); var cPlusPlusSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code"); var javaScriptSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)"); foreach (var oldContest in oldDb.Contests) { var contest = new Contest { OldId = oldContest.Id, CreatedOn = oldContest.AddedOn, PreserveCreatedOn = true, StartTime = oldContest.ActiveFrom, EndTime = oldContest.ActiveTo, ContestPassword = oldContest.Password, PracticePassword = oldContest.Password, OrderBy = oldContest.Order, Name = oldContest.Name.Trim(), IsVisible = oldContest.IsVisible, LimitBetweenSubmissions = oldContest.SubmissionsTimeLimit, }; // Practice times if (!oldContest.ActiveFrom.HasValue && !oldContest.ActiveTo.HasValue) { contest.PracticeStartTime = DateTime.Now; contest.PracticeEndTime = null; } else if (oldContest.CanBePracticedAfterContest) { contest.PracticeStartTime = oldContest.ActiveTo; contest.PracticeEndTime = null; } else if (oldContest.CanBePracticedDuringContest) { contest.PracticeStartTime = oldContest.ActiveFrom; contest.PracticeEndTime = null; } else { contest.PracticeStartTime = null; contest.PracticeEndTime = null; } // Contest category var categoryName = oldContest.ContestType.Name; var category = context.ContestCategories.FirstOrDefault(x => x.Name == categoryName); contest.Category = category; // Contest question if (oldContest.Question != null) { var question = new ContestQuestion { AskOfficialParticipants = true, AskPracticeParticipants = true, Text = oldContest.Question.Trim(), Type = ContestQuestionType.Default, }; if (oldContest.Answers != null) { var answers = oldContest.Answers.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var answerText in answers) { if (!string.IsNullOrWhiteSpace(answerText)) { var answer = new ContestQuestionAnswer { Text = answerText.Trim() }; question.Answers.Add(answer); } } } contest.Questions.Add(question); } // Contest submission types if (oldContest.ContestType.AllowCSharpCode) { contest.SubmissionTypes.Add(cSharpSubmissionType); } if (oldContest.ContestType.AllowCPlusPlusCode) { contest.SubmissionTypes.Add(cPlusPlusSubmissionType); } if (oldContest.ContestType.AllowJavaScriptCode) { contest.SubmissionTypes.Add(javaScriptSubmissionType); } context.Contests.Add(contest); } context.SaveChanges(); }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { var cSharpSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code"); var cPlusPlusSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code"); var javaScriptSubmissionType = context.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)"); foreach (var oldContest in oldDb.Contests) { var contest = new Contest { OldId = oldContest.Id, CreatedOn = oldContest.AddedOn, PreserveCreatedOn = true, StartTime = oldContest.ActiveFrom, EndTime = oldContest.ActiveTo, ContestPassword = oldContest.Password, PracticePassword = oldContest.Password, OrderBy = oldContest.Order, Name = oldContest.Name.Trim(), IsVisible = oldContest.IsVisible, LimitBetweenSubmissions = oldContest.SubmissionsTimeLimit, }; // Practice times if (!oldContest.ActiveFrom.HasValue && !oldContest.ActiveTo.HasValue) { contest.PracticeStartTime = DateTime.Now; contest.PracticeEndTime = null; } else if (oldContest.CanBePracticedAfterContest) { contest.PracticeStartTime = oldContest.ActiveTo; contest.PracticeEndTime = null; } else if (oldContest.CanBePracticedDuringContest) { contest.PracticeStartTime = oldContest.ActiveFrom; contest.PracticeEndTime = null; } else { contest.PracticeStartTime = null; contest.PracticeEndTime = null; } // Contest category var categoryName = oldContest.ContestType.Name; var category = context.ContestCategories.FirstOrDefault(x => x.Name == categoryName); contest.Category = category; // Contest question if (oldContest.Question != null) { var question = new ContestQuestion { AskOfficialParticipants = true, AskPracticeParticipants = true, Text = oldContest.Question.Trim(), Type = ContestQuestionType.Default, }; if (oldContest.Answers != null) { var answers = oldContest.Answers.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var answerText in answers) { if (!string.IsNullOrWhiteSpace(answerText)) { var answer = new ContestQuestionAnswer { Text = answerText.Trim() }; question.Answers.Add(answer); } } } contest.Questions.Add(question); } // Contest submission types if (oldContest.ContestType.AllowCSharpCode) { contest.SubmissionTypes.Add(cSharpSubmissionType); } if (oldContest.ContestType.AllowCPlusPlusCode) { contest.SubmissionTypes.Add(cPlusPlusSubmissionType); } if (oldContest.ContestType.AllowJavaScriptCode) { contest.SubmissionTypes.Add(javaScriptSubmissionType); } context.Contests.Add(contest); Console.WriteLine(contest); } context.SaveChanges(); }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { var tests = context.Tests.Select(x => new { x.Id, x.ProblemId, x.IsTrialTest, x.OrderBy }).ToList(); var count = oldDb.Submissions.Count(); const int ElementsByIteration = 500; var iterations = Math.Ceiling((decimal)count / ElementsByIteration); for (int i = 0; i < iterations; i++) { GC.Collect(); var newDb = new OjsDbContext(); newDb.Configuration.AutoDetectChangesEnabled = false; newDb.Configuration.ValidateOnSaveEnabled = false; var csharpSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code"); var cplusPlusSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code"); var javaScriptSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)"); oldDb = new TelerikContestSystemEntities(); var dataSource = oldDb.Submissions.AsNoTracking() .Where(x => x.Id != 127774) .OrderBy(x => x.Id) .Skip(i * ElementsByIteration) .Take(ElementsByIteration); foreach (var oldSubmission in dataSource) { var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldSubmission.Task); var participant = newDb.Participants.FirstOrDefault(x => x.OldId == oldSubmission.Participant); var submission = new Submission { Content = oldSubmission.File.ToText().Compress(), PreserveCreatedOn = true, CreatedOn = oldSubmission.SubmittedOn, Problem = problem, Participant = participant, Points = oldSubmission.Points, Processed = true, Processing = false, }; switch (oldSubmission.Language) { case "C# Code": submission.SubmissionType = csharpSubmissionType; break; case "C++ Code": submission.SubmissionType = cplusPlusSubmissionType; break; case "JavaScript Code": submission.SubmissionType = javaScriptSubmissionType; break; case "C + + код": submission.SubmissionType = cplusPlusSubmissionType; break; default: submission.SubmissionType = csharpSubmissionType; break; } var reportFragments = oldSubmission.FullReport.Split( new[] { this.contentHeader }, StringSplitOptions.RemoveEmptyEntries); if (string.IsNullOrWhiteSpace(oldSubmission.FullReport) || !reportFragments.Any()) { continue; } if (reportFragments.Count() == 1) { // Some kind of exception (e.g. "not enough disk space") var errorFragments = reportFragments[0].Split( new[] { this.contentFooter }, StringSplitOptions.RemoveEmptyEntries); submission.IsCompiledSuccessfully = false; submission.CompilerComment = errorFragments[0]; } else if (!reportFragments[1].Trim().StartsWith("Compilation successfull!!!")) { submission.IsCompiledSuccessfully = false; var compilerParts = reportFragments[0].Split( new[] { "\r\n" }, 3, StringSplitOptions.RemoveEmptyEntries); if (compilerParts.Count() > 2) { submission.CompilerComment = compilerParts[2].Trim( new[] { ' ', '\n', '\r', '\t', '-', '=' }); } else { submission.CompilerComment = null; } } else { submission.IsCompiledSuccessfully = true; var compilerParts = reportFragments[0].Split( new[] { "\r\n" }, 3, StringSplitOptions.RemoveEmptyEntries); if (compilerParts.Count() > 2) { submission.CompilerComment = compilerParts[2].Trim( new[] { ' ', '\n', '\r', '\t', '-', '=' }); } else { submission.CompilerComment = null; } for (int j = 2; j < reportFragments.Length - 1; j++) { var testRunText = reportFragments[j].Trim(); var testRunTextParts = testRunText.Split(new[] { this.contentFooter }, StringSplitOptions.None); var testRunTitle = testRunTextParts[0].Trim(); var testRunDescription = testRunTextParts[1].Trim() + Environment.NewLine; bool isZeroTest; if (testRunTitle.StartsWith("Zero")) { isZeroTest = true; } else { isZeroTest = false; } var testOrderAsString = testRunTitle.GetStringBetween("№", " "); var testOrder = int.Parse(testOrderAsString); var test = tests.FirstOrDefault( x => x.ProblemId == problem.Id && x.IsTrialTest == isZeroTest && x.OrderBy == testOrder); if (test == null) { continue; } var testRun = new TestRun { MemoryUsed = 0, TimeUsed = 0, Submission = submission, TestId = test.Id, }; if (testRunDescription.StartsWith("Answer correct!!!")) { testRun.ResultType = TestRunResultType.CorrectAnswer; testRun.ExecutionComment = null; testRun.CheckerComment = null; var timeUsedAsString = testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used"); if (timeUsedAsString != null) { double timeUsed; if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed)) { testRun.TimeUsed = (int)timeUsed; } } var memoryUsedAsString = testRunDescription.GetStringBetween( "Memory used (in bytes): ", Environment.NewLine); if (memoryUsedAsString != null) { testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim()); } } else if (testRunDescription.StartsWith("Answer incorrect!")) { testRun.ResultType = TestRunResultType.WrongAnswer; testRun.ExecutionComment = null; testRun.CheckerComment = testRunDescription.GetStringBetween( "Answer incorrect!", "Time used"); // Won't work with non-zero tests (will return null) if (testRun.CheckerComment != null) { testRun.CheckerComment = testRun.CheckerComment.Trim(); if (string.IsNullOrWhiteSpace(testRun.CheckerComment)) { testRun.CheckerComment = null; } } var timeUsedAsString = testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used"); if (timeUsedAsString != null) { double timeUsed; if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed)) { testRun.TimeUsed = (int)timeUsed; } } var memoryUsedAsString = testRunDescription.GetStringBetween( "Memory used (in bytes): ", Environment.NewLine); if (memoryUsedAsString != null) { testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim()); } } else if (testRunDescription.StartsWith("Runtime error:")) { testRun.ResultType = TestRunResultType.RunTimeError; testRun.ExecutionComment = testRunDescription.Replace("Runtime error:", string.Empty).Trim(); testRun.CheckerComment = null; testRun.TimeUsed = 0; testRun.MemoryUsed = 0; } else if (testRunDescription.StartsWith("Time limit!")) { testRun.ResultType = TestRunResultType.TimeLimit; testRun.ExecutionComment = null; testRun.CheckerComment = null; testRun.TimeUsed = problem.TimeLimit; testRun.MemoryUsed = 0; } else { testRun.ResultType = TestRunResultType.RunTimeError; testRun.ExecutionComment = testRunDescription.Trim(); testRun.CheckerComment = null; testRun.TimeUsed = 0; testRun.MemoryUsed = 0; } newDb.TestRuns.Add(testRun); } } newDb.Submissions.Add(submission); } newDb.SaveChanges(); Console.Write("."); } }