Ejemplo n.º 1
0
        public void TestDeserialize()
        {
            Assert.AreEqual(1, CompanySettingsEntry.Manipulator.InsertDataInto(TestConnection, TableName, Id1));
            CompanySettingsEntry toTest = CompanySettingsEntry.Manipulator.RetrieveDataWhere(TestConnection, TableName, "SettingKey=\"" + Id1.SettingKey + "\"")[0];

            Assert.AreEqual(Id1, toTest);
            Assert.AreEqual(1, CompanySettingsEntry.Manipulator.RemoveDataWhere(TestConnection, TableName, "SettingKey=\"" + Id1.SettingKey + "\""));
        }
Ejemplo n.º 2
0
        private JsonDictionaryStringConstructor WriteSettingToOutput(CompanySettingsEntry entry)
        {
            JsonDictionaryStringConstructor ret = new JsonDictionaryStringConstructor();

            ret.SetMapping("SettingValue", entry.SettingValue);
            ret.SetMapping("SettingKey", entry.SettingKey);
            ret.SetMapping("Id", entry.Id);
            ret.SetMapping("Options", GetOptionsForKey(entry.SettingKey));
            return(ret);
        }
Ejemplo n.º 3
0
 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");
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// POST request format located in the Web Api Enumeration v2
        /// under the tab Company/Forum, starting row 1
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePostRequest(HttpListenerContext ctx)
        {
            try {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                CompanyForumApiFullPostRequest entry = ParseForumEntry(ctx);
                if (!ValidateFullPostRequest(entry))
                {
                    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 (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (entry.PostText.Contains('<'))
                    {
                        WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks");
                        return;
                    }
                    CompanySettingsEntry isPublicSetting = connection.GetCompanySettingsWhere(entry.CompanyId, "SettingKey=\"" + CompanySettingsKey.Public + "\"")[0];
                    bool isPublic = bool.Parse(isPublicSetting.SettingValue);
                    if (!isPublic && mappedUser.Company != entry.CompanyId)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Cannot access other company's private data");
                        return;
                    }
                    #endregion

                    //user is good, add post text
                    res = connection.AddForumPost(entry.CompanyId, entry.JobEntryId, new UserToTextEntry()
                    {
                        Text = entry.PostText, UserId = entry.UserId
                    });
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                }
            }
            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);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// GET request format located in the Web Api Enumeration v2
        /// under the tab Company/Forum, starting row 49
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandleGetRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }

                CompanyForumApiGetRequest entry = JsonDataObjectUtil <CompanyForumApiGetRequest> .ParseObject(ctx);

                if (!ValidateGetRequest(entry))
                {
                    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 was not found on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    CompanySettingsEntry isPublicSetting = connection.GetCompanySettingsWhere(entry.CompanyId, "SettingKey=\"" + CompanySettingsKey.Public + "\"")[0];
                    bool isPublic = bool.Parse(isPublicSetting.SettingValue);
                    if (!isPublic && mappedUser.Company != entry.CompanyId)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Cannot access other company's private data");
                        return;
                    }
                    #endregion

                    #region Get Forum
                    RepairJobEntry forumEntry = connection.GetDataEntryById(entry.CompanyId, entry.JobEntryId);
                    if (forumEntry == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Job Data Entry was not found on the server");
                        return;
                    }
                    JsonListStringConstructor       returnListConstructor = new JsonListStringConstructor();
                    JsonDictionaryStringConstructor repairJobConstructor  = new JsonDictionaryStringConstructor();
                    repairJobConstructor.SetMapping("Make", forumEntry.Make);
                    repairJobConstructor.SetMapping("Model", forumEntry.Model);
                    if (forumEntry.Year == -1)
                    {
                        repairJobConstructor.SetMapping("Year", "Unknown");
                    }
                    else
                    {
                        repairJobConstructor.SetMapping("Year", forumEntry.Year);
                    }
                    repairJobConstructor.SetMapping("Complaint", forumEntry.Complaint);
                    repairJobConstructor.SetMapping("Problem", forumEntry.Problem);
                    RequirementsEntry repairJobRequirements = RequirementsEntry.ParseJsonString(forumEntry.Requirements);
                    List <string>     auxillaryRequirements = new List <string>(repairJobRequirements.Auxillary.Select(req => req.Requirement));
                    repairJobConstructor.SetMapping("AuxillaryRequirements", auxillaryRequirements);
                    repairJobConstructor.SetMapping("PartRequirements", repairJobRequirements.Parts);
                    repairJobConstructor.SetMapping("SafetyRequirements", repairJobRequirements.Safety);
                    returnListConstructor.AddElement(repairJobConstructor);

                    List <UserToTextEntry> forumPosts = connection.GetForumPosts(mappedUser.Company, entry.JobEntryId);
                    if (forumPosts == null)
                    {
                        WriteBodylessResponse(ctx, 404, "Not Found");
                        return;
                    }
                    forumPosts.ForEach(post => returnListConstructor.AddElement(ConvertForumPostToJson(post, connection)));
                    WriteBodyResponse(ctx, 200, "OK", returnListConstructor.ToString(), "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", e.Message);
            }
        }
