""" Load some test data into track_fingerprint.track_fp table to allow us to measure the performance of the sweeper process when a large number of records are in track_fp. This process only uses the tuid field in track_fp, so the other fields can contain dummy data. """ import argparse from datetime import datetime import random import tempfile import uuid from fpsweeper.connector.mysql import ar_session, fpc_session from fpsweeper.logic import util from fpsweeper.model import fp_message def get_random_tuids(tuid_count, db_session): """Get tuid_count random tuids from art_relations.track Args: tuid_count (int): number of tuids to retrieve db_session (sqlalchemy.orm.session.Session) Returns: tuids (list of int) """ sql = "SELECT id AS tuid FROM track ORDER BY RAND() LIMIT {}".format(tuid_count) res = db_session.execute(sql) return [row['tuid'] for row in res] def get_random_track_fp_row_csv(tuid): """return a track_fp row csv with random dummy values for given tuid Args: tuid (int) Returns: csv row (string) """ upc = str(random.randint(1, 999999999)) sample_rate_hertz = str(random.randint(10000, 30000)) duration_seconds = str(random.randint(60, 1200)) filename = str(uuid.uuid4()) samples_decoded = str(random.randint(1000000, 3000000)) given_duration_seconds = str(duration_seconds) start_offset_seconds = '0' codegen_version = '4.12' codegen_time_seconds = str(random.randint(4, 10)) decode_time_seconds = str(random.randint(1, 3)) code_count = str(random.randint(10000, 32000)) fingerprint = str(uuid.uuid4()) datetime_added_utc = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") created_by = 'load_test_data_into_track_fp.py' datetime_updated_utc = datetime_added_utc updated_by = 'load_test_data_into_track_fp.py' file_upload_time_utc = datetime_added_utc correlation_id = util.create_correlation_id() track_source = fp_message.SWEEPER return ",".join([str(tuid), upc, sample_rate_hertz, duration_seconds, filename, samples_decoded, given_duration_seconds, start_offset_seconds, codegen_version, codegen_time_seconds, decode_time_seconds, code_count, fingerprint, datetime_added_utc, created_by, datetime_updated_utc, updated_by, file_upload_time_utc, correlation_id, track_source]) def load_tuids_into_track_fp(tuids, db_session): """Generate temp file with dummy data that can be loaded fingerprint_capture.track_fp Args: tuids (list of int) db_session (sqlalchemy.orm.session.Session) """ # save data to temp file with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) as fp: filename = fp.name print("Generating file {}".format(filename)) for tuid in tuids: row = get_random_track_fp_row_csv(tuid) fp.write("{}\n".format(row)) sql = """LOAD DATA LOCAL INFILE '{}' INTO TABLE fingerprint_capture.track_fp FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\\n' (tuid, upc, sample_rate_hertz, duration_seconds, filename, samples_decoded, given_duration_seconds, start_offset_seconds, codegen_version, codegen_time_seconds, decode_time_seconds, code_count, fingerprint, datetime_added_utc, created_by, datetime_updated_utc, updated_by, file_upload_time_utc, correlation_id, track_source);""".format(filename) print("Run the following command in Sequel Pro to load track_fp:") print(sql) def main(): """Main entry point into script. """ args = parse_cl_args() print("Retrieving random tuids from track table") random_tuids = get_random_tuids(args.tuid_count, ar_session()) load_tuids_into_track_fp(random_tuids, fpc_session()) def parse_cl_args(): """Parse the command-line arguments and return them Returns: args (argparse.Namespace): namespace containing command-line arguments """ desc = """This script will retrieve specified number of random tuids from art_relations.track and generate test data that can be loaded into fingerprint_capture.track_fp. The script will print out a LOAD DATA LOCAL INFILE command that can be run from Sequel Pro to load the test data from temp file to track_fp.""" parser = argparse.ArgumentParser(description=desc) parser.add_argument('--tuid_count', type=int, help='Number of tuids to load into track_fingerprint.track_fp', required=True) return parser.parse_args() if __name__ == "__main__": main()