-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.py
executable file
·174 lines (138 loc) · 4.78 KB
/
timeline.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
#!/usr/bin/python
####################################################################################
#
# Provides Time Line (TL) REST services
#
####################################################################################
import os
import sys
import traceback
import logging
import multiprocessing
import bottle
import re
import subprocess
import gzip
import StringIO
import TLData
import TLDiff
try: import simplejson as json
except ImportError: import json
####################################################################################
#
# Globals
#
####################################################################################
app = bottle.Bottle()
logger = None
apihelp = '''
TL REST calls
-----------
1. /put
Put data into time line
2. /get[?time=time]
Get data from time line specified by time parameter. If no time parameter
is given, the data from the latest time is returned
3. /check
Hash of raw data is sent via payload, and for each, we return whether or not
hash values match those of the latest data. This avoids having to send data
that has not changed since the last time point.
4. /diff[?from=time1][&to=time2]
Return the differences between time1 and time2
5. /help
Show this API help
'''
####################################################################################
#
# Configurations
#
####################################################################################
TL_HOST = '0.0.0.0'
TL_PORT = 10252
TL_LOG = 'logs/tl.log'
INDEXER = 'ElasticSearch' #'Solr'
####################################################################################
#
# Functions
#
####################################################################################
# return API help
@app.route("/")
@app.route("/help")
def help():
''' print help page '''
bottle.response.content_type = 'text/plain'
return apihelp
@app.route("/put/<namespace>/<source>", method="POST")
def put(namespace, source):
''' save data '''
bottle.response.content_type = 'text/json'
namespaceInvalid = re.match('^[\w\-\.]+$', namespace) is None
if namespaceInvalid:
logger.error("Unexpected namespace: {0}".format(namespace))
return json.dumps({'success': False, 'stacktrace': traceback.format_exc().split('\n')}, indent=2)
sourceInvalid = re.match('^[\w-]+$', source) is None
if sourceInvalid:
logger.error("Unexpected source: {0}".format(source))
return json.dumps({'success': False, 'stacktrace': traceback.format_exc().split('\n')}, indent=2)
# Check if data is compressed
compressed = False
if bottle.request.query:
value = bottle.request.query.get('compressed', None)
if value:
if value.lower() == 'true':
compressed = True
else:
compressed = False
data = ''
dataSize = bottle.request.content_length
if (dataSize < bottle.request.MEMFILE_MAX):
# req.body in a string if its size is smaller than MEMFILE_MAX
data = bottle.request.body.getvalue()
else:
# req.body in a file if its size is greater than MEMFLE_MAX
data = bottle.request.body.read()
# Find current index for (namespace,origin)
tli = TLData.TLIndex(namespace, source)
baseDatafileName = tli.getBaseDatafileName()
tli.incrementIndex()
# Write out raw data collected by the agent
tlrd = TLData.TLRawData()
p = multiprocessing.Process(target=tlrd.write, args=(baseDatafileName, data, compressed))
p.start()
p.join()
# Flatten out raw data and write it out, which can be used as input to Solr
#tlrdi = TLData.TLRawDataIndex()
#p = multiprocessing.Process(target=tlrdi.write, args=(baseDatafileName, data, compressed))
#p.start()
#p.join()
# Write out diff of raw data
tld = TLDiff.TLDiffData()
p = multiprocessing.Process(target=tld.write, args=(baseDatafileName,))
p.start()
p.join()
# Flatten out raw diff data and write it out, which can be used as input to Solr
if INDEXER == 'Solr':
tldi = TLDiff.TLDiffDataIndexSolr()
p = multiprocessing.Process(target=tldi.write, args=(baseDatafileName,))
p.daemon = True
p.start()
elif INDEXER == 'ElasticSearch':
tldi = TLDiff.TLDiffDataIndexES()
p = multiprocessing.Process(target=tldi.write, args=(baseDatafileName,))
p.daemon = True
p.start()
else:
logger.error('Indexer {0} not supported'.format(INDEXER))
return json.dumps({'success': True})
# Main listen/exec loop
if __name__ == '__main__':
if not os.path.exists('logs'):
os.makedirs('logs')
print 'Starting Time Line process({0})'.format(os.getpid())
print 'Log output will be stored in {0}'.format(TL_LOG)
logging.basicConfig(filename=TL_LOG, filemode='w', format='%(asctime)s %(levelname)s : %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info('Started Time Line process({0})'.format(os.getpid()))
logger.info('Log output will be stored in {0}'.format(TL_LOG))
app.run(host=TL_HOST, port=TL_PORT, quiet=True)