From 8e199d8126dbf977a9abc3957feebdcca65ac8b3 Mon Sep 17 00:00:00 2001
From: martiivGylden <martin.iversen@gyldendal.no>
Date: Tue, 19 Mar 2024 15:27:07 +0100
Subject: [PATCH] Updated structure for matrix parsing FML

---
 dashBoard.py     | 52 +++++++++++++++++++++++++++++++++++++++++++++---
 diagramParser.py |  3 ++-
 matrices.py      | 39 +++++++++++++++++++-----------------
 3 files changed, 72 insertions(+), 22 deletions(-)

diff --git a/dashBoard.py b/dashBoard.py
index 29ecb3a..7f267dd 100644
--- a/dashBoard.py
+++ b/dashBoard.py
@@ -20,19 +20,65 @@ def openFile():
     )
     diagram = parse.parseDiagramFile(fileName)    
     
+    
+
     top = Toplevel()
     top.title("Parsed diagram")
     top.geometry("1500x1500")
-    lab = Label(top, text="Dynamic components in the diagrams")
+    
+    h = Scrollbar(top, orient="vertical")
+    h.pack()    
+    
+    lab = Label(top, text="Threat components in the diagrams")
     lab.config(font= ("Comic Sans MS", 25, "bold"))
     lab.pack(pady=20)    
     
-    createDynamic(diagram, top)
-    
+    createThreats(diagram, top) 
+        
     return diagram
 
+def createThreats(diagram: Diagram, top):
+    for threat in diagram.threats.values():
+        
+        metrics, components = findMetrics(diagram, threat.id)
+                
+        threatFrame = Frame(top)
+        threatFrame.pack()
 
 
+        l1 = Label(threatFrame, text= f"ID: {threat.id} Description: {threat.threatSource}")
+        l2 = Label(threatFrame, text= f"Associated vulnerability: {threat.vulnerability}")        
+        l3 = Label(threatFrame, text= f"Observable metrics: {metrics}")
+        l4 = Label(threatFrame, text = f"Linked Components: {components}") 
+                
+        l1.config(font=("Comic Sans MS", 20))
+        l2.config(font=("Comic Sans MS", 15))
+        l3.config(font=("Comic Sans MS", 15))
+        l4.config(font=("Comic Sans MS", 15))
+        
+        l1.pack()
+        l2.pack()
+        l3.pack()
+        l4.pack()
+        
+        
+        
+        
+def findMetrics(diagram, id):
+    relevantDynamic = []
+    relatedComponents = []
+    
+    for metric in diagram.metrics.values():
+        if id in metric.erID:
+            relevantDynamic.append(metric.ID)
+            relatedComponents.append(metric.erID)
+            
+        elif id in metric.bowtieID:
+            relevantDynamic.append(metric.ID)
+            relatedComponents.append(metric.bowtieID)
+            
+    
+    return relevantDynamic, relatedComponents
 
 def createDynamic(diagram: Diagram, top):
     
diff --git a/diagramParser.py b/diagramParser.py
index 57df129..b8241ea 100644
--- a/diagramParser.py
+++ b/diagramParser.py
@@ -23,8 +23,9 @@ def parseDiagramFile(csvFile) -> component.Diagram:
     consequences = parseConsequences(df, consequences)    
     
     metrics, dynamics = parseDynamic(df, metrics, dynamics)
-    metricsMatrix = matrix.parseTable(df, metrics) #Parse the table
+    metricsMatrix, metrics = matrix.parseTable(df, metrics) #Parse the table
 
+    print(metricsMatrix)
         
     return diagram    
     
diff --git a/matrices.py b/matrices.py
index 053a239..44d76aa 100644
--- a/matrices.py
+++ b/matrices.py
@@ -15,17 +15,20 @@ def parseTable(df, metricsDict):
     
     matrics = pd.DataFrame(columns=cols)
 
