summaryrefslogtreecommitdiff
path: root/eventStreamer/eventStreamer.py
blob: 326ff89df8d0f01211fb8f92f7b8586c037c0455 (plain)
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
#!/usr/bin/python

import MySQLdb,datetime,string
import config

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-t", "--time", type="string", dest="time", default="",help="start time HH:MM:SS.sss")
parser.add_option("-d", "--date", type="string", dest="date", default="",help="start date YYYY-MM-DD")
(options, args) = parser.parse_args()

def sqldatetime(t):
	return string.replace(t.isoformat(),"T"," ")
	
def main():

	startTime=None

	date=options.date
	time=options.time
	
	now=datetime.datetime.now()
	
	if date=="":
		date=str(now.year)+"-"+str(now.month)+"-"+str(now.day)
	
	if time=="":
		time="0:00:00.0"
		
	startTime=datetime.datetime.strptime(date+" "+time,"%Y-%m-%d %H:%M:%S.%f")

	db = MySQLdb.connect(config.mysql_ip,config.mysql_user,config.mysql_pword,config.mysql_db)
	cursor = db.cursor()
	
	sql = "SELECT * FROM IMSIs"
	devices={}
	
	try:
	   cursor.execute(sql)
	   results = cursor.fetchall()
	   for row in results:
		d={}
		d['IMSI']=row[1]
		d['number']=row[2]
		d['ts']=row[3]
		devices[row[0]]=d
	except:
	   print "Error: unable to fetch data"
	
	#get 1st event
	sql = "SELECT * FROM events Where ts Between '"+sqldatetime(startTime)+"' And '"+sqldatetime(now)+"' LIMIT 1"
	print sql

	try:
	   cursor.execute(sql)
	   results = cursor.fetchall()
	   id=long(results[0][0])
	   device_id=results[0][3]
	   print "id:",id,results[0][1],devices[device_id]['IMSI']
	   sql = "SELECT * FROM events Where id = "+str(id+1)
	   cursor.execute(sql)
	   results = cursor.fetchall()
	   print "next:",results[0][1]
	except:
	   print "Error: unable to fetch data"
	   
	#2 modes of operation depending whether realtime or historical
		   
	db.close()

if __name__ == '__main__': main()