Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
inlguy committed Dec 21, 2021
2 parents 42e56d8 + 3fcad13 commit e322d60
Show file tree
Hide file tree
Showing 408 changed files with 53,653 additions and 44,224 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ DesignTimeBuild/
/CSETWebApi/CSETWeb_Api/CSETWebCore.Reports/Resources/secrets.json
/CSETWebApi/CSETWeb_Api/CSETWeb_Api/Diagram/src/main/webapp/node_modules
/CSETWebApi/CSETWeb_Api/CSETWeb_Api/Diagram/src/main/webapp/js
/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/CSETWebCore.Api.csproj.user
/CSETWebApi/CSETWeb_Api/CSETWebCore.UpgradeLibrary/CSETWebCore.UpgradeLibrary.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public SprsScoreModel GetSPRSScore(int assessmentId)
var x = biz.GetMaturityStructure(assessmentId);


int handCalculatedScore = 110;
int calculatedScore = 110;

foreach (var goal in x.Descendants("Goal"))
{
Expand Down Expand Up @@ -213,13 +213,16 @@ public SprsScoreModel GetSPRSScore(int assessmentId)
break;
}

handCalculatedScore -= q.Score;

calculatedScore -= q.Score;
d.Questions.Add(q);
}
}

response.SprsScore = handCalculatedScore;
response.SprsScore = calculatedScore;

var sprsGauge = new Helpers.ReportWidgets.SprsScoreGauge(calculatedScore, 500, 100);
response.GaugeSvg = sprsGauge.ToString();

return response;
}
Expand All @@ -235,11 +238,20 @@ public List<LevelAnswers> GetAnswerDistributionByLevel(int assessmentId)

var model = _context.AVAILABLE_MATURITY_MODELS.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault();

var targetLevel = _context.ASSESSMENT_SELECTED_LEVELS.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault();
var selectedLevel = _context.ASSESSMENT_SELECTED_LEVELS.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault();
int targetLevel;
if (selectedLevel == null)
{
targetLevel = 1;
}
else
{
targetLevel = int.Parse(selectedLevel.Standard_Specific_Sal_Level);
}

var levels = _context.MATURITY_LEVELS
.Include(x => x.MATURITY_QUESTIONS)
.Where(x => x.Maturity_Model_Id == model.model_id && x.Level <= int.Parse(targetLevel.Standard_Specific_Sal_Level))
.Where(x => x.Maturity_Model_Id == model.model_id && x.Level <= targetLevel)
.ToList();

var answers = _context.Answer_Maturity.Where(x => x.Assessment_Id == assessmentId);
Expand Down Expand Up @@ -553,6 +565,11 @@ public MaturityResponse GetMaturityQuestions(int assessmentId, string installati
myModel.model_id == q.Maturity_Model_Id).ToList();



var rkw = questions.Where(x => x.Question_Title == "SI.L2-3.14.7").FirstOrDefault();



// Get all MATURITY answers for the assessment
var answers = from a in _context.ANSWER.Where(x => x.Assessment_Id == assessmentId && x.Question_Type == "Maturity")
from b in _context.VIEW_QUESTIONS_STATUS.Where(x => x.Answer_Id == a.Answer_Id).DefaultIfEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,38 @@ public GenSALTable GetGenSals()
return genSALTable;
}


/// <summary>
///
/// </summary>
/// <returns></returns>
public MaturityReportData.MaturityModel GetBasicMaturityModel()
{
var query = (
from amm in _context.AVAILABLE_MATURITY_MODELS
join mm in _context.MATURITY_MODELS on amm.model_id equals mm.Maturity_Model_Id
join asl in _context.ASSESSMENT_SELECTED_LEVELS on amm.Assessment_Id equals asl.Assessment_Id into xx
from asl2 in xx.DefaultIfEmpty()
where amm.Assessment_Id == _assessmentId
select new { amm, mm, asl2 }
).FirstOrDefault();


var response = new MaturityReportData.MaturityModel()
{
MaturityModelName = query.mm.Model_Name,
TargetLevel = null
};

if (query.asl2 != null)
{
response.TargetLevel = int.Parse(query.asl2.Standard_Specific_Sal_Level);
}

return response;
}


