mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-17 11:54:22 +01:00
(wip) refactor seeds
This commit is contained in:
parent
4a2c20d8bb
commit
40eb4c3bd3
133
db/seeds.rb
133
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":{"%<aggs_name>s":{"sum":{"field":"bookable_hours"}}}, "query":{"bool":{"must":[{"range":' \
|
||||
'{"start_at":{"gte":"%<start_date>s", "lte":"%<end_date>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":{"%<aggs_name>s":{"sum":{"field":"nb_total_places"}}}, "query":{"bool":{"must":[{"range":' \
|
||||
'{"start_at":{"gte":"%<start_date>s", "lte":"%<end_date>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":{"%<aggs_name>s":{"avg":{"field":"ca", ' \
|
||||
'"script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, ' \
|
||||
'"query":{"bool":{"must":[{"range": {"date":{"gte":"%<start_date>s", "lte":"%<end_date>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')
|
||||
|
137
db/seeds/statistics.rb
Normal file
137
db/seeds/statistics.rb
Normal file
@ -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":{"%<aggs_name>s":{"sum":{"field":"bookable_hours"}}}, "query":{"bool":{"must":[{"range":' \
|
||||
'{"start_at":{"gte":"%<start_date>s", "lte":"%<end_date>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":{"%<aggs_name>s":{"sum":{"field":"nb_total_places"}}}, "query":{"bool":{"must":[{"range":' \
|
||||
'{"start_at":{"gte":"%<start_date>s", "lte":"%<end_date>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":{"%<aggs_name>s":{"avg":{"field":"ca", ' \
|
||||
'"script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, ' \
|
||||
'"query":{"bool":{"must":[{"range": {"date":{"gte":"%<start_date>s", "lte":"%<end_date>s"}}}]}}}'
|
||||
)
|
||||
average_cart.save!
|
||||
end
|
24
lib/database/sequence.rb
Normal file
24
lib/database/sequence.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user