Skip to content

Working with Graphs

Sample application of using CDR to locate suspects in crime for police

Download Files Used in the below examples

mock_cdr_call.csv

mock_cdr_cellsite.csv

mock_cdr_people.csv

from c8 import C8Client
import pprint
import time

if __name__ == '__main__':

  # Variables - URLs
  global_url = "gdn1.macrometa.io"

    # Variables - DB
  email = "guest@macrometa.io"
  password = "guest"
  geo_fabric = "_system"
  collection_people = "CDRpeople"
  collection_calls = "CDRcalls"
  collection_cellsites = "CDRcellsites"
  collection_graph = "CDRgraph"
  person = "Dorree Spires"

  # Variables - Queries
  read_people = "FOR person IN CDRpeople RETURN person"

  read_calls = "FOR call IN CDRcalls RETURN call"

  graph_traversal1 = 'FOR c IN CDRpeople FILTER c.full_name == "{}" FOR v IN 1..1 INBOUND c CDRcalls RETURN v'.format(person)

  graph_traversal2 = 'FOR c IN CDRpeople FILTER c.full_name == "{}" FOR v IN 1..1 OUTBOUND c CDRcalls RETURN v'.format(person)

  pp = pprint.PrettyPrinter(indent=4)

  # Step1: Open connection to GDN. You will be routed to closest region.
  print("1. CONNECT: federation: {},  user: {}".format(global_url, email))
  client = C8Client(protocol='https', host=global_url, port=443)
  tenant = client.tenant(email, password)
  fabric = tenant.useFabric(geo_fabric)

  # Step2: Create collections if not exists
  print("2a. CREATE_PEOPLE_VERTEX_COLLECTION: region: {},  collection: {}".format(global_url, collection_people))
  if fabric.has_collection(collection_people):
      peopleCol = fabric.collection(collection_people)
  else:
      peopleCol = fabric.create_collection(collection_people)

  print("2b. CREATE_CALLS_EDGE_COLLECTION: region: {},  collection: {}".format(global_url, collection_calls))
  if fabric.has_collection(collection_calls):
      callsCol = fabric.collection(collection_calls)
  else:
      callsCol = fabric.create_collection(collection_calls, edge=True)

  # Step3: Insert data into collections.
  print("3a. INSERT_PEOPLE_DATA: region: {}, collection: {}".format(global_url, collection_people))

  # insert documents into the collection
  peopleCol.insert({'_key':'Jean', 'firstname': 'Jean', 'lastname':'Picard', 'email':'jean.picard@macrometa.io'})
  peopleCol.insert({'_key':'James', 'firstname': 'James', 'lastname':'Kirk', 'email':'james.kirk@macrometa.io'})
  peopleCol.insert({'_key': 'Han', 'firstname': 'Han', 'lastname':'Solo', 'email':'han.solo@macrometa.io'})
  peopleCol.insert({'_key': 'Bruce', 'firstname': 'Bruce', 'lastname':'Wayne', 'email':'bruce.wayne@macrometa.io'})

  print("3a. INSERT_CALL_RECORDS_DATA: region: {}, collection: {}".format(global_url, collection_calls))
  callsCol.insert({
    "calling_nbr": "757-697-9065",
    "called_nbr": "716-463-8993",
    "_from": "CDRpeople/757-697-9065",
    "_to": "CDRpeople/716-463-8993",
    "call_date": "1/4/2020",
    "call_time": "13:33",
    "call_duration": 30,
    "cell_site": 4044703906
  })
  callsCol.insert({
    "calling_nbr": "718-208-8096",
    "called_nbr": "817-624-0979",
    "_from": "CDRpeople/718-208-8096",
    "_to": "CDRpeople/817-624-0979",
    "call_date": "1/6/2020",
    "call_time": "3:28",
    "call_duration": 2,
    "cell_site": 2706076674
  })
  callsCol.insert({
    "calling_nbr": "765-623-5328",
    "called_nbr": "713-707-8699",
    "_from": "CDRpeople/765-623-5328",
    "_to": "CDRpeople/713-707-8699",
    "call_date": "1/28/2020",
    "call_time": "3:02",
    "call_duration": 18,
    "cell_site": 2289973823
  })

  #Step4: Create a graph
  print("4. CREATE_GRAPH...CDRgraph")
  graph = fabric.create_graph(collection_graph)
  register = graph.create_edge_definition(
        edge_collection=collection_calls,
        from_vertex_collections=[collection_people],
        to_vertex_collections=[collection_people]
    )

  # Step4: Read Data
  print("4a. GRAPH_TRAVERSAL: Find outbound calls TO: {}".format(person))
  cursor = fabric.c8ql.execute(graph_traversal1)
  docs = [document for document in cursor]
  pp.pprint(docs)

  print("4b. GRAPH_TRAVERSAL: Find inbound calls FROM: {}".format(person))
  cursor = fabric.c8ql.execute(graph_traversal2)
  docs = [document for document in cursor]
  pp.pprint(docs)

  # Step5: Delete Data
  print("5. DELETE_DATA...")
  callsCol.truncate()
  peopleCol.truncate()
  fabric.delete_graph(name=collection_graph, drop_collections=True)
