def picture_search(dic=None, hits=None, form=None, path=None): global es_index, old if request.method == 'POST': if dic and hits and form: return render_template('picture.html', title='Bird Pictures', form=old['form'], res=old['dic'], hits=old['hits']) if path: print(path) pass form = DateForm() dic = {} if form.is_submitted(): ts = request.form['date'] + 'T' + request.form['time'] te = request.form['date2'] + 'T' + request.form['time2'] date_object = time.strptime(ts, '%Y-%m-%dT%H:%M') date2_object = time.strptime(te, '%Y-%m-%dT%H:%M') date_utc = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(time.mktime(date_object))) date2_utc = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(time.mktime(date2_object))) body = { "size": 1000, # "_source": ["timestamp", "orginal_image", "found.bird", "dayofweek_text", "processing_time"], "sort": [{ "timestamp": { "order": "asc" } }] } body['query'] = { "range": { "timestamp": { 'gte': date_utc, "lte": date2_utc, "time_zone": "+07:00" } } } # body['query'] = { # "bool": { # "must": { # "exists": { # "field": "birds_count.bird" # } # }, # "filter": { # "range":{ # "timestamp":{ # "gte":"06 26 2019 @ 05:00:00", # "format": "MM dd yyyy @ HH:mm:SS", # "time_zone": "+07:00" # # } # } # } # } # } res = es.search(index=es_index, doc_type='_doc', body=body, scroll='1m') hits = res['hits']['total']['value'] res = res['hits']['hits'] print(hits) for e, i in enumerate(res): t_ = i['_source']['timestamp'] dt = parse(t_) try: new_time = datetime.strptime(t_, '%Y-%m-%dT%H:%M:%S') except: try: new_time = datetime.strptime(t_, '%Y-%m-%dT%H:%M:%S+07:00') except: new_time = datetime.strptime(t_, '%Y-%m-%dT%H:%M:%S.%f+07:00') central = datetime(year=new_time.year, month=new_time.month, day=new_time.day, hour=new_time.hour, minute=new_time.minute, second=new_time.second) # print(new_time, central)' try: numdetect = i['_source']['birds_count']['bird'] data_body = { 'time': central, 'numDetect': numdetect, 'index': e + 1, 'dayofweek': i['_source']['dayofweek_text'], 'processing_time': "{:.4f}".format(i['_source']['processing_time']) } dic[i['_id']] = data_body except Exception as e: print(e) # print(i['_id'], new_time, central) old['dic'] = dic old['hits'] = hits old['form'] = form return render_template('picture.html', title='Bird Pictures', form=form, res=dic, hits=hits) return render_template('picture.html', title='Bird Pictures', form=form, res=dic)
def picture_search(dic=None, hits=None, form=None): global es_index, old if request.method == 'POST': if dic and hits and form: return render_template('picture.html', title='Bird Pictures', form=old['form'], res=old['dic'], hits=old['hits']) form = DateForm() dic = {} if form.is_submitted(): ts = request.form['date'] + 'T' + request.form['time'] te = request.form['date2'] + 'T' + request.form['time2'] date_object = time.strptime(ts, '%Y-%m-%dT%H:%M') date2_object = time.strptime(te, '%Y-%m-%dT%H:%M') date_utc = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(time.mktime(date_object))) date2_utc = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(time.mktime(date2_object))) body = { "size": 1000, "_source": ["timestamp", "orginal_image", "found.bird", "dayofweek_text"], "query": { "range": { "timestamp": { 'gte': date_utc, "lte": date2_utc } } }, "sort": [{ "timestamp": { "order": "asc" } }] } res = es.search(index=es_index, doc_type='_doc', body=body, scroll='1m') # for i in res['hits']['total']: # print(i) # print(res['hits']['total']['value']) hits = res['hits']['total']['value'] res = res['hits']['hits'] for e, i in enumerate(res): # print(i['_source']['timestamp']) try: new_time = datetime.strptime(i['_source']['timestamp'], '%Y-%m-%dT%H:%M:%S') except: try: new_time = datetime.strptime(i['_source']['timestamp'], '%Y-%m-%dT%H:%M:%S+07:00') except: new_time = datetime.strptime(i['_source']['timestamp'], '%Y-%m-%dT%H:%M:%S.%f+07:00') from_zone = tz.gettz('UTC') to_zone = tz.gettz('Asia/Bangkok') utc = new_time.replace(tzinfo=from_zone) central = utc.astimezone(to_zone) central = str(central)[:str(central).index('+')] # print(new_time, central) try: dic[i['_id']] = { 'time': central, 'numDetect': i['_source']['found']['bird'], 'index': e + 1, 'dayofweek': i['_source']['dayofweek_text'] } except: pass # print(i['_id'], new_time, central) old['dic'] = dic old['hits'] = hits old['form'] = form return render_template('picture.html', title='Bird Pictures', form=form, res=dic, hits=hits) return render_template('picture.html', title='Bird Pictures', form=form, res=dic)