/// <summary> /// <para>Uses the supplied <see cref="MySqlDataManipulator"/> to add the setting to all of the specified targets</para> /// </summary> /// <param name="manipulator"></param> public override void PerformFunction(MySqlDataManipulator manipulator) { if (Target.Equals("user")) { var users = manipulator.GetUsersWhere("id > 0"); foreach (OverallUser user in users) { //Add the setting to the user if they do not already have a setting with the same key List <UserSettingsEntry> settings = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(user.Settings); bool found = false; foreach (UserSettingsEntry entry in settings) { if (entry.Key.Equals(Key)) { found = true; break; } } if (!found) { settings.Add(new UserSettingsEntry() { Key = Key, Value = Value }); user.Settings = JsonDataObjectUtil <List <UserSettingsEntry> > .ConvertObject(settings); if (!manipulator.UpdateUsersSettings(user)) { Console.WriteLine("Failed to update settings for user " + user.UserId); continue; } Console.WriteLine("Updated settings for user " + user.UserId); continue; } Console.WriteLine("User " + user.UserId + " already had a setting with key " + Key); } } else if (Target.Equals("company")) { var companies = manipulator.GetCompaniesWithNamePortion(""); foreach (CompanyId company in companies) { //Add the setting to the company if it does not already have one with the same key int companyId = company.Id; bool found = manipulator.GetCompanySettingsWhere(companyId, "SettingKey = \"" + Key + "\"").Count == 1; if (!found) { if (!manipulator.AddCompanySetting(companyId, new CompanySettingsEntry(Key, Value))) { Console.WriteLine("Company " + company.LegalName + " failed to have the setting added"); continue; } Console.WriteLine("Successfully added setting for company " + company.LegalName); continue; } Console.WriteLine("Company " + company.LegalName + " already had a setting with key " + Key); } } }
static void PerformTraining() { try { while (true) { MySqlDataManipulator manipulator = new MySqlDataManipulator(); if (!manipulator.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString())) { throw new ArgumentException("MySqlDataManipulator failed to connect to the database"); } Console.WriteLine("Checking company training statuses"); List <CompanyId> companies = manipulator.GetCompaniesWithNamePortion(""); foreach (CompanyId company in companies) { if (manipulator.GetCountInTable(TableNameStorage.CompanyValidatedRepairJobTable.Replace("(n)", company.Id.ToString())) != 0) { DateTime lastTrainedTime = DateTime.Parse(company.LastTrainedTime); CompanySettingsEntry trainInterval = manipulator.GetCompanySettingsWhere(company.Id, "SettingKey=\"" + CompanySettingsKey.RetrainInterval + "\"")[0]; bool shouldTrain = lastTrainedTime.AddDays(int.Parse(trainInterval.SettingValue)) <= DateTime.Now; if (shouldTrain) { Console.WriteLine("Performing training for company " + company.LegalName); DatabaseQueryProcessor processor = new DatabaseQueryProcessor(DatabaseQueryProcessorSettings.RetrieveCompanySettings(manipulator, company.Id)); CompanyModelUtils.TrainClusteringModel(manipulator, processor, company.Id, training: false); company.LastTrainedTime = DateTime.Now.ToString(); manipulator.UpdateCompanyTrainingTime(company); double automatedTestingResults = CompanyModelUtils.PerformAutomatedTesting(manipulator, company.Id, processor); company.ModelAccuracy = (float)(100 - automatedTestingResults); manipulator.UpdateCompanyAutomatedTestingResults(company); Console.WriteLine("Accuracy after training: " + company.ModelAccuracy); } } if (manipulator.GetCountInTable(TableNameStorage.CompanyNonValidatedRepairJobTable.Replace("(n)", company.Id.ToString())) != 0) { DateTime lastValidatedTime = DateTime.Parse(company.LastValidatedTime); bool shouldValidate = lastValidatedTime.AddDays(14) <= DateTime.Now; if (shouldValidate) { Console.WriteLine("Attempting to validate some non-validated data for company " + company.LegalName); DatabaseQueryProcessor processor = new DatabaseQueryProcessor(DatabaseQueryProcessorSettings.RetrieveCompanySettings(manipulator, company.Id)); CompanyModelUtils.PerformDataValidation(manipulator, company.Id, processor); } } } manipulator.Close(); Thread.Sleep(TimeSpan.FromMinutes(120)); } } catch (ThreadInterruptedException) { Console.WriteLine("Retraining Thread Exiting"); } }
private void HandlePutRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "Invalid Format", "Request did not contain a body"); return; } CompanyListRetrieveRequest entry = JsonDataObjectUtil <CompanyListRetrieveRequest> .ParseObject(ctx); if (entry == null) { WriteBodylessResponse(ctx, 400, "Invalid Format"); return; } if (!ValidateRetrieveRequest(entry)) { WriteBodyResponse(ctx, 400, "Invalid Format", "One or more fields contained an invalid value or were missing"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed"); return; } #region User Validation OverallUser mappedUser = connection.GetUserById(entry.UserId); if (mappedUser == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server"); return; } if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken)) { WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect."); return; } #endregion #region Input sanitation if (entry.NamePortion.Contains('`')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained a backtick character(`)." + "This character is disallowed due to SQL injection attacks."); return; } #endregion List <CompanyId> companies = connection.GetCompaniesWithNamePortion(entry.NamePortion); if (companies == null) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while retrieving companies: " + connection.LastException.Message); return; } JsonListStringConstructor retConstructor = new JsonListStringConstructor(); companies.ForEach(req => retConstructor.AddElement(WriteCompanyIdToOutput(req))); WriteBodyResponse(ctx, 200, "OK", retConstructor.ToString()); } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message); } }