forked from mihi-tr/glasnost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-json.py
54 lines (45 loc) · 1.96 KB
/
create-json.py
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
import json
from settings import sql,json_dir
cur=sql.cursor()
def dump_json(query,columns,file):
cur.execute(query)
f=open(file,"w")
json.dump([dict(zip(columns,r)) for r in cur.fetchall()],f)
f.close()
" countries.json "
dump_json("""select
cc,sum(total),sum(shaped),sum(shaped)/sum(total)::real*100 from
country_results group by cc;""", ["cc","total","shaped","percent"],
"%s/countries.json"%json_dir)
"countries-test.json"
cur.execute("""select distinct(test) from country_results;""")
tests=[r[0] for r in cur.fetchall()]
f=open("%s/tests.json"%json_dir,"w")
json.dump(tests,f)
f.close()
for test in tests:
dump_json("""select cc,total,shaped,percent_shaped from
country_results where
test='%s';"""%test,["cc","total","shaped","percent"],
"%s/countries-%s.json"%(json_dir,test))
cur.execute("""select distinct(cc) from client_results;""")
ccs=[r[0] for r in cur.fetchall()]
for cc in ccs:
dump_json("""select
owner,sum(total),sum(shaped),sum(shaped)::real/sum(total)::real*100
from provider_results where owner in (select owner from asn where asn
in (select asn from client_results where cc='%s')) group by
owner;"""%cc,["provider","total","shaped","percent"],"%s/country-%s.json"%(json_dir,cc))
for test in tests:
dump_json("""select owner,total,shaped,percent_shaped from
provider_results where owner in (select owner from asn where asn in
(select asn from client_results where cc='%s') and
test='%s');"""%(cc,test),["provider","total","shaped","percent"],
"%s/country-%s-%s.json"%(json_dir,cc,test))
cur.execute("""select distinct(owner) from provider_results;""")
for provider in [r[0] for r in cur.fetchall()]:
if provider:
provider_cleaned=provider.replace("/","-")
dump_json("""select test,total,shaped,percent_shaped from provider_results where
owner='%s';"""%(provider),["test","total","shaped","percent"],
"%s/provider-%s.json"%(json_dir,provider_cleaned))