diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 3370a429..a46b970c 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -408,7 +408,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) @@ -449,7 +449,7 @@ namespace AyaNova { AyaNova.Core.License.Fetch(apiServerState, dbContext, _newLog); //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.SeedDatabase(Util.Seeder.SeedLevel.MediumLocalServiceCompanyTrialDataSet, -7);//############################################################################################# + Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.SmallOneManShopTrialDataSet, -7);//############################################################################################# } //TESTING #endif diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 52d7898a..abc3bd54 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -83,45 +83,49 @@ namespace AyaNova.Biz outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - await ct.Widget.AddAsync(outObj); - await ct.SaveChangesAsync(); + //Save to db + var TheContext = ct; + if (TheContext == null) + TheContext = ServiceProviderProvider.DBContext; + await TheContext.Widget.AddAsync(outObj); + await TheContext.SaveChangesAsync(); //Handle child and associated items: - EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); + EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TheContext); SearchIndex(outObj, true); - TagUtil.ProcessUpdateTagsInRepository(ct, outObj.Tags, null); + TagUtil.ProcessUpdateTagsInRepository(TheContext, outObj.Tags, null); return outObj; } } - //Internal version for seeding - internal Widget Create(AyContext TempContext, Widget inObj) - { - Validate(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with widget - Widget outObj = inObj; + // //Internal version for seeding + // internal Widget Create(AyContext TempContext, Widget inObj) + // { + // Validate(inObj, null); + // if (HasErrors) + // return null; + // else + // { + // //do stuff with widget + // Widget outObj = inObj; - //Test get serial id visible id number from generator - outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext(); - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); + // //Test get serial id visible id number from generator + // outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext(); + // outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); + // outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - TempContext.Widget.Add(outObj); - TempContext.SaveChanges(); + // TempContext.Widget.Add(outObj); + // TempContext.SaveChanges(); - //Handle child and associated items: - EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext); - SearchIndex(outObj, true); - TagUtil.ProcessUpdateTagsInRepository(TempContext, outObj.Tags, null); + // //Handle child and associated items: + // EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext); + // SearchIndex(outObj, true); + // TagUtil.ProcessUpdateTagsInRepository(TempContext, outObj.Tags, null); - return outObj; - } - } + // return outObj; + // } + // } //////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/server/AyaNova/util/Seeder.cs b/server/AyaNova/util/Seeder.cs index 2b8658a3..b70c3e98 100644 --- a/server/AyaNova/util/Seeder.cs +++ b/server/AyaNova/util/Seeder.cs @@ -17,6 +17,37 @@ namespace AyaNova.Util public enum SeedLevel { SmallOneManShopTrialDataSet, MediumLocalServiceCompanyTrialDataSet, LargeCorporateMultiRegionalTrialDataSet, HugeForLoadTest }; public static int SeededUserCount = 0; + #region CACHED OBJECTS + + //DB CONTEXT + private static AyContext _context = null; + internal static AyContext Cached_Context + { + get + { + if (_context == null) + { + _context = ServiceProviderProvider.DBContext; + } + return _context; + } + } + + //WIDGETBIZ + private static WidgetBiz _widgetBiz = null; + internal static WidgetBiz Cached_WidgetBiz + { + get + { + if (_widgetBiz == null) + { + _widgetBiz = WidgetBiz.GetBizInternal(Cached_Context); + } + return _widgetBiz; + } + } + #endregion cached objects + ////////////////////////////////////////////////////// //Seed database for trial and testing purposes // @@ -68,12 +99,13 @@ namespace AyaNova.Util //Erase all the data except for the license, schema and the manager user DbUtil.EmptyBizDataFromDatabaseForSeedingOrImporting(log); + + //Set the time zone of the manager account { - var ct = ServiceProviderProvider.DBContext; - var mgr = ct.UserOptions.FirstAsync(m => m.Id == 1).Result; + var mgr = Cached_Context.UserOptions.FirstAsync(m => m.Id == 1).Result; mgr.TimeZoneOffset = timeZoneOffset; - ct.SaveChanges(); + Cached_Context.SaveChanges(); } @@ -127,7 +159,7 @@ namespace AyaNova.Util }; //Create and save to db - var res = FormCustomBiz.GetBizInternal(ServiceProviderProvider.DBContext).CreateAsync(fc).Result; + var res = FormCustomBiz.GetBizInternal(Cached_Context).CreateAsync(fc).Result; } //Seed special test data for integration testing @@ -158,7 +190,7 @@ namespace AyaNova.Util //100 widgets watch = new Stopwatch(); watch.Start(); - GenSeedWidget(log, 100); + GenSeedWidgetAsync(log, 100); //PERF watch.Stop(); LogStatus(JobId, LogJob, log, $"100 Widgets seeded in {watch.ElapsedMilliseconds} ms"); @@ -219,7 +251,7 @@ namespace AyaNova.Util watch = new Stopwatch(); watch.Start(); - GenSeedWidget(log, 500); + GenSeedWidgetAsync(log, 500); //PERF watch.Stop(); LogStatus(JobId, LogJob, log, $"500 Widgets seeded in {watch.ElapsedMilliseconds} ms"); @@ -294,7 +326,7 @@ namespace AyaNova.Util LogStatus(JobId, LogJob, log, $"Seeding 5,000 Widgets...."); watch = new Stopwatch(); watch.Start(); - GenSeedWidget(log, 5000); + GenSeedWidgetAsync(log, 5000); //PERF watch.Stop(); @@ -370,7 +402,7 @@ namespace AyaNova.Util LogStatus(JobId, LogJob, log, $"Seeding 20,000 Widgets...."); watch = new Stopwatch(); watch.Start(); - GenSeedWidget(log, 20000); + GenSeedWidgetAsync(log, 20000); watch.Stop(); LogStatus(JobId, LogJob, log, $"20k Widgets seeded in {watch.ElapsedMilliseconds} ms"); #endregion genhuge @@ -391,6 +423,8 @@ namespace AyaNova.Util { log.LogInformation($"Seeder: setting server state back to {wasServerState.ToString()}"); apiServerState.SetState(wasServerState, wasReason); + _context = null; + _widgetBiz = null; } } @@ -500,7 +534,7 @@ namespace AyaNova.Util public static void GenSeedUser(ILogger log, int count, AuthorizationRoles roles, UserType userType, decimal timeZoneOffset, bool active = true, string login = null, string password = null, long localeId = 0) { - UserBiz Biz = UserBiz.GetBizInternal(ServiceProviderProvider.DBContext); + UserBiz Biz = UserBiz.GetBizInternal(Cached_Context); //allow creation of not entirely ready users (missing client id or subcontractor vendor id etc) Biz.SeedOrImportRelaxedRulesMode = true; @@ -538,7 +572,7 @@ namespace AyaNova.Util 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 = Biz.Create(ServiceProviderProvider.DBContext, u); + var NewObject = Biz.Create(Cached_Context, u); if (NewObject == null) { log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString()); @@ -552,9 +586,9 @@ namespace AyaNova.Util ////////////////////////////////////////////////////// //Seed widget for testing // - public static void GenSeedWidget(ILogger log, int count) + public static async void GenSeedWidgetAsync(ILogger log, int count) { - WidgetBiz Biz = WidgetBiz.GetBizInternal(ServiceProviderProvider.DBContext); + var f = new Bogus.Faker(); //RANDOM ROLES @@ -590,11 +624,11 @@ namespace AyaNova.Util o.CustomFields = $@"{{c1:""{c1}"",c2:""{c2}"",c3:{c3},c4:{c4},c5:{c5}}}"; - var NewObject = Biz.Create(ServiceProviderProvider.DBContext, o); + var NewObject = await Cached_WidgetBiz.CreateAsync(o); if (NewObject == null) { - log.LogError($"Seeder::GenSeedWidget error creating widget {o.Name}\r\n" + Biz.GetErrorsAsString()); - throw new System.Exception("Seeder::GenSeedWidget error creating widget\r\n" + Biz.GetErrorsAsString()); + log.LogError($"Seeder::GenSeedWidget error creating widget {o.Name}\r\n" + Cached_WidgetBiz.GetErrorsAsString()); + throw new System.Exception("Seeder::GenSeedWidget error creating widget\r\n" + Cached_WidgetBiz.GetErrorsAsString()); } } }