/// <summary>
///
/// </summary>
Expand Down
153 changes: 153 additions & 0 deletions CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Sal/GeneralSalBusiness.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
////////////////////////////////
//
// Copyright 2021 Battelle Energy Alliance, LLC
//
//
////////////////////////////////
using CSETWebCore.DataLayer.Model;
using CSETWebCore.Interfaces.Helpers;
using CSETWebCore.Model.Sal;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;


namespace CSETWebCore.Business.Sal
{
public class GeneralSalBusiness
{
private readonly CSETContext _context;
private readonly ITokenManager _token;
private readonly IAssessmentUtil _assessmentUtil;

private Dictionary<double, string> SALThresholdDictionary { get; set; }


/// <summary>
/// Constructor
/// </summary>
/// <param name="context"></param>
public GeneralSalBusiness(CSETContext context, ITokenManager token, IAssessmentUtil assessmentUtil)
{
InitializeSALThresholds();
_context = context;
_token = token;
_assessmentUtil = assessmentUtil;
}


/// <summary>
/// Gets the current saved slider weights from the database and
/// feeds them to the calculation.
/// </summary>
/// <param name="assessmentId"></param>
/// <returns></returns>
public String GetCurrentSAL(int assessmentId)
{
//go to the database get the current list of selected weights
//pass that to the calculate

var weights = from a in _context.GENERAL_SAL
join b in _context.GEN_SAL_WEIGHTS on new { a.Sal_Name, a.Slider_Value } equals new { b.Sal_Name, b.Slider_Value }
where a.Assessment_Id == assessmentId
select b;
List<GEN_SAL_WEIGHTS> materialized = weights.ToList();
double weight = GetOverallSALWeight(materialized);
return CalculateSAL(weight);
}


private void InitializeSALThresholds()
{
//going to make this a little less conservative
this.SALThresholdDictionary = new Dictionary<double, string>();

// RKW - Barry says NONE is no longer a valid SAL level ..... this.SALThresholdDictionary.Add(0, Constants.SAL_NONE);
this.SALThresholdDictionary.Add(1, Constants.Constants.SAL_LOW);
this.SALThresholdDictionary.Add(255, Constants.Constants.SAL_MODERATE);
this.SALThresholdDictionary.Add(1000, Constants.Constants.SAL_HIGH);
this.SALThresholdDictionary.Add(20000, Constants.Constants.SAL_VERY_HIGH);
}


/// <summary>
/// calculates the overall GenSal weight
/// (its is a simple sum of the current selected weight values)
/// </summary>
/// <returns></returns>
private double GetOverallSALWeight(List<GEN_SAL_WEIGHTS> weights)
{
//need to pass in a list of the weights
//currently selected items can be retrieved from the database
double weight = 0;
foreach (GEN_SAL_WEIGHTS mapping in weights)
{ // TODO: probably better to iterate through the children on the page but dictionary works for now.
weight += (double)mapping.Weight;
}

return weight;
}


/// <summary>
/// Calculates the SAL based on the value of the sliders.
/// The new calculated level is saved to the database.
/// </summary>
private string CalculateSAL(double weight)
{
string SALToReturn = Constants.Constants.SAL_LOW;
foreach (double thresholdValue in this.SALThresholdDictionary.Keys)
{
if (weight >= thresholdValue)
{
SALToReturn = SALThresholdDictionary[thresholdValue];
}
}

// Persist the overall SAL to the database
int assessmentId = _token.AssessmentForUser();
STANDARD_SELECTION ss = _context.STANDARD_SELECTION.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault();
ss.Selected_Sal_Level = SALToReturn;
_context.SaveChanges();

return SALToReturn;
}


public string SaveWeightAndCalculate(SaveWeight ws)
{
//look up the weight id and save it to the db
var gensal = _context.GENERAL_SAL.Where(x => x.Sal_Name == ws.slidername && x.Assessment_Id == ws.assessmentid).FirstOrDefault();
if (gensal == null)
_context.GENERAL_SAL.Add(new GENERAL_SAL() { Assessment_Id = ws.assessmentid, Sal_Name = ws.slidername, Slider_Value = ws.Slider_Value });
else
gensal.Slider_Value = ws.Slider_Value;

_context.SaveChanges();
_assessmentUtil.TouchAssessment(ws.assessmentid);
string rval = GetCurrentSAL(ws.assessmentid);
return rval;
}


/// <summary>
/// Get the currently saved value, without regard for the sliders.
/// This is used for the initial display of the Gen SAL screen before the user starts
/// tweaking sliders.
/// </summary>
/// <param name="assessmentid"></param>
/// <returns></returns>
public string GetSavedSALValue(int assessmentid)
{
STANDARD_SELECTION sal = _context.STANDARD_SELECTION.Where(ss => ss.Assessment_Id == assessmentid).FirstOrDefault();
if (sal == null)
{
// default to Low if we don't yet have a record
return "Low";
}

return sal.Selected_Sal_Level;
}
}
}
84 changes: 0 additions & 84 deletions CSETWebApi/CSETWeb_Api/CSETWebCore.DatabaseManager/DbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public void SetupDb()
" (FILENAME = '" + csetDestLogFile + "') FOR ATTACH; ",
CurrentMasterConnectionString);

