Skip to content
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

Feature report #10

Open
wants to merge 8 commits into
base: feature_webAPI
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/ad_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def view
target_ids = Ad.pluck(:id).sample(params[:count].to_i)
ads = Ad.find(target_ids)
ads.each do |ad|
report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id],date: Date.today)
report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today)
unless report
report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today)
end
Expand All @@ -27,7 +27,7 @@ def view
end

def click
report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id],date: Date.today)
report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)
unless report
report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)
end
Expand All @@ -37,4 +37,3 @@ def click
end

end

24 changes: 23 additions & 1 deletion app/controllers/ad_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'date'
class AdController < ApplicationController
def index
@ads = Ad.all
Expand Down Expand Up @@ -38,9 +39,30 @@ def destroy
redirect_to(ad_index_path)
end

def report #report.htmlに全レポートを出力
@reports = Report.select("
ad_id,
SUM(reports.imp) AS imp,
SUM(reports.click) AS click,
SUM(reports.cv) AS cv,
SUM(reports.price) AS price
").group(:ad_id)
end

def report_period #report.htmlに一定期間のレポートを出力
@reports = Report.select("
ad_id,
SUM(reports.imp) AS imp,
SUM(reports.click) AS click,
SUM(reports.cv) AS cv,
SUM(reports.price) AS price
").where(date: Date.parse(params[:date_min])..Date.parse(params[:date_max])).group(:ad_id)
render('/ad/report')
end

private

def ad_params # Adオブジェクト作成時にフォームから入力したパラメーターを渡す。
params.require(:ad).permit(:price, :text, :advertiser_id, :image)
params.require(:ad).permit(:price, :text, :image)
end
end
2 changes: 1 addition & 1 deletion app/models/ad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Ad < ApplicationRecord
validates :text, presence: true
validates :price, presence: true
validates :image, presence: true
validates :advertiser_id, presence: true
has_many :reports

end
1 change: 1 addition & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class Report < ApplicationRecord
belongs_to :ad
end
2 changes: 0 additions & 2 deletions app/views/ad/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<%= f.text_area :text,value: @ad.text,class: "form-control" %>
<p>price</p>
<%= f.number_field :price,value: @ad.price %>
<p>advertiser_id</p>
<%= f.number_field :advertiser_id,value: @ad.advertiser_id %>
<p>image</p>
<%= f.file_field :image,value: @ad.image,class: "form-control-file" %>
<%= f.submit class: "btn btn-primary",value: "更新" %>
Expand Down
2 changes: 0 additions & 2 deletions app/views/ad/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<%= f.text_area :text,value: @ad.text,class: "form-control" %>
<p>price</p>
<%= f.number_field :price,value: @ad.price %>
<p>advertiser_id</p>
<%= f.number_field :advertiser_id,value: @ad.advertiser_id %>
<p>image</p>
<%= f.file_field :image,value: @ad.image,class: "form-control-file" %>
<%= f.submit class: "btn btn-primary",value: "入稿" %>
Expand Down
33 changes: 33 additions & 0 deletions app/views/ad/report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= form_tag(report_period_path, method: :get) do %>
<%= date_field_tag :date_min %>
<%= date_field_tag :date_max %>
<%= submit_tag "期間指定", class: "btn btn-primary" %>
<% end %>
<div class="ads">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" class="h2">広告ID</th>
<th scope="col" class="h2">Impression数</th>
<th scope="col" class="h2">Click数</th>
<th scope="col" class="h2">総費用(円)</th>
<th scope="col" class="h2">CTR</th>
<th scope="col" class="h2">CPM</th>
</tr>
</thead>
<tbody>
<tr>
<% @reports.each do |report| %>
<td class="h3 align-middle"><%= report.ad_id %></td>
<td class="h3 align-middle"><%= report.imp %></td>
<td class="h3 align-middle"><%= report.click %></td>
<td class="h3 align-middle"><%= report.price %></td>
<td class="h3 align-middle">
<%= (report.click.to_f / report.imp.to_f).round(3) %></td>
<td class="h3 align-middle">
<%= (report.price / (1000 / report.imp)) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
get '/view' => 'ad_api#view'
get '/click' => 'ad_api#click'
resources :ad
get '/report' => 'ad#report'
get '/report_period' => 'ad#report_period'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
1 change: 0 additions & 1 deletion db/migrate/20190528085221_create_ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
class CreateAds < ActiveRecord::Migration[5.2] # Reportテーブルのtotalpriceにpriceを加算する為に使用しているのでここでも使っています
def change
create_table :ads do |t|
t.integer :advertiser_id, null: false # 広告主ID
t.string :image, null: false, default: '' # 広告の画像URL
t.integer :price, null: false # 広告の価格
t.string :text, null: false, default: '' # 広告の説明文
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20190614041856_create_repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
t.integer :imp, null: false, default: 0
t.integer :cv, null: false, default: 0
t.integer :price, null: false, default: 0
t.date :date,null: false,unique: true
t.date :date, null: false, unique: true
t.timestamps
end
end
Expand Down
36 changes: 17 additions & 19 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -11,26 +12,23 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_14_041856) do

create_table "ads", force: :cascade do |t|
t.integer "advertiser_id", null: false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テーブル定義は消して大丈夫?
広告を作成する時に広告主IDを指定しないだけで、広告主に広告が紐づく構造は変わらないから消したらマズイ気が

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すり合わせが必要とかだったら明日やろう

t.string "image", default: "", null: false
t.integer "price", null: false
t.string "text", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'ads', force: :cascade do |t|
t.string 'image', default: '', null: false
t.integer 'price', null: false
t.string 'text', default: '', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

create_table "reports", force: :cascade do |t|
t.integer "ad_id", null: false
t.integer "adspot_id", null: false
t.integer "click", default: 0, null: false
t.integer "imp", default: 0, null: false
t.integer "cv", default: 0, null: false
t.integer "price", default: 0, null: false
t.date "date", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'reports', force: :cascade do |t|
t.integer 'ad_id', null: false
t.integer 'adspot_id', null: false
t.integer 'click', default: 0, null: false
t.integer 'imp', default: 0, null: false
t.integer 'cv', default: 0, null: false
t.integer 'price', default: 0, null: false
t.date 'date', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

end
121 changes: 56 additions & 65 deletions spec/controllers/ad_api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,94 @@

RSpec.describe AdApiController, type: :controller do
before do
@ad1 =FactoryBot.create(:ad)
@ad1 = FactoryBot.create(:ad)
end

describe 'GET #view' do
it 'returns http success with valid params' do
get :view,params: {adspot_id: 1,count: 1}
get :view, params: { adspot_id: 1, count: 1 }
expect(response).to have_http_status(:success)
end

describe "Report" do
it "an ad should be recorded" do
expect { get :view,params: {adspot_id: 1,count: 1} }.to change(Report, :count).by(1)
describe 'Report' do
it 'an ad should be recorded' do
expect { get :view, params: { adspot_id: 1, count: 1 } }.to change(Report, :count).by(1)
end

it "record 'imp' should be increased" do
get :view,params: {adspot_id: 1,count: 1}
report = Report.find_by(ad_id: @ad1.id, adspot_id: 1, date: Date.today)
get :view, params: { adspot_id: 1, count: 1 }
report = Report.find_by(ad_id: @ad1.id, adspot_id: 1, date: Date.today)
expect(report.imp).to eq 1
end

it "an ad shouldn't be recorded twice from the same adspot_id on the same day" do
get :view,params: {adspot_id: 100,count: 1}
expect { get :view,params: {adspot_id: 100,count: 1}}.to change(Report, :count).by(0)
get :view, params: { adspot_id: 100, count: 1 }
expect { get :view, params: { adspot_id: 100, count: 1 } }.to change(Report, :count).by(0)
end

it "an ad should be recorded from other adspots" do
get :view,params: {adspot_id: 100,count: 1}
expect { get :view,params: {adspot_id: 101,count: 1}}.to change(Report, :count).from(1).to(2)
it 'an ad should be recorded from other adspots' do
get :view, params: { adspot_id: 100, count: 1 }
expect { get :view, params: { adspot_id: 101, count: 1 } }.to change(Report, :count).from(1).to(2)
end

it "ads should be recorded at the same time" do
ad2 =FactoryBot.create(:ad)
expect { get :view,params: {adspot_id: 101,count: 2}}.to change(Report, :count).from(0).to(2)
it 'ads should be recorded at the same time' do
ad2 = FactoryBot.create(:ad)
expect { get :view, params: { adspot_id: 101, count: 2 } }.to change(Report, :count).from(0).to(2)
end

it "an ad should be recorded on the other day" do
get :view,params: {adspot_id: 101,count: 1}
it 'an ad should be recorded on the other day' do
get :view, params: { adspot_id: 101, count: 1 }
travel 1.day do
expect { get :view,params: {adspot_id: 101,count: 1}}.to change(Report, :count).from(1).to(2)
expect { get :view, params: { adspot_id: 101, count: 1 } }.to change(Report, :count).from(1).to(2)
end
end

end


context "when many ads are requested" do

it "all ads should be visible " do
@ad2 =FactoryBot.create(:ad)
@ad3 =FactoryBot.create(:ad)
get :view,params: {adspot_id: 1,count: 3}
jsons = JSON.parse(response.body)


ad1_ispresent = false
ad2_ispresent = false
ad3_ispresent = false

jsons.each do |json|
if json['ad_id'] == @ad1.id
ad1_ispresent = true
elsif json['ad_id'] == @ad2.id
ad2_ispresent = true
elsif json['ad_id'] == @ad3.id
ad3_ispresent = true
context 'when many ads are requested' do
it 'all ads should be visible ' do
@ad2 = FactoryBot.create(:ad)
@ad3 = FactoryBot.create(:ad)
get :view, params: { adspot_id: 1, count: 3 }
jsons = JSON.parse(response.body)

ad1_ispresent = false
ad2_ispresent = false
ad3_ispresent = false

jsons.each do |json|
if json['ad_id'] == @ad1.id
ad1_ispresent = true
elsif json['ad_id'] == @ad2.id
ad2_ispresent = true
elsif json['ad_id'] == @ad3.id
ad3_ispresent = true
end
end
expect(ad1_ispresent).to be_truthy
expect(ad2_ispresent).to be_truthy
expect(ad3_ispresent).to be_truthy
end
expect(ad1_ispresent).to be_truthy
expect(ad2_ispresent).to be_truthy
expect(ad3_ispresent).to be_truthy
end
end
end
end

describe 'GET #click' do
before do
@ad =FactoryBot.create(:ad)
get :view,params: {adspot_id: 1,count: 1}
# get :click,params: {ad_id: ad.id,adspot_id: 1}
#@report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)

end

it 'returns http success' do
get :click,params: {ad_id: @ad.id,adspot_id: 1}
expect(response).to have_http_status(:success)
end
describe 'GET #click' do
before do
@ad = FactoryBot.create(:ad)
get :view, params: { adspot_id: 1, count: 1 }
# get :click,params: {ad_id: ad.id,adspot_id: 1}
# @report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)
end

describe "Report" do
it 'returns http success' do
get :click, params: { ad_id: @ad.id, adspot_id: 1 }
expect(response).to have_http_status(:success)
end

it "an ad should be recorded " do
expect { get :click,params: {ad_id: @ad.id,adspot_id: 1} }.to change(Report, :count).by(0)
describe 'Report' do
it 'an ad should be recorded ' do
expect { get :click, params: { ad_id: @ad.id, adspot_id: 1 } }.to change(Report, :count).by(0)
end


# it "record 'click' should be increased" do
# report = Report.find_by(ad_id: @ad.id, adspot_id: 1,date: Date.today)
# expect{get :click,params: {ad_id: @ad.id,adspot_id: 1}}.to change(report.click).by(0)
Expand Down Expand Up @@ -125,8 +118,7 @@
# end
# end
end

end
end
end

# it 'returns http success with valid params' do
Expand Down Expand Up @@ -167,7 +159,6 @@
# end
# end


# end
# end
# end
Loading