def hello(): """ Welcome page, loads all tags for the autocomplete. Context variables: tags: List of tags. from_date: Oldest date with daily available data to_date: Latest date with daily available data """ tags = [tag.name for tag in db_session.query(Tag).all()] fnc_max = func.max(Question.date).label("max") fnc_min = func.min(Question.date).label("min") dates = db_session.query(fnc_max, fnc_min).one() return render_template('graphs.html', tags=tags, from_date=dates.min, to_date=dates.max)
def load_old_data(): """ Gets old data for each tags and updates the database. If DB contains data from 01/01/2015, the execution of this method will load the statistics from 31/01/2014. """ qry = db_session.query(func.max(Question.date).label("max_date"), func.min(Question.date).label("min_date")) maxx, minn = qry.all()[0] day_before = minn - datetime.timedelta(days=1) update_questions(day_before)
def get_samples(tag=None): """ Autocomplete query. Returning only available daily samples. """ datadb = db_session.query(Question).filter_by(tag=tag)\ .order_by(Question.date.asc()) if (datadb.count() == 0): # Tag not found in database, returning 404. return render_template('404.html'), 404 # Preparing json with data response_data = {'samples': [el.nquestions for el in datadb]} return jsonify(**response_data)
def update_questions(date=None): """ Retrieves questions from yesterday if no date is provided. Retrieves from the day passed by parameter otherwise """ tags = db_session.query(Tag).all() if date is None: # We get data from yesterday if we dont provide the parameter dateget = datetime.datetime.now() - datetime.timedelta(days=1) else: dateget = date logger.debug("Tags to download: %s" % tags) for tag in tags: logger.info("Obtaining data for tag %s date: %s" % (tag.name, dateget)) get_questions(tag.name, dateget)
def get_data_api(): """ We obtain data for one day only. """ # Obtaining when the last data was downloaded fnc_max = func.max(Question.date).label("max") fnc_min = func.min(Question.date).label("min") dates = db_session.query(fnc_max, fnc_min).one() logger.debug("We have data in the database from `%s` to `%s`" % (dates.min, dates.max)) next_day = dates.max + timedelta(days=1) yesterday = datetime.now().date() - timedelta(days=1) if next_day < yesterday: # If we have data before yesterday, we download data for that day # We download data for yesterday at midnight logger.info("Downloading data from API on day `%s`" % next_day) update_questions(next_day)