From 9a9641a96b380b28fef895e501595295061785d7 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 28 Jan 2020 19:02:58 +0000 Subject: [PATCH] --- devdocs/todo.txt | 3 -- server/AyaNova/Startup.cs | 4 +- server/AyaNova/util/Seeder.cs | 93 +++++++++++++++++++---------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/devdocs/todo.txt b/devdocs/todo.txt index bb8d80ca..9a741300 100644 --- a/devdocs/todo.txt +++ b/devdocs/todo.txt @@ -7,9 +7,6 @@ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNTcxODU5OTU0IiwiZXhwIjoiMTU3MjQ - -TODO: REFACTOR BIZ objects have getbiz internal which can be removed and just check if httpcontext is null - TODO: Seeder create widget is creating a new widgetbiz each loop iteration but create user is not, try both ways and performance test TODO: License.cs using httpclient directly albeit trying to do it right` diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 094883fb..d36b93de 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -409,7 +409,7 @@ namespace AyaNova // ******************** TESTING WIPE DB ***************************** // //Set this to true to wipe the db and reinstall a trial license and re-seed the data - var TESTING_REFRESH_DB = false;//####################################################################################### + var TESTING_REFRESH_DB = true;//####################################################################################### #if (DEBUG) @@ -450,7 +450,7 @@ namespace AyaNova { AyaNova.Core.License.FetchKeyAsync(apiServerState, dbContext, _newLog).Wait(); //NOTE: For unit testing make sure the time zone in util is set to the same figure as here to ensure list filter by date tests will work because server is on same page as user in terms of time - Util.Seeder.SeedDatabaseAsync(Util.Seeder.SeedLevel.SmallOneManShopTrialDataSet, -7).Wait();//############################################################################################# + Util.Seeder.SeedDatabaseAsync(Util.Seeder.SeedLevel.MediumLocalServiceCompanyTrialDataSet, -7).Wait();//############################################################################################# } //TESTING #endif diff --git a/server/AyaNova/util/Seeder.cs b/server/AyaNova/util/Seeder.cs index f09b0390..812200ac 100644 --- a/server/AyaNova/util/Seeder.cs +++ b/server/AyaNova/util/Seeder.cs @@ -507,60 +507,66 @@ namespace AyaNova.Util - public static async Task GenSeedUserAsync(ILogger log, int count, AuthorizationRoles roles, UserType userType, decimal timeZoneOffset, bool active = true, string login = null, string password = null, long localeId = 0) { if (localeId == 0) localeId = ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID; - using (var ct = ServiceProviderProvider.DBContext) - { - UserBiz Biz = UserBiz.GetBiz(ct); + //Don't do the following commented out, it's slower + // using (var ct = ServiceProviderProvider.DBContext) + // { + // UserBiz Biz = UserBiz.GetBiz(ct); + + + Faker Fake = new Faker(); + + for (int x = 0; x < count; x++) + { + User u = new User(); + u.Active = active; + + var p = new Bogus.Person(); + u.Name = Uniquify(p.FullName); + if (login != null) + { + u.Login = login; + u.Name += " - " + login; + } + else + u.Login = p.FirstName; + + if (password != null) + u.Password = password; + else + u.Password = u.Login; + u.Roles = roles; + u.LocaleId = localeId; + u.UserType = userType; + u.EmployeeNumber = "A-" + (454 + SeededUserCount).ToString() + "-Y"; + u.Notes = Fake.Lorem.Sentence();//Fake.Lorem.Paragraph(2); + //TODO: After have USER and HEADOFFICE and VENDOR, if usertype is subcontractor or client or headoffice it needs to set a corresponding user's parent org record id to go with it + u.Tags = RandomTags(Fake); + //Children and relations + u.UserOptions = new UserOptions(); + u.UserOptions.EmailAddress = p.Email.Replace("gmail.com", "helloayanova.com").Replace("hotmail.com", "helloayanova.com").Replace("yahoo.com", "helloayanova.com"); + u.UserOptions.TimeZoneOffset = timeZoneOffset; + + + //this seems wrong but is actually faster!? + UserBiz Biz = UserBiz.GetBiz(ServiceProviderProvider.DBContext); //allow creation of not entirely ready users (missing client id or subcontractor vendor id etc) Biz.SeedOrImportRelaxedRulesMode = true; - Faker Fake = new Faker(); - for (int x = 0; x < count; x++) + var NewObject = await Biz.CreateAsync(u); + if (NewObject == null) { - User u = new User(); - u.Active = active; - - var p = new Bogus.Person(); - u.Name = Uniquify(p.FullName); - if (login != null) - { - u.Login = login; - u.Name += " - " + login; - } - else - u.Login = p.FirstName; - - if (password != null) - u.Password = password; - else - u.Password = u.Login; - u.Roles = roles; - u.LocaleId = localeId; - u.UserType = userType; - u.EmployeeNumber = "A-" + (454 + SeededUserCount).ToString() + "-Y"; - u.Notes = Fake.Lorem.Sentence();//Fake.Lorem.Paragraph(2); - //TODO: After have USER and HEADOFFICE and VENDOR, if usertype is subcontractor or client or headoffice it needs to set a corresponding user's parent org record id to go with it - u.Tags = RandomTags(Fake); - //Children and relations - u.UserOptions = new UserOptions(); - u.UserOptions.EmailAddress = p.Email.Replace("gmail.com", "helloayanova.com").Replace("hotmail.com", "helloayanova.com").Replace("yahoo.com", "helloayanova.com"); - u.UserOptions.TimeZoneOffset = timeZoneOffset; - - var NewObject = await Biz.CreateAsync(u); - if (NewObject == null) - { - log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString()); - throw new System.Exception("Seeder::GenSeedUser error creating user\r\n" + Biz.GetErrorsAsString()); - } + log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString()); + throw new System.Exception("Seeder::GenSeedUser error creating user\r\n" + Biz.GetErrorsAsString()); } + //} } SeededUserCount += count; @@ -571,6 +577,9 @@ namespace AyaNova.Util // public static async Task GenSeedWidgetAsync(ILogger log, int count) { + //this is 4 times slower than doing it inside the loop below + //seems counterintuitive but maybe it's to do with the db context not being refreshed? + // WidgetBiz biz = WidgetBiz.GetBiz(ServiceProviderProvider.DBContext); var f = new Bogus.Faker(); @@ -605,7 +614,7 @@ namespace AyaNova.Util o.CustomFields = $@"{{c1:""{c1}"",c2:""{c2}"",c3:{c3},c4:{c4},c5:{c5}}}"; - + //This seems wrong to do in a loop but is 4 times faster this way ?!? WidgetBiz biz = WidgetBiz.GetBiz(ServiceProviderProvider.DBContext); var NewObject = await biz.CreateAsync(o); if (NewObject == null)