예제 #1
0
def handler_attribute(zmq_name, jsonobj, hasAlreadyBeenContributed=False):
    logger.info('Handling attribute')
    # check if jsonattr is an attribute object
    if 'Attribute' in jsonobj:
        jsonattr = jsonobj['Attribute']

    #Add trending
    categName = jsonattr['category']
    timestamp = jsonattr.get('timestamp', int(time.time()))
    trendings_helper.addTrendingCateg(categName, timestamp)
    tags = []
    for tag in jsonattr.get('Tag', []):
        try:
            tags.append(tag)
        except KeyError:
            pass
    trendings_helper.addTrendingTags(tags, timestamp)

    to_push = []
    for field in json.loads(cfg.get('Dashboard', 'fieldname_order')):
        if type(field) is list:
            to_join = []
            for subField in field:
                to_join.append(str(getFields(jsonobj, subField)))
            to_add = cfg.get('Dashboard', 'char_separator').join(to_join)
        else:
            to_add = getFields(jsonobj, field)
        to_push.append(to_add)

    #try to get coord from ip
    if jsonattr['category'] == "Network activity":
        geo_helper.getCoordFromIpAndPublish(jsonattr['value'],
                                            jsonattr['category'])

    #try to get coord from ip
    if jsonattr['type'] == "phone-number":
        geo_helper.getCoordFromPhoneAndPublish(jsonattr['value'],
                                               jsonattr['category'])

    if not hasAlreadyBeenContributed:
        eventLabeled = len(jsonobj.get('EventTag', [])) > 0
        action = jsonobj.get('action', None)
        contributor_helper.handleContribution(zmq_name,
                                              jsonobj['Event']['Orgc']['name'],
                                              'Attribute',
                                              jsonattr['category'],
                                              action,
                                              isLabeled=eventLabeled)
    # Push to log
    publish_log(zmq_name, 'Attribute', to_push)
예제 #2
0
def test():
    flag_error = False
    today = datetime.datetime.now()

    # IP -> Coord
    supposed_ip = '8.8.8.8'
    geo_helper.getCoordFromIpAndPublish(supposed_ip, categ)
    rep = geo_helper.getTopCoord(today)
    excpected_result = [[
        '{"lat": 37.751, "lon": -97.822, "categ": "Network Activity", "value": "8.8.8.8"}',
        1.0
    ]]
    if rep != excpected_result:
        print('ip to coord result not matching')
        flag_error = True

    # gethitmap
    rep = geo_helper.getHitMap(today)
    excpected_result = [['US', 1.0]]
    if rep != excpected_result:
        print('getHitMap result not matching')
        flag_error = True

    # getCoordsByRadius
    rep = geo_helper.getCoordsByRadius(today, today, 0.000, 0.000, '1')
    excpected_result = []
    if rep != excpected_result:
        print('getCoordsByRadius result not matching')
        flag_error = True

    rep = geo_helper.getCoordsByRadius(today, today, 37.750, -97.821, '10')
    excpected_result = [[['{"categ": "Network Activity", "value": "8.8.8.8"}'],
                         [-97.82200008630753, 37.75100012475438]]]
    if rep != excpected_result:
        print('getCoordsByRadius result not matching')
        flag_error = True

    wipeRedis()

    # Phone -> Coord
    phoneNumber = '(+352) 247-82000'
    geo_helper.getCoordFromPhoneAndPublish(phoneNumber, categ)
    rep = geo_helper.getTopCoord(datetime.datetime.now())[0]
    excpected_result = ['{"lat": "49.7500", "lon": "6.1667"}', 1.0]
    if rep != excpected_result:
        print('Phone to coord result not matching')
        flag_error = True

    return flag_error
예제 #3
0
def handler_attribute(zmq_name,
                      jsonobj,
                      hasAlreadyBeenContributed=False,
                      parentObject=False):
    logger.info('Handling attribute')
    # check if jsonattr is an attribute object
    if 'Attribute' in jsonobj:
        jsonattr = jsonobj['Attribute']
    else:
        jsonattr = jsonobj

    attributeType = 'Attribute' if jsonattr[
        'object_id'] == '0' else 'ObjectAttribute'

    #Add trending
    categName = jsonattr['category']
    timestamp = jsonattr.get('timestamp', int(time.time()))
    trendings_helper.addTrendingCateg(categName, timestamp)
    tags = []
    for tag in jsonattr.get('Tag', []):
        tags.append(tag)
    trendings_helper.addTrendingTags(tags, timestamp)

    #try to get coord from ip
    if jsonattr['category'] == "Network activity":
        geo_helper.getCoordFromIpAndPublish(jsonattr['value'],
                                            jsonattr['category'])

    #try to get coord from ip
    if jsonattr['type'] == "phone-number":
        geo_helper.getCoordFromPhoneAndPublish(jsonattr['value'],
                                               jsonattr['category'])

    if not hasAlreadyBeenContributed:
        eventLabeled = len(jsonobj.get('EventTag', [])) > 0
        action = jsonobj.get('action', None)
        contributor_helper.handleContribution(zmq_name,
                                              jsonobj['Event']['Orgc']['name'],
                                              attributeType,
                                              jsonattr['category'],
                                              action,
                                              isLabeled=eventLabeled)
    # Push to log
    live_helper.publish_log(zmq_name, attributeType, jsonobj)