From 09cdf163c992f597a94b1ef9b4d3028ed3c523b2 Mon Sep 17 00:00:00 2001 From: Kazuto Kusama Date: Sat, 17 Dec 2022 02:32:47 +0900 Subject: [PATCH] WIP --- app/models/audience_count.rb | 21 ++++ app/models/profile.rb | 4 + app/models/profile_survey.rb | 19 ++++ app/models/registered_talk.rb | 5 + app/models/talk.rb | 1 + .../20221127144943_create_audience_counts.rb | 14 +++ .../20221127151200_create_profile_surveys.rb | 16 +++ ...130125533_add_index_to_registered_talks.rb | 6 + db/schema.rb | 26 +++++ lib/tasks/generate_lead_cndt2022.rake | 103 ++++++++++++++++++ spec/factories/audience_counts.rb | 26 +++++ spec/factories/profile_surveys.rb | 28 +++++ spec/factories/registered_talks.rb | 5 + spec/models/audience_count_spec.rb | 22 ++++ spec/models/profile_survey_spec.rb | 21 ++++ spec/models/registered_talk_spec.rb | 5 + 16 files changed, 322 insertions(+) create mode 100644 app/models/audience_count.rb create mode 100644 app/models/profile_survey.rb create mode 100644 db/migrate/20221127144943_create_audience_counts.rb create mode 100644 db/migrate/20221127151200_create_profile_surveys.rb create mode 100644 db/migrate/20221130125533_add_index_to_registered_talks.rb create mode 100644 lib/tasks/generate_lead_cndt2022.rake create mode 100644 spec/factories/audience_counts.rb create mode 100644 spec/factories/profile_surveys.rb create mode 100644 spec/models/audience_count_spec.rb create mode 100644 spec/models/profile_survey_spec.rb diff --git a/app/models/audience_count.rb b/app/models/audience_count.rb new file mode 100644 index 000000000..ca4f85f47 --- /dev/null +++ b/app/models/audience_count.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: audience_counts +# +# id :bigint not null, primary key +# min :integer +# sub :string(255) +# talk_name :string(255) +# track_name :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# talk_id :integer +# +# Indexes +# +# index_audience_counts_on_talk_id (talk_id) +# +class AudienceCount < ApplicationRecord + belongs_to :profile, class_name: 'Profile', foreign_key: 'sub', primary_key: 'sub' + belongs_to :talk +end diff --git a/app/models/profile.rb b/app/models/profile.rb index 7586edd08..faddbba73 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -63,6 +63,8 @@ class Profile < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :company_name_prefix, shortcuts: [:name], class_name: '::FormModels::CompanyNamePrefix' belongs_to_active_hash :company_name_suffix, shortcuts: [:name], class_name: '::FormModels::CompanyNameSuffix' + belongs_to_active_hash :annual_sales, shortcuts: [:name], class_name: '::FormModels::AnnualSales' + belongs_to_active_hash :number_of_employee, shortcuts: [:name], class_name: '::FormModels::NumberOfEmployee' belongs_to :conference has_many :registered_talks @@ -72,6 +74,8 @@ class Profile < ApplicationRecord has_many :chat_messages has_many :orders has_many :check_ins + has_one :profile_survey, foreign_key: 'sub', primary_key: 'sub', class_name: 'ProfileSurvey' + has_many :audience_counts, foreign_key: 'sub', primary_key: 'sub', class_name: 'AudienceCount' before_create do self.calendar_unique_code = SecureRandom.uuid diff --git a/app/models/profile_survey.rb b/app/models/profile_survey.rb new file mode 100644 index 000000000..51e963c62 --- /dev/null +++ b/app/models/profile_survey.rb @@ -0,0 +1,19 @@ +# == Schema Information +# +# Table name: profile_surveys +# +# id :bigint not null, primary key +# department :string(255) +# filled_at :datetime +# generation :string(255) +# industry :string(255) +# occupation :string(255) +# position :string(255) +# sub :string(255) +# url :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# +class ProfileSurvey < ApplicationRecord + belongs_to :profile, class_name: 'Profile', foreign_key: 'sub', primary_key: 'sub' +end diff --git a/app/models/registered_talk.rb b/app/models/registered_talk.rb index cca288d70..9caca5726 100644 --- a/app/models/registered_talk.rb +++ b/app/models/registered_talk.rb @@ -8,6 +8,11 @@ # profile_id :integer # talk_id :integer # +# Indexes +# +# index_registered_talks_on_profile_id (profile_id) +# index_registered_talks_on_talk_id (talk_id) +# class RegisteredTalk < ApplicationRecord belongs_to :talk diff --git a/app/models/talk.rb b/app/models/talk.rb index df565986c..c6b31992e 100644 --- a/app/models/talk.rb +++ b/app/models/talk.rb @@ -45,6 +45,7 @@ class Talk < ApplicationRecord has_one :video_registration, dependent: :destroy has_one :video, dependent: :destroy + has_many :audience_counts belongs_to :talk_time, optional: true has_many :talks_speakers has_many :registered_talks diff --git a/db/migrate/20221127144943_create_audience_counts.rb b/db/migrate/20221127144943_create_audience_counts.rb new file mode 100644 index 000000000..1c23ed074 --- /dev/null +++ b/db/migrate/20221127144943_create_audience_counts.rb @@ -0,0 +1,14 @@ +class CreateAudienceCounts < ActiveRecord::Migration[7.0] + def change + create_table :audience_counts do |t| + t.string :sub + t.integer :min + t.string :track_name + t.integer :talk_id + t.string :talk_name + + t.timestamps + end + add_index :audience_counts, :talk_id + end +end diff --git a/db/migrate/20221127151200_create_profile_surveys.rb b/db/migrate/20221127151200_create_profile_surveys.rb new file mode 100644 index 000000000..0e2529789 --- /dev/null +++ b/db/migrate/20221127151200_create_profile_surveys.rb @@ -0,0 +1,16 @@ +class CreateProfileSurveys < ActiveRecord::Migration[7.0] + def change + create_table :profile_surveys do |t| + t.string :sub + t.datetime :filled_at + t.string :url + t.string :generation + t.string :industry + t.string :department + t.string :occupation + t.string :position + + t.timestamps + end + end +end diff --git a/db/migrate/20221130125533_add_index_to_registered_talks.rb b/db/migrate/20221130125533_add_index_to_registered_talks.rb new file mode 100644 index 000000000..a0aab56a9 --- /dev/null +++ b/db/migrate/20221130125533_add_index_to_registered_talks.rb @@ -0,0 +1,6 @@ +class AddIndexToRegisteredTalks < ActiveRecord::Migration[7.0] + def change + add_index :registered_talks, :profile_id + add_index :registered_talks, :talk_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 20c4ea026..8dab51688 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -51,6 +51,17 @@ t.index ["conference_id"], name: "index_announcements_on_conference_id" end + create_table "audience_counts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "sub" + t.integer "min" + t.string "track_name" + t.integer "talk_id" + t.string "talk_name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["talk_id"], name: "index_audience_counts_on_talk_id" + end + create_table "booths", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "conference_id", null: false t.bigint "sponsor_id", null: false @@ -213,6 +224,19 @@ t.index ["ticket_id"], name: "index_orders_tickets_on_ticket_id" end + create_table "profile_surveys", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "sub" + t.datetime "filled_at" + t.string "url" + t.string "generation" + t.string "industry" + t.string "department" + t.string "occupation" + t.string "position" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "profiles", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "sub" t.string "email" @@ -281,6 +305,8 @@ t.integer "talk_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["profile_id"], name: "index_registered_talks_on_profile_id" + t.index ["talk_id"], name: "index_registered_talks_on_talk_id" end create_table "rooms", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| diff --git a/lib/tasks/generate_lead_cndt2022.rake b/lib/tasks/generate_lead_cndt2022.rake new file mode 100644 index 000000000..f2937aef2 --- /dev/null +++ b/lib/tasks/generate_lead_cndt2022.rake @@ -0,0 +1,103 @@ +namespace :util do + desc 'generate_lead_cndt2022' + task generate_lead_cndt2022: :environment do + ActiveRecord::Base.logger = Logger.new($stdout) + Rails.logger.level = Logger::DEBUG + + conference = Conference.find_by(abbr: 'cndt2022') + + attr = [ + 'セッション登録フラグ', + 'セッション視聴フラグ', + '現地チェックイン', + '姓', + '名', + 'セイ', + 'メイ', + '前株', + '会社名/所属団体名', + '後株', + '所属部署名', + '郵便番号', + '都道府県', + '勤務先住所1(都道府県以下)', + '勤務先住所2(ビル名)', + '電話番号', + 'FAX番号', + 'メールアドレス(PC)', + '従業員数', + '年商規模', + '業種', + '所属部署', + '職種', + '役職区分' + ] + + conference.sponsors.each do |s| + talk_id = s.talks[0]&.id || 0 + event_id = conference.id + sponsor_types = s.sponsor_types.map(&:name) + + # target_sponsor_type = sponsor_types & ['Diamond','Platinum', 'Gold', 'Silver'] + target_sponsor_type = sponsor_types & ['Platinum'] + + threshold = ['f5', 'techmatrix', 'acquia', 'freee'].include?(s.abbr) ? 1 : 5 + + attr += ['メールを希望', '電話を希望'] if ['ibm', 'redhat'].include?(s.abbr) + + if target_sponsor_type.any? + generated_csv = CSV.generate do |csv| + csv << attr + Profile.includes(:profile_survey).where(conference_id: event_id).includes(:registered_talks, :audience_counts).each do |profile| + flagged = false + if target_sponsor_type[0] == 'Diamond' || + (target_sponsor_type[0] == 'Platinum' && (profile.registered_talks.where(talk_id:).exists? || profile.audience_counts.where("talk_id = #{talk_id} AND min > #{threshold}").exists?)) || + (target_sponsor_type[0] == 'Platinum' && (profile.registered_talks.where(talk_id:).exists? || profile.audience_counts.where("talk_id = #{talk_id} AND min > #{threshold}").exists?)) + flagged = true + end + line = [ + profile.registered_talks.where(talk_id:).exists? ? 1 : 0, + profile.audience_counts.where("talk_id = #{talk_id} AND min > #{threshold}").exists? ? 1 : 0, + profile.check_ins.find_by(ticket_id: '7b02e975-8418-4b40-a01d-f8011cc705e3').present? ? 1 : 0, + profile.last_name, + profile.first_name, + profile.last_name_kana, + profile.first_name_kana, + profile.company_name_prefix&.name, + profile.company_name, + profile.company_name_suffix&.name, + profile.department, + profile.company_postal_code, + profile.company_address_level1, + profile.company_address_level2 + profile.company_address_line1, + profile.company_address_line2, + profile.company_tel, + profile.company_fax, + profile.company_email, + profile.number_of_employee.name, + profile.annual_sales.name, + profile.profile_survey&.industry, + profile.profile_survey&.department, + profile.profile_survey&.occupation, + profile.profile_survey&.position + ] + if s.abbr == 'ibm' + line.append(profile.agreements.where(form_item_id: 6)&.present? ? 1 : 0) + line.append(profile.agreements.where(form_item_id: 7)&.present? ? 1 : 0) + end + + if s.abbr == 'redhat' + line.append(profile.agreements.where(form_item_id: 8)&.present? ? 1 : 0) + line.append(profile.agreements.where(form_item_id: 9)&.present? ? 1 : 0) + end + + csv << line if flagged + end + end + File.open("./tmp/csv/#{target_sponsor_type[0]}_#{s.abbr}_#{talk_id}.csv", 'w', encoding: 'UTF-8') do |file| + file.write(generated_csv) + end + end + end + end +end diff --git a/spec/factories/audience_counts.rb b/spec/factories/audience_counts.rb new file mode 100644 index 000000000..e27b88c91 --- /dev/null +++ b/spec/factories/audience_counts.rb @@ -0,0 +1,26 @@ +# == Schema Information +# +# Table name: audience_counts +# +# id :bigint not null, primary key +# min :integer +# sub :string(255) +# talk_name :string(255) +# track_name :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# talk_id :integer +# +# Indexes +# +# index_audience_counts_on_talk_id (talk_id) +# +FactoryBot.define do + factory :audience_count do + sub { 'MyString' } + min { 1 } + track_name { 'MyString' } + talk_id { 1 } + talk_name { 'MyString' } + end +end diff --git a/spec/factories/profile_surveys.rb b/spec/factories/profile_surveys.rb new file mode 100644 index 000000000..03b89edf5 --- /dev/null +++ b/spec/factories/profile_surveys.rb @@ -0,0 +1,28 @@ +# == Schema Information +# +# Table name: profile_surveys +# +# id :bigint not null, primary key +# department :string(255) +# filled_at :datetime +# generation :string(255) +# industry :string(255) +# occupation :string(255) +# position :string(255) +# sub :string(255) +# url :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# +FactoryBot.define do + factory :profile_survey do + sub { 'MyString' } + filled_at { '2022-11-28 00:12:00' } + url { 'MyString' } + generation { 'MyString' } + industry { 'MyString' } + department { 'MyString' } + occupation { 'MyString' } + position { 'MyString' } + end +end diff --git a/spec/factories/registered_talks.rb b/spec/factories/registered_talks.rb index 91721f3d7..0cd787c30 100644 --- a/spec/factories/registered_talks.rb +++ b/spec/factories/registered_talks.rb @@ -8,6 +8,11 @@ # profile_id :integer # talk_id :integer # +# Indexes +# +# index_registered_talks_on_profile_id (profile_id) +# index_registered_talks_on_talk_id (talk_id) +# FactoryBot.define do factory :registered_talk, class: RegisteredTalk diff --git a/spec/models/audience_count_spec.rb b/spec/models/audience_count_spec.rb new file mode 100644 index 000000000..136f635d1 --- /dev/null +++ b/spec/models/audience_count_spec.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: audience_counts +# +# id :bigint not null, primary key +# min :integer +# sub :string(255) +# talk_name :string(255) +# track_name :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# talk_id :integer +# +# Indexes +# +# index_audience_counts_on_talk_id (talk_id) +# +require 'rails_helper' + +RSpec.describe(AudienceCount, type: :model) do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/profile_survey_spec.rb b/spec/models/profile_survey_spec.rb new file mode 100644 index 000000000..3733d006c --- /dev/null +++ b/spec/models/profile_survey_spec.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: profile_surveys +# +# id :bigint not null, primary key +# department :string(255) +# filled_at :datetime +# generation :string(255) +# industry :string(255) +# occupation :string(255) +# position :string(255) +# sub :string(255) +# url :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# +require 'rails_helper' + +RSpec.describe(ProfileSurvey, type: :model) do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/registered_talk_spec.rb b/spec/models/registered_talk_spec.rb index 8108d57fd..7e0780d14 100644 --- a/spec/models/registered_talk_spec.rb +++ b/spec/models/registered_talk_spec.rb @@ -8,6 +8,11 @@ # profile_id :integer # talk_id :integer # +# Indexes +# +# index_registered_talks_on_profile_id (profile_id) +# index_registered_talks_on_talk_id (talk_id) +# require 'rails_helper'