1#!/usr/bin/env python3 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy of 7# the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations under 15# the License. 16"""Script that runs perpetually, flushing contents of shared BigqueryLoggerQueue 17to BigQuery on a specified schedule.""" 18 19import sys 20import time 21 22import acts.controllers.buds_lib.data_storage.bigquery.bigquery_logger_utils as utils 23 24 25def start_scheduled_automatic_logging(queue, project_id, credentials_path): 26 """Runs infinite while loop that flushes contents of queue to BigQuery 27 on schedule determined by flush_period.""" 28 29 client = utils.BigqueryLoggerClient(project_id, credentials_path) 30 31 while True: 32 # TODO: check if connected to internet before attempting to push to BQ 33 insert_iterator = queue.get_insert_iterator() 34 for dataset_table_tuple, rows_list in insert_iterator: 35 dataset_id, table_id = dataset_table_tuple 36 client.flush(rows_list, dataset_id, table_id) 37 38 time.sleep(queue.get_flush_period()) 39 40 41def main(): 42 """Pass shared BigqueryLoggerQueue to automatic logging method.""" 43 ip_address, port, authkey, project_id, credentials_path = sys.argv[1:] 44 queue = utils.get_queue(ip_address, port, authkey) 45 start_scheduled_automatic_logging(queue, project_id, credentials_path) 46 47 48if __name__ == '__main__': 49 main() 50