/// <summary> /// Called via ajax after a user successfully completes their chi^2 tests /// </summary> /// <param name="p_module">Call ID of module being completed</param> /// <returns></returns> public JsonResult FinishLab(int p_module) { Module module = db.Module.FirstOrDefault(t => t.Call_id == p_module); UseInstance instance = new UseInstance(); if (RecordLog(module, 3, out instance) > 0) { return Json(new { success = false }); } return Json(new { success = true }); }
/// <summary> /// Logs a lab's activity to a UseInstance, which is tied to a user profile. Stores a few browser metrics as well /// </summary> /// <param name="p_module">The Module being used in the current UseInstance</param> /// <param name="p_generation">The generation of flies being produced currently</param> /// <param name="p_instance">The Instance being updated with Fly data</param> /// <returns>A code, where 0 = success and anything else = failure </returns> private int RecordLog(Module p_module, int p_generation, out UseInstance p_instance) { UseInstance instance = new UseInstance(); LabUser labUser = new LabUser(); try { labUser = db.LabUser.OrderByDescending(t => t.Id).FirstOrDefault(t => t.GID == (CWSToolkit.AuthUser.GetUsername() ?? "_Guest_")); } catch (ArgumentNullException e) { Exception ex = new Exception("The log recorder exited with error code 2.", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); p_instance = null; return 2; } catch (InvalidOperationException) { //no labuser profile, so we will make one here labUser.GID = CWSToolkit.AuthUser.GetUsername() ?? Constants.Guest; labUser.Name = CWSToolkit.AuthUser.GetFullName() ?? Constants.Guest; labUser.Active = (labUser.Name != Constants.Guest); db.LabUser.Add(labUser); } catch (Exception e) { Exception ex = new Exception("The log recorder exited with error code 3.", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); p_instance = null; return 3; } //now we have a user to add an instance to //check if its an old user and reactivate them if (!labUser.Active && labUser.Name != Constants.Guest) { labUser.Active = true; db.Entry(labUser).State = EntityState.Modified; } instance.Module = p_module; instance.Time = DateTime.Now; switch (p_generation) { case 1: instance.Stage = Constants.Stage_1; break; case 2: instance.Stage = Constants.Stage_2; break; case 3: instance.Stage = Constants.Stage_3; break; default: instance.Stage = "Error"; break; } //grab some browser info try { var bc = Request.Browser; instance.OS = bc.Platform; instance.Browser = bc.Type; instance.IP = Request.UserHostAddress; p_instance = instance; } catch (NotImplementedException e) { Elmah.ErrorSignal.FromCurrentContext().Raise(e); instance.OS = ""; instance.Browser = ""; instance.IP = ""; } catch (Exception e) { Exception ex = new InvalidOperationException("The log recorder exited with error code 4.", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); p_instance = null; return 4; } p_instance = instance; labUser.UseInstances.Add(instance); db.UseInstance.Add(instance); try { db.SaveChanges(); } catch (Exception e) { Exception ex = new Exception("The log recorder exited with error code 5.", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); return 5; } return 0; }
/// <summary> /// helper method to create a fly from the trait strings javascript sends over /// </summary> /// <param name="p_module">Module parameter for the new fly</param> /// <param name="p_gender">Gender parameter for the new fly</param> /// <param name="p_traits">Array of TraitIDs for the new fly</param> /// <returns>A new Fly, null if it fails</returns> private Fly LogFly(int p_module, UseInstance p_instance, string p_gender, ReducedTraitModel[] p_traits) { Fly toAdd = new Fly() { Id = 0 }; try { toAdd.Gender = db.Gender.First(t => t.GenderName == p_gender); toAdd.Module = db.Module.First(t => t.Call_id == p_module); toAdd.UseInstance = p_instance; } catch (Exception e) { Exception ex = new InvalidOperationException("Something in the configuration of gender or module is wrong. This should be easily fixable through the Admin Dashboard.", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); return null; } //foreach (ReducedTraitModel cur in p_traits) //{ // Trait newTrait = GetTrait( db.Trait.Find(cur.Id), cur.IsHeterozygous ); // toAdd.Traits.Add(newTrait); // db.Entry(newTrait).State = EntityState.Detached; //} db.Fly.Add(toAdd); try { db.SaveChanges(); } catch (Exception e) { Exception ex = new InvalidOperationException("There was an issue storing a fly (Lab/NewFly).", e); Elmah.ErrorSignal.FromCurrentContext().Raise(ex); return null; } return toAdd; }
public ActionResult ModuleHub(int p_generation, int p_module, int p_offspring, string p_gender, string p_traits) { //just use json.decode //var traitLists = System.Web.Helpers.Json.Decode<int[][]>(p_traitStr); ReducedTraitModel[][] traitLists = System.Web.Helpers.Json.Decode<ReducedTraitModel[][]>(p_traits); String[] genderList = System.Web.Helpers.Json.Decode<string[]>(p_gender); List<FlyViewModel> currentFlies = new List<FlyViewModel>(); Module module = db.Module.First(t => t.Call_id == p_module); //p_offspring = (int)(.5 * p_offspring); if (module == null) { InvalidOperationException e = new InvalidOperationException("Someone tried to make a call to a nonexistent module. This exception was generated in LabController/ModuleHub."); Elmah.ErrorSignal.FromCurrentContext().Raise(e); return Json(new { success = false }); } //if we get here successfully we can consider the lab officially started UseInstance instance = new UseInstance(); if (RecordLog(module, p_generation, out instance) > 0) { return Json(new { success = false }); } //Fly f1 = LogFly(p_module, instance, genderList[0], traitLists[0]); //Fly f2 = LogFly(p_module, instance, genderList[1], traitLists[1]); List<CrossList> crossResults = new List<CrossList>(); ReducedTraitModel[] f_arr; //father ReducedTraitModel[] m_arr; //mother List<ReducedTraitModel> m_arr_dist = new List<ReducedTraitModel>(); //mothers distance linekd traits List<ReducedTraitModel> f_arr_dist = new List<ReducedTraitModel>(); //fathers distance linked traits if (genderList[0].ToLower() == Constants.Male.ToLower()) { f_arr = traitLists[0]; m_arr = traitLists[1]; } else { f_arr = traitLists[1]; m_arr = traitLists[0]; } //perform split into distance linked traits and non here for (int i = 0; i < f_arr.Length; i++) { var f = f_arr[i]; //father side trait var m = m_arr[i]; //mother side trait //checking chromosome number //all pairs will either be wild or 1 possible trait from that category, //so only check the non zero (non wild) chromo number int cnum_checking = (m.ChromosomeNumber > 1) ? m.ChromosomeNumber : f.ChromosomeNumber; if ((f_arr.Count(t => t.ChromosomeNumber == cnum_checking) + m_arr.Count(t => t.ChromosomeNumber == cnum_checking) > 1) && p_module > 2 && cnum_checking > 1) { //cram em in there m_arr_dist.Add(m); f_arr_dist.Add(f); } else { crossResults.Add(BasicCross(f_arr[i], m_arr[i])); } } //RUN THE DISTANCE CROSSER if (m_arr_dist.Count == 2) { //run 2 point cross method crossResults.Add(TwoPointCross(f_arr_dist.ToArray(), m_arr_dist.ToArray())); } if (m_arr_dist.Count == 3) { //run 3 point cross method crossResults.Add(ThreePointCross(f_arr_dist.ToArray(), m_arr_dist.ToArray())); } List<FlyViewModel> processedFlies = BuildChildren(p_module, p_offspring, p_generation, crossResults.ToArray()); List<String> catsToDisplay = new List<String>(); foreach (var fly in processedFlies) { var nonWilds = fly.Traits.Where(t => !t.Name.ToLower().Equals(Constants.Wild.ToLower())); foreach (var trait in nonWilds) { String catName = db.Trait.Find(trait.Id).Category.CatName; if (!catsToDisplay.Contains(catName)) { catsToDisplay.Add(catName); } } } processedFlies.RemoveAll(t => t.Frequency == 0); int offset = (int)(.01 * p_offspring); //initialize maxval to subtract at flylist population time Random randomizer = new Random(); foreach (var adjusting in processedFlies) { //save expected value for chi^2 test adjusting.Expected = adjusting.Frequency; if (p_offspring <= 10000) { adjusting.Frequency -= randomizer.Next((int)(.04 * adjusting.Frequency)); } else { adjusting.Frequency -= randomizer.Next((int)(.08 * adjusting.Frequency)); } } string returnFlies = System.Web.Helpers.Json.Encode(processedFlies); string catList = System.Web.Helpers.Json.Encode(catsToDisplay); return Json(new { flies = returnFlies, catList = catList }); }
public ActionResult ModalUserInfo(int id = 0) { LabUser user = db.LabUser.Find(id); if (user == null) { return Json(new { success = false }); } LabUserViewModel luvm = new LabUserViewModel(); luvm.User = user; UseInstance laststart = user.UseInstances.LastOrDefault(t => t.Stage.ToLower() == Constants.Stage_1 && (t.Active ?? true)); UseInstance lastfinish = user.UseInstances.LastOrDefault(t => t.Stage.ToLower() == Constants.Stage_3 && (t.Active ?? true)); if (laststart == null || lastfinish == null) { Module dummyMod = new Module() { ModuleName = Constants.Null_Instance }; UseInstance dummyIns = new UseInstance() { Browser = Constants.Null_Instance, OS = Constants.Null_Instance, IP = Constants.Null_Instance }; luvm.Module = dummyMod; luvm.LastStart = Constants.Null_Instance; luvm.LastFinish = Constants.Null_Instance; luvm.LabsCompleted = 0; luvm.lastInstance = dummyIns; } else { luvm.Module = laststart.Module; luvm.LabsCompleted = user.UseInstances.Count(t => t.Stage.ToLower() == Constants.Stage_3 && (t.Active ?? true)); luvm.LastStart = laststart.Time.ToString(); luvm.LastFinish = DateTime.Compare(lastfinish.Time, laststart.Time) > 0 ? lastfinish.Time.ToString() : Constants.Null_Instance; luvm.lastInstance = laststart; } return PartialView("_FullInfo", luvm); }