This commit is contained in:
2020-01-23 23:41:31 +00:00
parent 784ea46fd1
commit b14afa1726
3 changed files with 82 additions and 44 deletions

View File

@@ -408,7 +408,7 @@ namespace AyaNova
// ******************** TESTING WIPE DB ***************************** // ******************** TESTING WIPE DB *****************************
// //
//Set this to true to wipe the db and reinstall a trial license and re-seed the data //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) #if (DEBUG)
@@ -449,7 +449,7 @@ namespace AyaNova
{ {
AyaNova.Core.License.Fetch(apiServerState, dbContext, _newLog); 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 //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 //TESTING
#endif #endif

View File

@@ -83,45 +83,49 @@ namespace AyaNova.Biz
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
await ct.Widget.AddAsync(outObj); //Save to db
await ct.SaveChangesAsync(); var TheContext = ct;
if (TheContext == null)
TheContext = ServiceProviderProvider.DBContext;
await TheContext.Widget.AddAsync(outObj);
await TheContext.SaveChangesAsync();
//Handle child and associated items: //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); SearchIndex(outObj, true);
TagUtil.ProcessUpdateTagsInRepository(ct, outObj.Tags, null); TagUtil.ProcessUpdateTagsInRepository(TheContext, outObj.Tags, null);
return outObj; return outObj;
} }
} }
//Internal version for seeding // //Internal version for seeding
internal Widget Create(AyContext TempContext, Widget inObj) // internal Widget Create(AyContext TempContext, Widget inObj)
{ // {
Validate(inObj, null); // Validate(inObj, null);
if (HasErrors) // if (HasErrors)
return null; // return null;
else // else
{ // {
//do stuff with widget // //do stuff with widget
Widget outObj = inObj; // Widget outObj = inObj;
//Test get serial id visible id number from generator // //Test get serial id visible id number from generator
outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext(); // outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); // outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); // outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
TempContext.Widget.Add(outObj); // TempContext.Widget.Add(outObj);
TempContext.SaveChanges(); // TempContext.SaveChanges();
//Handle child and associated items: // //Handle child and associated items:
EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext); // EventLogProcessor.LogEventToDatabaseAndSaveEntireContext(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext);
SearchIndex(outObj, true); // SearchIndex(outObj, true);
TagUtil.ProcessUpdateTagsInRepository(TempContext, outObj.Tags, null); // TagUtil.ProcessUpdateTagsInRepository(TempContext, outObj.Tags, null);
return outObj; // return outObj;
} // }
} // }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -17,6 +17,37 @@ namespace AyaNova.Util
public enum SeedLevel { SmallOneManShopTrialDataSet, MediumLocalServiceCompanyTrialDataSet, LargeCorporateMultiRegionalTrialDataSet, HugeForLoadTest }; public enum SeedLevel { SmallOneManShopTrialDataSet, MediumLocalServiceCompanyTrialDataSet, LargeCorporateMultiRegionalTrialDataSet, HugeForLoadTest };
public static int SeededUserCount = 0; 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 //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 //Erase all the data except for the license, schema and the manager user
DbUtil.EmptyBizDataFromDatabaseForSeedingOrImporting(log); DbUtil.EmptyBizDataFromDatabaseForSeedingOrImporting(log);
//Set the time zone of the manager account //Set the time zone of the manager account
{ {
var ct = ServiceProviderProvider.DBContext; var mgr = Cached_Context.UserOptions.FirstAsync(m => m.Id == 1).Result;
var mgr = ct.UserOptions.FirstAsync(m => m.Id == 1).Result;
mgr.TimeZoneOffset = timeZoneOffset; mgr.TimeZoneOffset = timeZoneOffset;
ct.SaveChanges(); Cached_Context.SaveChanges();
} }
@@ -127,7 +159,7 @@ namespace AyaNova.Util
}; };
//Create and save to db //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 //Seed special test data for integration testing
@@ -158,7 +190,7 @@ namespace AyaNova.Util
//100 widgets //100 widgets
watch = new Stopwatch(); watch = new Stopwatch();
watch.Start(); watch.Start();
GenSeedWidget(log, 100); GenSeedWidgetAsync(log, 100);
//PERF //PERF
watch.Stop(); watch.Stop();
LogStatus(JobId, LogJob, log, $"100 Widgets seeded in {watch.ElapsedMilliseconds} ms"); LogStatus(JobId, LogJob, log, $"100 Widgets seeded in {watch.ElapsedMilliseconds} ms");
@@ -219,7 +251,7 @@ namespace AyaNova.Util
watch = new Stopwatch(); watch = new Stopwatch();
watch.Start(); watch.Start();
GenSeedWidget(log, 500); GenSeedWidgetAsync(log, 500);
//PERF //PERF
watch.Stop(); watch.Stop();
LogStatus(JobId, LogJob, log, $"500 Widgets seeded in {watch.ElapsedMilliseconds} ms"); 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...."); LogStatus(JobId, LogJob, log, $"Seeding 5,000 Widgets....");
watch = new Stopwatch(); watch = new Stopwatch();
watch.Start(); watch.Start();
GenSeedWidget(log, 5000); GenSeedWidgetAsync(log, 5000);
//PERF //PERF
watch.Stop(); watch.Stop();
@@ -370,7 +402,7 @@ namespace AyaNova.Util
LogStatus(JobId, LogJob, log, $"Seeding 20,000 Widgets...."); LogStatus(JobId, LogJob, log, $"Seeding 20,000 Widgets....");
watch = new Stopwatch(); watch = new Stopwatch();
watch.Start(); watch.Start();
GenSeedWidget(log, 20000); GenSeedWidgetAsync(log, 20000);
watch.Stop(); watch.Stop();
LogStatus(JobId, LogJob, log, $"20k Widgets seeded in {watch.ElapsedMilliseconds} ms"); LogStatus(JobId, LogJob, log, $"20k Widgets seeded in {watch.ElapsedMilliseconds} ms");
#endregion genhuge #endregion genhuge
@@ -391,6 +423,8 @@ namespace AyaNova.Util
{ {
log.LogInformation($"Seeder: setting server state back to {wasServerState.ToString()}"); log.LogInformation($"Seeder: setting server state back to {wasServerState.ToString()}");
apiServerState.SetState(wasServerState, wasReason); 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) 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) //allow creation of not entirely ready users (missing client id or subcontractor vendor id etc)
Biz.SeedOrImportRelaxedRulesMode = true; 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.EmailAddress = p.Email.Replace("gmail.com", "helloayanova.com").Replace("hotmail.com", "helloayanova.com").Replace("yahoo.com", "helloayanova.com");
u.UserOptions.TimeZoneOffset = timeZoneOffset; u.UserOptions.TimeZoneOffset = timeZoneOffset;
var NewObject = Biz.Create(ServiceProviderProvider.DBContext, u); var NewObject = Biz.Create(Cached_Context, u);
if (NewObject == null) if (NewObject == null)
{ {
log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString()); log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString());
@@ -552,9 +586,9 @@ namespace AyaNova.Util
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
//Seed widget for testing //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(); var f = new Bogus.Faker();
//RANDOM ROLES //RANDOM ROLES
@@ -590,11 +624,11 @@ namespace AyaNova.Util
o.CustomFields = $@"{{c1:""{c1}"",c2:""{c2}"",c3:{c3},c4:{c4},c5:{c5}}}"; 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) if (NewObject == null)
{ {
log.LogError($"Seeder::GenSeedWidget error creating widget {o.Name}\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" + Biz.GetErrorsAsString()); throw new System.Exception("Seeder::GenSeedWidget error creating widget\r\n" + Cached_WidgetBiz.GetErrorsAsString());
} }
} }
} }