Skip to content

Commit a8e6df5

Browse files
committed
Cleaned up code in mlab_maxmind_processed.py, fixed locId bug.
1 parent 992b94d commit a8e6df5

1 file changed

Lines changed: 16 additions & 44 deletions

File tree

mlab_maxmind_processed.py

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
# AX Axel Roest
88
#
99
# Version history
10-
# 20120629 AX first version, doesn't work yet
10+
# 20120629 AX first version
11+
# 20120701 AX cleanup of unused methods, Ruben fixed the locID bug
1112
#
1213
# test:
1314
# cd /DATA
@@ -18,6 +19,7 @@
1819
# v move files naar archive directory
1920
# v move error files naar error directory
2021
# v log process and errors
22+
# todo: loop through all maxmind tables and update full mlab set
2123

2224
import sys
2325
import re
@@ -44,12 +46,7 @@
4446

4547
# directories
4648
baseDir = '/DATA/mlab/'
47-
scratchDir = baseDir + 'scratch/'
48-
workDir = baseDir + 'work/'
49-
archiveDir = baseDir + 'archive/'
50-
errorDir = baseDir + 'error/'
5149
logDir = baseDir + 'logs/'
52-
cleanDir = baseDir + 'clean/'
5350

5451
#files
5552
errorLog = "error.log"
@@ -83,35 +80,6 @@ def extract_date(string):
8380
date = datetime(int(date_match.group(1)),int(date_match.group(2)),int(date_match.group(3)))
8481
return date
8582

86-
def exists_dbentry(cur, file_id, db_table, test_datetime, destination, source_ip):
87-
''' Test if the entry already exists in the database '''
88-
# Check if the entry exists already
89-
sql = "SELECT COUNT(*) FROM " + db_table + " WHERE date = '" + test_datetime.isoformat() + "' AND destination = '" + destination + "' AND source = '" + source_ip + "' AND file_id = " + str(file_id)
90-
cur.execute(sql)
91-
92-
if cur.fetchone()[0] < 1:
93-
return False
94-
else:
95-
return True
96-
97-
def blunt_insert_dbentry(cur, file_id, db_table, test_datetime, destination, source_ip):
98-
''' Insert a connection to the database without testing '''
99-
columns = ', '.join(['date', 'destination', 'source', 'file_id'])
100-
values = '"' + '", "'.join([test_datetime.isoformat(), destination, source_ip, str(file_id)]) + '"'
101-
sql = "INSERT INTO " + db_table + " (" + columns + ") VALUES(" + values + ") "
102-
cur.execute(sql)
103-
104-
def insert_dbentry(cur, file_id, db_table, test_datetime, destination, source_ip):
105-
''' Insert a test connection to the database, if it not already exists '''
106-
# Check if the entry exists already
107-
sql = "SELECT COUNT(*) FROM " + db_table + " WHERE date = '" + test_datetime.isoformat() + "' AND destination = '" + destination + "' AND source = '" + source_ip + "' AND file_id = " + str(file_id)
108-
cur.execute(sql)
109-
110-
# If not, then isert it
111-
if cur.fetchone()[0] < 1:
112-
print 'Found new test performed on the', test_datetime, 'from ' + destination + ' -> ' + source_ip + '.'
113-
blunt_insert_dbentry(cur, file_id, db_table, test_datetime, destination, source_ip)
114-
11583
# return True if the table exists in the database
11684
def check_maxmind_exist(cur, table):
11785
sql = "select * FROM maxmind.`" + table + "` LIMIT 1"
@@ -121,22 +89,25 @@ def check_maxmind_exist(cur, table):
12189
else:
12290
return True
12391

124-
def get_maxmind_dates(cur):
125-
datehash = {}
92+
def get_maxmind_tableset(cur):
12693
sql = "SHOW TABLES FROM `maxmind`"
12794
cur.execute(sql)
128-
rows = cur.fetchall()
129-
rows2 = []
95+
allrows = cur.fetchall()
96+
rows = []
13097
# filter rows
131-
for item in rows:
98+
for item in allrows:
13299
m = re.search('Blocks_GeoLiteCity_(\d+)$', item[0])
133100
if (m):
134-
rows2.append(m.group(0))
101+
rows.append(m.group(0))
102+
print rows
103+
return rows
135104

136-
# print rows2
105+
def get_maxmind_dates(cur):
106+
datehash = {}
107+
rows = get_maxmind_tableset(cur)
137108

138109
skipfirst = True
139-
for table in rows2:
110+
for table in rows:
140111
date = extract_datestring(table)
141112
if (skipfirst):
142113
skipfirst = False
@@ -156,7 +127,7 @@ def update_mlab_glasnost(cur,table):
156127
end_datum = maxmind_dates[start_datum]
157128
print 'updating between' + start_datum + ' AND ' + end_datum
158129
try:
159-
sql = 'UPDATE mlab.glasnost SET locID = M.`locId` FROM mlab.glasnost L , maxmind.' + table + ' M WHERE L.`source` BETWEEN M.`startnumip` AND M.`endnumip` AND L.`date` BETWEEN "' + start_datum + '" AND "' + end_datum + '" AND L.`locId` = 0'
130+
sql = 'UPDATE mlab.glasnost SET locId = M.`locId` FROM mlab.glasnost L , maxmind.' + table + ' M WHERE L.`source` BETWEEN M.`startnumip` AND M.`endnumip` AND L.`date` BETWEEN "' + start_datum + '" AND "' + end_datum + '" AND L.`locId` = 0'
160131
print sql
161132
cur.execute(sql)
162133
except MySQLdb.Error, e:
@@ -199,6 +170,7 @@ def update_mlab_glasnost(cur,table):
199170
sys.stderr.write('Error, cannot connect to database' + db_name + '\n')
200171

201172
# contains hash with key = start_date, value = enddate (= startdate of next table, except for the last one)
173+
# maxmind_all_tables = get_maxmind_tableset(cur)
202174
maxmind_dates = get_maxmind_dates(cur)
203175
print maxmind_dates
204176

0 commit comments

Comments
 (0)