Ejemplo n.º 6
0
 public void Init()
 {
     Id1 = new CompanySettingsEntry("*****@*****.**", "1");
     Id2 = new CompanySettingsEntry("*****@*****.**", "1");
     Id3 = new CompanySettingsEntry("*****@*****.**", "1");
 }
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                ArchiveApiPutRequest req = JsonDataObjectUtil <ArchiveApiPutRequest> .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 Validate User
                    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;
                    }
                    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 access other company's private data");
                        return;
                    }
                    #endregion

                    UserSettingsEntry numPredictionsRequested = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(mappedUser.Settings).FirstOrDefault(entry => entry.Key.Equals(UserSettingsEntryKeys.ArchiveQueryResults));

                    if (numPredictionsRequested == null)
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "User did not contain a setting with a key " + UserSettingsEntryKeys.ArchiveQueryResults);
                        return;
                    }
                    int numRequested = int.Parse(numPredictionsRequested.Value);
                    #region Input sanitation
                    string whereString = "";
                    bool   addedWhere  = false;
                    if (req.Entry.Complaint != null)
                    {
                        if (!PerformSanitization(req.Entry.Complaint))
                        {
                            return;
                        }
                        whereString += " Complaint like \"%" + req.Entry.Complaint + "%\"";
                        addedWhere   = true;
                    }
                    if (req.Entry.Problem != null)
                    {
                        if (!PerformSanitization(req.Entry.Problem))
                        {
                            return;
                        }
                        if (addedWhere)
                        {
                            whereString += " and";
                        }
                        whereString += " Problem like \"%" + req.Entry.Problem + "%\"";
                        addedWhere   = true;
                    }
                    if (req.Entry.Make != null)
                    {
                        if (!PerformSanitization(req.Entry.Make))
                        {
                            return;
                        }
                        if (addedWhere)
                        {
                            whereString += " and";
                        }
                        whereString += " Make like \"%" + req.Entry.Make + "%\"";
                        addedWhere   = true;
                    }
                    if (req.Entry.Model != null)
                    {
                        if (!PerformSanitization(req.Entry.Model))
                        {
                            return;
                        }
                        if (addedWhere)
                        {
                            whereString += " and";
                        }
                        whereString += " Model like \"%" + req.Entry.Model + "%\"";
                        addedWhere   = true;
                    }
                    if (req.Entry.Year != 0)
                    {
                        if (addedWhere)
                        {
                            whereString += " and";
                        }
                        whereString += " Year =" + req.Entry.Year;
                        addedWhere   = true;
                    }
                    #endregion

                    if (!addedWhere)
                    {
                        WriteBodyResponse(ctx, 400, "Bad Request", "No fields in the request's entry were filled");
                        return;
                    }
                    List <RepairJobEntry>     entries        = connection.GetDataEntriesWhere(req.CompanyId, whereString, true);
                    JsonListStringConstructor retConstructor = new JsonListStringConstructor();
                    try
                    {
                        entries.ForEach(entry => retConstructor.AddElement(ConvertEntry(entry)));
                    } catch (NullReferenceException)
                    {
                        WriteBodyResponse(ctx, 200, "OK", "[]", "application/json");
                        return;
                    }
                    WriteBodyResponse(ctx, 200, "OK", retConstructor.ToString(), "application/json");

                    bool PerformSanitization(string queryIn)
                    {
                        if (queryIn.Contains('`'))
                        {
                            WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the single quote character, which is disallowed due to MySQL injection attacks");
                            return(false);
                        }
                        return(true);
                    }
                }
            }
            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);
            }
        }
Ejemplo n.º 8
0
        private void HandlePostRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                PredictApiPostRequest req = JsonDataObjectUtil <PredictApiPostRequest> .ParseObject(ctx);

                if (!ValidateGetRequest(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;
                    }
                    List <UserSettingsEntry> userSettings = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(mappedUser.Settings);

                    UserSettingsEntry predictionQueryResultsSetting = userSettings.Where(entry => entry.Key.Equals(UserSettingsEntryKeys.PredictionQueryResults)).First();
                    int numQueriesRequested          = int.Parse(predictionQueryResultsSetting.Value);
                    DatabaseQueryProcessor processor = new DatabaseQueryProcessor();
                    string ret = processor.ProcessQueryForSimilarQueries(req.Entry, connection, req.CompanyId, req.ComplaintGroupId, numQueriesRequested);
                    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);
            }
        }