File size: 2,837 Bytes
8dc315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97


import os, sys 
import csv 
import struct 
from collections import defaultdict, Counter


def process(data_path, ofilename):
    ofile  = open(ofilename, "w")
    ofile_core  = open(ofilename+".core", "w")
    writer = csv.writer(ofile)
    writer_core = csv.writer(ofile_core)
    for f in sorted(os.listdir(data_path)):
        if not f.endswith("sort"):
            continue
        print(f)
        ifile = open("{}/{}".format(data_path, f))
        reader = csv.reader(ifile)
        for req in reader:
            if req[4] == "0" or req[4] == "-1" or req[4] == "":
                continue
            # if req[9] == "":
            #     continue
            req_new = [req[0], req[1], req[2], req[4], req[7], req[9], req[10], req[11]]
            req_core = [req[0], req[1], req[2], req[4], req[7], ]
            # print(req)
            # print([len(req), req[9], req[10], req[11], req[12], req[13]])
            # break 
            writer.writerow(req_new)
            writer_core.writerow(req_core)


def convert(ifilename, ofilename):
    ifile = open(ifilename, "r")
    reader = csv.reader(ifile)
    ofile = open(ofilename, "wb")
    s = struct.Struct("<IIBBH")

    country_map, server_map, cat_map = defaultdict(int), defaultdict(int), defaultdict(int)
    country_cnt, server_cnt, cat_cnt = defaultdict(int), defaultdict(int), defaultdict(int)

    for req in reader:
        country_cnt[req[0]] += 1
        server_cnt[req[1]] += 1
        if len(req[2].split("/")) > 2:
            cat_cnt[req[2].split("/")[1]] += 1
        else:
            cat_cnt[1] += 1

        if req[0] in country_map:
            country = country_map.get(req[0])
        else:
            country = len(country_map) + 1
            country_map[req[0]] = country

        if req[1] in server_map:
            server = server_map.get(req[1])
        else:
            server = len(server_map) + 1
            server_map[req[1]] = server

        # no cat 
        cat = 1
        if len(req[2].split("/")) > 2:
            if req[2].split("/")[1] in cat_map:
                cat = cat_map.get(req[2].split("/")[1])
            else:
                cat = len(cat_map) + 2
                cat_map[req[2].split("/")[1]] = cat

        obj_id = hash(req[2]) & 0xffffffff
        size = int(req[3])

        ofile.write(s.pack(obj_id, size, country, server, cat))
    ofile.close()

    for map_data, name in zip((country_map, server_map, cat_map), ("country_map", "server_map", "cat_map")):
        with open(name, "w") as ofile:
            for k, v in map_data.items():
                ofile.write("{}: {}\n".format(k, v))
    print(country_cnt)
    print(server_cnt)
    print(cat_cnt)



if __name__ == "__main__":
    # process("/disk2/twr/cdn/", "/disk2/twr/cdn.csv")
    convert("/disk2/twr/cdn.csv.core", "/disk2/twr/cdn.bin")