def map_index(): city_name = g.city_name today = time.strftime("%Y-%m-%d", time.localtime()) date_end = request.args.get('date_end', default=today) hd_type = request.args.get('type', default='aqi') condition = { 'date': date_end, } hd_types = ['aqi', 'quality', 'pm25', 'pm10', 'so2', 'co', 'no2', 'o3'] if hd_type not in hd_types: hd_type = 'aqi' field = 'hd_' + hd_type history_client = History.factory() city_all = history_client.all_city(condition) data = dict() data['current_page'] = 'map' data['city_name'] = city_name data['city_all'] = city_all data['req_args'] = dict(request.args.items()) data['date_end'] = date_end data['type'] = hd_type data['field'] = field return render_template('map/index.html', **data)
def report_aqi_trend(): city_name = g.city_name today = time.strftime("%Y-%m-%d", time.localtime()) day7_dt = datetime.datetime.now() - datetime.timedelta(days=7) day7 = day7_dt.strftime("%Y-%m-%d") date_start = request.args.get('date_start', default=day7) date_end = request.args.get('date_end', default=today) condition = { 'city_name': city_name, 'date_start': date_start, 'date_end': date_end } history_client = History.factory() history_all = history_client.all_day(condition) history_types = [u'优', u'良', u'轻度污染', u'中度污染', u'重度污染', u'严重污染'] for history in history_all: history['hd_quality_index'] = history_types.index( history['hd_quality']) data = dict() data['current_page'] = 'report' data['req_args'] = dict(request.args.items()) data['history_all'] = history_all data['city_name'] = city_name data['date_start'] = date_start data['date_end'] = date_end return render_template('report/aqi_trend.html', **data)
def report_aqi_total(): city_name = g.city_name today = time.strftime("%Y-%m-%d", time.localtime()) day7_dt = datetime.datetime.now() - datetime.timedelta(days=7) day7 = day7_dt.strftime("%Y-%m-%d") date_start = request.args.get('date_start', default=day7) date_end = request.args.get('date_end', default=today) condition = { 'city_name': city_name, 'date_start': date_start, 'date_end': date_end } history_client = History.factory() hd_quality = history_client.total_history(condition, 'hd_quality') data = dict() data['current_page'] = 'report' data['req_args'] = dict(request.args.items()) data['hd_quality'] = hd_quality data['city_name'] = city_name data['date_start'] = date_start data['date_end'] = date_end return render_template('report/aqi_total.html', **data)
def data_history(): city_name = g.city_name history_client = History.factory() history_city = history_client.get_city_by_name(city_name) if not history_city: return u'暂时不支持此城市的天气数据查询' weather_client = Weather.factory() weather_city = weather_client.get_city_by_name(city_name) today = time.strftime("%Y-%m-%d", time.localtime()) day7_dt = datetime.datetime.now() - datetime.timedelta(days=7) day7 = day7_dt.strftime("%Y-%m-%d") page = request.args.get('page', 1, type=int) date_start = request.args.get('date_start', default=day7) date_end = request.args.get('date_end', default=today) condition = { 'city_name': city_name, 'date_start': date_start, 'date_end': date_end } history_client = History.factory() info = history_client.search_day(condition, page, 31) # print json.dumps(dict(info), indent=7, ensure_ascii=False) # print str(info) info['pages'] = min(7, info['pages']) data = dict() data['current_page'] = 'data' data['date_start'] = date_start data['date_end'] = date_end data['history_city'] = history_city data['weather_city'] = weather_city data['req_args'] = dict(request.args.items()) data['info'] = info data['page'] = page return render_template('data/history.html', **data)
def data_index(): city_name = g.city_name history_client = History.factory() history_city = history_client.get_city_by_name(city_name) weather_client = Weather.factory() weather_city = weather_client.get_city_by_name(city_name) crawl_client = Crawl.factory() if request.method == 'POST': job_id = request.form.get('job_id', default=0) if not job_id: return json.dumps({'status': False, 'message': u'没有任务id!'}) job_info = crawl_client.get_job_info_by_id(job_id) if not job_info: return json.dumps({'status': False, 'message': u'没有任务信息!'}) if 'city_name' not in job_info or job_info['city_name'] != city_name: return json.dumps({'status': False, 'message': u'此城市没有任务信息!'}) # scrapy crawl aqistudy -a city_name=上海 -a month=2017-01 sp = Spider.factory() m = job_info['job_month'] if int(m) < 10: m = '0' + str(m) month = str(job_info['job_year']) + '-' + str(m) result = sp.schedule_job(spider=job_info['job_spider'], setting=[], jobid=job_id, city_name=city_name, month=month) if not result: return json.dumps({'status': False, 'message': u'启动任务失败!'}) return json.dumps({'status': True, 'message': u'启动任务成功!'}) current = datetime.datetime.now() job_list = crawl_client.get_job_list(city_name, current.year, current.month) # print job_list data = dict() data['current_page'] = 'data' data['history_city'] = history_city data['weather_city'] = weather_city data['req_args'] = dict(request.args.items()) data['job_list'] = job_list return render_template('data/index.html', **data)
def learn_index(): city_name = g.city_name today = time.strftime("%Y-%m-%d", time.localtime()) day7_dt = datetime.datetime.now() - datetime.timedelta(days=7) day7 = day7_dt.strftime("%Y-%m-%d") date_start = request.args.get('date_start', default=day7) date_end = request.args.get('date_end', default=today) history = request.args.get('history', default=1, type=int) weather = request.args.get('weather', default=1, type=int) condition = { 'city_name': city_name, 'date_start': date_start, 'date_end': date_end } history_client = History.factory() weather_client = Weather.factory() history_count = 0 weather_count = 0 if history == 1: history_count = history_client.count_history(condition) if weather == 1: weather_count = weather_client.count_weather(condition) data = dict() data['current_page'] = 'learn' data['req_args'] = dict(request.args.items()) data['city_name'] = city_name data['date_start'] = date_start data['date_end'] = date_end data['history'] = history data['weather'] = weather data['history_count'] = history_count data['weather_count'] = weather_count return render_template('learn/index.html', **data)
def learn_step2(): city_name = g.city_name today = time.strftime("%Y-%m-%d", time.localtime()) day7_dt = datetime.datetime.now() - datetime.timedelta(days=7) day7 = day7_dt.strftime("%Y-%m-%d") date_start = request.args.get('date_start', default=day7) date_end = request.args.get('date_end', default=today) history = request.args.get('history', default=1, type=int) weather = request.args.get('weather', default=1, type=int) day_num = request.args.get('day_num', default=1, type=int) condition = { 'city_name': city_name, 'date_start': date_start, 'date_end': date_end } history_client = History.factory() weather_client = Weather.factory() history_count = 0 weather_count = 0 if history == 1: history_count = history_client.count_history(condition) if weather == 1: weather_count = weather_client.count_weather(condition) if request.method == 'POST': histories = request.form.getlist('histories[]') weathers = request.form.getlist('weathers[]') # print histories, weathers if not histories and not weathers: return json.dumps({'status': False, 'message': u'请您勾选特征数据类型!'}) if history_count < 1 and weather_count < 1: return json.dumps({ 'status': False, 'message': u'选定的日期内没有数据,请返回上一步重新选择!' }) if (history_count + weather_count) < 7: return json.dumps({ 'status': False, 'message': u'数据至少应该有7条(天)以上才能进行机器学习!' }) learn_client = Learn.factory() job_id = learn_client.create_job({ 'learn_status': Learn.JOB_READY, 'city_name': city_name, 'date_start': date_start, 'date_end': date_end, 'history': history, 'weather': weather, 'histories': json.dumps(histories), 'weathers': json.dumps(weathers), 'target': 'PM25', 'day_num': day_num }) if not job_id: return json.dumps({'status': False, 'message': u'建立机器学习任务失败!'}) return json.dumps({'status': True, 'message': job_id}) data = dict() data['current_page'] = 'learn' data['req_args'] = dict(request.args.items()) data['city_name'] = city_name data['date_start'] = date_start data['date_end'] = date_end data['history'] = history data['weather'] = weather data['history_count'] = history_count data['weather_count'] = weather_count return render_template('learn/step2.html', **data)