Add Toggl data to attendances, fix attendance creation for past lectures. Change styling a little.

This commit is contained in:
Joshua Coles 2023-10-02 16:19:37 +01:00
parent 2530ee629a
commit 9a5116f29e
7 changed files with 43 additions and 23 deletions

View File

@ -77,6 +77,6 @@ class LectureDashboard < Administrate::BaseDashboard
# across all pages of the admin dashboard.
#
def display_resource(lecture)
lecture.title
"#{lecture.title} (#{lecture.course.title})"
end
end

View File

@ -12,7 +12,7 @@ class ScrapeTogglJob < ApplicationJob
# @param [Course] course
def scrape_course(course)
toggl_project_id = course.toggl_project
lectures = course.lectures.order(:start_time)
lectures = course.lectures.order(:start_time).includes(:attendance)
return if lectures.empty?
@ -24,26 +24,30 @@ class ScrapeTogglJob < ApplicationJob
end_time: Time.new('2024-01-01')
)
return if entries_data.empty?
lectures.each do |lecture|
return if lecture.attendance.present?
entries_data.each do |entry|
concurrent_time_entry = entry['time_entries'].find do |inner_entry|
(Time.new(inner_entry['start']) - lecture.start_time) < 10.minutes
(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: :concurrent
kind: :concurrent,
toggl_data: entry
)
elsif entry['description'] == lecture.nice_title
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
kind: :catchup,
toggl_data: entry
)
end
end

View File

@ -1,6 +1,6 @@
module Toggl
def self.entries_for_project(toggl_project_id, start_time:, end_time:)
HTTParty.post(
JSON.parse(HTTParty.post(
"http://localhost:3005/report",
body: {
"start_date": start_time.to_date.to_fs(),
@ -8,6 +8,6 @@ module Toggl
"project_ids": [toggl_project_id]
}.to_json,
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
)
).body)
end
end

View File

@ -1,7 +1,7 @@
class Course < ApplicationRecord
has_many :lectures
has_many :lectures, dependent: :destroy
# A course has a standalone connection to its recordings. To be shown they must be associated with a lecture but we
# track those not associated with a lecture to avoid duplication.
has_many :recordings
has_many :recordings, dependent: :destroy
end

View File

@ -27,7 +27,8 @@
<% lectures.each do |lecture| %>
<tr class="<%= class_names({
'lecture-future': lecture.start_time.future?,
'lecture-cancelled': lecture.cancelled,
'bg-slate-200': lecture.cancelled,
'bg-green-100': lecture.attendance.present?,
}) %>">
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
<%= lecture.title %>
@ -36,23 +37,32 @@
<%= lecture.start_time.to_fs(:dmy) %>
</td>
<% if lecture.start_time.future? || lecture.cancelled %>
<% if lecture.cancelled %>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500" colspan="2">
<div class="flex justify-center">
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">
</td>
<% else %>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<% if lecture.attendance.nil? %>
<i class="fa fa-times text-red-700"></i>
<% elsif lecture.attendance.kind == 'concurrent' %>
<i class="fa fa-chalkboard-user fa-green-700"></i>
<% elsif lecture.attendance.kind == 'catchup' %>
<i class="fa fa-video fa-green-700"></i>
<% else %>
<% lecture.attendance.kind %>
<% end %>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500 flex justify-center">
<div>
<% if lecture.attendance.nil? %>
<i class="fa fa-times text-red-700"></i>
<% elsif lecture.attendance.kind == 'concurrent' %>
<i class="fa fa-chalkboard-user text-green-700"></i>
<% elsif lecture.attendance.kind == 'catchup' %>
<i class="fa fa-video text-green-700"></i>
<% else %>
<% lecture.attendance.kind %>
<% end %>
</div>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">

View File

@ -0,0 +1,5 @@
class AddTogglDataToAttendance < ActiveRecord::Migration[7.0]
def change
add_column :attendances, :toggl_data, :jsonb
end
end

3
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_083755) do
ActiveRecord::Schema[7.0].define(version: 2023_10_02_150348) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -20,6 +20,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_02_083755) do
t.integer "kind"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.jsonb "toggl_data"
t.index ["lecture_id"], name: "index_attendances_on_lecture_id"
end