def test_cdt_wild_card_list_value(self):
        # This is does gthe value match [*]
        operation = lo.list_get_by_value(
            self.cdt_list_bin, aerospike.CDTWildcard(), aerospike.LIST_RETURN_VALUE)            

        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) == 7
    def test_map_value_wildcard(self):
        operation = map_ops.map_get_by_value(
            self.cdt_map_bin,
            [1, aerospike.CDTWildcard()],
            aerospike.MAP_RETURN_KEY,
        )
        result = get_list_result_from_operation(
            self.as_connection,
            self.cdt_key,
            operation,
            self.cdt_map_bin
        )

        assert set(result) == set(('b', 'c', 'd'))
    def test_cdt_wild_card_list_value_multi_element(self):
        operation = lo.list_get_by_value(
            self.cdt_list_bin, [1, aerospike.CDTWildcard()], aerospike.LIST_RETURN_VALUE)            

        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)
Esempio n. 5
0
    # get_all_by_value_list('l', [1,2], INDEX): [0, 2, 3, 4, 6]
    print("get_all_by_value_list('l', [1,2], REVERSE_INDEX): {}".format(
        bins[2][1]))
    # get_all_by_value_list('l', [1,2], REVERSE_INDEX): [6, 4, 3, 2, 0]
    print("get_all_by_value_list('l', [1,2], RANK): {}".format(bins[3][1]))
    # get_all_by_value_list('l', [1,2], RANK): [0, 1, 2, 3, 4]
    print("get_all_by_value_list('l', [1,2], REVERSE_RANK): {}".format(
        bins[4][1]))
    # get_all_by_value_list('l', [1,2], REVERSE_RANK): [6, 5, 4, 3, 2]
    print("get_all_by_value_list('l', [1,2], COUNT): {}".format(bins[5][1]))
    # get_all_by_value_list('l', [1,2], COUNT): 5

    # switch to more complex values, a list of (mostly) ordered pairs
    tuples = [["v1", 1], ["v2", 1], ["v1", 2], ["v2", 2], ["v1", 3, "z", 8],
              ["v1"]]
    wildcard = aerospike.CDTWildcard()
    ops = [
        operations.write("l", tuples),
        operations.read("l"),
        listops.list_get_by_value_list("l", [["v1"]],
                                       aerospike.LIST_RETURN_INDEX),
        listops.list_get_by_value_list("l", [["v1"], ["v1", wildcard]],
                                       aerospike.LIST_RETURN_VALUE),
        listops.list_get_by_value_list("l", [["v1"], ["v1", wildcard]],
                                       aerospike.LIST_RETURN_VALUE,
                                       inverted=True),
        listops.list_get_by_value_list("l", [["v1", 3, wildcard]],
                                       aerospike.LIST_RETURN_VALUE),
        listops.list_get_by_value_list("l", [[wildcard, 2]],
                                       aerospike.LIST_RETURN_VALUE),
        listops.list_get_by_value_list("l", [["z"], ["y"]],
    # Update multiple segments of a user profile
    segments = {}
    ttl_dt = now + datetime.timedelta(days=30)
    segment_ttl = int((ttl_dt - epoch).total_seconds() / 3600)
    for i in range(8):
        segment_id = random.randint(0, 81999)
        segments[segment_id] = [segment_ttl, {}]
    print("\nUpdating multiple segments for user u1")
    pp.pprint(segments)
    if options.interactive:
        pause()
    ops = [
        mh.map_put_items("u", segments),
        mh.map_get_by_value(
            "u", [segment_ttl, aerospike.CDTWildcard()], aerospike.MAP_RETURN_KEY_VALUE
        ),
    ]
    _, _, b = client.operate(key, ops)
    print("Show all segments with TTL {}:".format(segment_ttl))
    print(b["u"])
    print(spacer)

    # Update a segment's TTL by 5 extra hours
    # First, the context for the list increment (path to the TTL)
    ctx = [ctxh.cdt_ctx_map_key(segment_id)]
    ops = [
        lh.list_increment("u", 0, 5, ctx=ctx),
        mh.map_get_by_key("u", segment_id, aerospike.MAP_RETURN_KEY_VALUE),
    ]
    print("\nAdd 5 hours to the TTL of user u1's segment {}".format(segment_id))
Esempio n. 7
0
except ex.RecordError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    sys.exit(3)

pp = pprint.PrettyPrinter(indent=2)
try:
    version = client.info_all("version")
    release = list(version.values())[0][1].split(" ")[-1]
    # The following works starting Aerospike database 4.3.1 and client 3.5.0
    if version_tuple(aerospike.__version__) >= version_tuple(
            "3.5.0") and version_tuple(release) >= version_tuple("4.3.1"):
        print("\nGet all the 'comment' type events")
        v = client.map_get_by_value(
            key,
            "events",
            ["comment", aerospike.CDTWildcard()],
            aerospike.MAP_RETURN_KEY_VALUE,
        )
        pp.pprint(v)
        print("\nGet the count of all the 'viewed' type events")
        c = client.map_get_by_value(
            key,
            "events",
            ["viewed", aerospike.CDTWildcard()],
            aerospike.MAP_RETURN_COUNT,
        )
        print(c)
        print("\nGet the count of all the 'viewed' and 'comment' type events")
        c = client.map_get_by_value_list(
            key,
            "events",