cleanup(sendinblue): remove sendinblue tracking

This commit is contained in:
Paul Chavard 2022-05-05 16:54:30 +02:00
parent b629db2b51
commit 9e0b3b642f
7 changed files with 2 additions and 440 deletions

View file

@ -159,7 +159,6 @@ class ApplicationController < ActionController::Base
gon.sentry = sentry_config
if administrateur_signed_in?
gon.sendinblue = sendinblue_config
gon.crisp = crisp_config
end
end
@ -285,26 +284,6 @@ class ApplicationController < ActionController::Base
}
end
def sendinblue_config
sendinblue = Rails.application.secrets.sendinblue
{
key: sendinblue[:client_key],
enabled: sendinblue[:enabled],
administrateur: {
email: current_user&.email,
payload: {
DS_SIGN_IN_COUNT: current_user&.sign_in_count,
DS_CREATED_AT: current_administrateur&.created_at,
DS_ACTIVE: current_user&.active?,
DS_ID: current_administrateur&.id,
DS_GESTIONNAIRE_ID: current_instructeur&.id,
DS_ROLES: current_user_roles
}
}
}
end
def crisp_config
crisp = Rails.application.secrets.crisp

View file

@ -1,2 +1 @@
import '../shared/track/sendinblue';
import '../shared/track/crisp';

View file

@ -1,22 +0,0 @@
const { key, enabled, administrateur } = gon.sendinblue || {};
if (enabled) {
window.sib = {
equeue: [],
client_key: key,
email_id: administrateur.email
};
const script = document.createElement('script');
const firstScript = document.getElementsByTagName('script')[0];
script.type = 'text/javascript';
script.id = 'sendinblue-js';
script.async = true;
script.src = `https://sibautomation.com/sa.js?key=${window.sib.client_key}`;
firstScript.parentNode.insertBefore(script, firstScript);
window.sib.equeue.push({ page: [] });
window.sib.equeue.push({
identify: [administrateur.email, administrateur.payload]
});
}

View file

@ -1,7 +0,0 @@
class Cron::UpdateAdministrateurUsageStatisticsJob < Cron::CronJob
self.schedule_expression = "every day at 10 am"
def perform
AdministrateurUsageStatisticsService.new.update_administrateurs
end
end

View file

