def process_message(self, message, callback): received_time = time.time() decompressed = zlib.decompress(message) size = struct.unpack('=I', decompressed[:4]) frmt = "%ds" % size[0] unpacked = struct.unpack('=I' + frmt, decompressed) msg = json.loads(unpacked[1]) acknowledge = struct.pack('=b', 0) self.ioloop.add_callback( self.send_to_client, common.get_client(msg["from"]), acknowledge) latency = Float32() latency.data = received_time - msg["stamp"] MMServerProtocol.lat_pubs[msg["from"]].publish(latency) if msg["to"][0] == "*": for name in common.clients.keys(): if name != msg["from"]: self.ioloop.add_callback( self.send_to_client, common.get_client(name), message) else: for name in msg["to"]: self.ioloop.add_callback( self.send_to_client, common.get_client(name), message) return callback(message)
def dotransform(request, response, config): """ The dotransform function is our transform's entry point. The request object has the following properties: - value: a string containing the value of the input entity. - fields: a dictionary of entity field names and their respective values of the input entity. - params: any additional command-line arguments to be passed to the transform. - entity: the information above is serialized into an Entity object. The entity type is determined by the inputs field in @configure for local transforms. For remote transforms, the entity type is determined by the information in the body of the request. Local transforms suffer from one limitation: if more than one entity type is listed in the inputs field of @configure, the entity type might not be resolvable. Therefore, this should not be referenced in local transforms if there is more than one input entity type defined in @configure. The response object is a container for output entities, UI messages, and exception messages. The config object contains a key-value store of the configuration file. TODO: write your data mining logic below. """ client = get_client(config) prog = 10 progress(prog) debug('Starting RiskIQ blacklist lookup...') url = request.entities[0].value inc = client.get_blacklist_lookup(url) prog += 10 progress(prog) if not inc: progress(100) return response for ent in incident_children(inc): response += ent progress(100) return response
def test_db_blockhash(myserver, verbose=False): client = get_client(verbose=verbose) if verbose: print("Sending regtest_generate") res = client.command(command="regtest_generate", options=[2]) # Mine two blocks if verbose: print(f"Got res {res}") sleep(1) data = client.command(command="blocklastjson") if verbose: print(f"blocklastjson returns {data}") since = data['block_height'] - 2 r = client.command(command="api_getblocksince", options=[since]) if verbose: print(f"api_getblocksince returns {r}") db_block_hash_prev = r[0][7] db_block_hash = r[1][7] amount = '0.00000000' timestamp = f"{float(r[1][1]):.2f}" # prefer to python2 style % tx_list = [] tx_list.append((timestamp, r[1][2], r[1][3], amount, r[1][5], r[1][6], r[1][10], r[1][11])) block_hash = sha224( (str(tx_list) + db_block_hash_prev).encode("utf-8")).hexdigest() assert db_block_hash == block_hash assert type(db_block_hash) == str
def test_sender_and_recipient_balances(myserver, verbose=False): recipient = "8342c1610de5d7aa026ca7ae6d21bd99b1b3a4654701751891f08742" client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.clear_cache() balance_sender_before = float(client.balance()) balance = client.command(command="balanceget", options=[recipient]) if verbose: print(f"balanceget (before) returns {balance}") balance_recipient_before = float(balance[0]) client.send(recipient=recipient, amount=1.0) sleep(1) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.clear_cache() tx = client.latest_transactions(num=2) balance_sender_after = float(client.balance()) balance = client.command(command="balanceget", options=[recipient]) if verbose: print(f"balanceget (after) returns {balance}") balance_recipient_after = float(balance[0]) diff1 = balance_sender_after - balance_sender_before - float( tx[1]["reward"]) + float(tx[0]["fee"]) diff2 = balance_recipient_after - balance_recipient_before assert abs(diff1 + 1.0) < 1e-6 assert diff2 == 1.0
def test_api_getblockfromhash(myserver, verbose=False): client = get_client(verbose=verbose) if verbose: print("Sending regtest_generate") res = client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds if verbose: print(f"Got res {res}") client.send(recipient=client.address, amount=1.0, operation='12345', data='67890') client.command(command="regtest_generate", options=[1]) # Mine the next block if verbose: print("Sending regtest_generate") sleep(1) data1 = client.command(command="addlistlimjson", options=[client.address, 1]) if verbose: print(f"addlistlimjson returns {data1}") block_hash = data1[0]['block_hash'] data2 = client.command(command="api_getblockfromhash", options=[block_hash]) if verbose: print(f"api_getblockfromhash returns {data2}") block_height = str(data1[0]['block_height']) n = len(data2[block_height]['transactions']) # note: better split the asserts, one by line, so when it errors we have the why and value assert type(block_hash) == str assert data2[block_height]['block_hash'] == block_hash assert n == 2
def delete_alarms_for_campaign(campaign_arn): cw = get_client(service_name='cloudwatch', region_name=extract_region(campaign_arn)) alarm_names_to_delete = set() alarms_paginator = cw.get_paginator('describe_alarms') for alarms_page in alarms_paginator.paginate( AlarmNamePrefix=ALARM_NAME_PREFIX, AlarmTypes=['MetricAlarm']): for alarm in alarms_page['MetricAlarms']: for dim in alarm['Dimensions']: if dim['Name'] == 'CampaignArn' and dim[ 'Value'] == campaign_arn: tags_response = cw.list_tags_for_resource( ResourceARN=alarm['AlarmArn']) for tag in tags_response['Tags']: if tag['Key'] == 'CreatedBy' and tag[ 'Value'] == PROJECT_NAME: alarm_names_to_delete.add(alarm['AlarmName']) break if alarm_names_to_delete: # FUTURE: max check of 100 logger.info('Deleting CloudWatch alarms for campaign %s: %s', campaign_arn, alarm_names_to_delete) cw.delete_alarms(AlarmNames=list(alarm_names_to_delete)) alarms_deleted += len(alarm_names_to_delete) else: logger.info('No CloudWatch alarms to delete for campaign %s', campaign_arn)
def test_api_config(myserver, verbose=False): client = get_client(verbose=verbose) data = client.command(command="api_getconfig") if verbose: print(f"api_getconfig returns {data}") assert data['regnet'] is True # Do not enforce type. Old node sent string, this is an int. V2 returns an int (config is typed). # Voluntary break formal compatibility in favor of correct typing. assert int(data['port']) == 3030
def main(): client = common.get_client() print(len(client.list_boards())) print(client.list_boards()) board = common.get_board_by_name(client, 'home') lst = common.get_list_by_name(board, 'task') print(lst.list_cards())
def test_ecdsa_dict(myserver, verbose=False): client = get_client(verbose=verbose) res = client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds ecdsa_priv_key = '82b6d896cbb1ac4d5af96cd43d4f391b1e6d73ce9b3ce29dd378730b22a952d2' signer = bismuthcrypto.ecdsa_pk_to_signer(ecdsa_priv_key) if verbose: print(f"signer contains {signer}") assert signer['type'] == 'ECDSA' assert 'test' in signer['address'] assert signer['public_key'] == '035d3c145e518739f1e9b014ff00a5de2c8cd3478672f236ea13a07310d8a1a33a'
def fill_torrent_info(self): items = self.table_torrent.selectedItems() if not items: return torrent = items[0].data(Qt.UserRole) qb = get_client() torrent_details = qb.get_torrent(torrent['hash']) self.torrent_info_widget.fill(torrent_details)
def test_pubkey_address(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block sleep(1) data = client.command(command="blocklastjson") if verbose: print(f"blocklastjson returns {data}") pubkey = b64decode(data['public_key']).decode('utf-8') if verbose: print("Pubkey", pubkey, type(pubkey)) address = hashlib.sha224(pubkey.encode("utf-8")).hexdigest() assert address == client.address
def test_keygen_json(myserver, verbose=False): client = get_client(verbose=verbose) data1 = client.command(command="keygen") if verbose: print(f"keygen returns {data1}") data2 = client.command(command="keygenjson") if verbose: print(f"keygenjson returns {data2}") assert len(data1[1]) > 0 assert len(data1[2]) > 0 assert len(data1[1]) == len(data2['public_key']) assert len(data1[2]) == len(data2['address'])
def get_campaign_recipe_arn(campaign): recipe_arn = campaign.get('recipeArn') if not recipe_arn: campaign_region = extract_region(campaign['campaignArn']) personalize = get_client('personalize', campaign_region) response = personalize.describe_solution_version(solutionVersionArn = campaign['solutionVersionArn']) recipe_arn = response['solutionVersion']['recipeArn'] campaign['recipeArn'] = recipe_arn return recipe_arn
def test_blocklast_json(myserver, verbose=False): client = get_client(verbose=verbose) data1 = client.command(command="blocklast") if verbose: print(f"blocklast returns {data1}") data2 = client.command(command="blocklastjson") if verbose: print(f"blocklastjson returns {data2}") # note: better split the asserts, one by line, so when it errors we have the why and value assert int(data1[0]) == data2['block_height'] assert type(data2['block_height']) == int assert data1[7] == data2['block_hash'] assert type(data2['block_hash']) == str
def test_fee(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds data = '12345678901234567890123456789012345678901234567890' client.send(recipient=client.address, amount=0, data=data) client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) tx = client.latest_transactions(num=1) if verbose: print(f"latest_transactions returns {tx}") assert float(tx[0]["fee"]) == 0.01 + 1e-5 * len(data)
def main(): parser = argparse.ArgumentParser() parser.add_argument("-f", "--force", action="store_true") args = parser.parse_args() now = datetime.datetime.now() client = common.get_client() config = common.get_config()["add-periodical-task"] for task in config: add_periodical_task(client, task, now, args.force)
def test_tx_id(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds txid = client.send(recipient=client.address, amount=1.0) # Tries to send 1.0 to self client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) tx = client.latest_transactions(num=1) if verbose: print(f"latest_transactions returns {tx}") assert tx[0]["signature"][:56] == txid
def test_amount_and_recipient(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.send(recipient=client.address, amount=1.0) # Tries to send 1.0 to self client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) tx = client.latest_transactions(num=1) if verbose: print(f"latest_transactions returns {tx}") assert float(tx[0]["amount"]) == 1.0 assert tx[0]["recipient"] == client.address
def test_balance_json(myserver, verbose=False): client = get_client(verbose=verbose) data1 = client.command(command="balanceget", options=[client.address]) if verbose: print(f"balanceget returns {data1}") data2 = client.command(command="balancegetjson", options=[client.address]) if verbose: print(f"balancegetjson returns {data2}") assert data1[0] == data2['balance'] assert data1[1] == data2['credit'] assert data1[2] == data2['debit'] assert data1[3] == data2['fees'] assert data1[4] == data2['rewards'] assert data1[5] == data2['balance_no_mempool']
def test_diff_json(myserver, verbose=False): client = get_client(verbose=verbose) data1 = client.command(command="difflast") if verbose: print(f"difflast returns {data1}") data2 = client.command(command="difflastjson") if verbose: print(f"difflastjson returns {data2}") block1 = data1[0] diff1 = data1[1] block2 = data2['block'] diff2 = data2['difficulty'] assert block1 == block2 assert diff1 == diff2
def get_penguins(cpps=None, server=None, shape=None): servers = common.get_json("servers") shapes = common.get_json("shapes") try: cpps = common.get_cpps(servers, cpps) server = common.get_server(servers, cpps, server) shape = get_shape(shapes, shape) except (KeyboardInterrupt, EOFError): raise common.LoginError() logger = logging.getLogger() logger.addHandler(logging.NullHandler()) clients_offsets = [(common.get_client(servers, cpps, server, logger), int(offset["x"]), int(offset["y"])) for offset in shape["offsets"]] return cpps, server, shape, clients_offsets
def test_send_more_than_owned(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.clear_cache() balance = float(client.balance()) recipient = "8342c1610de5d7aa026ca7ae6d21bd99b1b3a4654701751891f08742" client.send(recipient=recipient, amount=balance) client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) client.clear_cache() balance = float(client.balance()) if verbose: print(f"balance returns {balance}") assert balance > 1.0
def test_operation_length(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds operation = '123456789012345678901234567890' client.send(recipient=client.address, amount=0, operation=operation) operation = '1234567890123456789012345678901' client.send(recipient=client.address, amount=0, operation=operation) client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) tx = client.latest_transactions(num=2) if verbose: print(f"latest_transactions returns {tx}") assert len(tx[0]["operation"]) == 30 assert len(tx[1]["operation"]) == 1
def test_operation_and_openfield(myserver, verbose=False): operation = "test:1" data = "Bismuth" client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.send(recipient=client.address, amount=0.0, operation=operation, data=data) client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) tx = client.latest_transactions(num=1) if verbose: print(f"latest_transactions returns {tx}") assert tx[0]["operation"] == operation assert tx[0]["openfield"] == data
def get_campaign_sum_requests_datapoints(campaign, start_time, end_time, period): campaign_region = extract_region(campaign['campaignArn']) cw = get_client(service_name = 'cloudwatch', region_name = campaign_region) metric_name = get_campaign_inference_metric_name(campaign) response = cw.get_metric_data( MetricDataQueries = [ { 'Id': 'm1', 'MetricStat': { 'Metric': { 'Namespace': 'AWS/Personalize', 'MetricName': metric_name, 'Dimensions': [ { 'Name': 'CampaignArn', 'Value': campaign['campaignArn'] } ] }, 'Period': period, 'Stat': 'Sum' }, 'ReturnData': True } ], StartTime = start_time, EndTime = end_time, ScanBy = 'TimestampDescending' ) datapoints = [] if response.get('MetricDataResults') and len(response['MetricDataResults']) > 0: results = response['MetricDataResults'][0] for idx, ts in enumerate(results['Timestamps']): datapoints.append({ 'Timestamp': ts, 'Value': results['Values'][idx] }) return datapoints
def delete_resource(event, _): campaign_arns = determine_campaign_arns(event.get('ResourceProperties')) logger.debug('Campaigns to check for resources to delete: %s', campaign_arns) regions = set() for campaign_arn in campaign_arns: regions.add(extract_region(campaign_arn)) logger.debug('Regions to check for resources to delete: %s', regions) alarms_deleted = 0 for region in regions: cw = get_client(service_name='cloudwatch', region_name=region) alarm_names_to_delete = set() alarms_paginator = cw.get_paginator('describe_alarms') for alarms_page in alarms_paginator.paginate( AlarmNamePrefix=ALARM_NAME_PREFIX, AlarmTypes=['MetricAlarm']): for alarm in alarms_page['MetricAlarms']: tags_response = cw.list_tags_for_resource( ResourceARN=alarm['AlarmArn']) for tag in tags_response['Tags']: if tag['Key'] == 'CreatedBy' and tag[ 'Value'] == PROJECT_NAME: alarm_names_to_delete.add(alarm['AlarmName']) break if alarm_names_to_delete: # FUTURE: max check of 100 logger.info( 'Deleting CloudWatch alarms in %s for campaigns %s: %s', region, campaign_arns, alarm_names_to_delete) cw.delete_alarms(AlarmNames=list(alarm_names_to_delete)) alarms_deleted += len(alarm_names_to_delete) logger.info('Deleted %d alarms', alarms_deleted)
def lambda_handler(event, context): ''' Initiates the delete of a Personalize campaign ''' if event.get('detail'): campaign_arn = event['detail']['CampaignARN'] reason = event['detail']['Reason'] else: campaign_arn = event['CampaignARN'] reason = event.get('Reason') region = extract_region(campaign_arn) if not region: raise Exception('Region could not be extracted from campaign_arn') personalize = get_client(service_name='personalize', region_name=region) response = personalize.delete_campaign(campaignArn=campaign_arn) if logger.isEnabledFor(logging.DEBUG): logger.debug(json.dumps(response, indent=2, default=str)) if not reason: reason = f'Amazon Personalize campaign {campaign_arn} deletion initiated (reason unspecified)' put_event(detail_type='PersonalizeCampaignDeleted', detail=json.dumps({ 'CampaignARN': campaign_arn, 'Reason': reason }), resources=[campaign_arn]) put_event(detail_type='BuildPersonalizeMonitorDashboard', detail=json.dumps({ 'CampaignARN': campaign_arn, 'Reason': reason }), resources=[campaign_arn]) logger.info({'campaignArn': campaign_arn}) delete_alarms_for_campaign(campaign_arn) return f'Successfully initiated delete of campaign {campaign_arn}'
def test_api_getaddresssince(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so that we have some funds client.send(recipient=client.address, amount=1.0) # Tries to send 1.0 to self client.command(command="regtest_generate", options=[10]) # Mine 10 more blocks sleep(1) data2 = client.command(command="blocklastjson") if verbose: print(f"blocklastjson returns {data2}") since = data2['block_height'] - 10 conf = 8 data = client.command(command="api_getaddresssince", options=[since, conf, client.address]) if verbose: print(f"api_getaddresssince returns {data}") n = len(data['transactions']) assert n == 3
def test_spend_entire_balance(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.clear_cache() balance = float(client.balance()) fee = 0.01 recipient = "8342c1610de5d7aa026ca7ae6d21bd99b1b3a4654701751891f08742" client.send(recipient=recipient, amount=balance - fee) client.command(command="regtest_generate", options=[1]) # Mine the next block sleep(1) client.clear_cache() balance = float(client.balance()) if verbose: print(f"balance returns {balance}") tx = client.latest_transactions(num=2) if verbose: print(f"tx returns {tx}") assert abs(balance - float(tx[1]["reward"])) < 1e-6
def test_mpget_json(myserver, verbose=False): client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.send(recipient=client.address, amount=1.0) # Tries to send 1.0 to self data1 = client.command(command="mpget") data2 = client.command(command="mpgetjson") client.command(command="regtest_generate", options=[1]) # Mine next block sleep(1) # print("data1", data1[0][5]) pubkey = b64decode(data1[0][5]).decode('utf-8').replace("\n", "") # print("pubkey", pubkey) # TODO: double check that output with stable release regnet # (precise pubkey format from mpgetjson) """ Note: this comment seem tnot to be true anymore with the current stable reference. Double check. mpgetjson seemed to send pubkey without boundaries, while other json answers gave the full string. Where is this used? can we harmonize with no risk? Did not change the behaviour to ensure compatibility if this is important. """ pubkey2 = b64decode(data2[0]['public_key']).decode('utf-8').replace( "\n", "") if verbose: # print("data2", data2) print("pubkey", pubkey) # print("data2 pk", data2[0]['public_key']) print("pubkey2", pubkey2) # i = pubkey.find(data2[0]['public_key']) assert data1[0][0] == data2[0]['timestamp'] assert type(data2[0]['timestamp']) == str assert data1[0][1] == data2[0]['address'] assert data1[0][2] == data2[0]['recipient'] assert data1[0][3] == data2[0]['amount'] assert data1[0][4] == data2[0]['signature'] # assert i > 0 assert pubkey == pubkey2 if verbose: print("test_mpget_json ok")
def lambda_handler(event, context): ''' Updates the minProvisionedTPS value for an existing Personalize campaign ''' if event.get('detail'): campaign_arn = event['detail']['CampaignARN'] min_tps = event['detail']['MinProvisionedTPS'] reason = event['detail']['Reason'] else: campaign_arn = event['CampaignARN'] min_tps = event['MinProvisionedTPS'] reason = event.get('Reason') if min_tps < 1: raise ValueError(f'"MinProvisionedTPS" must be >= 1') region = extract_region(campaign_arn) if not region: raise Exception('Region could not be extracted from campaign_arn') personalize = get_client(service_name='personalize', region_name=region) response = personalize.update_campaign(campaignArn=campaign_arn, minProvisionedTPS=min_tps) if logger.isEnabledFor(logging.DEBUG): logger.debug(json.dumps(response, indent=2, default=str)) if not reason: reason = f'Amazon Personalize campaign {campaign_arn} deletion initiated (reason unspecified)' put_event(detail_type='PersonalizeCampaignMinProvisionedTPSUpdated', detail=json.dumps({ 'CampaignARN': campaign_arn, 'NewMinProvisionedTPS': min_tps, 'Reason': reason }), resources=[campaign_arn]) logger.info({'campaignArn': campaign_arn, 'minProvisionedTPS': min_tps}) return f'Successfully initiated update of minProvisionedTPS to {min_tps} for campaign {campaign_arn}'
from dataiku import pandasutils as pdu import datetime import time import json import os import socket import dataikuapi # twitter client from birdy.twitter import UserClient,TwitterApiError from common import get_client, calc_interval input_dataset_name = get_input_names_for_role('main')[0] output_dataset_name = get_output_names_for_role('main')[0] client = get_client() # input dataset's column INPUT_COLUMN=get_recipe_config()['input_column'] # user_data dataset ud = dataiku.Dataset(input_dataset_name) # output dataset results = [] # for each user for record in ud.iter_rows(): user_id = record.get(INPUT_COLUMN, None) if user_id is None or len(user_id) == 0: print "Empty user id, ignoring"