-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.py
74 lines (69 loc) · 2.91 KB
/
metadata.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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import json
import os
import pymongo
import requests
import string
import sys
import urllib
client = pymongo.MongoClient()
mongo_collection = client.qdf.metadata
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('GET request received')
def do_POST(self):
self.query = self.rfile.read(int(self.headers['Content-Length']))
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET POST')
self.send_header('Content-type', 'text/html')
self.end_headers()
if self.query == 'select distinct Metadata/SourceName':
sources = set()
for stream in mongo_collection.find():
sources.add(stream['Metadata']['SourceName'])
self.wfile.write(json.dumps(list(sources)))
elif self.query.startswith('select distinct Path where Metadata/SourceName'):
source = self.query.split('"')[1]
paths = set()
for stream in mongo_collection.find({"$where": 'this.Metadata.SourceName === "{0}"'.format(source)}):
paths.add(stream['Path'])
self.wfile.write(json.dumps(list(paths)))
elif self.query.startswith('select * where Metadata/SourceName'):
parts = self.query.split('"')
source = parts[1]
path = parts[3]
streams = set()
for stream in mongo_collection.find({"$where": 'this.Metadata.SourceName === "{0}" && this.Path === "{1}"'.format(source, path)}):
del stream['_id']
streams.add(json.dumps(stream))
returnstr = '['
for stream in streams:
returnstr += stream + ", "
self.wfile.write(returnstr[:-2] + ']')
elif self.query.startswith('select * where uuid ='): # I assume that it's a sequence of ORs
parts = self.query.split('"')
uuids = []
i = 0
while i < len(parts):
if i % 2 != 0:
uuids.append('this.uuid === "{0}"'.format(parts[i]))
i += 1
streams = set()
for stream in mongo_collection.find({"$where": ' || '.join(uuids)}):
del stream['_id']
streams.add(json.dumps(stream))
returnstr = '['
for stream in streams:
returnstr += stream + ", "
self.wfile.write(returnstr[:-2] + ']')
else:
self.wfile.write('[]')
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
serv = ThreadedHTTPServer(('', 4523), HTTPRequestHandler)
serv.serve_forever()