diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java old mode 100644 new mode 100755 index 889eb76dd..cef85924e --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -5,6 +5,9 @@ public class globalvalue { public static String fusekiapi = "http://fuseki"; //public static String fusekiapi = "http://10.60.38.173"; public static String mongoapi = "mongo"; - //public static String fusekiapi = "10.60.38.173"; + //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; + public static String neo4japi = "bolt://10.60.38.173"; + //public static String neo4japi = "bolt://localhost"; + } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 7acf9d2ef..b17c5f560 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -1,5 +1,6 @@ package neo4j; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neo4jentities.DataAccessor; @@ -20,6 +21,7 @@ import org.springframework.stereotype.Component; import java.io.BufferedReader; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; @@ -27,10 +29,14 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.regex.Pattern; + import global.globalvalue; import static neo4j.MongoDriver.*; import static neo4j.prometheusDriver.getProInfor; +import static util.FileUtil.saveClusterResult; +import static util.HttpPostUtil.getImage; @Component @@ -55,7 +61,7 @@ public static Map getAllNodesAndLinks(){ QuerySolution qs = rs.next() ; String subject = qs.get("s").toString(); if(subject.contains("http")){ - System.out.println("Subject: " + subject); + //System.out.println("Subject: " + subject); if(subject.contains("server")){ result.add(getServer(subject)); linkList.addAll(getLink(subject, "manage")); @@ -1583,6 +1589,7 @@ public static boolean judgeExist(String url){ return false; } } + qExec.close(); } return true; } @@ -1680,7 +1687,7 @@ public static ArrayList getResourcesWithQuery(){ result.add(r); } } - System.out.println(result); + //System.out.println(result); return result; } @@ -1690,8 +1697,8 @@ public static void addLinkEvent2S(){ Date start = new Date(); Date end = new Date(); try { - start = DateFormat.parse("2019-08-06 00:00:00"); - end = DateFormat.parse("2019-08-06 23:59:59"); + start = DateFormat.parse("2019-10-20 00:00:00"); + end = DateFormat.parse("2019-10-20 23:59:59"); } catch(ParseException px) { px.printStackTrace(); } @@ -1705,10 +1712,11 @@ public static void addLinkEvent2S(){ times.add(i.getTime()/1000); } Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); for (Resource i:resources ) { Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); - System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); + //System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); //JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),times.get(0)+"", times.get(times.size()-1)+""); @@ -1726,7 +1734,8 @@ public static void addLinkEvent2S(){ } } } - System.out.println("request---------"); + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); System.out.println(timeList); System.out.println(proInfor); JSONObject jsonObject = new JSONObject(); @@ -1741,13 +1750,10 @@ public static void addLinkEvent2S(){ } } } - - - } - //查询指定范围内发生的时间 + //查询指定范围内发生的时间 只有 HW 事件 public static Map getEventInFuseki(Date startTime,Date endTime) { SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 @@ -1760,9 +1766,11 @@ public static Map getEventInFuseki(Date startTime,Date endTim ResIterator iter = model.listSubjects(); while (iter.hasNext()) { Resource r = iter.nextResource(); - if (r.toString().contains("event")) + if (Pattern.matches(".*event.*HW.*", r.toString())) { - String timeProperty=r.getProperty(model.createProperty(r.toString()+"/starts_at")).getResource().toString(); + Statement statement = r.getProperty(model.createProperty(r.toString()+"/starts_at")); + if (statement == null) continue; + String timeProperty=statement.getResource().toString(); //截取出时间字符串,去掉中间的“-” int length=timeProperty.length(); String time=timeProperty.substring(length-19,length-9)+" "+timeProperty.substring(length-8); @@ -1799,8 +1807,8 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe System.out.println(addRelation); System.out.println(setInfluenceValue); RDFConnectionRemoteBuilder builderAddRelation = RDFConnectionFuseki.create() - // .destination("http://10.60.38.173:3030/DevKGData/update"); - .destination("http://localhost:3030/gundam/update"); + .destination("http://10.60.38.173:3030/DevKGData/update"); + //.destination("http://localhost:3030/gundam/update"); // CredentialsProvider credsProvider = new BasicCredentialsProvider(); // Credentials credentials = new UsernamePasswordCredentials("admin", "D0rlghQl5IAgYOm"); // credsProvider.setCredentials(AuthScope.ANY, credentials); @@ -1821,7 +1829,145 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe } + //供算法导出数据使用 + public static void getDate(int month, int day){ + SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 + Date start = new Date(); + Date end = new Date(); + try { + start = DateFormat.parse("2019-"+month+"-"+day+" 00:00:00"); + end = DateFormat.parse("2019-"+month+"-"+day+" 23:59:59"); + } catch(ParseException px) { + px.printStackTrace(); + } + Model model = DataAccessor.getInstance().getModel(); + ArrayList resources = getResourcesWithQuery(); + //受到时区的影响 + Map dates = getEventInFuseki(start, end); + List times = new ArrayList<>(); + for (Date i:(ArrayList)dates.get("Date") + ) { + times.add(i.getTime()/1000); + } + Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); + for (Resource i:resources + ) { + Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); + //System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); + + JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); + //JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),times.get(0)+"", times.get(times.size()-1)+""); + if (proInfor == null)continue; + List timeList = new ArrayList(); + for (Object j:times + ) { + for (Object k:proInfor + ) { + if ((Long)j <= ((JSONArray)k).getLong(0)){ + if (!timeList.contains(((JSONArray)k).getLong(0))){ + timeList.add(((JSONArray)k).getLong(0)); + } + break; + } + } + } + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); + System.out.println(timeList); + System.out.println(proInfor); + stringBuffer.append(strings[strings.length-1]); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("file1",timeList); + jsonObject.put("file2", proInfor); + stringBuffer.append("\r\n"); + stringBuffer.append(jsonObject.toString()); + stringBuffer.append("\r\n"); + } + if (stringBuffer.length() == 0){ + return; + } + try { + FileOutputStream fos = new FileOutputStream("/Users/jiang/data/data"+month+"-"+day+".txt"); + fos.write(stringBuffer.toString().getBytes()); + fos.close(); + } + catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + //向算法部分发送数据,返回一个String、两个时间序列 + public static void getClusterResult(){ + SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 + Date start = new Date(); + Date end = new Date(); + try { + start = DateFormat.parse("2019-11-18 00:00:00"); + end = DateFormat.parse("2019-11-18 23:59:59"); + } catch(ParseException px) { + px.printStackTrace(); + } + Model model = DataAccessor.getInstance().getModel(); + ArrayList resources = getResourcesWithQuery(); + //受到时区的影响 + Map dates = getEventInFuseki(start, end); + List times = new ArrayList<>(); + for (Date i:(ArrayList)dates.get("Date") + ) { + times.add(i.getTime()/1000); + } + Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); + for (Resource i:resources + ) { + Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); + JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); + if (proInfor == null)continue; + List timeList = new ArrayList(); + for (Object j:times + ) { + for (Object k:proInfor + ) { + if ((Long)j <= ((JSONArray)k).getLong(0)){ + if (!timeList.contains(((JSONArray)k).getLong(0))){ + timeList.add(((JSONArray)k).getLong(0)); + } + break; + } + } + } + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); + System.out.println(timeList); + System.out.println(proInfor); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("file4", 1); + jsonObject.put("file3", strings[strings.length-1]); + jsonObject.put("file1",timeList); + jsonObject.put("file2", proInfor); + + String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); + System.out.println(re); + if (re != null){ + JSONObject jsonRe = JSON.parseObject(re); +// String correlation = jsonRe.getString("Correlation"); +// List SST = JSONArray.parseArray(jsonRe.getString("SST"), List.class); +// List alarm = JSONArray.parseArray(jsonRe.getString("Alarm"), List.class); + saveClusterResult(jsonRe.toJSONString(), strings[strings.length-1]); + + } + } + } public static void main(String[] args) { +// for (int i = 30; i <= 31; i++) { +// getDate(10,i); +// } +// for (int i = 1; i <= 14; i++) { +// getDate(11,i); +// } + + } } diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java old mode 100644 new mode 100755 index cfd113038..0607469e8 --- a/BackEnd19v/src/main/java/neo4j/MongoDriver.java +++ b/BackEnd19v/src/main/java/neo4j/MongoDriver.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; @@ -78,6 +79,7 @@ public static boolean save2Mongo(Map data){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -99,6 +101,7 @@ public static boolean save2MongoByTime(Map data, String time){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -128,6 +131,46 @@ public static boolean saveKapacitor2Mongo(String message){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); + } catch (Exception e){ + e.printStackTrace(); + return false; + } + return true; + } + + public static boolean saveEvent2Mongo(String content, String source){ + try { + //连接到mongodb服务 + MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + //MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("Event"); + //获取当前时间 + Date day=new Date(); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String time = df.format(day); + System.out.println(time); + Map data = new HashMap<>(); + data.put("content", content); + data.put("source", source); + switch (source) { + case "Kapacitor": + data.put("type", 1); + break; + case "K8s": + data.put("type", 2); + break; + default: + data.put("type", 0); + } + //插入文档 + Document document = new Document(data). + append("time", time); + collection.insertOne(document); + System.out.println("文档插入成功"); + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -154,6 +197,7 @@ public static Map getOneFromMongo(String time){ map.putAll(d); result.add(map); } + mongoClient.close(); if(result.size()==0) return null; System.out.println(result.get(0)); return result.get(0); @@ -219,9 +263,9 @@ public static List getEventMongByTime(String startDate,String startTime, S aL.add(d.getInteger("type")); result.add(aL); } + mongoClient.close(); if(result.size()==0) return null; //System.out.println(result); - return result; } catch (Exception e){ e.printStackTrace(); @@ -260,7 +304,79 @@ public static List getTimesFromMongo(){ System.out.println(d.get("time")); result.add(d.get("time").toString()); } + mongoClient.close(); return result; } + public static boolean saveSystemTypeAndNameFile(String type, String name){ + try { + //连接到mongodb服务 + //MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("SystemTypeAndName"); + //判断是否当前系统类型是否存在 + BasicDBObject query = new BasicDBObject(); + query.put("type",type); + FindIterable findIterable = collection.find(query); + MongoCursor mongoCursor = findIterable.iterator(); + if (!mongoCursor.hasNext()){ + Map data = new HashMap<>(); + data.put("type",type); + data.put("name",new ArrayList()); + //插入文档 + Document document = new Document(data); + collection.insertOne(document); + System.out.println("文档插入成功"); + } + else { + Document d=mongoCursor.next(); + //System.out.println(d); + JSONObject json=JSONObject.parseObject(d.toJson()); + JSONArray jsonArray = json.getJSONArray("name"); + jsonArray.add(name); +// DBCollection dbCol = db.getCollection(COLLECTION_NAME); +// DBCursor ret = dbCol.find(); + BasicDBObject doc = new BasicDBObject(); + BasicDBObject res = new BasicDBObject(); + res.put("name", JSONObject.parseArray(jsonArray.toString(), String.class)); +// System.out.println("将数据集中的所有文档的age修改成40!"); + doc.put("$set", res); + collection.findOneAndUpdate(query,doc); + System.out.println("文档修改成功"); + mongoClient.close(); + } + } catch (Exception e){ + e.printStackTrace(); + return false; + } + return true; + } + + public static JSONArray getAllTypeAndName(){ + JSONArray re = new JSONArray(); + try { + //连接到mongodb服务 + //MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("SystemTypeAndName"); + FindIterable findIterable = collection.find(); + MongoCursor mongoCursor = findIterable.iterator(); + while (mongoCursor.hasNext()){ + Document d=mongoCursor.next(); + re.add(JSONObject.parseObject(d.toJson())); + } + mongoClient.close(); + } catch (Exception e){ + e.printStackTrace(); + } + return re; + } + + public static void main(String[] args) { + saveSystemTypeAndNameFile("1","2"); + } } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java index e9d53848f..a57f0ba96 100755 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -15,6 +15,7 @@ import java.io.*; import java.util.*; +import static global.globalvalue.neo4japi; import static org.neo4j.driver.v1.Values.parameters; import static service.CsvService.csvTimestamp; @@ -105,7 +106,7 @@ public HashMap>> getOneNoderesult(String nam return resultgraph; } public HashMap>> getAllNodes() { - Driver driver = GraphDatabase.driver("bolt://10.60.38.173:7687", + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j","1234")); Map map = new HashMap<>(); HashMap>> resultgraph = new HashMap<>(); @@ -142,6 +143,7 @@ public HashMap>> getAllNodes() { } } driver.close(); + System.out.println(resultgraph); return resultgraph; } public HashMap> getAllLabel(){ @@ -255,7 +257,7 @@ public int AddContainerNode(String label, String name, String volumeMount, Array "SET n.volumeMount = $volumeMount "+"SET n.name = $name "+"SET n.arrayListAdd = $arrayListAdd "+"SET n.arrayListDrop = $arrayListDrop "+ "SET n.type = 'Container_Node' "+"SET n.performance = $performance "+ "return id(n)", parameters("volumeMount",volumeMount,"name",name,"arrayListAdd",arrayListAdd,"arrayListDrop",arrayListDrop, - "performance","name:"+name+";volumeMount:"+volumeMount+";arrayListAdd:"+arrayListAdd+";arrayListDrop:"+arrayListDrop+";type:Container_Node")); + "performance","name:"+name+";volumeMount:"+volumeMount+";arrayListAdd:"+arrayListAdd+";arrayListDrop:"+arrayListDrop+";type:Container_Node")); trueId = newID.single().get(0).asInt(); System.out.println("trueId:"+trueId); //System.out.println(relations); @@ -435,19 +437,19 @@ public static Boolean MetricToDataset(int ID,String dataBaseName,String resultNa Boolean flag = true; try(Session session = driver.session()) { - StatementResult result0 = session.run("Match (n:Dataset) where n.name = $name return ID(n)" - ,parameters("name",dataBaseName)); - if (!result0.hasNext()){ - return false; - } - while (result0.hasNext()){ - Record record = result0.next(); - System.out.println(record); - int curID = record.get(0).asInt(); - System.out.println(curID); - session.run( "Start a=node("+ID+"),b=node("+curID+") Merge (a)-[r:collected_by{type:'collected_by',name:$name}]->(b)" - ,parameters("name",resultName)); - } + StatementResult result0 = session.run("Match (n:Dataset) where n.name = $name return ID(n)" + ,parameters("name",dataBaseName)); + if (!result0.hasNext()){ + return false; + } + while (result0.hasNext()){ + Record record = result0.next(); + System.out.println(record); + int curID = record.get(0).asInt(); + System.out.println(curID); + session.run( "Start a=node("+ID+"),b=node("+curID+") Merge (a)-[r:collected_by{type:'collected_by',name:$name}]->(b)" + ,parameters("name",resultName)); + } } return true; @@ -504,7 +506,7 @@ private static Boolean DeploymentToNode(String timeStamp,String hostIP, Boolean flag = true; try(Session session = driver.session()) { StatementResult result0 = session.run("Match (n:Deployment_Node) where n.name = $name return ID(n)" - , parameters("name", deploymentName)); + , parameters("name", deploymentName)); if (!result0.hasNext()) { return false; } @@ -523,7 +525,7 @@ private static Boolean DeploymentToNode(String timeStamp,String hostIP, parameters("timeStamp",timeStamp,"hostIP",hostIP,"nameSpace",nameSpace, "name",name,"podIP",podIP, "performance","name:"+name+";timeStamp:"+timeStamp+";hostIP:"+hostIP+";nameSpace:"+nameSpace+ - ";podIP:"+podIP+";type:Node")); + ";podIP:"+podIP+";type:Node")); } int ID = result1.next().get(0).asInt(); session.run("Start a=node(" + curID + "),b=node(" + ID + ") Merge (a)-[r:deploys_at{type:'deploys_at'}]->(b)"); @@ -1124,8 +1126,101 @@ public static HashMap getElement(){ } + //第一次适用需要运行 CREATE INDEX ON :Resource(uri) + public static void importTtl(String typePath, String systemPath) { + Driver driver = GraphDatabase.driver(neo4japi+":7687", + AuthTokens.basic("neo4j","1234")); + //初始化驱动器 + try (Session session = driver.session()) { + try (Transaction tx = session.beginTransaction()) { + String namespace="CREATE (:NamespacePrefixDefinition " + + "{`http://localhost/KGns/#`:''," + + "`http://www.w3.org/1999/02/22-rdf-syntax-ns#`:'rdf'," + + "`http://www.w3.org/2000/01/rdf-schema#`:'rdfs'," + + "`http://xmlns.com/foaf/0.1/`:'foaf'," + + "`http://localhost/KGns/relationship#`:'rel'," + + "`http://localhost/KGns/Container_attributes#`:'Container_attributes'," + + "`http://localhost/KGns/Service_attributes#`:'Service_attributes'})"; + String ontology="CALL semantics.importRDF(\"file://" + typePath + "\", \"Turtle\")"; + String system="CALL semantics.importRDF(\"file://" + systemPath + "\", \"Turtle\")"; + String systemFileName=systemPath.substring(systemPath.lastIndexOf("/")+1); + String systemName=systemFileName.substring(0,systemFileName.lastIndexOf(".")); + String createSystemNode="create(n:System{name:'"+systemName+"',uri:'www.tongji.edu.cn/"+systemName+"'})return n"; + String addRelation= "match(a:System),(b)where(not b:System and not (a)-->(b))create (a)-[r:has]->(b) return r"; + System.out.println(ontology); + System.out.println(system); + + tx.run(namespace); + StatementResult result2 = tx.run(ontology); + tx.run(system); + tx.run(createSystemNode); + tx.run(addRelation); + + System.out.println(result2.toString()); + tx.success(); + } + } + driver.close(); + } + + public static HashMap>> getAllNodesandlinks(String systemName) { + Driver driver = GraphDatabase.driver(neo4japi+":7687", + AuthTokens.basic("neo4j","1234")); + HashMap>> resultgraph = new HashMap<>(); + try(Session session = driver.session()) { + try (Transaction tx = session.beginTransaction()) { + StatementResult result = tx.run("Match p=(n)-[r]-(m) , (a:System)-[e:has]-(n) where a.name = '"+systemName+"' return p as nodesrelation"); + // StatementResult result = tx.run("match (:System{name:'"+systemName+"'})--(n)return n as nodesrelation"); + List> allnodes = new ArrayList<>(); + List> allrelations = new ArrayList<>(); + while(result.hasNext()){ + Record record = result.next(); + Path path = record.get("nodesrelation").asPath(); + Iterable nodes = path.nodes(); + for(Node node:nodes) { + HashMap nod = new HashMap(); + nod.put("id",node.id()); + nod.put("properties", node.asMap()); + String[] name = node.asMap().get("uri").toString().split("#"); + nod.put("name", name[name.length-1]); + Iterator iterator = node.labels().iterator(); + iterator.next(); + if (iterator.hasNext()){ + String type = iterator.next().toString(); + if (type.contains("owl")) continue; + nod.put("type",type.substring(2)); + } else { +// nod.put("type", "null"); + continue; + } + if(!allnodes.contains(nod)) + allnodes.add(nod); + } + Iterable relations = path.relationships(); + for(Relationship relationship:relations) { + HashMap rela = new HashMap(); + rela.put("sid",relationship.startNodeId()); + rela.put("tid",relationship.endNodeId()); + rela.put("type",relationship.type()); + rela.put("name",relationship.type()); + allrelations.add(rela); + } + } + resultgraph.put("nodes",allnodes); + resultgraph.put("links",allrelations); + } + } + driver.close(); + System.out.println(resultgraph); + return resultgraph; + } + public static void main(String[] args) { + new Neo4jDriver().getAllNodesandlinks("0000"); +// importTtl("F:/Xlab/ontology.ttl", +// "F:/Xlab/system.ttl"); + } -} +} \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java old mode 100644 new mode 100755 index c66020412..c5801fc9a --- a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java +++ b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java @@ -210,9 +210,9 @@ public static JSONArray getProInfor(String urlNode, String start, String end) { // query=APIServiceOpenAPIAggregationControllerQueue1_adds{instance="192.168.199.191:6443",job="kubernetes-apiservers"} String url = new String(); try { - url = "http://10.60.38.181:30003/api/v1/query_range?query=" + URLEncoder.encode(urlNode, "UTF-8") + "&start=" + start + "&end=" + end + "&step=60"; + url = "http://10.60.38.181:31003/api/v1/query_range?query=" + URLEncoder.encode(urlNode, "UTF-8") + "&start=" + start + "&end=" + end + "&step=60"; // url = java.net.URLEncoder.encode(url); - System.out.println(url); + //System.out.println(url); }catch (Exception e){ System.out.println(); } diff --git a/BackEnd19v/src/main/java/neo4jentities/DataAccessor.java b/BackEnd19v/src/main/java/neo4jentities/DataAccessor.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/service/CsvService.java b/BackEnd19v/src/main/java/service/CsvService.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/service/MongoService.java b/BackEnd19v/src/main/java/service/MongoService.java old mode 100644 new mode 100755 index 3097f0cbe..f6f7a723f --- a/BackEnd19v/src/main/java/service/MongoService.java +++ b/BackEnd19v/src/main/java/service/MongoService.java @@ -1,8 +1,15 @@ package service; +import com.alibaba.fastjson.*; import neo4j.MongoDriver; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static neo4j.FusekiDriver.*; +import static neo4j.MongoDriver.getAllTypeAndName; public class MongoService { public Boolean storeAllService(String masterName, String podName, String serviceName, String address, String namespace){ @@ -20,4 +27,39 @@ public Boolean storeAllService(String masterName, String podName, String servic result &= podToServer(address, namespace); return result; } + + public static Map jsonarray2Map(){ + Map re = new HashMap(); + ArrayList opts = new ArrayList(); + ArrayList types = new ArrayList(); + JSONArray jsonArray = getAllTypeAndName(); + for (int i = 0; i < jsonArray.size(); i++) { + String typename = jsonArray.getJSONObject(i).getString("type"); + List list = JSONObject.parseArray(jsonArray.getJSONObject(i).getString("name"), String.class); + List children = new ArrayList(); + for (String j:list + ) { + HashMap child = new HashMap<>(); + child.put("value",j); + child.put("label",j); + children.add(child); + } + HashMap op = new HashMap(); + op.put("value",typename); + op.put("label",typename); + op.put("children",children); + opts.add(op); + HashMap type = new HashMap(); + type.put("value",typename); + type.put("label",typename); + types.add(type); + } + re.put("options",opts); + re.put("types",types); + return re; + } + + public static void main(String[] args) { + System.out.println(jsonarray2Map()); + } } diff --git a/BackEnd19v/src/main/java/service/Neo4jService.java b/BackEnd19v/src/main/java/service/Neo4jService.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/util/FileUtil.java b/BackEnd19v/src/main/java/util/FileUtil.java new file mode 100644 index 000000000..574dbfdd7 --- /dev/null +++ b/BackEnd19v/src/main/java/util/FileUtil.java @@ -0,0 +1,33 @@ +package util; + +import web.FileController; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Date; + +public class FileUtil { + + public static void saveClusterResult(String re, String name){ + try { + + String savePath = FileController.class.getResource("/").getPath().replace("classes","clusterResult"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File file = new File(savePath + name + new Date().getTime() + ".json"); + //判断路径是否存在,如果不存在就创建一个 + if(!file.exists()){ + file.createNewFile(); + } + FileOutputStream fos = new FileOutputStream(file); + fos.write(re.getBytes()); + fos.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/BackEnd19v/src/main/java/util/HttpPostUtil.java b/BackEnd19v/src/main/java/util/HttpPostUtil.java old mode 100644 new mode 100755 index 47712129c..3307fdbcb --- a/BackEnd19v/src/main/java/util/HttpPostUtil.java +++ b/BackEnd19v/src/main/java/util/HttpPostUtil.java @@ -1,6 +1,15 @@ package util; import okhttp3.*; +import web.FileController; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Date; public class HttpPostUtil { @@ -11,14 +20,8 @@ public static String postData(String con1) { RequestBody body = RequestBody.create(mediaType, con1); Request request = new Request.Builder() .url("http://10.60.38.173:10081") + //.url("http://192.168.31.205:8080") .post(body) - .addHeader("Content-Type", "application/json,application/json") - .addHeader("Accept", "*/*") - .addHeader("Cache-Control", "no-cache") - .addHeader("Host", "10.60.38.173:10081") - .addHeader("Accept-Encoding", "gzip, deflate") - .addHeader("Connection", "keep-alive") - .addHeader("cache-control", "no-cache") .build(); try { @@ -34,7 +37,65 @@ public static String postData(String con1) { } } - public static void main(String[] args) { + public static String getImage(String urlString) throws Exception { + //new一个URL对象 + URL url = new URL(urlString); + //打开链接 + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + //设置请求方式为"GET" + conn.setRequestMethod("GET"); + //超时响应时间为5秒 + conn.setConnectTimeout(5 * 1000); + //通过输入流获取图片数据 + InputStream inStream = conn.getInputStream(); + //得到图片的二进制数据,以二进制封装得到数据,具有通用性 + byte[] data = readInputStream(inStream); + //new一个文件对象用来保存图片,默认保存当前工程根目录 + String savePath = FileController.class.getResource("/").getPath().replace("classes","hierarchy"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File imageFile = new File(savePath + "hierarchy" + new Date().toString() + ".png"); + //判断路径是否存在,如果不存在就创建一个 + if(!imageFile.exists()){ + imageFile.createNewFile(); + } + //创建输出流 + FileOutputStream outStream = new FileOutputStream(imageFile); + //写入数据 + outStream.write(data); + //关闭输出流 + outStream.close(); + + return imageFile.getName(); + } + + public static byte[] readInputStream(InputStream inStream) throws Exception{ + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + //创建一个Buffer字符串 + byte[] buffer = new byte[1024]; + //每次读取的字符串长度,如果为-1,代表全部读取完毕 + int len = 0; + //使用一个输入流从buffer里把数据读取出来 + while( (len=inStream.read(buffer)) != -1 ){ + //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 + outStream.write(buffer, 0, len); + } + //关闭输入流 + inStream.close(); + //把outStream里的数据写入内存 + return outStream.toByteArray(); + } + + + public static void main(String[] args) { + try { + getImage("https://www.baidu.com/img/bd_logo1.png?where=super"); + }catch (Exception e){ + System.out.println(e); + } } } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/util/JsonUtil.java b/BackEnd19v/src/main/java/util/JsonUtil.java new file mode 100755 index 000000000..58ec44e51 --- /dev/null +++ b/BackEnd19v/src/main/java/util/JsonUtil.java @@ -0,0 +1,164 @@ +package util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.io.FileUtils; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +import org.xml.sax.SAXException; + +import java.io.*; +import java.util.*; + +import static javax.xml.transform.OutputKeys.ENCODING; + +public class JsonUtil { + + Map map=new HashMap(); + + public String jsonToXml(JSONObject jObj){ + try { + StringBuffer buffer = new StringBuffer(); + buffer.append(""); + jsonToXmlstr(jObj,buffer); + return buffer.toString(); + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + + public String jsonToXmlstr(JSONObject jObj,StringBuffer buffer ){ + Set> se = jObj.entrySet(); + for(Iterator> it = se.iterator(); it.hasNext(); ) + { + Map.Entry en = it.next(); + if(en.getValue()instanceof JSONObject){ + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"); + JSONObject jo = jObj.getJSONObject(en.getKey()); + jsonToXmlstr(jo,buffer); + buffer.append(""); + }else if(en.getValue()instanceof JSONArray){ + JSONArray jarray = jObj.getJSONArray(en.getKey()); + for (int i = 0; i < jarray.size(); i++) { + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"); + JSONObject jsonobject = jarray.getJSONObject(i); + jsonToXmlstr(jsonobject,buffer); + buffer.append(""); + } + }else{ + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"+en.getValue()); + buffer.append(""); + } + } + return buffer.toString(); + } + + public void readJson() + { + File file = new File("F:\\Xlab\\test.json"); + try { + String data = FileUtils.readFileToString(file); + JSONArray array=JSON.parseArray(data); + for(int i=0;i myResult =parseJSON2Map(object); + String xmlstr = jsonToXml(object); + System.out.println(xmlstr); + + + } + + + }catch (Exception e){ + e.printStackTrace(); + } + } + + /* + public Map parseJSON2Map(JSONObject json) { + Map map = new HashMap(); + // 最外层解析 + for (Object k : json.keySet()) { + Object v = json.get(k); + // 如果内层还是json数组的话,继续解析 + if (v instanceof JSONArray) { + List> list = new ArrayList>(); + for (int i=0;i<((JSONArray) v).size();i++) { + JSONObject json2 =((JSONArray) v).getJSONObject(i); + list.add(parseJSON2Map(json2)); + } + map.put(k.toString(), list); + } else if (v instanceof JSONObject) { + // 如果内层是json对象的话,继续解析 + map.put(k.toString(), parseJSON2Map((JSONObject) v)); + } else { + // 如果内层是普通对象的话,直接放入map中 + map.put(k.toString(), v); + } + } + return map; + } + */ + + public Element readXmlRoot(String soucePath){ + try { + File file = new File(soucePath); + SAXReader read = new SAXReader(); + org.dom4j.Document doc = read.read(file); + Element root = doc.getRootElement(); + return root; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + public void getNodes(Element node){ + //当前节点的名称、文本内容和属性 + String name=node.getName(); + String context=node.getTextTrim(); + if(context!=null){ + map.put(context,name); + } + //List listAttr=node.attributes();//当前节点的所有属性的list + /* + for(Attribute attr:listAttr){//遍历当前节点的所有属性 + + String name=attr.getName();//属性名称 + String value=attr.getValue();//属性的值 + System.out.println("属性名称:"+name+"属性值:"+value); + } + */ + + //递归遍历当前节点所有的子节点 + List listElement=node.elements();//所有一级子节点的list + for(Element e:listElement){//遍历所有一级子节点 + getNodes(e);//递归 + } + } + + public void readXMLModel(){ + Element root=readXmlRoot("F:\\Xlab\\testModel.XML"); + Map map=new HashMap(); + getNodes(root); + + + + } + + + public static void main(String[] args) { + JsonUtil exm=new JsonUtil(); + exm.readXMLModel(); + exm.readJson(); + } +} + + diff --git a/BackEnd19v/src/main/java/util/TimerUtil.java b/BackEnd19v/src/main/java/util/TimerUtil.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/util/TurtleUtil.java b/BackEnd19v/src/main/java/util/TurtleUtil.java new file mode 100755 index 000000000..62d1dfdfb --- /dev/null +++ b/BackEnd19v/src/main/java/util/TurtleUtil.java @@ -0,0 +1,204 @@ +package util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import org.apache.commons.io.FileUtils; + +import org.dom4j.Document; +import org.dom4j.DocumentHelper; + +import javax.swing.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.security.KeyStore; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TurtleUtil { + public static void readOntologyJson(String fileName) throws IOException { + File fileOntology = new File(TurtleUtil.class.getResource("/").getPath().replace("classes","upload/type")+fileName+".json"); + String ontologyData = FileUtils.readFileToString(fileOntology); + JSONObject ontology=JSON.parseObject(ontologyData); + String savePath = TurtleUtil.class.getResource("/").getPath().replace("classes","turtle/type"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File ontologyTtlFile=new File(savePath + fileName + ".ttl"); + if(!ontologyTtlFile.exists()){ + ontologyTtlFile.createNewFile(); + } + FileWriter ontologyTtlWriter =new FileWriter(ontologyTtlFile); + //命名空间声明 + ontologyTtlWriter.write("@prefix : .\n" + + "@prefix rdf: .\n" + + "@prefix rdfs: .\n" + + "@prefix foaf: .\n" + + "@prefix owl: .\n" + + "@prefix rel: .\n" + + "@prefix Container_attributes: .\n" + + "@prefix Service_attributes: .\n\n"); + ontologyTtlWriter.flush(); + ontologyJsonToTurtle(ontology,ontologyTtlWriter); + + } + + public static void ontologyJsonToTurtle(JSONObject object,FileWriter writer) throws IOException { + JSONArray types = object.getJSONArray("types"); + ListattributeList=new ArrayList<>(); + for (int i = 0; i < types.size(); i++) { + String type = types.getString(i); + writer.write(":" + type + " rdf:type owl:Class .\n"); + writer.flush(); + } + writer.write("\n"); + writer.flush(); + JSONArray relInfo = object.getJSONArray("rel_info"); + for (int i = 0; i < relInfo.size(); i++) { + JSONObject rel = relInfo.getJSONObject(i); + String relType = rel.getString("type"); + String domain=relType+"_attributes"; + JSONArray linkNodes = rel.getJSONArray("link_to_other_nodes"); + for (int j = 0; j < linkNodes.size(); j++) { + JSONObject obj = linkNodes.getJSONObject(j); + String relation = obj.getString("relation"); + String to = obj.getString("to"); + writer.write("rel:" + relation + " rdf:type owl:ObjectProperty;\n\t\t"); + writer.flush(); + writer.write("rdfs:domain :" + relType + ";\n\t\t"); + writer.flush(); + writer.write("rdfs:range :" + to + " .\n\n"); + } + JSONArray attributes = rel.getJSONArray("attributes"); + for (int k = 0; k < attributes.size(); k++) { + String attribute=attributes.getString(k); + attributeList.add(domain+"|"+attribute+"|"+relType); + } + } + for (String s:attributeList){ + String[]input=s.split("\\|"); + writer.write(input[0]+":"+input[1]+" rdf:type owl:ObjectProperty;\n\t\trdfs:domain :"+input[2]+" .\n\n"); + writer.flush(); + } + } + + public static void readSystemJson(String fileName) throws IOException { + File fileSystem = new File(TurtleUtil.class.getResource("/").getPath().replace("classes","upload/system")+fileName+".json"); + String systemData = FileUtils.readFileToString(fileSystem); + JSONObject system=JSON.parseObject(systemData); + String savePath = TurtleUtil.class.getResource("/").getPath().replace("classes","turtle/system"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File systemTtlFile=new File(savePath + fileName + ".ttl"); + if(!systemTtlFile.exists()){ + systemTtlFile.createNewFile(); + } + FileWriter systemTtlWriter =new FileWriter(systemTtlFile); + //命名空间声明 + systemTtlWriter.write("@prefix : .\n" + + "@prefix rdf: .\n" + + "@prefix rdfs: .\n" + + "@prefix foaf: .\n" + + "@prefix rel: .\n" + + "@prefix Container_attributes: .\n" + + "@prefix Service_attributes: .\n" + + "@prefix owl: .\n\n"); + systemTtlWriter.flush(); + systemJsonToTurtle(system,systemTtlWriter); + } + + public static void systemJsonToTurtle(JSONObject object,FileWriter writer) throws IOException { + List instanceTypes = new ArrayList<>(); + HashMap instanceNames = new HashMap<>(); + //写入 处理第一部分 + for (String instanceType : object.keySet()) { + instanceTypes.add(instanceType); + JSONArray instances = object.getJSONArray(instanceType); + for (int i = 0; i < instances.size(); i++) { + JSONObject instance = instances.getJSONObject(i); + //节点名字 + String name = instance.getString("name"); + instanceNames.put(name, instanceType); + } + } + for (int i = 0; i < instanceTypes.size(); i++) { + writer.write(":" + instanceTypes.get(i) + " rdf:type owl:Class .\n"); + writer.flush(); + } + for (String key : instanceNames.keySet()) { + String value = instanceNames.get(key); + writer.write(":" + key + " rdf:type :" + value + " .\n"); + writer.flush(); + } + writer.write("\n"); + writer.flush(); + for (String instanceType : object.keySet()) { + JSONArray instances = object.getJSONArray(instanceType); + for (int i = 0; i < instances.size(); i++) { + JSONObject instance = instances.getJSONObject(i); + //节点名字 + String name = instance.getString("name"); + writer.write(":" + name + "\n\t\t"); + writer.flush(); + //属性 + JSONObject attributes=instance.getJSONObject("attributes"); + for(String key:attributes.keySet()){ + String attribute=attributes.getString(key); + writer.write(instanceType+"_attributes:"+key+" "+attribute+";\n\t\t"); + writer.flush(); + } + JSONObject relations=instance.getJSONObject("relations"); + for (String k : relations.keySet()) { + List keyList=new ArrayList(relations.keySet()); + String lastKey=keyList.get(keyList.size()-1).toString(); + Object v = relations.get(k); + if (v instanceof JSONObject) { + for (String key :((JSONObject)v).keySet()){ + JSONArray array=((JSONObject)v).getJSONArray(key); + for(int j=0;j postType(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name){ + String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/type"); + Map res = new HashMap<>(); + try{ + if (springUpload(request, savePath, name)) { + res.put("succees",1); + readOntologyJson(name); + saveSystemTypeAndNameFile(name,""); + } + }catch (Exception e) { + e.printStackTrace(); + res.put("succees", 0); + res.put("Reason",e.toString()); + } + return res; + } + + + + @RequestMapping(value = "/api/uploadSystemFile",method = RequestMethod.POST,produces = "application/json") + //上传系统的system文件 + public Map postSystem(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("type") String type){ + String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/system"); + Map res = new HashMap<>(); + try{ + if (springUpload(request, savePath, name)) { + res.put("succees",1); + readSystemJson(name); + saveSystemTypeAndNameFile(type,name); + } + }catch (Exception e) { + e.printStackTrace(); + res.put("succees", 0); + res.put("Reason",e.toString()); + } + importTtl(FileController.class.getResource("/").getPath().replace("classes","turtle/type")+type+".ttl", + FileController.class.getResource("/").getPath().replace("classes","turtle/system")+name+".ttl"); + return res; + } + + private boolean springUpload(HttpServletRequest request, String savePath, String fileName) throws IllegalStateException, IOException + { + //将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) + CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver( + request.getSession().getServletContext()); + //检查form中是否有enctype="multipart/form-data" + if(multipartResolver.isMultipart(request)) + { + //将request变成多部分request + MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request; + //获取multiRequest 中所有的文件名 + Iterator iter=multiRequest.getFileNames(); + + while(iter.hasNext()) + { + + //一次遍历所有文件 + MultipartFile file=multiRequest.getFile(iter.next().toString()); + if(file!=null) + { + String oldName = file.getOriginalFilename(); + String path = savePath + fileName + oldName.substring(oldName.lastIndexOf(".")); + System.out.println(path); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File newFile = new File(path); + //判断路径是否存在,如果不存在就创建一个 + if(!newFile.exists()){ + newFile.mkdir(); + } + //上传 + file.transferTo(newFile); + + } + + } + + } + return true; + } + +} diff --git a/BackEnd19v/src/main/java/web/MongoDBController.java b/BackEnd19v/src/main/java/web/MongoDBController.java old mode 100644 new mode 100755 index 1ab673279..25b4db73f --- a/BackEnd19v/src/main/java/web/MongoDBController.java +++ b/BackEnd19v/src/main/java/web/MongoDBController.java @@ -79,4 +79,14 @@ public Boolean addKapacitorEvent(@RequestBody String message){ return saveKapacitor2Mongo(message); } + @RequestMapping(value = "/api/addVariousEvent", method = RequestMethod.POST, produces = "application/json") + public Boolean addEvent(@RequestParam("Source") String source, @RequestParam("Content") String content){ + return saveEvent2Mongo(content, source); + } + + @RequestMapping(value = "/api/getSystemTypeAndNameFile",method = RequestMethod.GET,produces = "application/json") + public Map getAllSystemTypeAndNameFile(){ + return MongoService.jsonarray2Map(); + } + } diff --git a/BackEnd19v/src/main/java/web/Neo4jController.java b/BackEnd19v/src/main/java/web/Neo4jController.java old mode 100644 new mode 100755 index 2ce848bd2..012171f75 --- a/BackEnd19v/src/main/java/web/Neo4jController.java +++ b/BackEnd19v/src/main/java/web/Neo4jController.java @@ -161,6 +161,12 @@ public HashMap getElementName(HttpServletRequest request, Htt return getElement(); } + //获取结点、连接 + @RequestMapping(value = "/api/getSystemNodesAndLinks",method = RequestMethod.GET,produces = "application/json") + public Map getNodeAndLink(@RequestParam("systemName")String systemName){ + return getAllNodesandlinks(systemName); + } + //以下为Fuseki部分 @RequestMapping(value = "/api/getNodesAndLinks",method = RequestMethod.GET,produces = "application/json")