consumer_3.py 948 B

123456789101112131415161718192021222324252627282930
  1. def getsales(self, message):
  2. #print len(self.sales)
  3. event1 = json.loads(message.value)
  4. prod_id = event1['product_id'].encode('ascii', 'ignore')
  5. units= event1['unit_sold'].encode('ascii', 'ignore')
  6. sales_rep = event1['sales_rep_id'].encode('ascii', 'ignore')
  7. tstamp = event1['timestamp'].encode('ascii','ignore')
  8. territory = self.reps[sales_rep]['TerritoryId']
  9. gallons = float(self.prods[prod_id]['Gallons'])*float(units)
  10. self.sales[territory]['Tstamp'] = tstamp
  11. actual = self.sales[territory]['Act'] + gallons
  12. self.sales[territory]['Act'] = actual
  13. ave = (self.sales[territory]['Ave'] + gallons)/2
  14. self.sales[territory]['Ave'] = ave
  15. plan = float(self.sales[territory]['Plan'])
  16. if actual > plan:
  17. self.sales[territory]['Stat'] = 1
  18. elif actual == plan:
  19. self.sales[territory]['Stat'] = 0
  20. elif actual < plan:
  21. self.sales[territory]['Stat'] = -1