def load_sheet_knowledge_map(self, spreadsheet): knowledge = self.knowledge repository = self.repository # authenticate and get service API for spreadsheet service = knowledge.get_service_spreadsheets() item = spreadsheet spreadsheetId = item['_source']['sheet_id'] owner = item['_source']['update_by'] last_activity = item['_source']['@timestamp'] flow = item['_source']['flow'] contract = item['_source']['contract'] logger.debug('processing spreadsheet: %s ' % spreadsheetId) rangeName = 'TC_Report!A2:K' values = [] try: result = service.spreadsheets().values().get( spreadsheetId=spreadsheetId, range=rangeName).execute() values = result.get('values', []) if not values: logger.warn('No data found.') else: items = knowledge.readSheetData(spreadsheetId, values) ## refresh flow and contract values from TC_Report sheet flow = items[0]['flow'] contract = items[0]['contract'] logger.debug('%s technologies on this spreadsheet ' % len(items)) q = "sheet_id:" + spreadsheetId repository.deleteByQuery('knowledge', q) repository.createTemplateIfNotExits('knowledge') repository.saveDocuments('knowledge', 'tech', items) logger.info('==> spreadsheet %s loads successfully ' % spreadsheetId) except errors.HttpError, err: ## send email to owner logger.error("==> exception on %s : %s" % (spreadsheetId, err)) subject = 'ACTION REQUIRED: Tech Gallery %s-%s' % (contract, flow) send_gmail.send(owner, subject, spreadsheetId, str(err))
def load_knowledge_map(self, sheet=None, notify=False): knowledge = self.knowledge repository = self.repository # authenticate and get service API for spreadsheet service = knowledge.get_service_spreadsheets() if sheet: query = {"query": {"match": {"sheet_id": sheet}}} else: query = {"query": {"match_all": {}}} #spreadsheets = '1uzyIZf2r3DLKptr8ikeym1NNwiav-BwmtX3qbtDzhA4,1bPKnCx9nhcpEHoY9iMsW3LSDe5cORlkV2eLmHr6Wl5Y'.split(',') spreadsheets = repository.searchByQuery('project', query) total_hits = spreadsheets['hits']['total'] print 'total %s sheets' % total_hits total_errs = 0 for item in spreadsheets['hits']['hits']: spreadsheetId = item['_source']['sheet_id'] owner = item['_source']['update_by'] flow = item['_source']['flow'] contract = item['_source']['contract'] logger.debug('processing spreadsheet: %s ' % spreadsheetId) rangeName = 'TC_Report!A2:K' values = [] try: result = service.spreadsheets().values().get( spreadsheetId=spreadsheetId, range=rangeName).execute() values = result.get('values', []) if not values: logger.warn('No data found.') else: items = knowledge.readSheetData(spreadsheetId, values) logger.debug('%s technologies on this spreadsheet ' % len(items)) q = "sheet_id:" + spreadsheetId repository.deleteByQuery('knowledge', q) repository.createTemplateIfNotExits('knowledge') repository.saveDocuments('knowledge', 'tech', items) logger.info('==> spreadsheet %s loads successfully ' % spreadsheetId) except errors.HttpError, err: if err.resp.status in [404, 500]: # delete sheet_id from index project q = "sheet_id:" + spreadsheetId repository.deleteByQuery('project', q) ## delete sheet_id from knowledge repository.deleteByQuery('knowledge', q) logger.warning("==> Spreadsheet %s doesn't exists: %s" % (spreadsheetId, err)) else: ## send email to owner total_errs += 1 logger.error("==> exception on %s : %s" % (spreadsheetId, err)) if notify: subject = 'ACTION REQUIRED: Tech Gallery %s-%s' % ( contract, flow) send_gmail.send(owner, subject, spreadsheetId, str(err)) except Exception, e: total_errs += 1 logger.error("==> exception on %s : %s" % (spreadsheetId, e)) if notify: subject = 'ACTION REQUIRED: Tech Gallery %s-%s' % ( contract, flow) send_gmail.send(owner, subject, spreadsheetId, str(e))
q = "sheet_id:" + spreadsheetId repository.deleteByQuery('knowledge', q) repository.createTemplateIfNotExits('knowledge') repository.saveDocuments('knowledge', 'tech', items) logger.info('==> spreadsheet %s loads successfully ' % spreadsheetId) except errors.HttpError, err: ## send email to owner logger.error("==> exception on %s : %s" % (spreadsheetId, err)) subject = 'ACTION REQUIRED: Tech Gallery %s-%s' % (contract, flow) send_gmail.send(owner, subject, spreadsheetId, str(err)) except Exception, e: logger.error("==> exception on %s : %s" % (spreadsheetId, e)) subject = 'ACTION REQUIRED: Tech Gallery %s-%s' % (contract, flow) send_gmail.send(owner, subject, spreadsheetId, str(e)) doc = { "key": spreadsheetId, "owner": contract, "name": flow, "last_activity_user": owner, "last_activity": last_activity, "index": 0 } return doc class Repository(object): def __init__(self, config):