-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
180 lines (152 loc) · 3.41 KB
/
query.py
File metadata and controls
180 lines (152 loc) · 3.41 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
from termcolor import colored
import json
import os
import requests
def clear():
return lambda: os.system("clear")
def query_by_song(song):
return f"""{{
var(func: eq(song, "{song}")) {{
~playedSong {{
v1 as uid
}}
}}
setlist(func: uid(v1)) {{
date
venue: atVenue {{
name: venue
}}
}}
}}"""
def query_by_location(location):
return f"""{{
var(func: anyofterms(location, "{location}")) {{
~isLocatedIn {{
v1 as uid
}}
}}
var(func: uid(v1)) {{
~atVenue {{
v2 as uid
}}
}}
songs(func: uid(v2), orderasc: date) {{
songs: playedSong {{
song
}}
}}
}}"""
def top_n_songs(n):
return f"""{{
var(func: type(song)) {{
v1 as uid
playcount as count(~playedSong)
}}
topSongs(func: uid(v1), orderdesc: val(playcount), first: {n}) {{
song
count: val(playcount)
}}
}}"""
def top_n_encores(n):
return f"""{{
var(func: type(song)) {{
v1 as uid
playcount as count(~playedEncore)
}}
topEncores(func: uid(v1), orderdesc: val(playcount), first: {n}) {{
song
count: val(playcount)
}}
}}"""
def top_n_venues(n):
return f"""{{
var(func: type(venue)) {{
v1 as uid
concerts as count(~atVenue)
}}
topVenues(func: uid(v1), orderdesc: val(concerts), first: {n}) {{
venue
count: val(concerts)
}}
}}"""
def query_by_location_and_date(location, date):
return f"""{{
var(func: allofterms(location, "{location}")) {{
~isLocatedIn {{
v1 as uid
}}
}}
var(func: uid(v1)) {{
~atVenue @filter(ge(date, "{date}")) {{
v2 as uid
}}
}}
songs(func: uid(v2), orderasc: date) {{
songs: playedSong {{
song
}}
}}
}}"""
def query_internation_shows():
return f"""{{
var(func: regexp(location, /.*, X.*/)) {{
~isLocatedIn {{
v1 as uid
}}
}}
var(func: uid(v1)) {{
~atVenue {{
v2 as uid
}}
}}
var(func: uid(v2)) {{
atVenue {{
isLocatedIn {{
v3 as uid
}}
}}
}}
locations(func: uid(v3), orderasc: location) {{
name: location
}}
}}"""
def query_data(q):
headers = {
"Content-Type": "application/graphql+-"
}
print(colored("query --> \n{}".format(q), "white"))
r = requests.post("http://localhost:8080/query?timeout=20s&debug=true&ro=true&be=true", headers=headers, data=q)
if r.status_code == 200:
input()
print(colored("result -->", "white"))
print(colored(f"{json.dumps(r.json()['data'], indent=2, sort_keys=True)}", "green"))
else:
print(colored(f"{r.text}", "red"))
clear()()
print(colored("Which Concerts Included 'Me and My Uncle'?", "cyan"))
query_data(query_by_song("Me and My Uncle"))
input()
clear()()
print(colored("Which Songs Were Performed At the Civic Center?", "cyan"))
query_data(query_by_location("providence"))
input()
clear()()
print(colored("What Were the Top 5 Songs?", "cyan"))
query_data(top_n_songs(5))
input()
clear()()
print(colored("What Were the Top 5 Encores?", "cyan"))
query_data(top_n_encores(5))
input()
clear()()
print(colored("What Were the Top 5 Venues?", "cyan"))
query_data(top_n_venues(5))
input()
clear()()
print(colored("Which Songs Were Performed In Boston in 1994?", "cyan"))
query_data(query_by_location_and_date("boston", "1994"))
input()
clear()()
print(colored("Which International Locations Did They Play At?", "cyan"))
query_data(query_internation_shows())