Esempio n. 1
0
def seed_chart_three(student_id):
    """ Passes data for minutes practiced over four weeks to chart #3 as JSON"""

    if 'student_id' in session:
        pass
    elif 'teacher_id' in session:
        teacher = crud.get_teacher_by_id(session['teacher_id'])
        valid_students = teacher.get_student_ids()

        # print('****' * 5, student_id, '****' * 5, sep='\n')

        if int(student_id or 0) in valid_students:
            crud.get_logs_by_student_id
        else:
            return jsonify({'error': 'student not valid'})

    # x-axis data: dates in month (eventually divded into four weeks)
    dates_in_month = [
    ]  # holds todays date and previous 27 dates as list items
    date = datetime.now()
    for _ in range(28):
        dater = str(date.year) + '-' + str(date.month) + '-' + str(
            date.day)  #formats each date
        dates_in_month.append(
            dater)  #adds formatted date to dates_in_month list
        date = date - timedelta(days=1)  #goes back a day from current date

    minutes_practiced = []

    # format_date = datetime.strptime(date, "%Y-%m-%d").date()

    # y-axis data: minutes practiced on each date in the month
    for date in dates_in_month:  # loops over the dates of the month
        monthly_dates = crud.search_logs_by_date(
            datetime.strptime(date, '%Y-%m-%d').date(),
            student_id)  #finds and formatts all logged practice dates in DB
        if monthly_dates:
            minutes_practiced.append((date, monthly_dates.minutes_practiced))
        else:
            minutes_practiced.append((date, 0))

    data = {}
    data['dates_in_month'] = [
        datetime.strptime(date, '%Y-%m-%d').date().ctime()[4:10]
        for date, date_prac in minutes_practiced
    ]
    #['2021-3-1', '2021-2-28', '2021-2-27', '2021-2-26', '2021-2-25', '2021-2-24', '2021-2-23', '2021-2-22', '2021-2-21', '2021-2-20', '2021-2-19', '2021-2-18', '2021-2-17', '2021-2-16', '2021-2-15', '2021-2-14', '2021-2-13', '2021-2-12', '2021-2-11', '2021-2-10', '2021-2-9', '2021-2-8', '2021-2-7', '2021-2-6', '2021-2-5', '2021-2-4', '2021-2-3', '2021-2-2']
    data['minutes_practiced'] = [
        min_prac for date, min_prac in minutes_practiced
    ]
    # [('2021-3-1', 45), ('2021-2-28', 0), ('2021-2-27', 0), ('2021-2-26', 120), ('2021-2-25', 12), ('2021-2-24', 45), ('2021-2-23', 35), ('2021-2-22', 100), ('2021-2-21', 22), ('2021-2-20', 0), ('2021-2-19', 45), ('2021-2-18', 22), ('2021-2-17', 23), ('2021-2-16', 45), ('2021-2-15', 0), ('2021-2-14', 10), ('2021-2-13', 0), ('2021-2-12', 72), ('2021-2-11', 0), ('2021-2-10', 42), ('2021-2-9', 0), ('2021-2-8', 50), ('2021-2-7', 65), ('2021-2-6', 35), ('2021-2-5', 122), ('2021-2-4', 40), ('2021-2-3', 25), ('2021-2-2', 0)]

    return jsonify(data)
Esempio n. 2
0
def seed_chart_two(student_id):
    """ Passes data for days practiced over four weeks to chart #2 as JSON"""

    if 'student_id' in session:
        pass
    elif 'teacher_id' in session:
        teacher = crud.get_teacher_by_id(session['teacher_id'])
        valid_students = teacher.get_student_ids()

        if int(student_id or 0) in valid_students:
            crud.get_logs_by_student_id(int(student_id))
        else:
            return jsonify({'error': 'student not valid'})

    # x-axis data: dates in month (eventually divded into four weeks)
    dates_in_month = [
    ]  # holds todays date and previous 27 dates as list items
    date = datetime.now()
    for _ in range(28):
        dater = str(date.year) + '-' + str(date.month) + '-' + str(
            date.day)  #formats each date
        dates_in_month.append(
            dater)  #adds formatted date to dates_in_month list
        date = date - timedelta(days=1)  #changed

    log_date = []

    # y-axis data: days practiced in each week of the month
    for date in dates_in_month:  # loops over each date of the month
        monthly_dates = crud.search_logs_by_date(
            datetime.strptime(date, '%Y-%m-%d').date(),
            student_id)  #finds and formatts all logged practice dates in DB
        if monthly_dates:
            log_date.append(
                (date, 1)
            )  #adds to log_date date in month, 1 to signify a practice session that date
        else:
            log_date.append(
                (date, 0)
            )  #adds date in month, 0 to signify no practice session that date

    data = {}
    data['dates_in_month'] = [
        datetime.strptime(date, '%Y-%m-%d').date().ctime()[4:10]
        for date, date_prac in log_date
    ]
    data['log_date'] = [date_prac for date, date_prac in log_date]

    return jsonify(data)
Esempio n. 3
0
def seed_chart_one(student_id):
    """
    Passes data for minutes practiced and log dates into chart #1 as JSON

    """

    if not student_id:
        raise ValueError(f'{student_id=}')

    if type(student_id) != int:
        student_id = int(student_id)

    if "student_id" in session:
        pass

    elif "teacher_id" in session:

        # Get the student in one query
        my_student = db.session.query(Student)\
            .join(Teacher)\
            .filter(
                Teacher.teacher_id==session['teacher_id'],
                Student.student_id==student_id
            )\
            .first()

        if my_student:
            # Get the logs from the relationship
            stu_logs = my_student.logs

        else:
            return jsonify({'error': 'student not valid'})

    #  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

    # YB: Consider utilizing pandas here to group by an interval.
    # Pandas is excelent at time series data. You could [bin] your data by week/month/etc

    # x-axis data: dates in the week
    practice_dates = [
    ]  # holds todays date and previous six days as list items
    date = datetime.now()
    for _ in range(7):
        dater = str(date.year) + '-' + str(date.month) + '-' + str(date.day)
        practice_dates.append(dater)
        date = date - timedelta(days=1)  # ex: first iteration = yesterday

    minutes_practiced = []

    #  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

    # y-axis data: minutes practiced on each date in the week
    for dt in practice_dates:  # loops over the dates of the week
        dates_practiced = crud.search_logs_by_date(
            datetime.strptime(dt, '%Y-%m-%d').date(),
            student_id)  #all practice dates

        if dates_practiced:
            minutes_practiced.append((dt, dates_practiced.minutes_practiced))
        else:
            minutes_practiced.append((dt, 0))

    #  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

    data = {}
    data['dates_practiced'] = [
        datetime.strptime(dt, '%Y-%m-%d').date().ctime()[4:10]
        for dt, min_prac in minutes_practiced
    ]
    #2021-02-28 21:05:57,764 INFO sqlalchemy.engine.base.Engine {'log_date_1': datetime.date(2021, 2, 23), 'param_1': 1}
    data['minutes_practiced'] = [
        min_prac for dt, min_prac in minutes_practiced
    ]
    #[('2021-2-28', 0), ('2021-2-27', 0), ('2021-2-26', 120), ('2021-2-25', 12), ('2021-2-24', 45), ('2021-2-23', 35), ('2021-2-22', 100)]

    return jsonify(data)