SetInstallationTable();

// Verify that the database exists now
using (SqlConnection conn = new SqlConnection(CurrentMasterConnectionString))
{
Expand All @@ -92,8 +90,6 @@ public void SetupDb()
{
log.Error(e.Message);
}

SetInstallationTable();

// Verify that the database has been copied over and exists now
using (SqlConnection conn = new SqlConnection(CurrentMasterConnectionString))
Expand Down Expand Up @@ -185,25 +181,6 @@ private void CopyDB(string csetDestDBFile, string csetDestLogFile)
log.Info("Not necessary to copy the database");
}

public void UpdateVersionString(string connectionString, string newVersion)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
string decimalVersion = ReplaceLastOccurrence(newVersion, ".", "");
cmd.CommandText = "UPDATE [dbo].[CSET_VERSION] SET Version_Id = '" + decimalVersion + "',[Cset_Version] = '" + newVersion + "'";
cmd.ExecuteNonQuery();
}
catch (SqlException sqle)
{
log.Error(sqle.Message);
}
}
}

public static string EscapeString(string value)
{
return value.Replace("'", "''");
Expand Down Expand Up @@ -290,18 +267,6 @@ private bool IsLocalDb2019Installed()
return false;
}

private string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
int Place = 0;
String result = Source;
while (result.Count(f => (f == '.')) > 1)
{
Place = result.LastIndexOf(Find);
result = result.Remove(Place, Find.Length).Insert(Place, Replace);
}
return result;
}

public void ExecuteNonQuery(string sql, string connectionString)
{
try
Expand All @@ -319,55 +284,6 @@ public void ExecuteNonQuery(string sql, string connectionString)
log.Error(sqle.Message);
}
}

/// <summary>
/// Creates a JWT_Secret in DB to be used for tokens.
/// A unique 'installation ID' is also created and stored. Does nothing if secret has already been set in DB.
/// </summary>
public void SetInstallationTable()
{
try
{
using (CSETContext context = new CSETContext())
{
var inst = context.INSTALLATION.FirstOrDefault();
if (inst != null)
{
return;
}

// This is the first run of CSET -- generate a new secret and installation identifier
string newSecret = null;
string newInstallID = null;

var byteArray = new byte[(int)Math.Ceiling(130 / 2.0)];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(byteArray);
newSecret = String.Concat(Array.ConvertAll(byteArray, x => x.ToString("X2")));
}

newInstallID = Guid.NewGuid().ToString();


// Store the new secret and installation ID
var installRec = new INSTALLATION
{
JWT_Secret = newSecret,
Generated_UTC = DateTime.UtcNow,
Installation_ID = newInstallID
};
context.INSTALLATION.Add(installRec);
context.SaveChanges();
}
}
catch (Exception e)
{
log.Error(e.Message);
}

}

public Version NewCSETVersion { get; private set; }
public string DatabaseCode { get; private set; } = "CSETWeb";
public string ClientCode { get; private set; } = "DHS";
Expand Down
Loading

0 comments on commit e322d60

Please sign in to comment.