'use strict'

const jsc8 = require('jsc8');
const csv = require('csv-parser');
const fs = require('fs');

// Variables - DB
const global_url = "gdn1.macrometa.io";
// Crete a authenticated instance with Token / Apikey
// const client = new jsc8({url: global_url, token: "XXXX", fabricName= '_system'});
// const client = new jsc8({url: global_url, apiKey: "XXXX", fabricName= '_system'});
// await console.log("Authentication done!!...");

// Or use Email & Password to Authenticate client instance
const client = new jsc8(global_url);

await client.login("guest@macrometa.io", "guest");

//Variables
const collection_people = "CDRpeople";
const collection_calls = "CDRcalls";
const collection_cellsites = "CDRcellsites";
const collection_graph = "CDRgraph";
const person = "Dorree Spires";

let datalist = [];

// Variables - Queries
const read_people = "FOR person IN CDRpeople RETURN person";
const read_calls = "FOR call IN CDRcalls RETURN call";
const graph_traversal1 = `FOR c IN CDRpeople FILTER c.full_name == \"${person}\" FOR v IN 1..1 INBOUND c CDRcalls RETURN v`;
const graph_traversal2 = `FOR c IN CDRpeople FILTER c.full_name == \"${person}\" FOR v IN 1..1 OUTBOUND c CDRcalls RETURN v`;

async function processcsv(file){
  datalist = [];
 return new Promise(function(resolve,reject){
  fs.createReadStream(file)
  .pipe(csv())
  .on('data', (row) => {
    datalist.unshift(row);
  })
  .on('end', () => {
    console.log('CSV file successfully processed');
      resolve(datalist);
    });
  })
}

async function createCollection() {
  console.log("\n 2. CREATE_COLLECTION");

  try{
    console.log(`Creating the collection ${collection_people}...`);
    const exists_coll = await client.hasCollection(collection_people);
    if (exists_coll === false) {
      await client.createCollection(collection_people);
    }

    console.log(`Creating the collection ${collection_calls}...`);
    const exists_coll = await client.hasCollection(collection_calls);
    if (exists_coll === false) {
      await client.createCollection(collection_calls);
    }
  }
  catch (e) {
    await console.log("Collection creation did not succeed due to " + e);
  }
}

async function insertData() {
  console.log(`\n 3a. INSERT_PEOPLE_DATA in region ${global_url}`);
  let data = await processcsv('mock_cdr_people.csv');
  await client.insertDocumentMany(collection_people, data);

  console.log(`\n 3a. INSERT_CALL_RECORDS_DATA in region ${global_url}`);
  data = await processcsv('mock_cdr_call.csv');
  await client.insertDocumentMany(collection_calls, data);
}


async function createGraph(){
  console.log(`\n 4. CREATE_GRAPH region ${global_url}`);

  await client.createGraph(collection_graph, {
    edgeDefinitions: [{
      collection: collection_calls,
      from: [collection_people],
      to: [collection_people]
    }]
  });
}

async function readData(){
 console.log(`5a. GRAPH_TRAVERSAL: Find outbound calls TO: ${person}`);
  let result = await client.executeQuery(graph_traversal1);
  console.log(result);

  console.log(`5b. GRAPH_TRAVERSAL: Find inbound calls FROM: ${person}`);
  result = await client.executeQuery(graph_traversal2);
  console.log(result);
}



async function deleteData(){
  console.log("\ 6. DELETE_DATA");
  await client.deleteGraph(collection_graph, true);
  await client.deleteCollection(collection_people);
  await client.deleteCollection(collection_calls);
}

(async function(){
  await createCollection();
  await insertData();
  await createGraph();
  await readData();
  await deleteData();
})();