@ -1,163 +0,0 @@
# Note: this class uses a `synthetic_state` for Dossier, that diverges from the standard state:
# - 'termine' is the synthetic_state for all dossiers
# whose state is 'accepte', 'refuse' or 'sans_suite',
# even when `archive` is true
# - 'archive' is the synthetic_state for all dossiers
# where archive is true,
# except those whose synthetic_state is already 'termine'
# - For all other dossiers, the synthetic_state and the state are the same
class AdministrateurUsageStatisticsService
def update_administrateurs
Administrateur.includes(:user).find_each do |administrateur|
stats = administrateur_stats(administrateur)
api.update_contact(administrateur.email, stats)
end
api.run
end
private
def api
@api ||= Sendinblue::API.new_properly_configured!
end
def administrateur_stats(administrateur)
nb_dossiers_by_procedure_id = nb_dossiers_by_procedure_id(administrateur.id)
nb_dossiers_by_synthetic_state = nb_dossiers_by_synthetic_state(administrateur.id)
nb_dossiers_roi = nb_dossiers_by_procedure_id.reject { |procedure_id, _count| is_brouillon(procedure_id) }.map { |_procedure_id, count| count }.sum
result = {
ds_sign_in_count: administrateur&.user&.sign_in_count,
ds_created_at: administrateur.created_at,
ds_active: administrateur.user.active?,
ds_id: administrateur.id,
nb_services: nb_services_by_administrateur_id[administrateur.id],
nb_instructeurs: nb_instructeurs_by_administrateur_id[administrateur.id],
ds_nb_demarches_actives: nb_demarches_by_administrateur_id_and_state[[administrateur.id, "publiee"]],
ds_nb_demarches_archives: nb_demarches_by_administrateur_id_and_state[[administrateur.id, "close"]],
ds_nb_demarches_brouillons: nb_demarches_by_administrateur_id_and_state[[administrateur.id, "brouillon"]],
nb_demarches_test: nb_dossiers_by_procedure_id
.filter { |procedure_id, count| count > 0 && is_brouillon(procedure_id) }
.count,
nb_demarches_prod: nb_dossiers_by_procedure_id
.reject { |procedure_id, count| count == 0 || is_brouillon(procedure_id) }
.count,
nb_demarches_prod_20: nb_dossiers_by_procedure_id
.reject { |procedure_id, count| count < 20 || is_brouillon(procedure_id) }
.count,
nb_dossiers: nb_dossiers_by_procedure_id
.reject { |procedure_id, _count| is_brouillon(procedure_id) }
.map { |_procedure_id, count| count }
.sum,
nb_dossiers_max: nb_dossiers_by_procedure_id
.reject { |procedure_id, _count| is_brouillon(procedure_id) }
.map { |_procedure_id, count| count }
.max || 0,
nb_dossiers_traite: nb_dossiers_by_synthetic_state['termine'],
nb_dossiers_dossier_en_instruction: nb_dossiers_by_synthetic_state['en_instruction'],
admin_roi_low: nb_dossiers_roi * 7,
admin_roi_high: nb_dossiers_roi * 17
}
if administrateur&.user&.current_sign_in_at.present?
result[:ds_current_sign_in_at] = administrateur.user.current_sign_in_at
end
if administrateur&.user&.last_sign_in_at.present?
result[:ds_last_sign_in_at] = administrateur.user.last_sign_in_at
end
result
end
# Returns a hash { procedure_id => dossier_count }:
# - The keys are the ids of procedures owned by administrateur_id
# - The values are the number of dossiers for that procedure.
# Brouillons, and dossiers that are 'archive' but not 'termine', are not counted.
def nb_dossiers_by_procedure_id(administrateur_id)
with_default(
0,
nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state[administrateur_id]
.transform_values do |nb_dossiers_by_synthetic_state|
nb_dossiers_by_synthetic_state
.reject { |synthetic_state, _count| ['brouillon', 'archive'].include?(synthetic_state) }
.map { |_synthetic_state, count| count }
.sum
end
)
end
# Returns a hash { synthetic_state => dossier_count }
# - The keys are dossier synthetic_states (see class comment)
# - The values are the number of dossiers in that synthetic state, for procedures owned by `administrateur_id`
# Dossier on procedures en test are not counted
def nb_dossiers_by_synthetic_state(administrateur_id)
with_default(
0,
nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state[administrateur_id]
.reject { |procedure_id, _nb_dossiers_by_synthetic_state| is_brouillon(procedure_id) }
.flat_map { |_procedure_id, nb_dossiers_by_synthetic_state| nb_dossiers_by_synthetic_state.to_a }
.group_by { |synthetic_state, _count| synthetic_state }
.transform_values { |synthetic_states_and_counts| synthetic_states_and_counts.map { |_synthetic_state, count| count }.sum }
)
end
def nb_demarches_by_administrateur_id_and_state
@nb_demarches_by_administrateur_id_and_state ||= with_default(0, Procedure.joins(:administrateurs).group('administrateurs.id', :aasm_state).count)
end
def nb_services_by_administrateur_id
@nb_services_by_administrateur_id ||= with_default(0, Service.group(:administrateur_id).count)
end
def nb_instructeurs_by_administrateur_id
@nb_instructeurs_by_administrateur_id ||= with_default(0, Administrateur.joins(:instructeurs).group('administrateurs.id').count)
end
def nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state
if @nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state.present?
return @nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state
end
result = {}
Dossier
.joins(revision: { procedure: [:administrateurs] })
.group(
'administrateurs.id',
'procedure_revisions.procedure_id',
<<~EOSQL
CASE
WHEN state IN('accepte', 'refuse', 'sans_suite') THEN 'termine'
WHEN archived THEN 'archive'
ELSE state
END
EOSQL
)
.count
.each do |(administrateur_id, procedure_id, synthetic_state), count|
result.deep_merge!(
{ administrateur_id => { procedure_id => { synthetic_state => count } } }
)
end
@nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state =
with_default({}, result)
end
def is_brouillon(procedure_id)
procedure_states[procedure_id] == 'brouillon'
end
def procedure_states
@procedure_states ||= Procedure.pluck(:id, :aasm_state).to_h
end
def with_default(default, hash)
hash.default = default
hash
end
end

