Skip to content

OTWO-6680 - Ability to put Openhub into read only state #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
23aab3b
OTWO-6680 - Ability to put Openhub into read only state
sig-krasnick May 10, 2022
1676ea4
OTWO-6680 - Added route
sig-krasnick May 10, 2022
3bd38a9
OTWO-6680 changes to expiration after 3 weeks
sig-krasnick May 11, 2022
41e62cf
OTWO-6680 Fix to expiration comparison
sig-krasnick May 11, 2022
223ebdf
OTWO-6680 - Added tests to account and clearance
sig-krasnick May 16, 2022
e5aa408
OTWO-6680 - Fix to tests
sig-krasnick May 16, 2022
f107037
OTWO-6680 - second attempt to fix tests
sig-krasnick May 16, 2022
19b2428
OTWO-6680 - another fix to rubocop
sig-krasnick May 16, 2022
56490be
OTWO-openhub_restarts (#1611)
sig-krasnick May 17, 2022
8c061a7
OTWO-6672 - Rubocop change (#1612)
sig-krasnick May 17, 2022
ce9b5f4
OTWO-6695 Print errors to stdout
May 19, 2022
cede85a
Fixed failing test
May 19, 2022
5d7ac90
Merge pull request #1614 from blackducksoftware/OTWO-6695
May 20, 2022
9ca677c
OTWO-6700 Admin page improvement
May 25, 2022
30c0900
OTWO-6680 Changes for test coverage
sig-krasnick May 25, 2022
e53bcc1
OTWO-6700 coverage test has added for dashboar
May 26, 2022
b69dc47
Merge pull request #1616 from blackducksoftware/OTWO-6700
May 27, 2022
c7bb27f
OTWO-6708 Updated Sidekiq gem (#1618)
May 27, 2022
67afa70
OTWO-6706 removed KUBERENETS_PORT from code (#1620)
Niharika1117 Jun 7, 2022
bd038c7
OTWO-6718 Handle nil thirty_day_summary for active project (#1621)
notalex Jun 8, 2022
132c7bb
OTWO-6680 - Ability to put Openhub into read only state
sig-krasnick May 10, 2022
1c4ea97
OTWO-6680 - Added route
sig-krasnick May 10, 2022
86b4baf
OTWO-6680 changes to expiration after 3 weeks
sig-krasnick May 11, 2022
1c1482d
OTWO-6680 Fix to expiration comparison
sig-krasnick May 11, 2022
477970c
OTWO-6680 - Added tests to account and clearance
sig-krasnick May 16, 2022
e7d271e
OTWO-6680 - Fix to tests
sig-krasnick May 16, 2022
cf33efc
OTWO-6680 - second attempt to fix tests
sig-krasnick May 16, 2022
fe556b3
OTWO-6680 - another fix to rubocop
sig-krasnick May 16, 2022
53f7c1f
OTWO-6680 Changes for test coverage
sig-krasnick May 25, 2022
e1b6e0b
OTWO-6680 correct route path
sig-krasnick May 26, 2022
65598c7
OTWO-6680 - Fix to tests
sig-krasnick Jun 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ OHLOH_ANALYTICS_CLIENT_REGISTRATION_ID=

JWT_SECRET=
COVERITY_SCAN_URL = 'https://scan.coverity.com'

EXPIRATION_DAYS=21
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ end

group :test do
gem 'haml_lint', '~> 0.21'
gem 'm', '~> 1.5.0'
gem 'minitest-rails'
gem 'minitest-spec-rails'
gem 'mocha'
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ GEM
sax-machine (1.3.2)
set (1.0.2)
sexp_processor (4.16.0)
sidekiq (6.4.0)
sidekiq (6.4.2)
connection_pool (>= 2.2.2)
rack (~> 2.0)
redis (>= 4.2.0)
Expand Down
9 changes: 9 additions & 0 deletions app/admin/accounts_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
filter :last_seen_ip
filter :created_at

action_item :maintenance, only: :index do
link_to 'Set Maintenance Mode', maintenance_admin_accounts_path
end

index do
column :id
column :name do |account|
Expand Down Expand Up @@ -64,4 +68,9 @@
end
f.actions
end

collection_action :maintenance do
Account.maintenance
redirect_to_saved_path(notice: 'Accounts successfully logged out')
end
end
13 changes: 6 additions & 7 deletions app/assets/javascripts/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ var Charts = {
var first_day = new Date(this.value)
var last_day = new Date(first_day.getFullYear(), first_day.getMonth()+1, 0);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
return '';
// if (months.includes(this.value.split(' ')[0]))
// return '<a href="/admin/accounts?commit=Filter&q[created_at_gteq_datetime]=' + first_day + '&q[created_at_lteq_datetime]=' + last_day + '" target="_blank">' +
// this.value + '</a>';
// else
// return '<a href="/admin/accounts?commit=Filter&q[created_at_gteq_datetime]=' + this.value + '&q[created_at_lteq_datetime]=' + this.value + '" target="_blank">' +
// this.value + '</a>';
if (months.includes(this.value.split(' ')[0]))
return '<a href="/admin/accounts?commit=Filter&q[created_at_gteq_datetime]=' + first_day + '&q[created_at_lteq_datetime]=' + last_day + '" target="_blank">' +
this.value + '</a>';
else
return '<a href="/admin/accounts?commit=Filter&q[created_at_gteq_datetime]=' + this.value + '&q[created_at_lteq_datetime]=' + this.value + '" target="_blank">' +
this.value + '</a>';
}});
}
data.chart.renderTo = chart;
Expand Down
11 changes: 2 additions & 9 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,7 @@ def access_denied
end

def report_errors(exception)
if ENV['KUBERNETES_PORT']
notify_airbrake(exception)
else
logger = Logger.new(Rails.root.join('log/errors.log'))
logger.error(exception.message)
logger.error(exception.backtrace)
logger.error('-' * 150)
logger.close
end
notify_airbrake(exception)
end

def set_session_projects
Expand Down Expand Up @@ -333,6 +325,7 @@ def check_project_authorization

def update_last_seen_at_and_ip
return unless logged_in?
return if expired_token?

current_user.update_columns(last_seen_at: Time.current, last_seen_ip: request.remote_ip)
end
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/concerns/clearance_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def current_user
super || NilAccount.new
end

def expired_token?
return false unless current_user&.last_seen_at && current_user.last_seen_at < expiration_days.days.ago

current_user.reset_remember_token!
request.env[:clearance].sign_out
true
end

def expiration_days
ENV['EXPRIATION_DAYS'] ? ENV['EXPRIATION_DAYS'].to_i : 21 # default to 3 weeks
end

private

def sign_in_url
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ def create
end

def health
if ApplicationRecord.connected?
render plain: Time.current
else
head :internal_server_error
end
render plain: Time.current
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/decorators/home_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def most_active_contributors
def commit_count
projects = most_active_projects
map = projects.map do |project|
project.best_analysis.thirty_day_summary.commits_count if project.best_analysis.present?
project.best_analysis&.thirty_day_summary&.commits_count
end
map.compact
end
Expand Down
43 changes: 31 additions & 12 deletions app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,47 @@ def get_revision_details
end

def accounts_count(level)
number_with_delimiter(Account.where(level: level).count)
Rails.cache.fetch("Admin-accounts-count-cache_#{level}", expires_in: 1.day) do
number_with_delimiter(Account.group(:level).size[level])
end
end

def updated_projects_count(from, to = nil)
from = convert_to_datetime(from)
to = convert_to_datetime(to) || Time.current
projects_count = Project.active_enlistments.joins(:best_analysis)
.where(analyses: { updated_on: from..to }).distinct.count
def days_projects_count
projects_count = Rails.cache.fetch('Admin-updated-project-count-cache')
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
end

def outdated_projects(date)
projects_count = Project.active_enlistments.joins(:best_analysis)
.where('analyses.updated_on < ?', date).distinct.count
def weeks_projects_count
projects_count = Rails.cache.fetch('Admin-updated-project-count-cache')
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
end

def convert_to_datetime(value)
Time.current.ago(value).utc if value
def outdated_projects
projects_count = Rails.cache.fetch('Admin-outdated-project-count-cache') || 0
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
end

def active_projects_count
Project.active_enlistments.distinct.count
Rails.cache.fetch('Admin-active-project-count-cache') { Project.active_enlistments.distinct.size }
end

def analyses_count
Rails.cache.fetch('Admin-project-analyses-count-cache') || 0
end

def project_count
Rails.cache.fetch('Admin-project-count-cache') { Project.active.size }
end

def admin_project_trends
Rails.cache.fetch 'admin_project_trend', expires_in: 1.day do
render partial: 'project_trend_graph'
end
end

def admin_accounts_trends
Rails.cache.fetch 'admin_accounts_trend', expires_in: 1.day do
render partial: 'accounts_trend_graph'
end
end
end
4 changes: 2 additions & 2 deletions app/helpers/statsd_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module StatsdHelper
def statsd_increment(msg)
StatsD.increment(msg) unless ENV['KUBERNETES_PORT']
StatsD.increment(msg) if Rails.env.development?
end

def statsd_set(msg, params)
StatsD.set(msg, params) unless ENV['KUBERNETES_PORT']
StatsD.set(msg, params) if Rails.env.development?
end
end
9 changes: 4 additions & 5 deletions app/lib/data_dog_report.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# frozen_string_literal: true

# rubocop:disable Rails/Output

module DataDogReport
module_function

def error(message)
request = DatadogAPIClient::V1::EventCreateRequest.new(text: message.truncate(4000),
title: "OpenHub #{Rails.env} Error",
alert_type: :error, date_happened: Time.current.to_i,
host: ENV['HOSTNAME'])
api_instance.create_event(request)
puts message
end

def api_instance
DatadogAPIClient::V1::EventsAPI.new
end
end
# rubocop:enable Rails/Output
4 changes: 4 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,9 @@ def fetch_by_login_or_email(username_or_email)
def find_or_create_anonymous_account
find_by(login: AnonymousAccount::LOGIN) || AnonymousAccount.create!
end

def maintenance
logged_in.find_each(&:reset_remember_token!)
end
end
end
2 changes: 2 additions & 0 deletions app/models/concerns/account_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module AccountScopes
ANONYMOUS_ACCOUNTS = %w[anonymous_coward ohloh_slave uber_data_crawler].freeze
ANONYMOUS_ACCOUNTS_EMAILS = %w[anon@openhub.net info@openhub.net uber_data_crawler@openhub.net].freeze
EXPIRATION_DAYS = (ENV['EXPIRATION_DAYS'].to_i || 21).days

extend ActiveSupport::Concern

Expand Down Expand Up @@ -33,6 +34,7 @@ module AccountScopes
scope :in_good_standing, -> { where('level >= 0') }
scope :from_param, ->(param) { in_good_standing.where(arel_table[:login].eq(param).or(arel_table[:id].eq(param))) }
scope :active, -> { where(level: 0) }
scope :logged_in, -> { where("last_seen_at > '#{EXPIRATION_DAYS.ago}'") }
scope :non_anonymous, -> { where.not(login: ANONYMOUS_ACCOUNTS, email: ANONYMOUS_ACCOUNTS_EMAILS) }

scope :reverification_not_initiated, lambda { |limit = 0|
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/project_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ module ProjectScopes
.having('count(*) >= ?', tags.split.flatten.length)
}
scope :with_analysis, -> { active.where.not(best_analysis_id: nil) }
scope :active_enlistments, -> { active.joins(:enlistments).where(enlistments: { deleted: false }) }
scope :active_enlistments, -> { active.joins(:enlistments) }
end
end
26 changes: 26 additions & 0 deletions app/views/oh_admin/dashboard/_accounts_trend_graph.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:ruby
chart_url = charts_oh_admin_accounts_url.to_s

#three_months.account
%h1 3 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=3' + '&filter_by=weekly' }

#six_months.account
%h1 6 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=6' + '&filter_by=weekly'}

#one_year.account
%h1 1 Year Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=12' + '&filter_by=weekly'}

#three_months_monthly.account
%h1 3 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=3' + '&filter_by=monthly'}

#six_months_monthly.account
%h1 6 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=6' + '&filter_by=monthly'}

#one_year_monthly.account
%h1 1 Year Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=12' + '&filter_by=monthly'}
26 changes: 26 additions & 0 deletions app/views/oh_admin/dashboard/_project_trend_graph.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:ruby
project_chart_url = charts_oh_admin_projects_url.to_s

#three_months_project.project
%h1 3 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=3' + '&filter_by=weekly' }

#six_months_project.project
%h1 6 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=6' + '&filter_by=weekly'}

#one_year_project.project
%h1 1 Year Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=12' + '&filter_by=weekly'}

#three_months_monthly_project.project
%h1 3 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=3' + '&filter_by=monthly'}

#six_months_monthly_project.project
%h1 6 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=6' + '&filter_by=monthly'}

#one_year_monthly_project.project
%h1 1 Year Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=12' + '&filter_by=monthly'}
55 changes: 3 additions & 52 deletions app/views/oh_admin/dashboard/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
'Projects with enlistments & Best Analysis (Not updated since 2 weeks)']
project_colors = ['bg-aqua', 'bg-green', 'bg-light-blue-active', 'bg-green', 'bg-yellow', 'bg-red']
project_icons = ['fa-area-chart', 'fa-bar-chart', 'fa-shield']
analyses_count = Analysis.where(updated_on: get_window.utc..Time.current).count
project_values = [analyses_count, Project.active.count, active_projects_count, updated_projects_count(3.days),
updated_projects_count(2.weeks, 3.days), outdated_projects(2.weeks.ago.to_date)]
project_values = [analyses_count, project_count, active_projects_count, days_projects_count, weeks_projects_count, outdated_projects]

last_activity_icons = ['fa-calendar', 'fa-check-square', 'fa-server']
last_activity_text = ['Monthly Language Analysis', 'CII Projects', 'Deployment']
last_activity_info = [Analysis::MonthlyLanguage.last_run, ProjectBadge.check_cii_projects_last_run, last_deployment]
last_activity_bg = ['bg-aqua', 'bg-green', 'bg-yellow']

chart_url = charts_oh_admin_accounts_url.to_s
project_chart_url = charts_oh_admin_projects_url.to_s

%h1 Project Stats Overview
%p
.row
Expand Down Expand Up @@ -58,29 +53,7 @@
%input{name: "project_filter", type: "radio", value: "monthly"}
by monthly

#three_months_project.project
%h1 3 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=3' + '&filter_by=weekly' }

#six_months_project.project
%h1 6 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=6' + '&filter_by=weekly'}

#one_year_project.project
%h1 1 Year Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=12' + '&filter_by=weekly'}

#three_months_monthly_project.project
%h1 3 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=3' + '&filter_by=monthly'}

#six_months_monthly_project.project
%h1 6 Months Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=6' + '&filter_by=monthly'}

#one_year_monthly_project.project
%h1 1 Year Projects Creation Trend
.chart{ 'datasrc' => project_chart_url + '?period=12' + '&filter_by=monthly'}
= admin_project_trends

%h1 Accounts Overview
%p
Expand Down Expand Up @@ -114,29 +87,7 @@
%input{name: "radio", type: "radio", value: "monthly"}
by monthly

#three_months.account
%h1 3 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=3' + '&filter_by=weekly' }

#six_months.account
%h1 6 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=6' + '&filter_by=weekly'}

#one_year.account
%h1 1 Year Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=12' + '&filter_by=weekly'}

#three_months_monthly.account
%h1 3 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=3' + '&filter_by=monthly'}

#six_months_monthly.account
%h1 6 Months Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=6' + '&filter_by=monthly'}

#one_year_monthly.account
%h1 1 Year Accounts Creation Trend
.chart{ 'datasrc' => chart_url + '?period=12' + '&filter_by=monthly'}
= admin_accounts_trends


%h1 Last Activities
Expand Down
Loading