-    print(len(table))
+    length = len(table.columns)
+    length = length-7 # Setting the length equal to the amount of text fields
+    stopLength = length-15
+    print(stopLength)
 
     # ! Parsing the first row of the table which has a fucked structure because lucidchart is garbage : ) 
     erID1 = table[const.textArea4].item()              # Define the ER ID
     BowTieID1 = table[const.textArea5].item()          # Define the Bowtie ID
     MetricID1 = table[const.textArea6].item()          # Define the Metric ID
-    MetricName1 = table[const.textArea15].item()        # Define the Metric Name
-    Value1 = table[const.textArea16].item()            # Define the Value
-    MeasureDate1 = table[const.textArea17].item()      # Define the Measure Date
-    Frequency1 = table[const.textArea18].item()        # Define the Frequency
-    Guide1 = table[const.textArea19].item()            # Define the Measurement guide
+    MetricName1 = table[f"Text Area {length-9}"].item()        # Define the Metric Name
+    Value1 = table[f"Text Area {length-8}"].item()            # Define the Value
+    MeasureDate1 = table[f"Text Area {length-7}"].item()      # Define the Measure Date
+    Frequency1 = table[f"Text Area {length-6}"].item()        # Define the Frequency
+    Guide1 = table[f"Text Area {length-5}"].item()            # Define the Measurement guide
     
     
     matrics.loc[0] = erID1, BowTieID1, MetricID1, MetricName1, Value1, MeasureDate1, Frequency1, Guide1 # Fill inn dataframe
@@ -38,11 +41,11 @@ def parseTable(df, metricsDict):
     erID2 = table[const.textArea7].item()              # Define the ER ID
     BowTieID2 = table[const.textArea8].item()          # Define the Bowtie ID
     MetricID2 = table[const.textArea9].item()          # Define the Metric ID
-    MetricName2 = table[const.textArea20].item()        # Define the Metric Name
-    Value2 = table[const.textArea21].item()            # Define the Value
-    MeasureDate2 = table[const.textArea22].item()      # Define the Measure Date
-    Frequency2 = table[const.textArea23].item()        # Define the Frequency
-    Guide2 = table[const.textArea24].item()            # Define the Measurement guide    
+    MetricName2 = table[f"Text Area {length-4}"].item()        # Define the Metric Name
+    Value2 = table[f"Text Area {length-3}"].item()            # Define the Value
+    MeasureDate2 = table[f"Text Area {length-2}"].item()      # Define the Measure Date
+    Frequency2 = table[f"Text Area {length-1}"].item()        # Define the Frequency
+    Guide2 = table[f"Text Area {length}"].item()            # Define the Measurement guide    
     
     matrics.loc[1] = erID2, BowTieID2, MetricID2, MetricName2, Value2, MeasureDate2, Frequency2, Guide2
     
@@ -52,15 +55,13 @@ def parseTable(df, metricsDict):
     if metric == None: 
         handleMissingMetric()
     
-    
-        
     matricsIndex = 2
 
     # Parsing from the third row and onwards is standard format
-    for i in range(25, len(table.columns), 8):
-        if pd.isnull(table["Text Area "+str(i)].item()) == True or pd.isnull(table["Text Area "+str(i+1)].item()) == True:
+    for i in range(10, stopLength, 8):
+        if i >= stopLength:
             break
-        
+                        
         erID = table["Text Area "+str(i)].item()              # Define the ER ID
         BowTieID = table["Text Area "+str(i+1)].item()          # Define the Bowtie ID
         MetricID = table["Text Area "+str(i+2)].item()          # Define the Metric ID
@@ -73,9 +74,11 @@ def parseTable(df, metricsDict):
         series = pd.Series([erID, BowTieID, MetricID, MetricName, Value, MeasureDate, Frequency, Guide])
         
         matrics.loc[matricsIndex] = erID, BowTieID, MetricID, MetricName, Value, MeasureDate, Frequency, Guide
-        matricsIndex += 1    
+        matricsIndex += 1   
+         
+    print(matrics)
     
-    return matrics
+    return matrics, metricsDict
 
 def handleMissingMetric():
     """Function will communicate to end user that a metric is in the matrics but is not used in the diagram 
-- 
GitLab