View file

@ -12,7 +12,7 @@ Rails.application.config.content_security_policy do |policy|
# Javascript: allow us, SendInBlue and Matomo.
# We need unsafe_inline because miniprofiler and us have some inline buttons :(
scripts_whitelist = ["*.sendinblue.com", "*.crisp.chat", "crisp.chat", "*.sibautomation.com", "sibautomation.com", "cdn.jsdelivr.net", "maxcdn.bootstrapcdn.com", "code.jquery.com"]
scripts_whitelist = ["*.crisp.chat", "crisp.chat", "cdn.jsdelivr.net", "maxcdn.bootstrapcdn.com", "code.jquery.com"]
scripts_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
policy.script_src(:self, :unsafe_eval, :unsafe_inline, :blob, *scripts_whitelist)
@ -20,7 +20,7 @@ Rails.application.config.content_security_policy do |policy|
# It's too complicated to be fixed right now (and it wouldn't add value: this is hardcoded in views, so not subject to injections)
policy.style_src(:self, :unsafe_inline, "*.crisp.chat", "crisp.chat", 'cdn.jsdelivr.net', 'maxcdn.bootstrapcdn.com')
connect_whitelist = ["wss://*.crisp.chat", "*.crisp.chat", "in-automate.sendinblue.com", "app.franceconnect.gouv.fr", "sentry.io", "openmaptiles.geo.data.gouv.fr", "openmaptiles.github.io", "tiles.geo.api.gouv.fr", "wxs.ign.fr"]
connect_whitelist = ["wss://*.crisp.chat", "*.crisp.chat", "app.franceconnect.gouv.fr", "sentry.io", "openmaptiles.geo.data.gouv.fr", "openmaptiles.github.io", "tiles.geo.api.gouv.fr", "wxs.ign.fr"]
connect_whitelist << ENV.fetch('APP_HOST')
connect_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
connect_whitelist << URI(API_ADRESSE_URL).host if API_ADRESSE_URL.present?

View file

