#!/usr/bin/python
"""Writes and reads a lightweight custom metric.

This is an example of how to use the Google Cloud Monitoring API to write
and read a lightweight custom metric. Lightweight custom metrics have no
labels and you do not need to create a metric descriptor for them.

Prerequisites: Run this Python example on a Google Compute Engine virtual
machine instance that has been set up using these intructions:
https://cloud.google.com/monitoring/demos/setup_compute_instance.

Typical usage: Run the following shell commands on the instance:
    python write_lightweight_metric.py
    python write_lightweight_metric.py
    python write_lightweight_metric.py
"""

import os
import time

# Google Cloud:
from apiclient.discovery import build
import httplib2
from oauth2client.gce import AppAssertionCredentials

# AWZ:
from amazon.api import AmazonAPI
import creds


PRODUCTS = [
	'B0199EW96U', # Year In White: Kindle
	'0813571197', # Year In White: Paperback
	'0813571200', # Year In White: Hardcover
	'1573980420', # April Fools RFCs: Paperback
	'B00N7N2CRQ', # The Practice of Cloud: Kindle
	'032194318X', # The Practice of Cloud: Paperback
	'B004JLMUJ0', # The Practice of Sysadmin: Kindle
	'0321492668', # The Practice of Sysadmin: Paperback
	'B0026OR2WM', # Time Mgmt for Sysadmins: Kindle
	'0596007833', # Time Mgmt for Sysadmins: Paperback
	'B00AZRBLHO', # Phoenix Project: Kindle
	'0988262509', # Phoenix Project: Paperback
	'0988262592', # Phoenix Project: Hardcover
	'B00VATFAMI', # Phoenix Project: Audible
]

CUSTOM_METRIC_NAME_BASE = "custom.cloudmonitoring.googleapis.com/amz_%s"


def GetProjectId():
  """Read the numeric project ID from metadata service."""
  http = httplib2.Http()
  resp, content = http.request(
      ("http://metadata.google.internal/"
       "computeMetadata/v1/project/numeric-project-id"),
      "GET", headers={"Metadata-Flavor": "Google"})
  if resp["status"] != "200":
    raise Exception("Unable to get project ID from metadata service")
  return content


def GetNowRfc3339():
  """Give the current time formatted per RFC 3339."""
  return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())


def GetProductRanks(product_codes):
  """
  product_codes: A list of asin codes.
  Returns a dict of asin: sales_rank.
  """
  result = {}
  for x in range(0, len(product_codes), 10):
    result.update(Get10ProductRanks(product_codes[x:x+10]))
  return result

def Get10ProductRanks(product_codes):
  """Amazon only permits 10 codes at a time."""
  amazon = AmazonAPI(creds.AMAZON_ACCESS_KEY, creds.AMAZON_SECRET_KEY, creds.AMAZON_ASSOC_TAG)
  products = amazon.lookup(ItemId=','.join(product_codes))
  return dict((x.asin, int(x.sales_rank)) for x in products)


def StoreProductRank(service, project_id, asin, sales_rank):
  """
  Stores the product rank to the monitoring system.
  """

  # Set up the write request.
  now = GetNowRfc3339()
  desc = {"project": project_id,
          "metric": CUSTOM_METRIC_NAME_BASE % (asin,)}
  point = {"start": now,
           "end": now,
           "doubleValue": sales_rank}
  print "Writing %d at %s" % (point["doubleValue"], now)

  # Write a new data point.
  try:
    write_request = service.timeseries().write(
        project=project_id,
        body={"timeseries": [{"timeseriesDesc": desc, "point": point}]})
    _ = write_request.execute()  # Ignore the response.
  except Exception as e:
    print "Failed to write custom metric data: exception=%s" % e
    raise  # propagate exception


def main():
  project_id = GetProjectId()

  # Create a cloudmonitoring service to call. Use OAuth2 credentials.
  credentials = AppAssertionCredentials(
      scope="https://www.googleapis.com/auth/monitoring")
  http = credentials.authorize(httplib2.Http())
  service = build(serviceName="cloudmonitoring", version="v2beta2", http=http)

  rankings = GetProductRanks(PRODUCTS)
  for asin, rank in rankings.iteritems():
    print "WRITING", asin, rank
    StoreProductRank(service, project_id, asin, rank)


if __name__ == "__main__":
  main()

@@ -18,655 +18,63 @@
 %0A%22%22%22
-Writes and reads a lightweight custom metric.%0A%0AThis is an example of how to use the Google Cloud Monitoring API to write%0Aand read a lightweight custom metric. Lightweight custom metrics have no%0Alabels and you do not need to create a metric descriptor for them.%0A%0APrerequisites: Run this Python example on a Google Compute Engine virtual%0Amachine instance that has been set up using these intructions:%0Ahttps://cloud.google.com/monitoring/demos/setup_compute_instance.%0A%0ATypical usage: Run the following shell commands on the instance:%0A    python write_lightweight_metric.py%0A    python write_lightweight_metric.py%0A    python write_lightweight_metric.py
+Records ASIN SalesRank and uploads to Google Monitoring
 %0A%22%22%22
@@ -417,100 +417,8 @@
 ver%0A
-%09'1573980420', # April Fools RFCs: Paperback%0A%09'B00N7N2CRQ', # The Practice of Cloud: Kindle%0A
 %09'03
@@ -472,15 +472,15 @@
 'B00
-4JLMUJ0
+N7N2CRQ
 ', #
@@ -496,24 +496,21 @@
 tice of 
-Sysadmin
+Cloud
 : Kindle
@@ -572,33 +572,35 @@
 'B00
-26OR2WM', # Time Mgmt for
+4JLMUJ0', # The Practice of
  Sys
@@ -604,17 +604,16 @@
 Sysadmin
-s
 : Kindle
@@ -661,24 +661,118 @@
 : Paperback%0A
+%09'B0026OR2WM', # Time Mgmt for Sysadmins: Kindle%0A%09'1573980420', # April Fools RFCs: Paperback%0A
 %09'B00AZRBLHO
@@ -934,11 +934,91 @@
 ble%0A
+%09'B016CJ5HUA', # Beyond Blame: Kindle%0A%09'1491906413', # Beyond Blame: Paperback%0A
 %5D%0A%0A
+%0A
 CUST
@@ -1892,16 +1892,17 @@
 result%0A%0A
+%0A
 def Get1

Add more books