<s> """<STR_LIT>""" <EOL> import argparse <EOL> import datetime <EOL> import pprint <EOL> import random <EOL> import time <EOL> import list_resources <EOL> def format_rfc3339 ( datetime_instance = None ) : <EOL> """<STR_LIT>""" <EOL> return datetime_instance . isoformat ( "<STR_LIT:T>" ) + "<STR_LIT>" <EOL> def get_start_time ( ) : <EOL> start_time = datetime . datetime . utcnow ( ) - datetime . timedelta ( minutes = <NUM_LIT:5> ) <EOL> return format_rfc3339 ( start_time ) <EOL> def get_now_rfc3339 ( ) : <EOL> return format_rfc3339 ( datetime . datetime . utcnow ( ) ) <EOL> def create_custom_metric ( client , project_id , <EOL> custom_metric_type , metric_kind ) : <EOL> """<STR_LIT>""" <EOL> metrics_descriptor = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" . format ( <EOL> project_id , custom_metric_type ) , <EOL> "<STR_LIT:type>" : custom_metric_type , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" <EOL> } <EOL> ] , <EOL> "<STR_LIT>" : metric_kind , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> client . projects ( ) . metricDescriptors ( ) . create ( <EOL> name = project_id , body = metrics_descriptor ) . execute ( ) <EOL> def get_custom_metric ( client , project_id , custom_metric_type ) : <EOL> """<STR_LIT>""" <EOL> request = client . projects ( ) . metricDescriptors ( ) . list ( <EOL> name = project_id , <EOL> filter = '<STR_LIT>' . format ( custom_metric_type ) ) <EOL> response = request . execute ( ) <EOL> print ( '<STR_LIT>' ) <EOL> pprint . pprint ( response ) <EOL> try : <EOL> return response [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return None <EOL> def get_custom_data_point ( ) : <EOL> """<STR_LIT>""" <EOL> length = random . randint ( <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> print ( "<STR_LIT>" . format ( str ( length ) ) ) <EOL> return length <EOL> def write_timeseries_value ( client , project_resource , <EOL> custom_metric_type , instance_id , metric_kind ) : <EOL> """<STR_LIT>""" <EOL> now = get_now_rfc3339 ( ) <EOL> timeseries_data = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : custom_metric_type , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : { <EOL> '<STR_LIT>' : instance_id , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> "<STR_LIT>" : metric_kind , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : now , <EOL> "<STR_LIT>" : now <EOL> } , <EOL> "<STR_LIT:value>" : { <EOL> "<STR_LIT>" : get_custom_data_point ( ) <EOL> } <EOL> } <EOL> ] <EOL> } <EOL> request = client . projects ( ) . timeSeries ( ) . create ( <EOL> name = project_resource , body = { "<STR_LIT>" : [ timeseries_data ] } ) <EOL> request . execute ( ) <EOL> def read_timeseries ( client , project_resource , custom_metric_type ) : <EOL> """<STR_LIT>""" <EOL> request = client . projects ( ) . timeSeries ( ) . list ( <EOL> name = project_resource , <EOL> filter = '<STR_LIT>' . format ( custom_metric_type ) , <EOL> pageSize = <NUM_LIT:3> , <EOL> interval_startTime = get_start_time ( ) , <EOL> interval_endTime = get_now_rfc3339 ( ) ) <EOL> response = request . execute ( ) <EOL> return response <EOL> def main ( project_id ) : <EOL> CUSTOM_METRIC_DOMAIN = "<STR_LIT>" <EOL> CUSTOM_METRIC_TYPE = "<STR_LIT>" . format ( CUSTOM_METRIC_DOMAIN ) <EOL> INSTANCE_ID = "<STR_LIT>" <EOL> METRIC_KIND = "<STR_LIT>" <EOL> project_resource = "<STR_LIT>" . format ( project_id ) <EOL> client = list_resources . get_client ( ) <EOL> create_custom_metric ( client , project_resource , <EOL> CUSTOM_METRIC_TYPE , METRIC_KIND ) <EOL> custom_metric = None <EOL> while not custom_metric : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> custom_metric = get_custom_metric ( <EOL> client , project_resource , CUSTOM_METRIC_TYPE ) <EOL> write_timeseries_value ( client , project_resource , <EOL> CUSTOM_METRIC_TYPE , INSTANCE_ID , METRIC_KIND ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> timeseries = read_timeseries ( client , project_resource , CUSTOM_METRIC_TYPE ) <EOL> print ( '<STR_LIT>' . format ( pprint . pformat ( timeseries ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( <EOL> description = __doc__ , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , required = True ) <EOL> args = parser . parse_args ( ) <EOL> main ( args . project_id ) </s>
