public static void PerformDataValidation(MySqlDataManipulator manipulator, int companyId, DatabaseQueryProcessor processor, int numShuffleTests = 5, int numGroups = 3)
        {
            List <RepairJobEntry>      validatedData    = manipulator.GetDataEntriesWhere(companyId, "id>0", validated: true);
            List <RepairJobEntry>      nonValidatedData = manipulator.GetDataEntriesWhere(companyId, "id>0", validated: false);
            List <NonValidatedMapping> mappings         = nonValidatedData.Select(entry => new NonValidatedMapping()
            {
                Entry = entry, Vote = 0
            }).ToList();
            double currCompanyAccuracy = manipulator.GetCompanyAccuracy(companyId);

            for (int i = 0; i < numShuffleTests; i++)
            {
                mappings.Shuffle();
                List <List <NonValidatedMapping> > nonValidatedTestingGroups = mappings.Split(numGroups);
                foreach (List <NonValidatedMapping> currentTestGroup in nonValidatedTestingGroups)
                {
                    List <RepairJobEntry> testGroup = new List <RepairJobEntry>(validatedData);
                    testGroup.AddRange(currentTestGroup.Select(mapping => mapping.Entry));
                    processor.TrainClusteringModels(manipulator, companyId, testGroup.Select(entry => entry.Complaint).ToList(), training: true);
                    double accuracy = 100 - PerformAutomatedTestingWithData(manipulator, companyId, processor, testGroup);
                    double vote     = (accuracy - currCompanyAccuracy) / currCompanyAccuracy;
                    foreach (NonValidatedMapping mapping in currentTestGroup)
                    {
                        mapping.Vote += vote;
                    }
                }
            }
            bool changed = false;

            foreach (NonValidatedMapping mapping in mappings)
            {
                if (mapping.Vote > 0.01)
                {
                    if (!manipulator.UpdateValidationStatus(companyId, mapping.Entry, wasValidated: false))
                    {
                        Console.WriteLine("Failed to update validation status of Repair Job Entry: " + mapping.Entry.Serialize(TableNameStorage.CompanyNonValidatedRepairJobTable.Replace("(n)", companyId.ToString())));
                        continue;
                    }
                    changed = true;
                }
            }
            if (changed)
            {
                TrainClusteringModel(manipulator, processor, companyId, false);
            }
        }
Exemple #2
0
        /// <summary>
        /// GET request format located in the Web Api Enumeration v2 google sheets document in the shared drive,
        /// under the tab Company/Accuracy, starting at row 1
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                CompanyAccuracyApiPutRequest entry = JsonDataObjectUtil <CompanyAccuracyApiPutRequest> .ParseObject(ctx);

                if (!ValidatePutRequest(entry))
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    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;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.AdminMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "User was not an admin");
                        return;
                    }
                    #endregion

                    double companyAccuracy = connection.GetCompanyAccuracy(mappedUser.Company);


                    WriteBodyResponse(ctx, 200, "OK", companyAccuracy.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);
            }
        }