def getTargetDate(targetDate): if targetDate is None: #默认是昨天 targetDate = datetime.now()+timedelta(days=-1) commonutil.info("use default target hour : date=[{0}]".format(targetDate)) else: targetDate = datetime.strptime(targetDate,'%Y-%m-%d') commonutil.info("target date : date=[{0}]".format(targetDate)) return targetDate
if d != "" : os.chdir(d) #解析参数 options = getOptions() targetDate = getTargetDate(options.targetDate) #准备工作 outputPath = os.path.abspath("./output")+"/"+targetDate.strftime("%Y-%m-%d") commonutil.runCmd("mkdir -p", outputPath) mailConf = reportutil.getConf('mail.conf') dbConf = reportutil.getConf('db.conf') conns = reportutil.getConns(dbConf) targetDateStart = targetDate.strftime("%Y-%m-%d 00:00:00") targetDateEnd = (targetDate+timedelta(days=1)).strftime("%Y-%m-%d 00:00:00") targetDateEarly = (targetDate+timedelta(days=-7)).strftime("%Y-%m-%d 00:00:00") outputFile = outputPath+"/data" reportutil.truncateFile(outputFile) #拉取数据 vars = {'VAR_START':targetDateStart, 'VAR_END':targetDateEnd, 'VAR_EARLY':targetDateEarly} isSuccess = dumpmysql.dump(conns['mall'], 'report.sql', outputFile, vars, True) if not isSuccess: commonutil.fatal("[{0}] failed".format(targetDateStart)) sys.exit(1) commonutil.info("[{0}] done".format(targetDateStart)) #生成报表 html = reportutil.readFile('template.head') html = html + makeTrs('template.mid', outputFile) html = html + reportutil.readFile('template.tail') #发送邮件 sendMail(targetDate, html, mailConf)