@ -1,224 +0,0 @@
describe AdministrateurUsageStatisticsService do
describe '#administrateur_stats' do
let(:service) { AdministrateurUsageStatisticsService.new }
subject { service.send(:administrateur_stats, administrateur) }
before { Timecop.freeze(Time.zone.now) }
after { Timecop.return }
context 'for an administrateur that has nothing' do
let(:administrateur) { create(:administrateur) }
it do
is_expected.to eq(
ds_sign_in_count: 0,
ds_created_at: Time.zone.now,
ds_active: false,
ds_id: administrateur.id,
nb_services: 0,
nb_instructeurs: 0,
ds_nb_demarches_actives: 0,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 0,
nb_demarches_test: 0,
nb_demarches_prod: 0,
nb_demarches_prod_20: 0,
nb_dossiers: 0,
nb_dossiers_max: 0,
nb_dossiers_traite: 0,
nb_dossiers_dossier_en_instruction: 0,
admin_roi_low: 0,
admin_roi_high: 0
)
end
end
context 'for an administrateur that has plenty of things' do
let(:administrateur) do
create(:administrateur,
user: create(:user, :with_strong_password, sign_in_count: 17, current_sign_in_at: Time.zone.local(2019, 3, 7), last_sign_in_at: Time.zone.local(2019, 2, 27)),
services: [create(:service)],
instructeurs: [create(:instructeur)])
end
it do
is_expected.to include(
ds_sign_in_count: 17,
ds_current_sign_in_at: Time.zone.local(2019, 3, 7),
ds_last_sign_in_at: Time.zone.local(2019, 2, 27),
ds_created_at: Time.zone.now,
ds_active: true,
ds_id: administrateur.id,
nb_services: 1,
nb_instructeurs: 1
)
end
end
context 'counting procedures and dossiers' do
let(:administrateur) do
create(:administrateur, procedures: [procedure])
end
context 'with a freshly active procedure' do
let(:procedure) { create(:procedure, :published) }
it do
is_expected.to include(
ds_nb_demarches_actives: 1,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 0,
nb_demarches_test: 0,
nb_demarches_prod: 0,
nb_demarches_prod_20: 0,
nb_dossiers: 0,
nb_dossiers_max: 0,
nb_dossiers_traite: 0,
nb_dossiers_dossier_en_instruction: 0,
admin_roi_low: 0,
admin_roi_high: 0
)
end
end
context 'with a procedure close' do
let(:procedure) { create(:procedure, :closed) }
let!(:dossiers) do
(1..7).flat_map do
[
create(:dossier, :en_construction, procedure: procedure),
create(:dossier, :en_instruction, procedure: procedure),
create(:dossier, :accepte, procedure: procedure)
]
end
end
it do
is_expected.to include(
ds_nb_demarches_actives: 0,
ds_nb_demarches_archives: 1,
ds_nb_demarches_brouillons: 0,
nb_demarches_test: 0,
nb_demarches_prod: 1,
nb_demarches_prod_20: 1,
nb_dossiers: 21,
nb_dossiers_max: 21,
nb_dossiers_traite: 7,
nb_dossiers_dossier_en_instruction: 7,
admin_roi_low: 147,
admin_roi_high: 357
)
end
end
context 'with a procedure brouillon' do
let(:procedure) { create(:procedure) }
it do
is_expected.to include(
ds_nb_demarches_actives: 0,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 1,
nb_demarches_test: 0,
nb_demarches_prod: 0,
nb_demarches_prod_20: 0,
nb_dossiers: 0,
nb_dossiers_max: 0,
nb_dossiers_traite: 0,
nb_dossiers_dossier_en_instruction: 0,
admin_roi_low: 0,
admin_roi_high: 0
)
end
end
context 'with a procedure en test' do
let(:procedure) { create(:procedure) }
let!(:dossiers) do
(1..7).flat_map do
[
create(:dossier, :en_construction, procedure: procedure),
create(:dossier, :en_instruction, procedure: procedure),
create(:dossier, :accepte, procedure: procedure)
]
end
end
it do
is_expected.to include(
ds_nb_demarches_actives: 0,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 1,
nb_demarches_test: 1,
nb_demarches_prod: 0,
nb_demarches_prod_20: 0,
nb_dossiers: 0,
nb_dossiers_max: 0,
nb_dossiers_traite: 0,
nb_dossiers_dossier_en_instruction: 0,
admin_roi_low: 0,
admin_roi_high: 0
)
end
end
context 'with a procedure en prod' do
let(:procedure) { create(:procedure, :published) }
let!(:dossiers) do
[
create(:dossier, :en_construction, procedure: procedure),
create(:dossier, :en_instruction, procedure: procedure),
create(:dossier, :accepte, procedure: procedure)
]
end
it do
is_expected.to include(
ds_nb_demarches_actives: 1,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 0,
nb_demarches_test: 0,
nb_demarches_prod: 1,
nb_demarches_prod_20: 0,
nb_dossiers: 3,
nb_dossiers_max: 3,
nb_dossiers_traite: 1,
nb_dossiers_dossier_en_instruction: 1,
admin_roi_low: 21,
admin_roi_high: 51
)
end
end
context 'with a procedure en prod and more than 20 dossiers' do
let(:procedure) { create(:procedure, :published) }
let!(:dossiers) do
(1..7).flat_map do
[
create(:dossier, :en_construction, procedure: procedure),
create(:dossier, :en_instruction, procedure: procedure),
create(:dossier, :accepte, procedure: procedure)
]
end
end
it do
is_expected.to include(
ds_nb_demarches_actives: 1,
ds_nb_demarches_archives: 0,
ds_nb_demarches_brouillons: 0,
nb_demarches_test: 0,
nb_demarches_prod: 1,
nb_demarches_prod_20: 1,
nb_dossiers: 21,
nb_dossiers_max: 21,
nb_dossiers_traite: 7,
nb_dossiers_dossier_en_instruction: 7,
admin_roi_low: 147,
admin_roi_high: 357
)
end
end
end
end
end