From 40eb4c3bd33ebdf5ed4fd5534a9b177f1b6f034d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 1 Feb 2023 16:37:43 +0100 Subject: [PATCH] (wip) refactor seeds --- db/seeds.rb | 133 +------------------------------------ db/seeds/statistics.rb | 137 +++++++++++++++++++++++++++++++++++++++ lib/database/sequence.rb | 24 +++++++ 3 files changed, 163 insertions(+), 131 deletions(-) create mode 100644 db/seeds/statistics.rb create mode 100644 lib/database/sequence.rb diff --git a/db/seeds.rb b/db/seeds.rb index 68fd6b45c..d5ce95b9b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -4,78 +4,8 @@ # Some of them are just some placeholders to prevent having an empty palce when starting fab-manager first. # Other data are required default values, for various settings. -if StatisticIndex.count.zero? - StatisticIndex.create!([ - { id: 1, es_type_key: 'subscription', label: I18n.t('statistics.subscriptions') }, - { id: 2, es_type_key: 'machine', label: I18n.t('statistics.machines_hours') }, - { id: 3, es_type_key: 'training', label: I18n.t('statistics.trainings') }, - { id: 4, es_type_key: 'event', label: I18n.t('statistics.events') }, - { id: 5, es_type_key: 'account', label: I18n.t('statistics.registrations'), ca: false }, - { id: 6, es_type_key: 'project', label: I18n.t('statistics.projects'), ca: false }, - { id: 7, es_type_key: 'user', label: I18n.t('statistics.users'), table: false, ca: false } - ]) - connection = ActiveRecord::Base.connection - connection.execute("SELECT setval('statistic_indices_id_seq', 7);") if connection.instance_values['config'][:adapter] == 'postgresql' -end - -if StatisticField.count.zero? - StatisticField.create!([ - # available data_types : index, number, date, text, list - { key: 'trainingId', label: I18n.t('statistics.training_id'), statistic_index_id: 3, data_type: 'index' }, - { key: 'trainingDate', label: I18n.t('statistics.training_date'), statistic_index_id: 3, data_type: 'date' }, - { key: 'eventId', label: I18n.t('statistics.event_id'), statistic_index_id: 4, data_type: 'index' }, - { key: 'eventDate', label: I18n.t('statistics.event_date'), statistic_index_id: 4, data_type: 'date' }, - { key: 'themes', label: I18n.t('statistics.themes'), statistic_index_id: 6, data_type: 'list' }, - { key: 'components', label: I18n.t('statistics.components'), statistic_index_id: 6, data_type: 'list' }, - { key: 'machines', label: I18n.t('statistics.machines'), statistic_index_id: 6, data_type: 'list' }, - { key: 'name', label: I18n.t('statistics.event_name'), statistic_index_id: 4, data_type: 'text' }, - { key: 'userId', label: I18n.t('statistics.user_id'), statistic_index_id: 7, data_type: 'index' }, - { key: 'eventTheme', label: I18n.t('statistics.event_theme'), statistic_index_id: 4, data_type: 'text' }, - { key: 'ageRange', label: I18n.t('statistics.age_range'), statistic_index_id: 4, data_type: 'text' } - ]) -end - -unless StatisticField.find_by(key: 'groupName').try(:label) - field = StatisticField.find_or_initialize_by(key: 'groupName') - field.label = 'Groupe' - field.statistic_index_id = 1 - field.data_type = 'text' - field.save! -end - -if StatisticType.count.zero? - StatisticType.create!([ - { statistic_index_id: 2, key: 'booking', label: I18n.t('statistics.bookings'), graph: true, simple: true }, - { statistic_index_id: 2, key: 'hour', label: I18n.t('statistics.hours_number'), graph: true, simple: false }, - { statistic_index_id: 3, key: 'booking', label: I18n.t('statistics.bookings'), graph: false, simple: true }, - { statistic_index_id: 3, key: 'hour', label: I18n.t('statistics.hours_number'), graph: false, simple: false }, - { statistic_index_id: 4, key: 'booking', label: I18n.t('statistics.tickets_number'), graph: false, - simple: false }, - { statistic_index_id: 4, key: 'hour', label: I18n.t('statistics.hours_number'), graph: false, simple: false }, - { statistic_index_id: 5, key: 'member', label: I18n.t('statistics.users'), graph: true, simple: true }, - { statistic_index_id: 6, key: 'project', label: I18n.t('statistics.projects'), graph: false, simple: true }, - { statistic_index_id: 7, key: 'revenue', label: I18n.t('statistics.revenue'), graph: false, simple: false } - ]) -end - -if StatisticSubType.count.zero? - StatisticSubType.create!([ - { key: 'created', label: I18n.t('statistics.account_creation'), - statistic_types: StatisticIndex.find_by(es_type_key: 'account').statistic_types }, - { key: 'published', label: I18n.t('statistics.project_publication'), - statistic_types: StatisticIndex.find_by(es_type_key: 'project').statistic_types } - ]) -end - -if StatisticGraph.count.zero? - StatisticGraph.create!([ - { statistic_index_id: 1, chart_type: 'stackedAreaChart', limit: 0 }, - { statistic_index_id: 2, chart_type: 'stackedAreaChart', limit: 0 }, - { statistic_index_id: 3, chart_type: 'discreteBarChart', limit: 10 }, - { statistic_index_id: 4, chart_type: 'discreteBarChart', limit: 10 }, - { statistic_index_id: 5, chart_type: 'lineChart', limit: 0 }, - { statistic_index_id: 7, chart_type: 'discreteBarChart', limit: 10 } - ]) +Dir[Rails.root.join('db', 'seeds/*', '*.rb')].sort.each do |seed| + load seed end if Group.count.zero? @@ -1032,65 +962,6 @@ Setting.set('accounting_Error_label', 'Erroneous invoices to refund') unless Set Setting.set('external_id', false) unless Setting.find_by(name: 'external_id').try(:value) -if StatisticCustomAggregation.count.zero? - # available reservations hours for machines - machine_hours = StatisticType.find_by(key: 'hour', statistic_index_id: 2) - - available_hours = StatisticCustomAggregation.new( - statistic_type_id: machine_hours.id, - es_index: 'fablab', - es_type: 'availabilities', - field: 'available_hours', - query: '{"size":0, "aggregations":{"%s":{"sum":{"field":"bookable_hours"}}}, "query":{"bool":{"must":[{"range":' \ - '{"start_at":{"gte":"%s", "lte":"%s"}}}, {"match":{"available_type":"machines"}}]}}}' - ) - available_hours.save! - - # available training tickets - training_bookings = StatisticType.find_by(key: 'booking', statistic_index_id: 3) - - available_tickets = StatisticCustomAggregation.new( - statistic_type_id: training_bookings.id, - es_index: 'fablab', - es_type: 'availabilities', - field: 'available_tickets', - query: '{"size":0, "aggregations":{"%s":{"sum":{"field":"nb_total_places"}}}, "query":{"bool":{"must":[{"range":' \ - '{"start_at":{"gte":"%s", "lte":"%s"}}}, {"match":{"available_type":"training"}}]}}}' - ) - available_tickets.save! -end - -unless StatisticIndex.find_by(es_type_key: 'space') - index = StatisticIndex.create!(es_type_key: 'space', label: I18n.t('statistics.spaces')) - StatisticType.create!([ - { statistic_index_id: index.id, key: 'booking', label: I18n.t('statistics.bookings'), - graph: true, simple: true }, - { statistic_index_id: index.id, key: 'hour', label: I18n.t('statistics.hours_number'), - graph: true, simple: false } - ]) -end - -unless StatisticIndex.find_by(es_type_key: 'order') - index = StatisticIndex.create!(es_type_key: 'order', label: I18n.t('statistics.orders')) - type = StatisticType.create!({ statistic_index_id: index.id, key: 'store', label: I18n.t('statistics.store'), graph: true, simple: true }) - StatisticSubType.create!([ - { key: 'paid-processed', label: I18n.t('statistics.paid-processed'), statistic_types: [type] }, - { key: 'aborted', label: I18n.t('statistics.aborted'), statistic_types: [type] } - ]) - - # average cart price for orders - average_cart = StatisticCustomAggregation.new( - statistic_type_id: type.id, - es_index: 'stats', - es_type: 'order', - field: 'average_cart', - query: '{"size":0, "aggregations":{"%s":{"avg":{"field":"ca", ' \ - '"script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, ' \ - '"query":{"bool":{"must":[{"range": {"date":{"gte":"%s", "lte":"%s"}}}]}}}' - ) - average_cart.save! -end - ProfileCustomField.find_or_create_by(label: 'N° SIRET') ProfileCustomField.find_or_create_by(label: 'Code NAF') ProfileCustomField.find_or_create_by(label: 'N° TVA intracommunautaire') diff --git a/db/seeds/statistics.rb b/db/seeds/statistics.rb new file mode 100644 index 000000000..218b9b00c --- /dev/null +++ b/db/seeds/statistics.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true + +require_relative '../../lib/database/sequence' + +StatisticIndex.create!({ id: 1, es_type_key: 'subscription', label: I18n.t('statistics.subscriptions') }) unless StatisticIndex.find_by(es_type_key: 'subscription') +StatisticIndex.create!({ id: 2, es_type_key: 'machine', label: I18n.t('statistics.machines_hours') }) unless StatisticIndex.find_by(es_type_key: 'machine') +StatisticIndex.create!({ id: 3, es_type_key: 'training', label: I18n.t('statistics.trainings') }) unless StatisticIndex.find_by(es_type_key: 'training') +StatisticIndex.create!({ id: 4, es_type_key: 'event', label: I18n.t('statistics.events') }) unless StatisticIndex.find_by(es_type_key: 'event') +StatisticIndex.create!({ id: 5, es_type_key: 'account', label: I18n.t('statistics.registrations'), ca: false }) unless StatisticIndex.find_by(es_type_key: 'account') +StatisticIndex.create!({ id: 6, es_type_key: 'project', label: I18n.t('statistics.projects'), ca: false }) unless StatisticIndex.find_by(es_type_key: 'project') +StatisticIndex.create!({ id: 7, es_type_key: 'user', label: I18n.t('statistics.users'), table: false, ca: false }) unless StatisticIndex.find_by(es_type_key: 'user') +StatisticIndex.create!({ id: 8, es_type_key: 'space', label: I18n.t('statistics.spaces') }) unless StatisticIndex.find_by(es_type_key: 'space') +StatisticIndex.create!({ id: 9, es_type_key: 'order', label: I18n.t('statistics.orders') }) unless StatisticIndex.find_by(es_type_key: 'order') + +Database::Sequence.update_id_seq(StatisticIndex.table_name) + + +StatisticField.create!({ key: 'spaceDates', label: I18n.t('statistics.space_dates'), statistic_index_id: index.id, data_type: 'list' }) +if StatisticField.count.zero? + StatisticField.create!([ + # available data_types : index, number, date, text, list + { key: 'machineDates', label: I18n.t('statistics.machine_dates'), statistic_index_id: 2, data_type: 'list' }, + { key: 'trainingId', label: I18n.t('statistics.training_id'), statistic_index_id: 3, data_type: 'index' }, + { key: 'trainingDate', label: I18n.t('statistics.training_date'), statistic_index_id: 3, data_type: 'date' }, + { key: 'eventId', label: I18n.t('statistics.event_id'), statistic_index_id: 4, data_type: 'index' }, + { key: 'eventDate', label: I18n.t('statistics.event_date'), statistic_index_id: 4, data_type: 'date' }, + { key: 'themes', label: I18n.t('statistics.themes'), statistic_index_id: 6, data_type: 'list' }, + { key: 'components', label: I18n.t('statistics.components'), statistic_index_id: 6, data_type: 'list' }, + { key: 'machines', label: I18n.t('statistics.machines'), statistic_index_id: 6, data_type: 'list' }, + { key: 'name', label: I18n.t('statistics.event_name'), statistic_index_id: 4, data_type: 'text' }, + { key: 'userId', label: I18n.t('statistics.user_id'), statistic_index_id: 7, data_type: 'index' }, + { key: 'eventTheme', label: I18n.t('statistics.event_theme'), statistic_index_id: 4, data_type: 'text' }, + { key: 'ageRange', label: I18n.t('statistics.age_range'), statistic_index_id: 4, data_type: 'text' } + ]) +end + +unless StatisticField.find_by(key: 'groupName').try(:label) + field = StatisticField.find_or_initialize_by(key: 'groupName') + field.label = 'Groupe' + field.statistic_index_id = 1 + field.data_type = 'text' + field.save! +end + +if StatisticType.count.zero? + StatisticType.create!([ + { statistic_index_id: 2, key: 'booking', label: I18n.t('statistics.bookings'), graph: true, simple: true }, + { statistic_index_id: 2, key: 'hour', label: I18n.t('statistics.hours_number'), graph: true, simple: false }, + { statistic_index_id: 3, key: 'booking', label: I18n.t('statistics.bookings'), graph: false, simple: true }, + { statistic_index_id: 3, key: 'hour', label: I18n.t('statistics.hours_number'), graph: false, simple: false }, + { statistic_index_id: 4, key: 'booking', label: I18n.t('statistics.tickets_number'), graph: false, + simple: false }, + { statistic_index_id: 4, key: 'hour', label: I18n.t('statistics.hours_number'), graph: false, simple: false }, + { statistic_index_id: 5, key: 'member', label: I18n.t('statistics.users'), graph: true, simple: true }, + { statistic_index_id: 6, key: 'project', label: I18n.t('statistics.projects'), graph: false, simple: true }, + { statistic_index_id: 7, key: 'revenue', label: I18n.t('statistics.revenue'), graph: false, simple: false } + ]) +end + +if StatisticSubType.count.zero? + StatisticSubType.create!([ + { key: 'created', label: I18n.t('statistics.account_creation'), + statistic_types: StatisticIndex.find_by(es_type_key: 'account').statistic_types }, + { key: 'published', label: I18n.t('statistics.project_publication'), + statistic_types: StatisticIndex.find_by(es_type_key: 'project').statistic_types } + ]) +end + +if StatisticGraph.count.zero? + StatisticGraph.create!([ + { statistic_index_id: 1, chart_type: 'stackedAreaChart', limit: 0 }, + { statistic_index_id: 2, chart_type: 'stackedAreaChart', limit: 0 }, + { statistic_index_id: 3, chart_type: 'discreteBarChart', limit: 10 }, + { statistic_index_id: 4, chart_type: 'discreteBarChart', limit: 10 }, + { statistic_index_id: 5, chart_type: 'lineChart', limit: 0 }, + { statistic_index_id: 7, chart_type: 'discreteBarChart', limit: 10 } + ]) +end + +if StatisticCustomAggregation.count.zero? + # available reservations hours for machines + machine_hours = StatisticType.find_by(key: 'hour', statistic_index_id: 2) + + available_hours = StatisticCustomAggregation.new( + statistic_type_id: machine_hours.id, + es_index: 'fablab', + es_type: 'availabilities', + field: 'available_hours', + query: '{"size":0, "aggregations":{"%s":{"sum":{"field":"bookable_hours"}}}, "query":{"bool":{"must":[{"range":' \ + '{"start_at":{"gte":"%s", "lte":"%s"}}}, {"match":{"available_type":"machines"}}]}}}' + ) + available_hours.save! + + # available training tickets + training_bookings = StatisticType.find_by(key: 'booking', statistic_index_id: 3) + + available_tickets = StatisticCustomAggregation.new( + statistic_type_id: training_bookings.id, + es_index: 'fablab', + es_type: 'availabilities', + field: 'available_tickets', + query: '{"size":0, "aggregations":{"%s":{"sum":{"field":"nb_total_places"}}}, "query":{"bool":{"must":[{"range":' \ + '{"start_at":{"gte":"%s", "lte":"%s"}}}, {"match":{"available_type":"training"}}]}}}' + ) + available_tickets.save! +end + +unless StatisticIndex.find_by(es_type_key: 'space') + index = StatisticIndex.create!(es_type_key: 'space', label: I18n.t('statistics.spaces')) + StatisticType.create!([ + { statistic_index_id: index.id, key: 'booking', label: I18n.t('statistics.bookings'), + graph: true, simple: true }, + { statistic_index_id: index.id, key: 'hour', label: I18n.t('statistics.hours_number'), + graph: true, simple: false } + ]) +end + +unless StatisticIndex.find_by(es_type_key: 'order') + index = StatisticIndex.create!(es_type_key: 'order', label: I18n.t('statistics.orders')) + type = StatisticType.create!({ statistic_index_id: index.id, key: 'store', label: I18n.t('statistics.store'), graph: true, simple: true }) + StatisticSubType.create!([ + { key: 'paid-processed', label: I18n.t('statistics.paid-processed'), statistic_types: [type] }, + { key: 'aborted', label: I18n.t('statistics.aborted'), statistic_types: [type] } + ]) + + # average cart price for orders + average_cart = StatisticCustomAggregation.new( + statistic_type_id: type.id, + es_index: 'stats', + es_type: 'order', + field: 'average_cart', + query: '{"size":0, "aggregations":{"%s":{"avg":{"field":"ca", ' \ + '"script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, ' \ + '"query":{"bool":{"must":[{"range": {"date":{"gte":"%s", "lte":"%s"}}}]}}}' + ) + average_cart.save! +end diff --git a/lib/database/sequence.rb b/lib/database/sequence.rb new file mode 100644 index 000000000..e5ef8a8aa --- /dev/null +++ b/lib/database/sequence.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + + +# Database helpers +module Database; end + +# Manage sequences +class Database::Sequence + class << self + # update the ID sequence for the given table + # @param table_name [String] + def update_id_seq(table_name) + return unless @connection.instance_values['config'][:adapter] == 'postgresql' + + @connection.execute <<~SQL.squish + WITH max_id AS ( + SELECT max(id) as max FROM #{table_name} + ) + SELECT setval('#{table_name}_id_seq', max_id.max) + FROM max_id; + SQL + end + end +end