-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbread.py
executable file
·201 lines (179 loc) · 6.49 KB
/
dbread.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import json
import os
import pprint
import sys
import traceback
from pymongo import errors
from pymongo.mongo_client import MongoClient
print("Connecting to local mongodb... (if this hangs run systemctl start mongod)")
client = MongoClient("mongodb:https://localhost:27017")
client.admin.command('ping')
current_db = 'Jun9-1090'
last_planes = []
def list_command(thing):
global current_db
global last_planes
if not len(thing):
if current_db is not None:
thing = 'db/'+current_db
else:
print("Set a database with use first!")
return
else:
thing = thing[0]
if thing in ['db', 'dbs', 'database', 'databases']:
dbs = list(client.list_databases())
for db in dbs:
print(db['name']+":", "size="+str(db['sizeOnDisk']))
if thing.startswith('db/'):
cols = list(client.get_database(thing.replace('db/', '')).list_collections())
for col in cols:
print(col['name'])
last_planes = [col['name'] for col in cols]
if thing.startswith('plane/'):
if current_db is None:
print("Set a database with use first!")
return
o = list(client[current_db][thing.replace('plane/', '')].find())
fid = 'no flight id given'
l = 0
for a in o:
l += 1
if len(a['flight_name_id']):
fid = a['flight_name_id'][0][0][:-1]
print("Trip", str(l)+":", a['commentary'])
print("Aircraft "+thing.replace('plane/', ''), "("+fid+')', 'made', l, 'trips.')
print('\nMessages Received')
for ind, trip in enumerate(o):
print("Trip", ind+1)
for item in trip.keys():
if item in ['_id', 'start_time', 'end_time', 'commentary']:
continue
print(item.replace('_history', '')+":", len(list(trip[item])))
def packet_command(args):
pass
def reset_command(args):
global current_db
print("Are you sure you want to reset the database?")
i = input("Type exactly this: \"I understand what will happen, and I consent to resetting the database.\"")
if i == 'I understand what will happen, and I consent to resetting the database.':
current_db = None
else:
print("Does not match.")
return
for table in list(client.list_database_names()):
try:
client.drop_database(table)
except errors.OperationFailure:
print("failed to drop", table)
def search_command(params):
global current_db
searchable = {'alt': 'alt_geom_history', 'dist': 'distance_history', 'alarm': 'alarm_history'}
arg = {'>': lambda x, y: x > y, '<': lambda x, y: x < y, '=': lambda x, y: x == y, '>=': lambda x, y: x >= y}
must_satisfy = []
for param in params:
for src in searchable.keys():
if param.startswith(src):
argument = src
param = param.replace(src, '')
op = ''
for ar in arg.keys():
if ar in param:
if len(ar) > len(op):
op = ar
val = float(param.replace(op, ''))
must_satisfy.append([argument, arg[op], val])
cols = list(client[current_db].list_collection_names())
planes_match = []
for col in cols:
things = list(client[current_db][col].find())[0]
ok = True
mt = {}
for i, must in enumerate(must_satisfy):
t_mt = []
values = things[searchable[must[0]]]
fnd = False
for val in values:
if must[1](val[0], must[2]):
#print(col, val, must[2])
t_mt.append(val[1])
fnd = True
mt.update({i: t_mt})
if not fnd:
ok = False
inter = set(mt[0])
for mo in list(mt.values())[1:]:
inter = inter & set(mo)
inter = list(inter)
if len(inter):
print(col)
planes_match.append(col)
global last_planes
last_planes = planes_match[:]
def dump_command(arg):
if len(arg) == 0:
for item in last_planes:
th = list(client[current_db][item].find())[0]
pprint.pprint(th)
if len(arg) == 1:
if '.' in arg[0]:
if os.path.exists(arg[0]):
os.remove(arg[0])
f = open(arg[0], 'x')
thin = {}
for item in last_planes:
th = list(client[current_db][item].find())[0]
del th['_id']
thin.update({item: th})
json.dump(thin, f, indent=4, sort_keys=True)
print("Dumped", len(last_planes), "planes to", arg[0])
f.close()
else:
pprint.pprint(list(client[current_db][arg[0]].find())[0])
elif len(arg) == 2:
if os.path.exists(arg[1]):
os.remove(arg[1])
f = open(arg[1], 'x')
th = list(client[current_db][arg[0]].find())[0]
del th['_id']
json.dump(th, f, indent=4, sort_keys=True)
f.close()
def drop_command(table):
table = table[0]
if table not in list(client.list_database_names()):
print("ERR: table doesn't exist")
return
chk = input("To drop this table, enter its name: ")
if chk == table:
try:
client.drop_database(table)
except errors.OperationFailure:
print("failed to drop table: operation failure (tried to drop admin?)")
else:
print('abort')
def use_command(db):
global current_db
if len(db) == 0:
print("Using database", current_db)
return
current_db = db[:][0]
command = {'list': {'args': 1, 'help': 'List something (db/database) to list database, or type plane/<db name> to list collections, or col/<collection> to list data in a collection', 'cmd': list_command},
'use': {'args': 1, 'help': '', 'cmd': use_command},
'reset': {'cmd': reset_command},
'drop': {'cmd': drop_command},
'search': {'cmd': search_command},
'dump': {'cmd': dump_command},
'packet': {'cmd': packet_command},
'exit': {'cmd': lambda ags: sys.exit(0)}}
while True:
prompt = input("> ")
if not prompt:
continue
cmds = prompt.split(' & ')
for cmd in cmds:
args = cmd.split(' ')
try:
command[args[0]]['cmd'](args[1:])
except Exception as err:
print('failed')
print(traceback.print_exc())