#!/usr/bin/env python

import gaffer
from tabulate import tabulate
import json

g = gaffer.Gaffer("https://analytics.trustnetworks.com/gaffer-threat")
g.use_cert()

op = gaffer.OperationChain([
    gaffer.GetAllElements(entities=[
        gaffer.ViewGroup("ip", exclude=["count", "time"])
    ], edges=None),
    gaffer.GetWalks(
        operations=[
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("ipflow", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ]), 
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("matches", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ])
        ],
        limit=1000000)
])

import json

print json.dumps(op.encode(), indent=4)

res = g.execute_chunked(op)

data = []
for v in res:
    try:
        ent = json.loads(v)
        print ent
    except:
        pass
    data.append([
        ent["entities"][0].keys()[0],
        ent["entities"][1].keys()[0],
        ent["entities"][2].keys()[0]
    ])

print tabulate(data, headers=["src", "dest", "block"])

