def send_settings(): try: if request.method == 'POST': sfields = request.form.getlist('selectedfields') vfields = dict() json_fields = red.lrange("JSON_FIELDS", 0, -1) # Check if the fields are valid for field in sfields: if field in json_fields: red.sadd("ENFIELDS", field) vfields[field] = True # TODO log invalid fields # Find checkboxes that were not set or unticketed and remove them for f in json_fields: if (f in vfields) is False: # Found a field that was not selected but is marked as being set # in a previous iteration if red.sismember("ENFIELDS", f): red.srem("ENFIELDS", f) fields = load_selected_fields() return render_template('settings.html', fields=fields, desc=create_program_meta(), params=build_params()) except redis.ConnectionError as err: errormsg("Cannot connect to redis. " + str(err)) return render_template('offline.html', prefix=prefix)
def deliver_evolution(date, field, key): try: desc = create_program_meta() params = build_params() if check_date(date) is False: emsg = "Invalid date specified" return render_template('content.html', desc=desc, params=params, emsg=emsg) data = [] daterange = enum_last_days(date, coverage) rkey = translate_human_to_redis(field, key) for date in daterange: entry = dict() k = sensorname + ":" + date + ":" + field score = red.zscore(k, rkey) if (score is not None): entry['date'] = date entry['score'] = score data.append(entry) # Convert date d = datetime.datetime.strptime(date, "%Y%m%d") showdate = d.strftime("%Y-%m-%d") return render_template("evol.html", desc=desc, date=showdate, field=field, key=key, data=data, params=params) except redis.ConnectionError as err: errormsg("Cannot connect to redis " + str(err)) return render_template('offline.html', prefix=prefix)
def welcome(): try: desc = create_program_meta() params = build_params() emsg = check_database() if emsg is not None: return render_template('content.html', desc=desc, params=params, emsg=emsg) # By default use latest day day = get_latest_day() if request.method == 'POST': p = request.form.get('datepicker') # JavaScript Library does also some checks. Do not trust the # code running on client machines day = check_user_day(p) if day is None: return render_template('content.html', desc=desc, params=params, emsg="Invalid date specified") fields = [] for field in red.smembers("ENFIELDS"): fields.append(field) topdata = get_top_10_per_day(day, fields) # Convert back the selected date d = datetime.datetime.strptime(day, "%Y%m%d") selday = d.strftime("%Y-%m-%d") # Put a warning when no fields are selected if get_enabled_fields_num() == 0: emsg = "No data fields are selected. Please select some fields in \ the settings menu." return render_template('content.html', desc=desc, fields=fields, topdata=topdata, params=build_params(), seldate=selday, emsg=emsg) return render_template('content.html', desc=desc, fields=fields, topdata=topdata, params=build_params(), seldate=selday) except redis.ConnectionError as err: errormsg("Could not connect to redis. " + str(err)) return render_template('offline.html', prefix=prefix)
def check_user_day(day): try: if day is None: return None if len(day) > 20: raise ValueError("User day string is too large." + day) # Let the datetime library check if the parameters correspond to # the right date format. If bad parameters are specified, # the most recent date is used d = datetime.datetime.strptime(day, "%Y-%m-%d") day = d.strftime("%Y%m%d") # Check if there is data for this day if red.sismember(f"{sensorname}_DAYS", day) == 1: return day except ValueError as error: errormsg("check_user_day: " + str(error)) return None
def deliver_custom(): try: field = request.form.get("field") fieldname = request.form.get("fieldname") date = request.form.get("date") if field == "" or fieldname == "" or date == "": emsg = "An empty parameter was provided." errormsg(emsg) return render_template('content.html', desc=create_program_meta, params=build_params(), emsg=emsg) # By default the latest day is used. When another date was specified # this one is used today = get_latest_day() if date is not None: try: d = datetime.datetime.strptime(date, "%Y-%m-%d") today = d.strftime("%Y%m%d") except ValueError as e: errormsg("deliver_custom: Invalid timestamp. " + str(e)) if fieldname in red.lrange("JSON_FIELDS", 0, -1): return deliver_evolution(today, fieldname, field) emsg = "An invalid parameter was provided" return render_template('content.html', desc=create_program_meta, params=build_params(), emsg=emsg) except redis.ConnectionError as err: errormsg("deliver_custom: Cannot connect to redis " + str(err)) return render_template('offline.html', prefix=prefix)
def check_date(date): try: if date is None: errormsg("check_date: No date was specified") return False if len(date) > 20: errormsg("check_date: Date field is too large") return False # Try to parse it. If it fails exception is thrown datetime.datetime.strptime(date, "%Y%m%d") return True except ValueError as e: errormsg("check_date: Wrong date format." + str(e)) return False