Rephrase toggl scraping in term of attendances
This commit is contained in:
parent
be4f3f75c3
commit
2934d21f5f
@ -12,7 +12,8 @@ class ScrapeTogglJob < ApplicationJob
|
||||
# @param [Course] course
|
||||
def scrape_course(course)
|
||||
toggl_project_id = course.toggl_project
|
||||
lectures = course.lectures.order(:start_time).where(associated_toggl_entry: nil)
|
||||
lectures = course.lectures.order(:start_time)
|
||||
|
||||
return if lectures.empty?
|
||||
|
||||
entries_data = Toggl::entries_for_project(
|
||||
@ -24,13 +25,24 @@ class ScrapeTogglJob < ApplicationJob
|
||||
)
|
||||
|
||||
lectures.each do |lecture|
|
||||
entries_data.find do |entry|
|
||||
associated_entry = entry['time_entries'].find do |inner_entry|
|
||||
(Time.new(inner_entry['start']) - lecture.start_time) < 15.seconds
|
||||
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
|
||||
end
|
||||
|
||||
if associated_entry.present?
|
||||
lecture.update!(associated_toggl_entry: associated_entry['id'])
|
||||
if concurrent_time_entry.present?
|
||||
Attendance.create!(
|
||||
lecture:,
|
||||
associated_toggl_entry: concurrent_time_entry['id'],
|
||||
kind: :concurrent
|
||||
)
|
||||
elsif entry['description'] == lecture.nice_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
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
8
app/models/attendance.rb
Normal file
8
app/models/attendance.rb
Normal file
@ -0,0 +1,8 @@
|
||||
class Attendance < ApplicationRecord
|
||||
belongs_to :lecture
|
||||
|
||||
enum :kind, [
|
||||
:concurrent,
|
||||
:catchup
|
||||
]
|
||||
end
|
||||
@ -1,5 +1,6 @@
|
||||
class Lecture < ApplicationRecord
|
||||
belongs_to :course
|
||||
has_one :attendance
|
||||
|
||||
enum :status, [
|
||||
:undetermined,
|
||||
|
||||
11
db/migrate/20230929201207_create_attendances.rb
Normal file
11
db/migrate/20230929201207_create_attendances.rb
Normal file
@ -0,0 +1,11 @@
|
||||
class CreateAttendances < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :attendances do |t|
|
||||
t.references :lecture, null: false, foreign_key: true
|
||||
t.string :associated_toggl_entry
|
||||
t.integer :kind
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,5 @@
|
||||
class RemoveTogglEntryIdFromLecture < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
remove_column :lectures, :associated_toggl_entry
|
||||
end
|
||||
end
|
||||
13
db/schema.rb
generated
13
db/schema.rb
generated
@ -10,10 +10,19 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_09_29_200245) do
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_09_29_201903) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
create_table "attendances", force: :cascade do |t|
|
||||
t.bigint "lecture_id", null: false
|
||||
t.string "associated_toggl_entry"
|
||||
t.integer "kind"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["lecture_id"], name: "index_attendances_on_lecture_id"
|
||||
end
|
||||
|
||||
create_table "courses", force: :cascade do |t|
|
||||
t.string "title"
|
||||
t.string "unit_code"
|
||||
@ -31,9 +40,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_29_200245) do
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.bigint "course_id"
|
||||
t.string "associated_toggl_entry"
|
||||
t.index ["course_id"], name: "index_lectures_on_course_id"
|
||||
t.index ["recording_id"], name: "index_lectures_on_recording_id", unique: true
|
||||
end
|
||||
|
||||
add_foreign_key "attendances", "lectures"
|
||||
end
|
||||
|
||||
11
test/fixtures/attendances.yml
vendored
Normal file
11
test/fixtures/attendances.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
lecture: one
|
||||
associated_toggl_entry: MyString
|
||||
kind: 1
|
||||
|
||||
two:
|
||||
lecture: two
|
||||
associated_toggl_entry: MyString
|
||||
kind: 1
|
||||
7
test/models/attendance_test.rb
Normal file
7
test/models/attendance_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class AttendanceTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user