Add toggl scraping
This commit is contained in:
parent
0d97b5fdc0
commit
be4f3f75c3
38
app/jobs/scrape_toggl_job.rb
Normal file
38
app/jobs/scrape_toggl_job.rb
Normal file
@ -0,0 +1,38 @@
|
||||
class ScrapeTogglJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(*args)
|
||||
courses = Course.all
|
||||
|
||||
courses.each do |course|
|
||||
self.scrape_course(course)
|
||||
end
|
||||
end
|
||||
|
||||
# @param [Course] course
|
||||
def scrape_course(course)
|
||||
toggl_project_id = course.toggl_project
|
||||
lectures = course.lectures.order(:start_time).where(associated_toggl_entry: nil)
|
||||
return if lectures.empty?
|
||||
|
||||
entries_data = Toggl::entries_for_project(
|
||||
toggl_project_id,
|
||||
|
||||
# TODO: Work out better limits
|
||||
start_time: Time.new('2023-01-01'),
|
||||
end_time: Time.new('2024-01-01')
|
||||
)
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
if associated_entry.present?
|
||||
lecture.update!(associated_toggl_entry: associated_entry['id'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
app/lib/toggl.rb
Normal file
13
app/lib/toggl.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module Toggl
|
||||
def self.entries_for_project(toggl_project_id, start_time:, end_time:)
|
||||
HTTParty.post(
|
||||
"http://localhost:3005/report",
|
||||
body: {
|
||||
"start_date": start_time.to_date.to_fs(),
|
||||
"end_date": end_time.to_date.to_fs(),
|
||||
"project_ids": [toggl_project_id]
|
||||
}.to_json,
|
||||
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,5 @@
|
||||
class AddAssociatedTogglEntryToLecture < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
add_column :lectures, :associated_toggl_entry, :integer, null: true
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,5 @@
|
||||
class ChangeTogglEntryIdToString < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
change_column :lectures, :associated_toggl_entry, :string, null: true
|
||||
end
|
||||
end
|
||||
3
db/schema.rb
generated
3
db/schema.rb
generated
@ -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_09_29_184126) do
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_09_29_200245) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
@ -31,6 +31,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_29_184126) 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
|
||||
|
||||
3
sig/toggl.rbs
Normal file
3
sig/toggl.rbs
Normal file
@ -0,0 +1,3 @@
|
||||
module Toggl
|
||||
def self.entries_for_project: -> Array[untyped]
|
||||
end
|
||||
7
test/jobs/scrape_toggl_job_test.rb
Normal file
7
test/jobs/scrape_toggl_job_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class ScrapeTogglJobTest < ActiveJob::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user