Commit 1a184903 authored by Andy Soiron's avatar Andy Soiron Committed by Grzegorz Bizon

Update last_activity_on field on every API request

It updates User#last_activity_on, on every API request
when user is logged in and last_activity_on is a date
earlier than today
parent 203aaa57
---
title: Log user last activity on REST API
merge_request: 21725
author:
type: fixed
...@@ -1372,6 +1372,7 @@ The activities that update the timestamp are: ...@@ -1372,6 +1372,7 @@ The activities that update the timestamp are:
- Git HTTP/SSH activities (such as clone, push) - Git HTTP/SSH activities (such as clone, push)
- User logging in into GitLab - User logging in into GitLab
- User visiting pages related to Dashboards, Projects, Issues and Merge Requests ([introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/54947) in GitLab 11.8) - User visiting pages related to Dashboards, Projects, Issues and Merge Requests ([introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/54947) in GitLab 11.8)
- User using the API
By default, it shows the activity for all users in the last 6 months, but this can be By default, it shows the activity for all users in the last 6 months, but this can be
amended by using the `from` parameter. amended by using the `from` parameter.
......
...@@ -26,3 +26,4 @@ How do we measure the activity of users? GitLab considers a user active if: ...@@ -26,3 +26,4 @@ How do we measure the activity of users? GitLab considers a user active if:
- The user signs in. - The user signs in.
- The user has Git activity (whether push or pull). - The user has Git activity (whether push or pull).
- The user visits pages related to Dashboards, Projects, Issues and Merge Requests ([introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/54947) in GitLab 11.8). - The user visits pages related to Dashboards, Projects, Issues and Merge Requests ([introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/54947) in GitLab 11.8).
- The user uses the API
...@@ -99,6 +99,8 @@ module API ...@@ -99,6 +99,8 @@ module API
at_least_one_of :title, :description, :start_date_fixed, :start_date_is_fixed, :due_date_fixed, :due_date_is_fixed, :labels, :state_event at_least_one_of :title, :description, :start_date_fixed, :start_date_is_fixed, :due_date_fixed, :due_date_is_fixed, :labels, :state_event
end end
put ':id/(-/)epics/:epic_iid' do put ':id/(-/)epics/:epic_iid' do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab/issues/194104')
authorize_can_admin! authorize_can_admin!
update_params = declared_params(include_missing: false) update_params = declared_params(include_missing: false)
update_params.delete(:epic_iid) update_params.delete(:epic_iid)
......
...@@ -103,94 +103,101 @@ module API ...@@ -103,94 +103,101 @@ module API
helpers ::API::Helpers helpers ::API::Helpers
helpers ::API::Helpers::CommonHelpers helpers ::API::Helpers::CommonHelpers
# Keep in alphabetical order namespace do
mount ::API::AccessRequests after do
mount ::API::Appearance ::Users::ActivityService.new(@current_user).execute if Feature.enabled?(:api_activity_logging)
mount ::API::Applications end
mount ::API::Avatar
mount ::API::AwardEmoji # Keep in alphabetical order
mount ::API::Badges mount ::API::AccessRequests
mount ::API::Boards mount ::API::Appearance
mount ::API::Branches mount ::API::Applications
mount ::API::BroadcastMessages mount ::API::Avatar
mount ::API::Commits mount ::API::AwardEmoji
mount ::API::CommitStatuses mount ::API::Badges
mount ::API::DeployKeys mount ::API::Boards
mount ::API::Deployments mount ::API::Branches
mount ::API::Environments mount ::API::BroadcastMessages
mount ::API::ErrorTracking mount ::API::Commits
mount ::API::Events mount ::API::CommitStatuses
mount ::API::Features mount ::API::DeployKeys
mount ::API::Files mount ::API::Deployments
mount ::API::GroupBoards mount ::API::Environments
mount ::API::GroupClusters mount ::API::ErrorTracking
mount ::API::GroupExport mount ::API::Events
mount ::API::GroupLabels mount ::API::Features
mount ::API::GroupMilestones mount ::API::Files
mount ::API::Groups mount ::API::GroupBoards
mount ::API::GroupContainerRepositories mount ::API::GroupClusters
mount ::API::GroupVariables mount ::API::GroupExport
mount ::API::ImportGithub mount ::API::GroupLabels
mount ::API::GroupMilestones
mount ::API::Groups
mount ::API::GroupContainerRepositories
mount ::API::GroupVariables
mount ::API::ImportGithub
mount ::API::Issues
mount ::API::JobArtifacts
mount ::API::Jobs
mount ::API::Keys
mount ::API::Labels
mount ::API::Lint
mount ::API::Markdown
mount ::API::Members
mount ::API::MergeRequestDiffs
mount ::API::MergeRequests
mount ::API::Namespaces
mount ::API::Notes
mount ::API::Discussions
mount ::API::ResourceLabelEvents
mount ::API::NotificationSettings
mount ::API::Pages
mount ::API::PagesDomains
mount ::API::Pipelines
mount ::API::PipelineSchedules
mount ::API::ProjectClusters
mount ::API::ProjectContainerRepositories
mount ::API::ProjectEvents
mount ::API::ProjectExport
mount ::API::ProjectImport
mount ::API::ProjectHooks
mount ::API::ProjectMilestones
mount ::API::Projects
mount ::API::ProjectSnapshots
mount ::API::ProjectSnippets
mount ::API::ProjectStatistics
mount ::API::ProjectTemplates
mount ::API::ProtectedBranches
mount ::API::ProtectedTags
mount ::API::Releases
mount ::API::Release::Links
mount ::API::RemoteMirrors
mount ::API::Repositories
mount ::API::Runner
mount ::API::Runners
mount ::API::Search
mount ::API::Services
mount ::API::Settings
mount ::API::SidekiqMetrics
mount ::API::Snippets
mount ::API::Statistics
mount ::API::Submodules
mount ::API::Subscriptions
mount ::API::Suggestions
mount ::API::SystemHooks
mount ::API::Tags
mount ::API::Templates
mount ::API::Todos
mount ::API::Triggers
mount ::API::UserCounts
mount ::API::Users
mount ::API::Variables
mount ::API::Version
mount ::API::Wikis
end
mount ::API::Internal::Base mount ::API::Internal::Base
mount ::API::Internal::Pages mount ::API::Internal::Pages
mount ::API::Issues
mount ::API::JobArtifacts
mount ::API::Jobs
mount ::API::Keys
mount ::API::Labels
mount ::API::Lint
mount ::API::Markdown
mount ::API::Members
mount ::API::MergeRequestDiffs
mount ::API::MergeRequests
mount ::API::Namespaces
mount ::API::Notes
mount ::API::Discussions
mount ::API::ResourceLabelEvents
mount ::API::NotificationSettings
mount ::API::Pages
mount ::API::PagesDomains
mount ::API::Pipelines
mount ::API::PipelineSchedules
mount ::API::ProjectClusters
mount ::API::ProjectContainerRepositories
mount ::API::ProjectEvents
mount ::API::ProjectExport
mount ::API::ProjectImport
mount ::API::ProjectHooks
mount ::API::ProjectMilestones
mount ::API::Projects
mount ::API::ProjectSnapshots
mount ::API::ProjectSnippets
mount ::API::ProjectStatistics
mount ::API::ProjectTemplates
mount ::API::ProtectedBranches
mount ::API::ProtectedTags
mount ::API::Releases
mount ::API::Release::Links
mount ::API::RemoteMirrors
mount ::API::Repositories
mount ::API::Runner
mount ::API::Runners
mount ::API::Search
mount ::API::Services
mount ::API::Settings
mount ::API::SidekiqMetrics
mount ::API::Snippets
mount ::API::Statistics
mount ::API::Submodules
mount ::API::Subscriptions
mount ::API::Suggestions
mount ::API::SystemHooks
mount ::API::Tags
mount ::API::Templates
mount ::API::Todos
mount ::API::Triggers
mount ::API::UserCounts
mount ::API::Users
mount ::API::Variables
mount ::API::Version
mount ::API::Wikis
route :any, '*path' do route :any, '*path' do
error!('404 Not Found', 404) error!('404 Not Found', 404)
......
# frozen_string_literal: true
require 'spec_helper'
describe API::API do
let(:user) { create(:user, last_activity_on: Date.yesterday) }
describe 'Record user last activity in after hook' do
# It does not matter which endpoint is used because last_activity_on should
# be updated on every request. `/groups` is used as an example
# to represent any API endpoint
it 'updates the users last_activity_on date' do
expect { get api('/groups', user) }.to change { user.reload.last_activity_on }.to(Date.today)
end
context 'when the the api_activity_logging feature is disabled' do
it 'does not touch last_activity_on' do
stub_feature_flags(api_activity_logging: false)
expect { get api('/groups', user) }.not_to change { user.reload.last_activity_on }
end
end
end
end
...@@ -148,6 +148,7 @@ describe API::ProjectContainerRepositories do ...@@ -148,6 +148,7 @@ describe API::ProjectContainerRepositories do
let(:lease_key) { "container_repository:cleanup_tags:#{root_repository.id}" } let(:lease_key) { "container_repository:cleanup_tags:#{root_repository.id}" }
it 'schedules cleanup of tags repository' do it 'schedules cleanup of tags repository' do
stub_last_activity_update
stub_exclusive_lease(lease_key, timeout: 1.hour) stub_exclusive_lease(lease_key, timeout: 1.hour)
expect(CleanupContainerRepositoryWorker).to receive(:perform_async) expect(CleanupContainerRepositoryWorker).to receive(:perform_async)
.with(maintainer.id, root_repository.id, worker_params) .with(maintainer.id, root_repository.id, worker_params)
......
...@@ -46,4 +46,8 @@ module ApiHelpers ...@@ -46,4 +46,8 @@ module ApiHelpers
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.map { |item| item['id'] }).to eq(Array(items)) expect(json_response.map { |item| item['id'] }).to eq(Array(items))
end end
def stub_last_activity_update
allow_any_instance_of(Users::ActivityService).to receive(:execute)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment