Track associated toggl entry id for uniqueness, show stats for time spent in different parts of the lecture flow.

This commit is contained in:
Joshua Coles 2023-10-06 22:00:01 +01:00
parent ef301f6592
commit 0cb03812cf
6 changed files with 135 additions and 47 deletions

View File

@ -28,30 +28,62 @@ class ScrapeTogglJob < ApplicationJob
return if entries_data.empty?
lectures.each do |lecture|
next if lecture.attendance.present?
entries_data.each do |entry|
entry_title = entry['description']
entries_data.each do |entry|
concurrent_time_entry = entry['time_entries'].find do |inner_entry|
if (lecture = lectures.find_by(title: entry_title))
is_concurrent = entry['time_entries'].any? do |inner_entry|
(Time.new(inner_entry['start']) - lecture.start_time).abs < 10.minutes
end
if concurrent_time_entry.present?
Attendance.create!(
lecture:,
associated_toggl_entry: concurrent_time_entry['id'],
kind = if is_concurrent
:concurrent
else
:catchup
end
lecture.tracked_time_entries.create!(
toggl_data: entry,
associated_toggl_entry_id: entry['time_entries'][0]['id'],
# TODO: What should happen if the time entry is materially before the lecture?
kind:
) and next
end
concurrent_unlabeled = entry['time_entries'].any? do |inner_entry|
lecture = lectures.find do |lecture|
(Time.new(inner_entry['start']) - lecture.start_time).abs < 10.minutes
end
if lecture.present?
lecture.tracked_time_entries.create!(
kind: :concurrent,
toggl_data: entry
)
elsif entry['description'] == lecture.title
# If the title matches but it wasn't concurrent, then it was a catchup
Attendance.create!(
lecture:,
associated_toggl_entry: entry['time_entries'][0]['id'],
kind: :catchup,
toggl_data: entry
toggl_data: entry,
associated_toggl_entry_id: inner_entry['id']
)
end
lecture.present?
end
next if concurrent_unlabeled
preparation_regex = /^(.+): ?(?:Prep|Preparation)?$/
review_regex = /^(.+): ?(?:Review|Recap)/
if (lecture_title_match = entry_title.match(preparation_regex)) and (lecture = lectures.find_by(title: lecture_title_match[1]))
lecture.tracked_time_entries.create!(
kind: :preparation,
toggl_data: entry,
associated_toggl_entry_id: entry['time_entries'][0]['id'],
)
elsif (lecture_title_match = entry_title.match(review_regex)) and (lecture = lectures.find_by(title: lecture_title_match[1]))
lecture.tracked_time_entries.create!(
kind: :review,
toggl_data: entry,
associated_toggl_entry_id: entry['time_entries'][0]['id'],
)
end
end
end

View File

@ -10,6 +10,30 @@ class Lecture < ApplicationRecord
:problems_class,
], default: :lecture
def prepared?
tracked_time_entries.where(kind: [:preparation]).any?
end
def attended?
tracked_time_entries.where(kind: [:concurrent, :catchup]).any?
end
def reviewed?
tracked_time_entries.where(kind: [:review]).any?
end
def total_preparation_time
tracked_time_entries.preparation.sum(&:duration).seconds
end
def total_attendance_time
tracked_time_entries.where(kind: [:concurrent, :catchup]).sum(&:duration).seconds
end
def total_review_time
tracked_time_entries.review.sum(&:duration).seconds
end
def week_number
((start_time.beginning_of_week - course.semester_start_date.to_time) / 1.week).floor + 1
end

View File

@ -28,7 +28,7 @@
<% lectures.reject(&:cancelled).each do |lecture| %>
<tr class="<%= class_names({
'lecture-future': lecture.start_time.future?,
'bg-green-100': lecture.attendance.present?,
'bg-green-100': lecture.attended?,
}) %>">
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
<%= lecture.title %>
@ -43,49 +43,56 @@
Cancelled
</div>
</td>
<% elsif lecture.start_time.future? %>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<div class="flex justify-center">
<%= button_to "Prepare",
lecture_start_preparation_path(id: lecture.id),
class: 'action-button'
%>
</div>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
</td>
<% else %>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500" data-controller="popover" data-action="mouseenter->popover#show mouseleave->popover#hide">
<div class="flex flex-row gap-2">
<% if lecture.attendance.nil? %>
<i class="fa fa-times text-red-700"></i>
<% elsif lecture.attendance.kind == 'concurrent' %>
<i class="fa fa-hourglass-start text-green-700"></i>
<% prep_icon_class = if lecture.prepared?
'text-green-700'
else
'text-slate-300'
end %>
<i class="fa fa-hourglass-start <%= prep_icon_class %>"></i>
<% if lecture.attended? %>
<i class="fa fa-chalkboard-user text-green-700"></i>
<i class="fa fa-hourglass-end text-green-700"></i>
<% elsif lecture.attendance.kind == 'catchup' %>
<i class="fa fa-video text-green-700"></i>
<% elsif lecture.start_time.past? %>
<i class="fa fa-times text-red-700"></i>
<% else %>
<% lecture.attendance.kind %>
<div class="flex-grow"></div>
<% end %>
<% reviewed_icon_class = if lecture.start_time.future?
'invisible'
elsif lecture.reviewed?
'text-green-700'
else
'text-slate-300'
end %>
<i class="fa fa-hourglass-end <%= reviewed_icon_class %>"></i>
</div>
<template data-popover-target="content">
<% if lecture.attendance.present? %>
<div class="absolute p-1 w-max whitespace-normal break-words rounded-lg border border-blue-gray-50 bg-white text-blue-gray-500 shadow-lg shadow-blue-gray-500/10 focus:outline-none" data-popover-target="card">
In lecture: <%= lecture.attendance.duration.in_minutes.round %> minutes
<div class="absolute p-1 w-max whitespace-normal break-words rounded-lg border border-blue-gray-50 bg-white text-blue-gray-500 shadow-lg shadow-blue-gray-500/10 focus:outline-none" data-popover-target="card">
<div>
Preparation: <%= lecture.total_preparation_time.in_minutes.round %> minutes
</div>
<% end %>
<div>
In lecture: <%= lecture.total_attendance_time.in_minutes.round %> minutes
</div>
<div>
Review: <%= lecture.total_review_time.in_minutes.round %> minutes
</div>
</div>
</template>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<% if lecture.attendance.nil? %>
<% if lecture.start_time.future? %>
<%= button_to "Prepare",
lecture_start_preparation_path(id: lecture.id),
class: 'action-button'
%>
<% elsif lecture.start_time.past? && !lecture.attended? %>
<%= button_to "Start",
lectures_start_path(id: lecture.id),
class: 'action-button'

View File

@ -0,0 +1,8 @@
class AddAssociatedTogglEntryId < ActiveRecord::Migration[7.1]
def change
add_column :tracked_time_entries, :associated_toggl_entry_id, :integer, null: false
# Make column unique
add_index :tracked_time_entries, :associated_toggl_entry_id, unique: true
end
end

View File

@ -0,0 +1,5 @@
class MakeAssociatedTogglEntryIdLarger < ActiveRecord::Migration[7.1]
def change
change_column :tracked_time_entries, :associated_toggl_entry_id, :bigint
end
end

14
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_10_02_152546) do
ActiveRecord::Schema[7.1].define(version: 2023_10_06_204103) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -59,6 +59,18 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_02_152546) do
t.index ["recording_uuid"], name: "index_recordings_on_recording_uuid", unique: true
end
create_table "tracked_time_entries", force: :cascade do |t|
t.bigint "lecture_id", null: false
t.integer "kind"
t.jsonb "toggl_data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "associated_toggl_entry_id", null: false
t.index ["associated_toggl_entry_id"], name: "index_tracked_time_entries_on_associated_toggl_entry_id", unique: true
t.index ["lecture_id"], name: "index_tracked_time_entries_on_lecture_id"
end
add_foreign_key "attendances", "lectures"
add_foreign_key "recordings", "courses"
add_foreign_key "tracked_time_entries", "lectures"
end