From 02c3d5c4cea2401e128a7b5e2352067d3d3a0fef Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Fri, 30 Sep 2022 23:08:14 +0200 Subject: [PATCH] added dashboard --- app/controllers/dashboard_controller.rb | 47 ++++ app/controllers/home_controller.rb | 18 +- app/helpers/application_helper.rb | 1 + app/helpers/dashboard_helper.rb | 2 + app/models/expert.rb | 2 +- app/models/visit_history.rb | 41 +++ app/views/dashboard/index.html.erb | 266 ++++++++++++++++++ .../20220930202617_create_visit_histories.rb | 17 ++ db/schema.rb | 16 +- test/controllers/dashboard_controller_test.rb | 9 + test/fixtures/visit_histories.yml | 13 + test/models/visit_history_test.rb | 7 + 12 files changed, 435 insertions(+), 4 deletions(-) create mode 100644 app/controllers/dashboard_controller.rb create mode 100644 app/helpers/dashboard_helper.rb create mode 100644 app/models/visit_history.rb create mode 100644 app/views/dashboard/index.html.erb create mode 100644 db/migrate/20220930202617_create_visit_histories.rb create mode 100644 test/controllers/dashboard_controller_test.rb create mode 100644 test/fixtures/visit_histories.yml create mode 100644 test/models/visit_history_test.rb diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb new file mode 100644 index 0000000..dddca49 --- /dev/null +++ b/app/controllers/dashboard_controller.rb @@ -0,0 +1,47 @@ +class DashboardController < ApplicationController + def index + @monitor = [] + general_data + visits_data + pdfs_data + search_data + end + + private + + def general_data + @monitor << [ + Dotation.all.size, + Dotation.public_dot.size, + FilterForEmail.all.size, + EmailMessage.all.size + ] + end + + def visits_data + @monitor << [ + VisitHistory.visits.count, + VisitHistory.visits.by_today.size, + VisitHistory.visits.by_7days.size, + VisitHistory.visits.by_30days.size + ] + end + + def pdfs_data + @monitor << [ + VisitHistory.pdf_files.count, + VisitHistory.pdf_files.by_today.size, + VisitHistory.pdf_files.by_7days.size, + VisitHistory.pdf_files.by_30days.size + ] + end + + def search_data + @monitor << [ + VisitHistory.search.count, + VisitHistory.search.by_today.size, + VisitHistory.search.by_7days.size, + VisitHistory.search.by_30days.size + ] + end +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 4c52b43..e6316e7 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -14,6 +14,8 @@ class HomeController < ApplicationController end def index + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 0) + puts request.remote_ip require 'json' prepare_filters cookies[:filter] = JSON.generate(build_filter_hash) @@ -25,6 +27,7 @@ class HomeController < ApplicationController end def search + VisitHistory.create(ip_address: request.remote_ip, event: 1) require 'json' prepare_filters cookies[:filter] = JSON.generate(build_filter_hash) @@ -36,10 +39,12 @@ class HomeController < ApplicationController end def contact + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 1) @email_message = EmailMessage.new end def statute + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 2) @setting = Setting.first end @@ -64,9 +69,12 @@ class HomeController < ApplicationController SendContactEmailJob.perform_later(@consultation_email.id) end - def arrange_consultation; end + def arrange_consultation + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 3) + end def monitor + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 4) prepare_filters cookies[:filter] = JSON.generate(build_filter_hash) end @@ -77,8 +85,13 @@ class HomeController < ApplicationController .where.not(id: @dotation.id).limit(4) @company_sizes = CompanySize.all respond_to do |format| - format.html + format.html do + VisitHistory.create(ip_address: request.remote_ip, event: 0, site: 5, + dotation_id: @dotation.id) + end format.pdf do + VisitHistory.create(ip_address: request.remote_ip, event: 2, site: 5, + dotation_id: @dotation.id) pdf1a = WickedPdf.new.pdf_from_string( render_cover_header(@dotation, @company_sizes), {pdf: 'cover_header', @@ -136,6 +149,7 @@ class HomeController < ApplicationController @dotations = Dotation.search_with_filters(filters).public_dot .point_desc.end_date_asc dotation = @dotations.first + VisitHistory.create(ip_address: request.remote_ip, event: 2, site: 0) respond_to do |format| format.pdf do pdf1a = WickedPdf.new.pdf_from_string( diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3e1f2b0..f1cd26f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,6 +5,7 @@ module ApplicationHelper # icon, name, controller, def nav_menu ret = '' + ret += menu_item('fas fa-tachometer-alt', 'Statystyki', '/dashboard', 'dashboard') if admin? ret += menu_item('far fa-circle', 'Dotacje', '/grants', 'grants') if role?('dotations') ret += menu_item('far fa-circle', 'Wydatki', '/expenses', 'expenses') if role?('expenses') ret += menu_item('far fa-circle', 'Wielkość firmy', '/company_sizes', 'company_sizes') if role?('company_sizes') diff --git a/app/helpers/dashboard_helper.rb b/app/helpers/dashboard_helper.rb new file mode 100644 index 0000000..a94ddfc --- /dev/null +++ b/app/helpers/dashboard_helper.rb @@ -0,0 +1,2 @@ +module DashboardHelper +end diff --git a/app/models/expert.rb b/app/models/expert.rb index c262918..2a0797b 100644 --- a/app/models/expert.rb +++ b/app/models/expert.rb @@ -13,7 +13,7 @@ class Expert < ApplicationRecord # == Validations ========================================================== validates :name, presence: true - + validates :description, presence: true, length: { maximum: 500 } # == Scopes =============================================================== # == Callbacks ============================================================ diff --git a/app/models/visit_history.rb b/app/models/visit_history.rb new file mode 100644 index 0000000..ff54ab9 --- /dev/null +++ b/app/models/visit_history.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +# event +# 0 - vist +# 1 - search +# 2 - pdf generation + +# site +# 0 - index +# 1 - contact +# 2 - regulamin +# 3 - umow_konsultacje +# 4 - monitoring +# 5 - dotacja + +# History model +class VisitHistory < ApplicationRecord + # == Constants ============================================================ + + # == Attributes =========================================================== + + # == Extensions =========================================================== + + # == Relationships ======================================================== + + # == Validations ========================================================== + + # == Scopes =============================================================== + scope :visits, -> { where(event: 0) } + scope :search, -> { where(event: 1) } + scope :pdf_files, -> { where(event: 2) } + scope :by_today, -> { where('created_at > ?', Time.now.midnight) } + scope :by_7days, -> { where('created_at > ?', Time.now.midnight - 7.days) } + scope :by_30days, -> { where('created_at > ?', Time.now.midnight - 30.days) } + scope :dist_ip, -> { select(:ip_address).uniq } + # == Callbacks ============================================================ + + # == Class Methods ======================================================== + + # == Instance Methods ===================================================== +end diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb new file mode 100644 index 0000000..943f94a --- /dev/null +++ b/app/views/dashboard/index.html.erb @@ -0,0 +1,266 @@ + +
+
+
+
+

Statystyki

+
+ +
+ +
+ +
+ +
+
+
+
Ogólne dane
+
+
+ +
+
+

<%= @monitor[0][0] %>

+

Dotacji w bazie

+
+
+ + +
+   +
+
+ +
+ +
+
+

<%= @monitor[0][1] %>

+

Aktywnych dotacji

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[0][2] %>

+

Zapisane filtry

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[0][3] %>

+

Kontakty od klientów

+
+
+ +
+   +
+
+ +
+ +
+
Wizyty na stronie
+
+
+ +
+
+

<%= @monitor[1][0] %>

+

Wszystkie wizyty

+
+
+ + +
+   +
+
+ +
+ +
+
+

<%= @monitor[1][1] %>

+

Wizyty - dzisiaj

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[1][2] %>

+

Wizyty - 7 dni

+
+
+ +
+   +
+
+ + +
+ +
+
+

<%= @monitor[1][3] %>

+

Wizyty - 30 dni

+
+
+ +
+   +
+
+
+ +
+
Wygenerowane pliki PDF
+
+
+ +
+
+

<%= @monitor[2][0] %>

+

Pobrane pliki

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[2][1] %>

+

Pobrane pliki - dzisiaj

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[2][2] %>

+

Pobrane pliki - 7 dni

+
+
+ +
+   +
+
+ + +
+ +
+
+

<%= @monitor[2][3] %>

+

Pobrane pliki - 30 dni

+
+
+ +
+   +
+
+ +
+ +
+
Wyszukiwanie
+
+
+ +
+
+

<%= @monitor[3][0] %>

+

Wszystkie wyszukania

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[3][1] %>

+

Wyszukiwanie - dzisiaj

+
+
+ +
+   +
+
+ +
+ +
+
+

<%= @monitor[3][2] %>

+

Wyszukiwanie - 7 dni

+
+
+ +
+   +
+
+ + +
+ +
+
+

<%= @monitor[3][3] %>

+

Wyszukiwanie - 30 dni

+
+
+ +
+   +
+
+
+ + +
+
diff --git a/db/migrate/20220930202617_create_visit_histories.rb b/db/migrate/20220930202617_create_visit_histories.rb new file mode 100644 index 0000000..39650c8 --- /dev/null +++ b/db/migrate/20220930202617_create_visit_histories.rb @@ -0,0 +1,17 @@ +class CreateVisitHistories < ActiveRecord::Migration[5.2] + def change + create_table :visit_histories do |t| + t.string :ip_address + t.bigint :dotation_id + t.integer :event + t.integer :site + t.string :add_data + + t.timestamps + end + add_index :visit_histories, :event + add_index :visit_histories, :ip_address + add_index :visit_histories, :dotation_id + add_index :visit_histories, :site + end +end diff --git a/db/schema.rb b/db/schema.rb index 73c0aed..e9b7a92 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_08_30_110315) do +ActiveRecord::Schema.define(version: 2022_09_30_202617) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false @@ -308,5 +308,19 @@ ActiveRecord::Schema.define(version: 2022_08_30_110315) do t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true end + create_table "visit_histories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + t.string "ip_address" + t.bigint "dotation_id" + t.integer "event" + t.integer "site" + t.string "add_data" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["dotation_id"], name: "index_visit_histories_on_dotation_id" + t.index ["event"], name: "index_visit_histories_on_event" + t.index ["ip_address"], name: "index_visit_histories_on_ip_address" + t.index ["site"], name: "index_visit_histories_on_site" + end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" end diff --git a/test/controllers/dashboard_controller_test.rb b/test/controllers/dashboard_controller_test.rb new file mode 100644 index 0000000..a0b2184 --- /dev/null +++ b/test/controllers/dashboard_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class DashboardControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get dashboard_index_url + assert_response :success + end + +end diff --git a/test/fixtures/visit_histories.yml b/test/fixtures/visit_histories.yml new file mode 100644 index 0000000..4de21ec --- /dev/null +++ b/test/fixtures/visit_histories.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + ip_address: MyString + dotation_id: + event: 1 + add_data: MyString + +two: + ip_address: MyString + dotation_id: + event: 1 + add_data: MyString diff --git a/test/models/visit_history_test.rb b/test/models/visit_history_test.rb new file mode 100644 index 0000000..09eb2d5 --- /dev/null +++ b/test/models/visit_history_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class VisitHistoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end