-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_support_funcs.py
751 lines (615 loc) · 26.1 KB
/
app_support_funcs.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import bisect
import contextlib
import csv
from datetime import datetime
import functools
import json
import logging
import random
import threading
import time
import unittest
import uuid
import redis
def to_bytes(x):
return x.encode() if isinstance(x, str) else x
def to_str(x):
return x.decode() if isinstance(x, bytes) else x
QUIT = False
SAMPLE_COUNT = 100
config_connection = None
# <start id="recent_log"/>
SEVERITY = { # A
logging.DEBUG: 'debug', # A
logging.INFO: 'info', # A
logging.WARNING: 'warning', # A
logging.ERROR: 'error', # A
logging.CRITICAL: 'critical', # A
} # A
SEVERITY.update((name, name) for name in list(SEVERITY.values())) # A
def log_recent(conn, name, message, severity=logging.INFO, pipe=None):
severity = str(SEVERITY.get(severity, severity)).lower() # B
destination = 'recent:%s:%s' % (name, severity) # C
message = time.asctime() + ' ' + message # D
pipe = pipe or conn.pipeline() # E
pipe.lpush(destination, message) # F
pipe.ltrim(destination, 0, 99) # G
pipe.execute() # H
# <end id="recent_log"/>
# A Set up a mapping that should help turn most logging severity levels into something consistent
# B Actually try to turn a logging level into a simple string
# C Create the key that messages will be written to
# D Add the current time so that we know when the message was sent
# E Set up a pipeline so we only need 1 round trip
# F Add the message to the beginning of the log list
# G Trim the log list to only include the most recent 100 messages
# H Execute the two commands
# END
# <start id="common_log"/>
def log_common(conn, name, message, severity=logging.INFO, timeout=5):
severity = str(SEVERITY.get(severity, severity)).lower() # A
destination = 'common:%s:%s' % (name, severity) # B
start_key = destination + ':start' # C
pipe = conn.pipeline()
end = time.time() + timeout
while time.time() < end:
try:
pipe.watch(start_key) # D
now = datetime.utcnow().timetuple() # E
hour_start = datetime(*now[:4]).isoformat() # F
existing = pipe.get(start_key)
pipe.multi() # H
if existing and existing < to_bytes(hour_start): # G
pipe.rename(destination, destination + ':last') # I
pipe.rename(start_key, destination + ':pstart') # I
pipe.set(start_key, hour_start) # J
elif not existing: # J
pipe.set(start_key, hour_start) # J
pipe.zincrby(destination, 1, message) # K
log_recent(pipe, name, message, severity, pipe) # L
return
except redis.exceptions.WatchError:
continue # M
# <end id="common_log"/>
# A Handle the logging level
# B Set up the destination key for keeping recent logs
# C Keep a record of the start of the hour for this set of messages
# D We are going to watch the start of the hour key for changes that only happen at the beginning of the hour
# E Get the current time
# F Find the current start hour
# G If the current list of common logs is for a previous hour
# H Set up the transaction
# I Move the old common log information to the archive
# J Update the start of the current hour for the common logs
# K Actually increment our common counter
# L Call the log_recent() function to record these there, and rely on its call to execute()
# M If we got a watch error from someone else archiving, try again
# END
# <start id="update_counter"/>
PRECISION = [1, 5, 60, 300, 3600, 18000, 86400] # A
def update_counter(conn, name, count=1, now=None):
now = now or time.time() # B
pipe = conn.pipeline() # C
for prec in PRECISION: # D
pnow = int(now / prec) * prec # E
hash = '%s:%s' % (prec, name) # F
pipe.zadd('known:', {hash: 0}) # G
pipe.hincrby('count:' + hash, pnow, count) # H
pipe.execute()
# <end id="update_counter"/>
# A The precision of the counters in seconds: 1 second, 5 seconds, 1 minute, 5 minutes, 1 hour, 5 hours, 1 day - adjust as necessary
# B Get the current time to know when is the proper time to add to
# C Create a transactional pipeline so that later cleanup can work correctly
# D Add entries for all precisions that we record
# E Get the start of the current time slice
# F Create the named hash where this data will be stored
# G Record a reference to the counters into a ZSET with the score 0 so we can clean up after ourselves
# H Update the counter for the given name and time precision
# END
# <start id="get_counter"/>
def get_counter(conn, name, precision):
hash = '%s:%s' % (precision, name) # A
data = conn.hgetall('count:' + hash) # B
to_return = [] # C
for key, value in data.items(): # C
to_return.append((int(key), int(value))) # C
to_return.sort() # D
return to_return
# <end id="get_counter"/>
# A Get the name of the key where we will be storing counter data
# B Fetch the counter data from Redis
# C Convert the counter data into something more expected
# D Sort our data so that older samples are first
# END
# <start id="clean_counters"/>
def clean_counters(conn):
pipe = conn.pipeline(True)
passes = 0 # A
while not QUIT: # C
start = time.time() # D
index = 0 # E
while index < conn.zcard('known:'): # E
hash = conn.zrange('known:', index, index) # F
index += 1
if not hash:
break
hash = hash[0]
prec = int(hash.partition(b':')[0]) # G
bprec = int(prec // 60) or 1 # H
if passes % bprec: # I
continue
hkey = 'count:' + to_str(hash)
cutoff = time.time() - SAMPLE_COUNT * prec # J
samples = list(map(int, conn.hkeys(hkey))) # K
samples.sort() # L
remove = bisect.bisect_right(samples, cutoff) # L
if remove: # M
conn.hdel(hkey, *samples[:remove]) # M
if remove == len(samples): # N
try:
pipe.watch(hkey) # O
if not pipe.hlen(hkey): # P
pipe.multi() # P
pipe.zrem('known:', hash) # P
pipe.execute() # P
index -= 1 # B
else:
pipe.unwatch() # Q
except redis.exceptions.WatchError: # R
pass # R
passes += 1 # S
duration = min(int(time.time() - start) + 1, 60) # S
time.sleep(max(60 - duration, 1)) # T
# <end id="clean_counters"/>
# A Keep a record of the number of passes so that we can balance cleaning out per-second vs. per-day counters
# C Keep cleaning out counters until we are told to stop
# D Get the start time of the pass to calculate the total duration
# E Incrementally iterate over all known counters
# F Get the next counter to check
# G Get the precision of the counter
# H We are going to be taking a pass every 60 seconds or so, so we are going to try to clean out counters at roughly the rate that they are written to
# I Try the next counter if we aren't supposed to check this one on this pass (for example, we have taken 3 passes, but the counter has a precision of 5 minutes)
# J Find the cutoff time for the earliest sample that we should keep, given the precision and number of samples that we want to keep
# K Fetch the times of the samples, and convert the strings to integers
# L Determine the number of samples that should be deleted
# M Remove the samples as necessary
# N We have a reason to potentially remove the counter from the list of known counters ZSET
# O Watch the counter hash for changes
# P Verify that the counter hash is empty, and if so, remove it from the known counters
# B If we deleted a counter, then we can use the same index next pass
# Q The hash is not empty, keep it in the list of known counters
# R Someone else changed the counter hash by adding counters, which means that it has data, so we will leave the counter in the list of known counters
# S Update our passes and duration variables for the next pass, as an attempt to clean out counters as often as they are seeing updates
# T Sleep the remainder of the 60 seconds, or at least 1 second, just to offer a bit of a rest
# END
# <start id="update_stats"/>
def update_stats(conn, context, type, value, timeout=5):
""" Access time on every page """
destination = 'stats:%s:%s' % (context, type) # A
start_key = destination + ':start' # B
pipe = conn.pipeline(True)
end = time.time() + timeout
while time.time() < end:
try:
pipe.watch(start_key) # B
now = datetime.utcnow().timetuple() # B
hour_start = datetime(*now[:4]).isoformat() # B
existing = pipe.get(start_key)
pipe.multi()
if not existing:
pipe.set(start_key, hour_start)
elif existing < hour_start:
pipe.rename(destination, destination + ':last') # B
pipe.rename(start_key, destination + ':pstart') # B
pipe.set(start_key, hour_start) # B
tkey1 = str(uuid.uuid4())
tkey2 = str(uuid.uuid4())
pipe.zadd(tkey1, {'min': value}) # C
pipe.zadd(tkey2, {'max': value}) # C
pipe.zunionstore(destination, # D
[destination, tkey1], aggregate='min') # D
pipe.zunionstore(destination, # D
[destination, tkey2], aggregate='max') # D
pipe.delete(tkey1, tkey2) # E
pipe.zincrby(destination, 1, 'count') # F
pipe.zincrby(destination, value, 'sum') # F
pipe.zincrby(destination, value * value, 'sumsq') # F
return pipe.execute()[-3:] # G
except redis.exceptions.WatchError:
continue # H
# <end id="update_stats"/>
# A Set up the destination statistics key
# B Handle the current hour/last hour like in common_log()
# C Add the value to the temporary keys
# D Union the temporary keys with the destination stats key with the appropriate min/max aggregate
# E Clean up the temporary keys
# F Update the count, sum, and sum of squares members of the zset
# G Return the base counter info so that the caller can do something interesting if necessary
# H If the hour just turned over and the stats have already been shuffled over, try again
# END
# <start id="get_stats"/>
def get_stats(conn, context, type):
key = 'stats:%s:%s' % (context, type) # A
data = dict(conn.zrange(key, 0, -1, withscores=True)) # B
data[b'average'] = data[b'sum'] / data[b'count'] # C
numerator = data[b'sumsq'] - data[b'sum'] ** 2 / data[b'count'] # D
data[b'stddev'] = (numerator / (data[b'count'] - 1 or 1)) ** .5 # E
return data
# <end id="get_stats"/>
# A Set up the key that we are fetching our statistics from
# B Fetch our basic statistics and package them as a dictionary
# C Calculate the average
# D Prepare the first part of the calculation of standard deviation
# E Finish our calculation of standard deviation
# END
# <start id="access_time_context_manager"/>
@contextlib.contextmanager # A
def access_time(conn, context):
start = time.time() # B
yield # C
delta = time.time() - start # D
stats = update_stats(conn, context, 'AccessTime', delta) # E
average = stats[1] / stats[0] # F
pipe = conn.pipeline(True)
pipe.zadd('slowest:AccessTime', {context: average}) # G
pipe.zremrangebyrank('slowest:AccessTime', 0, -101) # H
pipe.execute()
# <end id="access_time_context_manager"/>
# A Make this Python generator into a context manager
# B Record the start time
# C Let the block of code that we are wrapping run
# D Calculate the time that the block took to execute
# E Update the stats for this context
# F Calculate the average
# G Add the average to a ZSET that holds the slowest access times
# H Keep the slowest 100 items in the AccessTime ZSET
# END
# <start id="access_time_use"/>
def process_view(conn, callback): # A
with access_time(conn, request.path): # B
return callback() # C
# <end id="access_time_use"/>
# A This example web view takes the Redis connection as well as a callback to generate the content
# B This is how you would use the access time context manager to wrap a block of code
# C This is executed when the 'yield' statement is hit from within the context manager
# END
# <start id="_1314_14473_9188"/>
def ip_to_score(ip_address):
score = 0
for v in ip_address.split('.'):
score = score * 256 + int(v, 10)
return score
# <end id="_1314_14473_9188"/>
# END
# <start id="_1314_14473_9191"/>
def import_ips_to_redis(conn, filename): # A
csv_file = csv.reader(open(filename, 'rb'))
for count, row in enumerate(csv_file):
start_ip = row[0] if row else '' # B
if 'i' in start_ip.lower():
continue
if '.' in start_ip: # B
start_ip = ip_to_score(start_ip) # B
elif start_ip.isdigit(): # B
start_ip = int(start_ip, 10) # B
else:
continue # C
city_id = row[2] + '_' + str(count) # D
conn.zadd('ip2cityid:', {city_id: start_ip}) # E
# <end id="_1314_14473_9191"/>
# A Should be run with the location of the GeoLiteCity-Blocks.csv file
# B Convert the IP address to a score as necessary
# C Header row or malformed entry
# D Construct the unique city id
# E Add the IP address score and City ID
# END
# <start id="_1314_14473_9194"/>
def import_cities_to_redis(conn, filename): # A
for row in csv.reader(open(filename, 'rb')):
if len(row) < 4 or not row[0].isdigit():
continue
row = [i.decode('latin-1') for i in row]
city_id = row[0] # B
country = row[1] # B
region = row[2] # B
city = row[3] # B
conn.hset('cityid2city:', city_id, # C
json.dumps([city, region, country])) # C
# <end id="_1314_14473_9194"/>
# A Should be run with the location of the GeoLiteCity-Location.csv file
# B Prepare the information for adding to the hash
# C Actually add the city information to Redis
# END
# <start id="_1314_14473_9197"/>
def find_city_by_ip(conn, ip_address):
if isinstance(ip_address, str): # A
ip_address = ip_to_score(ip_address) # A
city_id = conn.zrevrangebyscore( # B
'ip2cityid:', ip_address, 0, start=0, num=1) # B
if not city_id:
return None
city_id = city_id[0].partition('_')[0] # C
return json.loads(conn.hget('cityid2city:', city_id)) # D
# <end id="_1314_14473_9197"/>
# A Convert the IP address to a score for zrevrangebyscore
# B Find the uique city ID
# C Convert the unique city ID to the common city ID
# D Fetch the city information from the hash
# END
# <start id="is_under_maintenance"/>
LAST_CHECKED = None
IS_UNDER_MAINTENANCE = False
def is_under_maintenance(conn):
global LAST_CHECKED, IS_UNDER_MAINTENANCE # A
if (not LAST_CHECKED) or LAST_CHECKED < time.time() - 1: # B
LAST_CHECKED = time.time() # C
IS_UNDER_MAINTENANCE = bool( # D
conn.get('is-under-maintenance')) # D
return IS_UNDER_MAINTENANCE # E
# <end id="is_under_maintenance"/>
# A Set the two variables as globals so we can write to them later
# B Check to see if it has been at least 1 second since we last checked
# C Update the last checked time
# D Find out whether the system is under maintenance
# E Return whether the system is under maintenance
# END
# <start id="set_config"/>
def set_config(conn, type, component, config):
conn.set(
'config:%s:%s' % (type, component),
json.dumps(config))
# <end id="set_config"/>
# END
# <start id="get_config"/>
CONFIGS = {}
CHECKED = {}
def get_config(conn, type, component, wait=1):
key = 'config:%s:%s' % (type, component)
ch = CHECKED.get(key)
if (not ch) or ch < time.time() - wait: # A
CHECKED[key] = time.time() # B
config = json.loads(conn.get(key) or '{}') # C
config = dict((str(k), config[k]) for k in config) # G
old_config = CONFIGS.get(key) # D
if config != old_config: # E
CONFIGS[key] = config # F
return CONFIGS.get(key)
# <end id="get_config"/>
# A Check to see if we should update the configuration information about this component
# B We can, so update the last time we checked this connection
# C Fetch the configuration for this component
# G Convert potentially unicode keyword arguments into string keyword arguments
# D Get the old configuration for this component
# E If the configurations are different
# F Update the configuration
# END
# <start id="redis_connection"/>
REDIS_CONNECTIONS = {}
def redis_connection(component, wait=1): # A
key = 'config:redis:' + component # B
def wrapper(function): # C
@functools.wraps(function) # D
def call(*args, **kwargs): # E
old_config = CONFIGS.get(key, object()) # F
config = get_config( # G
config_connection, 'redis', component, wait) # G
if config != old_config: # H
REDIS_CONNECTIONS[key] = redis.Redis(**config) # H
return function( # I
REDIS_CONNECTIONS.get(key), *args, **kwargs) # I
return call # J
return wrapper # K
# <end id="redis_connection"/>
# A We pass the name of the application component to the decorator
# B We cache the configuration key because we will be fetching it every time the function is called
# C Our wrapper takes a function that it wraps with another function
# D Copy some useful metadata from the original function to the configuration handler
# E Create the actual function that will be managing connection information
# F Fetch the old configuration, if any
# G Get the new configuration, if any
# L Make the configuration usable for creating a Redis connection
# H If the new and old configuration do not match, create a new connection
# I Call and return the result of our wrapped function, remembering to pass the connection and the other matched arguments
# J Return the fully wrapped function
# K Return a function that can wrap our Redis function
# END
'''
# <start id="recent_log_decorator"/>
@redis_connection('logs') #A
def log_recent(conn, app, message): #B
'the old log_recent() code'
log_recent('main', 'User 235 logged in') #C
# <end id="recent_log_decorator"/>
#A The redis_connection() decorator is very easy to use
#B The function definition doesn't change
#C You no longer need to worry about passing the log server connection when calling log_recent()
#END
'''
# --------------- Below this line are helpers to test the code ----------------
class request:
pass
# a faster version with pipelines for actual testing
def import_ips_to_redis(conn, filename):
csv_file = csv.reader(open(filename, 'rb'))
pipe = conn.pipeline(False)
for count, row in enumerate(csv_file):
start_ip = row[0] if row else ''
if 'i' in start_ip.lower():
continue
if '.' in start_ip:
start_ip = ip_to_score(start_ip)
elif start_ip.isdigit():
start_ip = int(start_ip, 10)
else:
continue
city_id = row[2] + '_' + str(count)
pipe.zadd('ip2cityid:', {city_id: start_ip})
if not (count + 1) % 1000:
pipe.execute()
pipe.execute()
def import_cities_to_redis(conn, filename):
pipe = conn.pipeline(False)
for count, row in enumerate(csv.reader(open(filename, 'rb'))):
if len(row) < 4 or not row[0].isdigit():
continue
row = [i.decode('latin-1') for i in row]
city_id = row[0]
country = row[1]
region = row[2]
city = row[3]
pipe.hset('cityid2city:', city_id,
json.dumps([city, region, country]))
if not (count + 1) % 1000:
pipe.execute()
pipe.execute()
class TestCh05(unittest.TestCase):
def setUp(self):
global config_connection
import redis
self.conn = config_connection = redis.Redis(db=15)
self.conn.flushdb()
def tearDown(self):
self.conn.flushdb()
del self.conn
global config_connection, QUIT, SAMPLE_COUNT
config_connection = None
QUIT = False
SAMPLE_COUNT = 100
print()
print()
def test_log_recent(self):
import pprint
conn = self.conn
print("Let's write a few logs to the recent log")
for msg in range(5):
log_recent(conn, 'test', 'this is message %s' % msg)
recent = conn.lrange('recent:test:info', 0, -1)
print("The current recent message log has this many messages:", len(recent))
print("Those messages include:")
pprint.pprint(recent[:10])
self.assertTrue(len(recent) >= 5)
def test_log_common(self):
import pprint
conn = self.conn
print("Let's write some items to the common log")
for count in range(1, 6):
for i in range(count):
log_common(conn, 'test', "message-%s" % count)
common = conn.zrevrange('common:test:info', 0, -1, withscores=True)
print("The current number of common messages is:", len(common))
print("Those common messages are:")
pprint.pprint(common)
self.assertTrue(len(common) >= 5)
def test_counters(self):
import pprint
global QUIT, SAMPLE_COUNT
conn = self.conn
print("Let's update some counters for now and a little in the future")
now = time.time()
for delta in range(10):
update_counter(conn, 'test', count=random.randrange(1, 5), now=now + delta)
counter = get_counter(conn, 'test', 1)
print("We have some per-second counters:", len(counter))
self.assertTrue(len(counter) >= 10)
counter = get_counter(conn, 'test', 5)
print("We have some per-5-second counters:", len(counter))
print("These counters include:")
pprint.pprint(counter[:10])
self.assertTrue(len(counter) >= 2)
print()
tt = time.time
def new_tt():
return tt() + 2 * 86400
time.time = new_tt
print("Let's clean out some counters by setting our sample count to 0")
SAMPLE_COUNT = 0
t = threading.Thread(target=clean_counters, args=(conn,))
t.setDaemon(1) # to make sure it dies if we ctrl+C quit
t.start()
time.sleep(1)
QUIT = True
time.time = tt
counter = get_counter(conn, 'test', 86400)
print("Did we clean out all of the counters?", not counter)
self.assertFalse(counter)
def test_stats(self):
import pprint
conn = self.conn
print("Let's add some data for our statistics!")
for i in range(5):
r = update_stats(conn, 'temp', 'example', random.randrange(5, 15))
print("We have some aggregate statistics:", r)
rr = get_stats(conn, 'temp', 'example')
print("Which we can also fetch manually:")
pprint.pprint(rr)
self.assertTrue(rr[b'count'] >= 5)
def test_access_time(self):
import pprint
conn = self.conn
print("Let's calculate some access times...")
for i in range(10):
with access_time(conn, "req-%s" % i):
time.sleep(.5 + random.random())
print("The slowest access times are:")
atimes = conn.zrevrange('slowest:AccessTime', 0, -1, withscores=True)
pprint.pprint(atimes[:10])
self.assertTrue(len(atimes) >= 10)
print()
def cb():
time.sleep(1 + random.random())
print("Let's use the callback version...")
for i in range(5):
request.path = 'cbreq-%s' % i
process_view(conn, cb)
print("The slowest access times are:")
atimes = conn.zrevrange('slowest:AccessTime', 0, -1, withscores=True)
pprint.pprint(atimes[:10])
self.assertTrue(len(atimes) >= 10)
def test_ip_lookup(self):
conn = self.conn
try:
open('GeoLiteCity-Blocks.csv', 'rb')
open('GeoLiteCity-Location.csv', 'rb')
except:
print("********")
print("You do not have the GeoLiteCity database available, aborting test")
print("Please have the following two files in the current path:")
print("GeoLiteCity-Blocks.csv")
print("GeoLiteCity-Location.csv")
print("********")
return
print("Importing IP addresses to Redis... (this may take a while)")
import_ips_to_redis(conn, 'GeoLiteCity-Blocks.csv')
ranges = conn.zcard('ip2cityid:')
print("Loaded ranges into Redis:", ranges)
self.assertTrue(ranges > 1000)
print()
print("Importing Location lookups to Redis... (this may take a while)")
import_cities_to_redis(conn, 'GeoLiteCity-Location.csv')
cities = conn.hlen('cityid2city:')
print("Loaded city lookups into Redis:", cities)
self.assertTrue(cities > 1000)
print()
print("Let's lookup some locations!")
rr = random.randrange
for i in range(5):
print(find_city_by_ip(conn, '%s.%s.%s.%s' % (rr(1, 255), rr(256), rr(256), rr(256))))
def test_is_under_maintenance(self):
print("Are we under maintenance (we shouldn't be)?", is_under_maintenance(self.conn))
self.conn.set('is-under-maintenance', 'yes')
print("We cached this, so it should be the same:", is_under_maintenance(self.conn))
time.sleep(1)
print("But after a sleep, it should change:", is_under_maintenance(self.conn))
print("Cleaning up...")
self.conn.delete('is-under-maintenance')
time.sleep(1)
print("Should be False again:", is_under_maintenance(self.conn))
def test_config(self):
print("Let's set a config and then get a connection from that config...")
set_config(self.conn, 'redis', 'test', {'db': 15})
@redis_connection('test')
def test(conn2):
return bool(conn2.info())
print("We can run commands from the configured connection:", test())
if __name__ == '__main__':
unittest.main()