#!/usr/bin/env python

import json
import sys
import gaffer
from tabulate import tabulate

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

op = gaffer.OperationChain([
    gaffer.GetAllElements(entities=["ip"], 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("dns", exclude=["count", "time"])
                ], entities=None, include="INCOMING")
            ]),
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("indomain", 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)
])

res = g.execute(op)

print len(res)

data = [
    [ent["entities"][0].keys()[0], ent["entities"][1].keys()[0],
     ent["entities"][2].keys()[0][0:30], ent["entities"][3].keys()[0],
     ent["entities"][4].keys()[0]]
    for ent in res
]

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

