You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spot.apache.org by ev...@apache.org on 2017/03/29 16:51:38 UTC

[14/50] [abbrv] incubator-spot git commit: use bulk insert for scored connections

use bulk insert for scored connections


Project: http://git-wip-us.apache.org/repos/asf/incubator-spot/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-spot/commit/a32d3ed3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-spot/tree/a32d3ed3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-spot/diff/a32d3ed3

Branch: refs/heads/SPOT-35_graphql_api
Commit: a32d3ed36398bd4faeff12ac24fda5e2f1e2cf14
Parents: c35a4e9
Author: Everardo Lopez Sandoval (Intel) <el...@elopezsa-mac02.zpn.intel.com>
Authored: Tue Mar 7 18:22:06 2017 -0600
Committer: Diego Ortiz Huerta <di...@intel.com>
Committed: Wed Mar 15 11:49:48 2017 -0700

----------------------------------------------------------------------
 spot-oa/api/resources/dns.py         | 19 ++++++++++++-------
 spot-oa/api/resources/flow.py        | 19 +++++++++++--------
 spot-oa/api/resources/hdfs_client.py |  8 ++++++--
 spot-oa/api/resources/proxy.py       | 14 +++++++++-----
 4 files changed, 38 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/a32d3ed3/spot-oa/api/resources/dns.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/resources/dns.py b/spot-oa/api/resources/dns.py
index 6000fa6..767454e 100644
--- a/spot-oa/api/resources/dns.py
+++ b/spot-oa/api/resources/dns.py
@@ -153,7 +153,14 @@ def  score_connection(date,ip="", dns="", ip_sev=0, dns_sev=0):
     connections = ImpalaEngine.execute_query(sq_query + connections_filter)
 
     # add score to connections
+
+    insert_command = ("""INSERT INTO {0}.dns_threat_investigation
+                        PARTITION (y={1},m={2},d={3})
+                        VALUES (""") \
+                        .format(db,date.year,date.month,date.day)
+
     fb_data =  []
+    first = True
     for row in connections:
         # insert into dns_threat_investigation.
         threat_data = (row[1],row[3],row[4],ip_sev if ip == row[3] else 0,\
@@ -163,13 +170,11 @@ def  score_connection(date,ip="", dns="", ip_sev=0, dns_sev=0):
         row[8],row[9],row[10],row[11],ip_sev,dns_sev,row[12],row[13],row[14],\
         row[15],row[1]])
 
-        insert_command = ("""
-            INSERT INTO {0}.dns_threat_investigation
-            PARTITION (y={1},m={2},d={3})
-            VALUES {4}
-            """).format(db,date.year,date.month,date.day,threat_data)
+        insert_command += "{0}{1}".format("," if not first else "", threat_data)
+        first = False
 
-        ImpalaEngine.execute_query(insert_command)
+    insert_command += ")"
+    ImpalaEngine.execute_query(insert_command)
 
     # create feedback file.
     app_path = Configuration.spot()
@@ -324,7 +329,7 @@ def  save_comments(ip,query,title,text,date):
 
 """
 --------------------------------------------------------------------------
-Return a list(dict) with all the data ingested during the timeframe 
+Return a list(dict) with all the data ingested during the timeframe
 provided.
 --------------------------------------------------------------------------
 """

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/a32d3ed3/spot-oa/api/resources/flow.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/resources/flow.py b/spot-oa/api/resources/flow.py
index 211932c..14d7132 100755
--- a/spot-oa/api/resources/flow.py
+++ b/spot-oa/api/resources/flow.py
@@ -228,24 +228,27 @@ def score_connection(score,date,src_ip=None,dst_ip=None,src_port=None,dst_port=N
 
     connections_filter += " AND dport = {0}" \
     .format(str(dst_port)) if dst_port else ""
-
     connections = ImpalaEngine.execute_query(connections_query + connections_filter)
 
 
     # add score to connections
+    insert_command = ("""
+        INSERT INTO {0}.flow_threat_investigation
+        PARTITION (y={1},m={2},d={3})
+        VALUES (""")
+        .format(db,date.year,date.month,date.day)
+
     fb_data =  []
+    first = True
     for row in connections:
         # insert into flow_threat_investigation.
         threat_data = (row[0],row[1],row[2],row[3],row[4],score)
         fb_data.append([score,row[0],row[1],row[2],row[3],row[4],row[5],row[6]])
+        insert_command += "{0}{1}".format("," if not first else "", threat_data)
+        first = False
 
-        insert_command = ("""
-            INSERT INTO {0}.flow_threat_investigation
-            PARTITION (y={1},m={2},d={3})
-            VALUES {4}
-            """).format(db,date.year,date.month,date.day,threat_data)
-
-        ImpalaEngine.execute_query(insert_command)
+    insert_command += ")"
+    ImpalaEngine.execute_query(insert_command)
 
     # create feedback file.
     app_path = Configuration.spot()

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/a32d3ed3/spot-oa/api/resources/hdfs_client.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/resources/hdfs_client.py b/spot-oa/api/resources/hdfs_client.py
index d47d16c..41d4214 100644
--- a/spot-oa/api/resources/hdfs_client.py
+++ b/spot-oa/api/resources/hdfs_client.py
@@ -1,5 +1,6 @@
 
 from hdfs import InsecureClient
+from hdfs.util import HdfsError
 from json import dump
 import api.resources.configurator as Config
 
@@ -39,8 +40,11 @@ def delete_folder(hdfs_file,user=None):
     client.delete(hdfs_file,recursive=True)
 
 def list_dir(hdfs_path):
-    client = _get_client()
-    return client.list(hdfs_path)
+    try:
+        client = _get_client()
+        return client.list(hdfs_path)
+    except HdfsError:
+        return {}
 
 def file_exists(hdfs_path,file_name):
     files = list_dir(hdfs_path)

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/a32d3ed3/spot-oa/api/resources/proxy.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/resources/proxy.py b/spot-oa/api/resources/proxy.py
index cbfb368..5edafe2 100644
--- a/spot-oa/api/resources/proxy.py
+++ b/spot-oa/api/resources/proxy.py
@@ -89,8 +89,13 @@ def score_request(date,score,uri):
     connections = ImpalaEngine.execute_query(p_query)
 
     # add score to connections
-    fb_data =  []
+    insert_command = ("""
+		INSERT INTO {0}.proxy_threat_investigation PARTITION (y={1},m={2},d={3})
+		VALUES (""")
+        .format(db,date.year,date.month,date.day)
 
+    fb_data =  []
+    first = True
     for row in connections:
         cip_index = row[2]
         uri_index = row[18]
@@ -103,11 +108,10 @@ def score_request(date,score,uri):
 			,row[8],row[9],row[10],row[11],row[12],row[13],row[14],row[15] \
 			,row[16],row[17],row[18],row[19],score,row[20],row[21],row[22], \
 			row[23],hash_field])
+        insert_command += "{0}{1}".format("," if not first else "", threat_data)
+        first = False
 
-	insert_command = ("""
-		INSERT INTO {0}.proxy_threat_investigation PARTITION (y={1},m={2},d={3})
-		VALUES {4}
-		""").format(db,date.year,date.month,date.day,threat_data)
+    insert_command += ")"
 	ImpalaEngine.execute_query(insert_command)
 
     # create feedback file.