public static double PerformAutomatedTestingWithData(MySqlDataManipulator manipulator, int companyId, DatabaseQueryProcessor processor, List <RepairJobEntry> entries)
        {
            double currentDifference = 0;

            foreach (RepairJobEntry entry in entries)
            {
                RepairJobEntry testEntryCopy = new RepairJobEntry()
                {
                    Make      = entry.Make,
                    Model     = entry.Model,
                    Year      = entry.Year,
                    Complaint = entry.Complaint
                };

                List <string>             entryProblemKeywords = processor.PredictKeywordsInJobData(entry, false);
                int                       startingNumKeywords  = entryProblemKeywords.Count;
                string                    complaintGroupsJson  = processor.ProcessQueryForComplaintGroups(testEntryCopy, manipulator, companyId);
                List <ComplaintGroupJson> complaintGroups      = JsonDataObjectUtil <List <ComplaintGroupJson> > .ParseObject(complaintGroupsJson);

                List <int> complaintGroupIds = complaintGroups.Select(group => { return(group.Id); }).ToList();
                foreach (int complaintGroupId in complaintGroupIds)
                {
                    string dataEntryJson = processor.ProcessQueryForSimilarQueries(testEntryCopy, manipulator, companyId, complaintGroupId, 5);
                    List <SimilarQueryJson> jobDataEntries = JsonDataObjectUtil <List <SimilarQueryJson> > .ParseObject(dataEntryJson);

                    List <RepairJobEntry> dataEntries = jobDataEntries.Select(query => new RepairJobEntry()
                    {
                        Make = query.Make, Model = query.Model, Problem = query.Problem, Complaint = query.Complaint
                    }).ToList();
                    foreach (RepairJobEntry currEntry in dataEntries)
                    {
                        List <string> currEntryKeywords = processor.PredictKeywordsInJobData(currEntry, false);
                        List <string> toRemove          = entryProblemKeywords.Where(keyword => currEntryKeywords.Contains(keyword)).ToList();
                        foreach (string keyword in toRemove)
                        {
                            entryProblemKeywords.Remove(keyword);
                        }
                    }
                }
                currentDifference += (double)entryProblemKeywords.Count / startingNumKeywords;
            }
            return((currentDifference / entries.Count) * 100);
        }
Ejemplo n.º 2
0
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                PredictApiPutRequest req = JsonDataObjectUtil <PredictApiPutRequest> .ParseObject(ctx);

                if (!ValidatePutRequest(req))
                {
                    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(req.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, req.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    CompanySettingsEntry isPublicSetting = connection.GetCompanySettingsWhere(req.CompanyId, "SettingKey=\"" + CompanySettingsKey.Public + "\"")[0];
                    bool isPublic = bool.Parse(isPublicSetting.SettingValue);
                    if (!isPublic && mappedUser.Company != req.CompanyId)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Cannot predict using other company's private data");
                        return;
                    }
                    UserSettingsEntry numPredictionsRequested = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(mappedUser.Settings).FirstOrDefault(entry => entry.Key.Equals(UserSettingsEntryKeys.ComplaintGroupResults));

                    if (numPredictionsRequested == null)
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "User did not contain a setting with a key " + UserSettingsEntryKeys.ComplaintGroupResults);
                        return;
                    }
                    int numRequested = int.Parse(numPredictionsRequested.Value);
                    DatabaseQueryProcessor processor = new DatabaseQueryProcessor();
                    string ret = processor.ProcessQueryForComplaintGroups(req.Entry, connection, req.CompanyId, numRequested);
                    WriteBodyResponse(ctx, 200, "OK", ret, "application/json");
                    #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", "Error occurred during processing of request: " + e.Message);
            }
        }