def test_or(self): f1 = RiakKeyFilter("starts_with", "2005-") f2 = RiakKeyFilter("ends_with", "-01") f3 = f1 | f2 self.assertEqual( list(f3), [["or", [["starts_with", "2005-"]], [["ends_with", "-01"]]]])
def test_multi_or(self): f1 = RiakKeyFilter("starts_with", "2005-") f2 = RiakKeyFilter("ends_with", "-01") f3 = RiakKeyFilter("matches", "-11-") f4 = f1 | f2 | f3 self.assertEqual(list(f4), [[ "or", [["starts_with", "2005-"]], [["ends_with", "-01"]], [["matches", "-11-"]], ]])
def query3(client, start, duration=HOURS2): """ Count number of loopdata records with speed over 100 """ # get length of Foster NB station stations_bucket = Bucket(client, 'stations') results = stations_bucket.get(FOSTER_NB_STATIONID) length = results.data['length'] # lookup detector ids for Foster NB station detectorids = lookup_detectors(FOSTER_NB_STATIONID) # build list of key filters for any key starting with detector id # for Foster NB station # starts_with 1361 OR starts_with 1362 OR starts_with 1363 foster = None for detector in detectorids: f = RiakKeyFilter().starts_with(detector) if not foster: foster = f else: foster = foster | f # filter records where volume is zero volume = RiakKeyFilter().tokenize("-", 4) volume += RiakKeyFilter().neq("0") # key: <detector id>-<epoch>-<speed>-<volume> # build key filters for epoch being between start and end times start_epoch, end_epoch = timerange_convert(start, duration) timerange = RiakKeyFilter().tokenize("-", 2) timerange += RiakKeyFilter().string_to_int() timerange += RiakKeyFilter().between(start_epoch, end_epoch) mr = RiakMapReduce(client) mr.add_bucket('loopdata') mr.add_key_filters(volume & timerange & foster) # return speed for each loopdata record matching key filters above mr.map(""" function(record) { var data = Riak.mapValuesJson(record)[0]; return [parseInt(data.speed)]; } """) mr.reduce(""" function(values) { return values; } """) # compute peak travel time from all mapped records response = mr.run(timeout=HOURS24 * 1000) if not response: return None avg_speed = sum(response) / float(len(response)) peak_travel_time = (float(length) / avg_speed) * 3600 return peak_travel_time
def query2(client, start, duration=HOURS24): """ Count number of loopdata records with speed over 100 """ # lookup detector ids for Foster NB station detectorids = lookup_detectors(FOSTER_NB_STATIONID) # build list of key filters for any key starting with detector id # for Foster NB station # starts_with 1361 OR starts_with 1362 OR starts_with 1363 foster = None for detector in detectorids: f = RiakKeyFilter().starts_with(detector) if not foster: foster = f else: foster = foster | f # filter records where volume is zero volume = RiakKeyFilter().tokenize("-", 4) volume += RiakKeyFilter().neq("0") # key: <detector id>-<epoch>-<speed>-<volume> # build key filters for epoch being between start and end times start_epoch, end_epoch = timerange_convert(start, duration) timerange = RiakKeyFilter().tokenize("-", 2) timerange += RiakKeyFilter().string_to_int() timerange += RiakKeyFilter().between(start_epoch, end_epoch) mr = RiakMapReduce(client) mr.add_bucket('loopdata') mr.add_key_filters(volume & timerange & foster) # return 1 for each record with speed greater than 100 # and reduce into total count mr.map(""" function(record) { var data = Riak.mapValuesJson(record)[0]; return [parseInt(data.volume)]; } """) mr.reduce_sum() response = mr.run(timeout=HOURS24 * 1000) if response: return response[0] return None
def test_add(self): f1 = RiakKeyFilter("tokenize", "-", 1) f2 = RiakKeyFilter("eq", "2005") f3 = f1 + f2 self.assertEqual(list(f3), [["tokenize", "-", 1], ["eq", "2005"]])
def test_simple(self): f1 = RiakKeyFilter("tokenize", "-", 1) self.assertEqual(f1._filters, [["tokenize", "-", 1]])