/// <summary>
        /// Function to perform the update of the <see cref="RepairJobEntry"/>'s validation status
        /// </summary>
        /// <param name="manipulator"><see cref="MySqlDataManipulator"/> used to update the <see cref="RepairJobEntry"/>'s
        /// validation status</param>
        public override void PerformFunction(MySqlDataManipulator manipulator)
        {
            RepairJobEntry entry = manipulator.GetDataEntryById(CompanyId, RepairEntryId, CurrentlyValidated);

            if (!manipulator.UpdateValidationStatus(CompanyId, entry, CurrentlyValidated))
            {
                Console.WriteLine("Failed to update validation status");
                return;
            }
            Console.WriteLine("Updated validation status for entry with id " + RepairEntryId);
        }
        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);
            }
        }
Example #3
0
        /// <summary>
        /// Request for either negatively or positively reporting a repair job entry. Documention is found in the Web API Enumeration file
        /// in the /RepairJob/Report tab, starting at row 1
        /// </summary>
        /// <param name="ctx">The 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;
                }
                RepairJobVoteRequest entry = JsonDataObjectUtil <RepairJobVoteRequest> .ParseObject(ctx);

                if (entry == null)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    return;
                }
                #endregion

                //Otherwise we have a valid entry, validate user
                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(entry.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User Not Found");
                        return;
                    }
                    if (!ValidateVoteRequest(entry))
                    {
                        WriteBodyResponse(ctx, 400, "Invalid Format", "Request was in incorrect format");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was expired or incorrect");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    RepairJobEntry repairEntry = connection.GetDataEntryById(mappedUser.Company, entry.RepairJobId, true);
                    if (repairEntry == null && connection.LastException == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Referenced Repair Job Was Not Found");
                        return;
                    }
                    else if (repairEntry == null)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message);
                        return;
                    }
                    if (entry.Upvote == 0)
                    {
                        if (!connection.UpdateValidationStatus(mappedUser.Company, repairEntry, true))
                        {
                            WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message);
                            return;
                        }
                    }
                    else
                    {
                        NotSupported(ctx);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                    #endregion
                }
            }
            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);
            }
        }