def test_get_by_value_range_inverted(self): operation = list_operations.list_get_by_value_range( self.test_bin, aerospike.LIST_RETURN_VALUE, 6, 8, inverted=True) result = get_list_result_from_operation(self.as_connection, self.test_key, operation, self.test_bin) assert len(result) == 4 and set(result) == set([5, 8, 9, 10])
def test_get_by_value_range(self): operation = list_operations.list_get_by_value_range( self.test_bin, aerospike.LIST_RETURN_INDEX, 5, 8) result = get_list_result_from_operation(self.as_connection, self.test_key, operation, self.test_bin) assert len(result) == 3 and set(result) == set([0, 1, 2])
def test_get_by_value_range(self): operation = list_operations.list_get_by_value_range( self.test_bin, aerospike.LIST_RETURN_INDEX, 5, 8 ) result = get_list_result_from_operation( self.as_connection, self.test_key, operation, self.test_bin) assert len(result) == 3 and set(result) == set([0, 1, 2])
def test_get_by_value_range_inverted(self): operation = list_operations.list_get_by_value_range( self.test_bin, aerospike.LIST_RETURN_VALUE, 6, 8, inverted=True ) result = get_list_result_from_operation( self.as_connection, self.test_key, operation, self.test_bin) assert len(result) == 4 and set(result) == set([5, 8, 9, 10])
def test_cdt_infinite_list_range_value(self): operation = lo.list_get_by_value_range( self.cdt_list_bin, aerospike.LIST_RETURN_VALUE, [1, aerospike.null()], [1, aerospike.CDTInfinite()] ) result = get_list_result_from_operation( self.as_connection, self.keys[0], operation, self.cdt_list_bin ) # All items starting with 1 assert len(result) == 3 for lst in result: assert lst[0] == 1
def query_metrics(): req = request.get_json() # Determine the local and UTC timezones localTZ = tz.tzlocal() utcTZ = tz.tzutc() # Extract the range of requested times from the request and adjust to the local timezone lowDate = datetime.strptime(req['range']['from'], "%Y-%m-%dT%H:%M:%S.%fZ") lowDate = lowDate.replace(tzinfo=utcTZ) lowDate = lowDate.astimezone(localTZ) highDate = datetime.strptime(req['range']['to'], "%Y-%m-%dT%H:%M:%S.%fZ") highDate = highDate.replace(tzinfo=utcTZ) highDate = highDate.astimezone(localTZ) timeDiff = highDate - lowDate # Extract the requested ticker symbols from the request targetTickers = [] for aTarget in req['targets']: targetTickers.append(aTarget['target']) # Setup a batch read for the range of dates results = [] # If the requested timeframe is less than a day... if timeDiff.days < 1: for aTicker in targetTickers: key = ('test', 'tickerSet', aTicker + "-" + lowDate.strftime("%Y%m%d")) # Setop a List operation to ONLY retrieve the range of list values within a single record for the requested timestmnp range # This will greatly minimize the data that has to be sent from the server to the client. ops = [ list_ops.list_get_by_value_range( 'datapoints', aerospike.LIST_RETURN_VALUE, [int(lowDate.timestamp()) * 1000, aerospike.CDTWildcard()], [ int(highDate.timestamp() * 1000), aerospike.CDTWildcard() ]) ] _, _, as_results = client.operate(key, ops) res = {"target": aTicker} res["datapoints"] = [] for bin in as_results["datapoints"]: tmp_bin = bin[0] bin[0] = float(bin[1]) bin[1] = tmp_bin res["datapoints"].append(bin) results.append(res) else: for aTicker in targetTickers: as_keys = [] if timeDiff.days < 3: for i in range(0, timeDiff.days + 1): # If the range of days is less than 3... # Then we want to retrieve the detailed ticks (with a second granularity) from Aerospike key = ('test', 'tickerSet', aTicker + "-" + (lowDate + timedelta(days=i)).strftime("%Y%m%d")) as_keys.append(key) else: # For >= 3 days, get a record for each year with that year's closing daily prices print("Low and High year: " + str(lowDate.year) + " " + str(highDate.year)) for aYear in range(lowDate.year, highDate.year + 1): print("Key Value") print(aTicker + "-" + str(aYear)) key = ('test', 'tickerSet', aTicker + "-" + str(aYear)) # Append to the list of keys as_keys.append(key) # This is the Aerospike call that will retrieve the multiple records from the Aerospike DB in parallel as_results = client.get_many(as_keys) # The remainder is all formatting for the return from the HTTP request res = {"target": aTicker} res["datapoints"] = [] for aRes in as_results: if aRes[1] is None: continue dps = aRes[2] for bin in dps["datapoints"]: tmp_bin = bin[0] bin[0] = float(bin[1]) bin[1] = tmp_bin res["datapoints"].append(bin) results.append(res) #Return the results to the caller return jsonify(results)
sys.exit(3) pp = pprint.PrettyPrinter(indent=2) sensor_id = options.sensor_id spacer = "=" * 30 try: key = (namespace, set, "sensor{}-12-31".format(sensor_id)) print("\nRetrieve sensor{} data for 8-11am, December 31st".format(sensor_id)) if options.interactive: pause() starts = 8 * 60 ends = 11 * 60 ops = [ lh.list_get_by_value_range( "t", aerospike.LIST_RETURN_VALUE, [starts, aerospike.null()], [ends, aerospike.null()], ) ] _, _, b = client.operate(key, ops) pp.pprint(b["t"]) print(spacer) print("\nGet sensor{} data for April 2nd".format(sensor_id)) key = (namespace, set, "sensor{}-04-02".format(sensor_id)) if options.interactive: pause() _, _, b = client.get(key) pp.pprint(b["t"]) print(spacer)