def save_city(uid, ip, timestamp, sensitive):
    date = ts2datetime(timestamp)
    ts = datetime2ts(date)
    key = str(uid)
    ip_count_dict = dict()
    sensitive_ip_count_dict = dict()
    ip_count_string = redis_ip.hget('ip_'+str(ts), str(uid))
    if ip_count_string:
        ip_count_dict = json.loads(ip_count_string)
        if ip_count_dict.has_key(str(ip)):
            ip_count_dict[str(ip)] += 1
        else:
            ip_count_dict[str(ip)] = 1
    else:
        ip_count_dict[str(ip)] = 1
    redis_ip.hset('ip_'+str(ts), str(uid), json.dumps(ip_count_dict))

    if sensitive:
        sensitive_ip_count_string = redis_ip.hget('sensitive_ip_'+str(ts), str(uid))
        if sensitive_ip_count_string:
            sensitive_ip_count_dict = json.loads(sensitive_ip_count_string)
            if sensitive_ip_count_dict.has_key(str(ip)):
                sensitive_ip_count_dict[str(ip)] += 1
            else:
                sensitive_ip_count_dict[str(ip)] = 1
        else:
            sensitive_ip_count_dict[str(ip)] = 1
        redis_ip.hset('sensitive_ip_'+str(ts), str(uid), json.dumps(sensitive_ip_count_dict))
def save_city(uid, ip, timestamp, sensitive):
    date = ts2datetime(timestamp)
    ts = datetime2ts(date)
    key = str(uid)
    ip_count_dict = dict()
    sensitive_ip_count_dict = dict()
    ip_count_string = redis_ip.hget('ip_' + str(ts), str(uid))
    if ip_count_string:
        ip_count_dict = json.loads(ip_count_string)
        if ip_count_dict.has_key(str(ip)):
            ip_count_dict[str(ip)] += 1
        else:
            ip_count_dict[str(ip)] = 1
    else:
        ip_count_dict[str(ip)] = 1
    redis_ip.hset('ip_' + str(ts), str(uid), json.dumps(ip_count_dict))

    if sensitive:
        sensitive_ip_count_string = redis_ip.hget('sensitive_ip_' + str(ts),
                                                  str(uid))
        if sensitive_ip_count_string:
            sensitive_ip_count_dict = json.loads(sensitive_ip_count_string)
            if sensitive_ip_count_dict.has_key(str(ip)):
                sensitive_ip_count_dict[str(ip)] += 1
            else:
                sensitive_ip_count_dict[str(ip)] = 1
        else:
            sensitive_ip_count_dict[str(ip)] = 1
        redis_ip.hset('sensitive_ip_' + str(ts), str(uid),
                      json.dumps(sensitive_ip_count_dict))
Beispiel #3
0
def filter_ip(user_set):
    results = []
    #run_type
    if RUN_TYPE == 1:
        now_date = ts2datetime(time.time())
    else:
        now_date = RUN_TEST_TIME
    ts = datetime2ts(now_date) - DAY
    for user in user_set:
        ip_set = set()
        for i in range(0,7):
            timestamp = ts - DAY*i
            ip_result = redis_ip.hget('ip_'+str(ts), str(user))
            if ip_result:
                result_dict = json.loads(ip_result)
            else:
                result_dict = {}
            for ip in result_dict:
                ip_set.add(ip)
        if len(ip_set) < ip_threshold:
            results.append(user)
        else:
            writer.writerow([user, 'ip'])
    return results