Skip to content

Commit

Permalink
testes de adição de relacionamentos em cliques
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Oct 21, 2020
1 parent bcb0ae6 commit fcf8ba3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
39 changes: 33 additions & 6 deletions src/main/java/sacip/sti/agents/TrackingAgent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sacip.sti.agents;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -8,29 +10,54 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.midas.as.AgentServer;
import org.midas.as.agent.board.Board;
import org.midas.as.agent.board.Message;
import org.midas.as.agent.board.MessageListener;
import org.midas.as.agent.templates.Agent;
import org.midas.as.agent.templates.LifeCycleException;
import org.midas.as.agent.templates.ServiceException;
import org.midas.as.manager.execution.ServiceWrapper;
import org.midas.as.manager.execution.ServiceWrapperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TrackingAgent extends Agent implements MessageListener{
public class TrackingAgent extends Agent implements MessageListener {

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

@Override
public void provide(String service, Map in, List out) throws ServiceException {

if(service.equals("storeData"))
{

if (service.equals("storeData")) {
JsonNode dados = (JsonNode) in.get("dados");
Iterator<Entry<String, JsonNode>> fields = dados.fields();
if(dados.isObject())
{
if (dados.isObject()) {
ObjectNode jsonobject = (ObjectNode) dados;
Iterator<Entry<String, JsonNode>> nodes = jsonobject.get("cliques").fields();

while (nodes.hasNext()) {
Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodes.next();
List<String> dadosC = new ArrayList<>();
List<String> dadosO = new ArrayList<>();
List<String> dadosA = new ArrayList<>();
List<String> dadosE = new ArrayList<>();

//TODO: arrumar os dados do usuario em listas

Map<String, Object> dadoss = Map.of("Conteudo", dadosC, "OGPor", dadosO, "Ajuda", dadosA, "Exemplos", dadosE);

try {
ServiceWrapper wrapper = require("SACIP", "storeStudentUseData");
wrapper.addParameter("name", "NOMEDOUSUARIO????");
wrapper.addParameter("data", dadoss);
out.add(wrapper.run().get(0));
} catch (ServiceWrapperException e) {
// TODO Auto-generated catch block
LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e);
e.printStackTrace();
}


//logger.info("key --> " + entry.getKey() + " value-->" + entry.getValue());
}
Expand Down
53 changes: 38 additions & 15 deletions src/main/java/sacip/sti/components/DBConnection.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sacip.sti.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import org.midas.as.AgentServer;
import org.midas.as.agent.templates.Component;
Expand Down Expand Up @@ -50,6 +53,9 @@ public void provide(String service, Map in, List out) throws ServiceException {
out.add(deleteUser((String) in.get("name")));
break;

case "storeStudentUseData":
out.add(addClickInformation((String)in.get("name"), (Map)in.get("data")));
break;
case "createContent":
out.add(createContent((Content)in.get("conta")));
break;
Expand Down Expand Up @@ -141,57 +147,58 @@ private Object addClickInformation(String name, Map<String, Object> cliqueReg){
try
{
//Cria query necessaria
StringBuilder query = new StringBuilder("MATCH (n:USER {name: $name }),");
Map updates = Map.of("name", name);
query.append("MERGE");
StringBuilder query = new StringBuilder("MATCH (n:USER {name: $name }) \n");
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("name", name);
query.append(" MERGE");
for (Map.Entry<String, Object> entry : cliqueReg.entrySet()) {
switch(entry.getKey())
{
case "Conteudo":
query.append("(n)-[:CLICKS]-(c:CONTENT{name: $name}),");
query.append("\n(n)-[:CLICKS]-(c:CONTENT{name: $name}),");
break;

case "Exemplos":
query.append("(n)-[:CLICKS]-(e:EXEMPLO{name: $name}),");
query.append("\n(n)-[:CLICKS]-(e:EXEMPLO{name: $name}),");
break;

case "OGPor":
query.append("(n)-[:CLICKS]-(o:OGPOR{name: $name}),");
query.append("\n(n)-[:CLICKS]-(o:OGPOR{name: $name}),");
break;

case "Ajuda":
query.append("(n)-[:CLICKS]-(a:AJUDA{name: $name}),");
query.append("\n(n)-[:CLICKS]-(a:AJUDA{name: $name}),");
break;
}
}
query.deleteCharAt(query.length()-1);
query.append("SET");
query.append("\nSET");
for (Map.Entry<String, Object> entry : cliqueReg.entrySet()) {
switch(entry.getKey())
{
case "Conteudo":
query.append("c.log = c.coalesce(c.log, []) + $clog");
updates.put("clog", cliqueReg.get(entry.getKey()));
query.append("\nc.log = coalesce(c.log, []) + $clog");
updates.put("clog", cliqueReg.get(entry.getKey()));
break;

case "Exemplos":
query.append("e.log = e.coalesce(e.log, []) + $elog");
query.append("\ne.log = coalesce(e.log, []) + $elog");
updates.put("elog", cliqueReg.get(entry.getKey()));
break;

case "OGPor":
query.append("o.log = o.coalesce(o.log, []) + $olog");
query.append("\no.log = coalesce(o.log, []) + $olog");
updates.put("olog", cliqueReg.get(entry.getKey()));
break;

case "Ajuda":
query.append("a.log = a.coalesce(a.log, []) + $alog");
query.append("\na.log = coalesce(a.log, []) + $alog");
updates.put("alog", cliqueReg.get(entry.getKey()));
break;
}
}
query.append("RETURN n");

query.append("\nRETURN n");
System.out.println(query.toString());
//realisa o set
var result = cypher.writequery(query.toString(), updates);
if(result.isEmpty())
Expand Down Expand Up @@ -381,6 +388,14 @@ private void showNodes()
}
}

private void showNodeRelationships(String name)
{
var result = cypher.readquery("MATCH (n:USER {name: $name})--(r) RETURN n,r", Map.of("name", name));
for (Map<String,Object> map : result) {
System.out.println(map.toString());
}
}

private void resetDB()
{
cypher.writequery("MATCH (n) DETACH DELETE n", Map.of());
Expand All @@ -389,7 +404,15 @@ private void resetDB()
public static void main(String[] args) {

DBConnection conect = new DBConnection();
String dado1 = "{'componente':'DEbug', 'timestamp':'1603315704', 'IP':'177.132.153.244'}";
String dado2 = "{'componente':'DESU', 'timestamp':'1603315704', 'IP':'177.132.153.244'}";
List<String> dadosL = new ArrayList<>();
dadosL.add(dado1);
dadosL.add(dado2);
Map<String, Object> dados = Map.of("Conteudo", dadosL);
System.out.println(conect.addClickInformation("Andre", dados));
conect.showNodes();
// conect.showNodeRelationships("Andre");
System.exit(0);
}

Expand Down

0 comments on commit fcf8ba3

Please sign in to comment.