コード例 #1
0
def demo(key, feed_id=None, datastream_id=None, timestamp=None):

    client = Client()

    if feed_id and datastream_id:
        
        if timestamp:
            # read the datapoint at the specified timestamp

            try:
                logging.info("Reading the datapoint at %s" % timestamp)
                datapoint = yield client.read_datapoint(api_key=key, 
                                                        feed_id=feed_id, 
                                                        datastream_id=datastream_id, 
                                                        timestamp=timestamp)
                if datapoint:
                    logging.info("Success reading the datapoint:\n%s\n" % datapoint)
                else:
                    logging.error("Problem occurred reading the datapoint")
            except Exception, ex:
                logging.error("Problem reading the datapoint: %s" % str(ex))
                                
        else:
            # no specific datapoint was requested so just show the last 10 minutes worth
            
            # create historical query parameters spanning the last 10 minutes
            ten_minutes_ago_timestamp = datetime.datetime.utcnow() - datetime.timedelta(minutes=10)
            start_timestamp = "%sZ" % (ten_minutes_ago_timestamp.isoformat())
            parameters = {'start':start_timestamp, 'interval':0}
            
            try:
                logging.info("Requesting to view the last 10 minutes of historical datapoints for datastream %s in feed %s starting from %s" % (datastream_id, feed_id, start_timestamp))
                datastream = yield client.read_datastream(api_key=key, 
                                                          feed_id=feed_id, 
                                                          datastream_id=datastream_id,
                                                          parameters=parameters)
                if datastream:
                    logging.info("Success retrieving datastream historical datapoints:\n%s\n" % datastream)
                else:
                    logging.error("Problem reading datastream")                
            except Exception, ex:
                logging.error("Error reading datastream: %s" % str(ex))