-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add cronjob to backfill corporate partner user groups' memberships and classifications #57
Merged
+241
−3
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7367cfa
update comments on backfill scripts to give context of what previous …
yuenmichelle1 ace522e
adding comments for context on old scripts
yuenmichelle1 2827e81
update backfill script in ruby
yuenmichelle1 82a65c6
Merge branch 'main' into add-cron-scripts
yuenmichelle1 4b8f5ea
update env variables to use Rails.app.credentials. attempt at cron jo…
yuenmichelle1 f29f610
Update cron_sync.yml
yuenmichelle1 0d4e2a6
Update user_group_membership_classification_backfill.py
yuenmichelle1 2c335ad
update panoptes_membership_client.rb to create insert query using arr…
yuenmichelle1 bf12607
adding a more descriptive comment insert query
yuenmichelle1 f06f187
add manual sync
yuenmichelle1 fe179a9
Update .github/workflows/manual_corp_user_group_sync.yml
yuenmichelle1 a5ed815
Update .github/workflows/manual_corp_user_group_sync.yml
yuenmichelle1 83739df
Update .github/workflows/manual_corp_user_group_sync.yml
yuenmichelle1 85453a9
Update .github/workflows/manual_corp_user_group_sync.yml
yuenmichelle1 90d15c5
Update .github/workflows/manual_corp_user_group_sync.yml
yuenmichelle1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Manually Sync Eras Corporate User Group Sync | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
manual_sync: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/[email protected] | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/[email protected] | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: azure/login@v2 | ||
with: | ||
creds: ${{ secrets.AZURE_AKS }} | ||
|
||
- name: Set the target AKS cluster | ||
uses: Azure/[email protected] | ||
with: | ||
cluster-name: microservices | ||
resource-group: kubernetes | ||
|
||
- name: Modify & apply template | ||
run: kubectl create -f kubernetes/manual_corp_user_group_sync.yml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lCfkIMPH/fwl6Cf1K+EWfeiHK/ITstJhGzRXMTS7XqvUG2odRAmlIlbRDibfLoGPj7kPMJzwvnpHgZN2ey09RS1dp3ZzrorY6JC3gnnsrB9rX0+dLZihtj+Tlvtzwd8H4Yv3OM3um0jWr9DD3sOq4N3a7kSm1Wqkr+UqqGrWLusESvEeDV/c6sJkjjI8q0Sbv34Mc3inSOZVcIk1DocxneYA1dSWDOWqbhF1vd2p53ubqGJIq3RPQEt7881IECek3iDuBXPPVaw6H2KaerBv0utnMhmST3mcNtAeAEzWcw/j8TztMqvaFowmp5+lo0jNJR4wNOUQaT7XiOtRFZfA6i9T0gUtqAz+gZxJshRarYpbKUb703PvQ4WAsh2Fs9g+v0PPuW2sSK6tfEzn84c9EQUCeQVKTeATsQlXAGuO9vYdKTw0ahnJzdFfql5LXUH7VJ+12cNbOanjVFjykcs3qY2N69PK1/ESAPqkyK8Fe0fosfAepUVm3rd7VLNMc2qGdWAxTAfdj59xYNaGU97+xRSATBMUdsGNoFcyv7WTUUxcn01Qz9WqyLdCzyIABEwvcXWtc3pNJ3U7bwC0fsqQkL+zqt883eYGDxU4Mn0H5IX21OfqTYDuPB3mQltuQe2KUq2KZcCLtBVce0n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i0EKA5e8hjGt1roAgGh51/sezhrLVGmAeIPodxbxbWOWH3UTt+HUP5QpnRq8k2qILk4FuiFRlHUTNLvVtSdk1Z8XybLwmYMdoGTSQjD1mSmVSouA==--UPEZvym9PPXcBjaM--xrk0OyBCCPOBgaRp2gjxIg== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: eras-corp-partners-cron-sync-job | ||
spec: | ||
schedule: "0 3 * * *" | ||
jobTemplate: | ||
spec: | ||
template: | ||
metadata: | ||
name: eras-corp-partners-sync | ||
spec: | ||
containers: | ||
- name: eras-corp-partners-sync | ||
image: ghcr.io/zooniverse/eras | ||
env: | ||
- name: RAILS_LOG_TO_STDOUT | ||
value: "true" | ||
- name: RAILS_ENV | ||
value: production | ||
- name: RAILS_MASTER_KEY | ||
valueFrom: | ||
secretKeyRef: | ||
name: eras-production | ||
key: rails-master-key | ||
command: ['ruby', './scripts/user_group_membership_classification_backfill.rb'] | ||
restartPolicy: Never | ||
backoffLimit: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
generateName: eras-corp-partners-sync- | ||
spec: | ||
template: | ||
metadata: | ||
name: eras-corp-partners-sync | ||
spec: | ||
containers: | ||
- name: eras-corp-partners-sync | ||
image: ghcr.io/zooniverse/eras | ||
env: | ||
- name: RAILS_LOG_TO_STDOUT | ||
value: "true" | ||
- name: RAILS_ENV | ||
value: production | ||
- name: RAILS_MASTER_KEY | ||
valueFrom: | ||
secretKeyRef: | ||
name: eras-production | ||
key: rails-master-key | ||
command: ['ruby', './scripts/user_group_membership_classification_backfill.rb'] | ||
restartPolicy: Never | ||
backoffLimit: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pg' | ||
require '../config/environment' | ||
|
||
class PanoptesMembershipClient | ||
def user_ids_not_in_user_group(user_group_id, domain_formats) | ||
conn.exec( | ||
"SELECT id FROM users | ||
WHERE email ILIKE ANY(STRING_TO_ARRAY('#{domain_formats.join(',')}', ',')) | ||
AND id NOT IN (SELECT user_id FROM memberships where user_group_id=#{user_group_id}) | ||
" | ||
).entries.map { |res| res['id'].to_i } | ||
end | ||
|
||
def insert_memberships(user_group_id, user_ids) | ||
memberships_to_create = user_memberships(user_group_id, user_ids) | ||
|
||
member_creation_sql_query = memberships_insert_query(memberships_to_create) | ||
|
||
conn.exec_params(member_creation_sql_query, memberships_to_create.flatten) | ||
end | ||
|
||
private | ||
|
||
def conn | ||
@conn ||= PG.connect(Rails.application.credentials.panoptes_db_uri, sslmode: 'require') | ||
end | ||
|
||
def user_memberships(user_group_id, user_ids) | ||
memberships_to_create = [] | ||
user_ids.each do |user_id| | ||
# membership in array order: user_id, user_group_id, state, roles | ||
membership = [ | ||
user_id, | ||
user_group_id, | ||
0, | ||
'{"group_member"}' | ||
] | ||
memberships_to_create << membership | ||
end | ||
memberships_to_create | ||
end | ||
|
||
def memberships_insert_query(memberships_to_create) | ||
# Values is a string that will look like ($1, $2, $3, $4), ($5, $6, $7, $8), ..etc.. | ||
values = Array.new(memberships_to_create.length) do |i| | ||
"($#{(4 * i) + 1}, $#{(4 * i) + 2}, $#{(4 * i) + 3}, $#{(4 * i) + 4})" | ||
end.join(',') | ||
"INSERT INTO memberships (user_id, user_group_id, state, roles) VALUES #{values}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require '../config/environment' | ||
require './panoptes_membership_client' | ||
require 'json' | ||
|
||
corporate_user_groups_str = Rails.application.credentials.corporate_user_groups | ||
yuenmichelle1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
corporate_partners = JSON.parse(corporate_user_groups_str) | ||
|
||
puts 'Starting Classification and Membership Backfill...' | ||
|
||
panoptes_client = PanoptesMembershipClient.new | ||
|
||
corporate_partners.each do |corporate_partner| | ||
yuenmichelle1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
puts "Geting Ids of users that are not in group yet for #{corporate_partner['corp_name']}..." | ||
not_yet_member_user_ids = panoptes_client.user_ids_not_in_user_group(corporate_partner['user_group_id'], corporate_partner['domain_formats']) | ||
|
||
puts "Query found #{not_yet_member_user_ids.length} users not in the #{corporate_partner['corp_name']} user_group..." | ||
|
||
next unless not_yet_member_user_ids.length.positive? | ||
|
||
puts "Creating Memberships for #{corporate_partner['corp_name']}..." | ||
panoptes_client.insert_memberships(corporate_partner['user_group_id'], not_yet_member_user_ids) | ||
|
||
puts 'Querying Eras ClassificationEvents of newly created members...' | ||
classification_events_to_backfill = ClassificationEvent.where('user_id IN (?)', not_yet_member_user_ids) | ||
|
||
next unless classification_events_to_backfill.length.positive? | ||
|
||
puts 'Creating Classification User Groups...' | ||
classification_user_groups_to_create = [] | ||
classification_events_to_backfill.each do |classification| | ||
classification_user_group = { | ||
classification_id: classification.classification_id, | ||
event_time: classification.event_time, | ||
project_id: classification.project_id, | ||
workflow_id: classification.workflow_id, | ||
user_id: classification.user_id, | ||
session_time: classification.session_time, | ||
user_group_id: corporate_partner['user_group_id'] | ||
} | ||
classification_user_groups_to_create << classification_user_group | ||
end | ||
|
||
ClassificationUserGroup.upsert_all(classification_user_groups_to_create, | ||
unique_by: %i[classification_id event_time user_group_id user_id]) | ||
|
||
puts 'ClassificationUserGroup Upsert Finished...' | ||
end | ||
|
||
today = Date.today.to_s | ||
two_days_ago = (Date.today - 2).to_s | ||
puts 'Classification and Membership Backfill Finished. Starting CA Refresh...' | ||
puts 'Refreshing Continuous Aggregates dealing with User Groups...' | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time Per Project...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time_per_project', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time Per User...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time_per_user', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time Per User And Project...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time_per_user_per_project', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time Per User And Workflow...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time_per_user_per_workflow', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Refreshing Daily Group Classifications Count And Time Per Workflow...' | ||
ActiveRecord::Base.connection.exec_query("CALL refresh_continuous_aggregate('daily_group_classification_count_and_time_per_workflow', '#{two_days_ago}', '#{today}')") | ||
|
||
puts 'Stats User Group Membership and Classification Backfill Completed' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like kind of a kludgy way to build a parameterized query. Since this doesn't take any input, I don't feel like you gotta use
conn.exec_params
instead of just building the query string directly. Even though it took me second to parse this, I'm still only like 3/10 on it.