Merge branch '20842-todos-queries-cache' into 'master'
Try to get back todo's cache or at least avoid hitting the database See merge request !5789
This commit is contained in:
commit
20f9b8be2f
|
@ -275,6 +275,7 @@ v 8.10.0
|
||||||
- Fix new snippet style bug (elliotec)
|
- Fix new snippet style bug (elliotec)
|
||||||
- Instrument Rinku usage
|
- Instrument Rinku usage
|
||||||
- Be explicit to define merge request discussion variables
|
- Be explicit to define merge request discussion variables
|
||||||
|
- Use cache for todos counter calling TodoService
|
||||||
- Metrics for Rouge::Plugins::Redcarpet and Rouge::Formatters::HTMLGitlab
|
- Metrics for Rouge::Plugins::Redcarpet and Rouge::Formatters::HTMLGitlab
|
||||||
- RailsCache metris now includes fetch_hit/fetch_miss and read_hit/read_miss info.
|
- RailsCache metris now includes fetch_hit/fetch_miss and read_hit/read_miss info.
|
||||||
- Allow [ci skip] to be in any case and allow [skip ci]. !4785 (simon_w)
|
- Allow [ci skip] to be in any case and allow [skip ci]. !4785 (simon_w)
|
||||||
|
|
|
@ -37,8 +37,8 @@ class Dashboard::TodosController < Dashboard::ApplicationController
|
||||||
|
|
||||||
def todos_counts
|
def todos_counts
|
||||||
{
|
{
|
||||||
count: TodosFinder.new(current_user, state: :pending).execute.count,
|
count: current_user.todos_pending_count,
|
||||||
done_count: TodosFinder.new(current_user, state: :done).execute.count
|
done_count: current_user.todos_done_count
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
module TodosHelper
|
module TodosHelper
|
||||||
def todos_pending_count
|
def todos_pending_count
|
||||||
@todos_pending_count ||= TodosFinder.new(current_user, state: :pending).execute.count
|
@todos_pending_count ||= current_user.todos_pending_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def todos_done_count
|
def todos_done_count
|
||||||
@todos_done_count ||= TodosFinder.new(current_user, state: :done).execute.count
|
@todos_done_count ||= current_user.todos_done_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def todo_action_name(todo)
|
def todo_action_name(todo)
|
||||||
|
|
|
@ -809,13 +809,13 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
def todos_done_count(force: false)
|
def todos_done_count(force: false)
|
||||||
Rails.cache.fetch(['users', id, 'todos_done_count'], force: force) do
|
Rails.cache.fetch(['users', id, 'todos_done_count'], force: force) do
|
||||||
todos.done.count
|
TodosFinder.new(self, state: :done).execute.count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def todos_pending_count(force: false)
|
def todos_pending_count(force: false)
|
||||||
Rails.cache.fetch(['users', id, 'todos_pending_count'], force: force) do
|
Rails.cache.fetch(['users', id, 'todos_pending_count'], force: force) do
|
||||||
todos.pending.count
|
TodosFinder.new(self, state: :pending).execute.count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -144,8 +144,9 @@ class TodoService
|
||||||
def mark_todos_as_done(todos, current_user)
|
def mark_todos_as_done(todos, current_user)
|
||||||
todos = current_user.todos.where(id: todos.map(&:id)) unless todos.respond_to?(:update_all)
|
todos = current_user.todos.where(id: todos.map(&:id)) unless todos.respond_to?(:update_all)
|
||||||
|
|
||||||
todos.update_all(state: :done)
|
marked_todos = todos.update_all(state: :done)
|
||||||
current_user.update_todos_count_cache
|
current_user.update_todos_count_cache
|
||||||
|
marked_todos
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user marks an issue as todo
|
# When user marks an issue as todo
|
||||||
|
|
|
@ -61,9 +61,9 @@ module API
|
||||||
#
|
#
|
||||||
delete ':id' do
|
delete ':id' do
|
||||||
todo = current_user.todos.find(params[:id])
|
todo = current_user.todos.find(params[:id])
|
||||||
todo.done
|
TodoService.new.mark_todos_as_done([todo], current_user)
|
||||||
|
|
||||||
present todo, with: Entities::Todo, current_user: current_user
|
present todo.reload, with: Entities::Todo, current_user: current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
# Mark all todos as done
|
# Mark all todos as done
|
||||||
|
@ -73,9 +73,7 @@ module API
|
||||||
#
|
#
|
||||||
delete do
|
delete do
|
||||||
todos = find_todos
|
todos = find_todos
|
||||||
todos.each(&:done)
|
TodoService.new.mark_todos_as_done(todos, current_user)
|
||||||
|
|
||||||
todos.length
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -117,6 +117,12 @@ describe API::Todos, api: true do
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
expect(pending_1.reload).to be_done
|
expect(pending_1.reload).to be_done
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'updates todos cache' do
|
||||||
|
expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original
|
||||||
|
|
||||||
|
delete api("/todos/#{pending_1.id}", john_doe)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -139,6 +145,12 @@ describe API::Todos, api: true do
|
||||||
expect(pending_2.reload).to be_done
|
expect(pending_2.reload).to be_done
|
||||||
expect(pending_3.reload).to be_done
|
expect(pending_3.reload).to be_done
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'updates todos cache' do
|
||||||
|
expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original
|
||||||
|
|
||||||
|
delete api("/todos", john_doe)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -472,6 +472,42 @@ describe TodoService, services: true do
|
||||||
expect(john_doe.todos_pending_count).to eq(1)
|
expect(john_doe.todos_pending_count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#mark_todos_as_done' do
|
||||||
|
let(:issue) { create(:issue, project: project, author: author, assignee: john_doe) }
|
||||||
|
|
||||||
|
it 'marks a relation of todos as done' do
|
||||||
|
create(:todo, :mentioned, user: john_doe, target: issue, project: project)
|
||||||
|
|
||||||
|
todos = TodosFinder.new(john_doe, {}).execute
|
||||||
|
expect { TodoService.new.mark_todos_as_done(todos, john_doe) }
|
||||||
|
.to change { john_doe.todos.done.count }.from(0).to(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks an array of todos as done' do
|
||||||
|
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
|
||||||
|
|
||||||
|
expect { TodoService.new.mark_todos_as_done([todo], john_doe) }
|
||||||
|
.to change { todo.reload.state }.from('pending').to('done')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the number of updated todos' do # Needed on API
|
||||||
|
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
|
||||||
|
|
||||||
|
expect(TodoService.new.mark_todos_as_done([todo], john_doe)).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'caches the number of todos of a user', :caching do
|
||||||
|
create(:todo, :mentioned, user: john_doe, target: issue, project: project)
|
||||||
|
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
|
||||||
|
TodoService.new.mark_todos_as_done([todo], john_doe)
|
||||||
|
|
||||||
|
expect_any_instance_of(TodosFinder).not_to receive(:execute)
|
||||||
|
|
||||||
|
expect(john_doe.todos_done_count).to eq(1)
|
||||||
|
expect(john_doe.todos_pending_count).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def should_create_todo(attributes = {})
|
def should_create_todo(attributes = {})
|
||||||
attributes.reverse_merge!(
|
attributes.reverse_merge!(
|
||||||
project: project,
|
project: project,
|
||||||
|
|
Loading…
Reference in New Issue