From 6c1c8bca0a57f95f87bd196d79b34750e586e9be Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 5 Jul 2023 18:04:31 -0400 Subject: [PATCH] Added Sequel ORM for database handling; added navbar --- .gitignore | 3 ++ Gemfile | 4 ++- Gemfile.lock | 4 +++ Rakefile | 6 ++++ config.ru | 4 +++ config/defaults.yaml | 3 ++ data/.gitkeep | 0 db/migrations/0001_add_initial_tables.rb | 40 ++++++++++++++++++++++++ src/config.rb | 26 +++++++++++++++ src/models/benchmark.rb | 5 +++ src/models/hardware.rb | 5 +++ src/models/init.rb | 3 ++ src/models/result.rb | 6 ++++ src/routes/index.rb | 6 ++-- src/server.rb | 9 ++++++ views/partials/navbar.erb | 2 +- 16 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 config/defaults.yaml create mode 100644 data/.gitkeep create mode 100644 db/migrations/0001_add_initial_tables.rb create mode 100644 src/config.rb create mode 100644 src/models/benchmark.rb create mode 100644 src/models/hardware.rb create mode 100644 src/models/init.rb create mode 100644 src/models/result.rb diff --git a/.gitignore b/.gitignore index d6aa672..53df52f 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,6 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* +# Ignore local data +data/ + diff --git a/Gemfile b/Gemfile index 7087b2c..f98efc2 100644 --- a/Gemfile +++ b/Gemfile @@ -4,8 +4,10 @@ gem 'sinatra', '~> 3.0' gem 'sinatra-contrib', '~> 3.0' gem 'puma', '~> 6.3' +gem 'sequel', '~> 5.70' +gem 'sqlite3', '~> 1.6' + group :development, :test do gem 'rerun' gem 'wdm', '>= 0.1.0' if Gem.win_platform? end - diff --git a/Gemfile.lock b/Gemfile.lock index 2c32f87..c9590b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,6 +20,7 @@ GEM rerun (0.14.0) listen (~> 3.0) ruby2_keywords (0.0.5) + sequel (5.70.0) sinatra (3.0.6) mustermann (~> 3.0) rack (~> 2.2, >= 2.2.4) @@ -31,6 +32,7 @@ GEM rack-protection (= 3.0.6) sinatra (= 3.0.6) tilt (~> 2.0) + sqlite3 (1.6.3-x86_64-linux) tilt (2.2.0) PLATFORMS @@ -39,8 +41,10 @@ PLATFORMS DEPENDENCIES puma (~> 6.3) rerun + sequel (~> 5.70) sinatra (~> 3.0) sinatra-contrib (~> 3.0) + sqlite3 (~> 1.6) BUNDLED WITH 2.3.5 diff --git a/Rakefile b/Rakefile index 615918e..384d50e 100644 --- a/Rakefile +++ b/Rakefile @@ -7,6 +7,12 @@ task :setup do system("bundle install") end +namespace :db do + task :migrate do + %x{sequel -m 'db/migrations/' 'sqlite://data/gamedata.db'} + end +end + namespace :server do task :start do ENV['APP_ENV'] = 'production' diff --git a/config.ru b/config.ru index 5548770..9f91c9b 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,7 @@ +# Load application config +require_relative 'src/config.rb' +$conf = Config.new(File.join(__dir__, 'config/defaults.yaml')) + root = ::File.dirname(__FILE__) require ::File.join( root, 'src', 'server' ) run GameData.new diff --git a/config/defaults.yaml b/config/defaults.yaml new file mode 100644 index 0000000..8f173a4 --- /dev/null +++ b/config/defaults.yaml @@ -0,0 +1,3 @@ +database: + adapter: 'sqlite' + database: 'data/gamedata.db' diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/db/migrations/0001_add_initial_tables.rb b/db/migrations/0001_add_initial_tables.rb new file mode 100644 index 0000000..d59ad35 --- /dev/null +++ b/db/migrations/0001_add_initial_tables.rb @@ -0,0 +1,40 @@ +Sequel.migration do + + up do + create_table(:hardware) do + primary_key :id + String :name, null: false + String :type, null: false + DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP + DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP + end + + create_table(:benchmarks) do + primary_key :id + String :name, null: false + String :description + String :scoring, null: false + DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP + DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP + end + + create_table(:results) do + primary_key :id + String :name, null: false + String :score, null: false + DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP + DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP + end + alter_table(:results) do + add_foreign_key :hardware_id, :hardware + add_foreign_key :benchmark_id, :benchmarks + end + end + + down do + drop_table(:hardware) + drop_table(:benchmarks) + drop_table(:results) + end + +end diff --git a/src/config.rb b/src/config.rb new file mode 100644 index 0000000..da2d625 --- /dev/null +++ b/src/config.rb @@ -0,0 +1,26 @@ +require 'yaml' + +class Config + + DEFAULT_CONFIG = 'config/defaults.yaml' + + def initialize(config_path) + @data = YAML::load_file(DEFAULT_CONFIG) + + if File.exists?(config_path) + @data.merge!(YAML::load_file(config_path)) + end + end + + def get(key, depth = 0) + bits = key.split('.') + value = @data + + bits.each do |bit| + value = value[bit] + end + + return value + end + +end diff --git a/src/models/benchmark.rb b/src/models/benchmark.rb new file mode 100644 index 0000000..bef091f --- /dev/null +++ b/src/models/benchmark.rb @@ -0,0 +1,5 @@ +class Benchmark < Sequel::Model + + one_to_many :results + +end diff --git a/src/models/hardware.rb b/src/models/hardware.rb new file mode 100644 index 0000000..0bdff73 --- /dev/null +++ b/src/models/hardware.rb @@ -0,0 +1,5 @@ +class Hardware < Sequel::Model(:hardware) + + one_to_many :results + +end diff --git a/src/models/init.rb b/src/models/init.rb new file mode 100644 index 0000000..fb936c7 --- /dev/null +++ b/src/models/init.rb @@ -0,0 +1,3 @@ +require_relative 'hardware' +require_relative 'benchmark' +require_relative 'result' diff --git a/src/models/result.rb b/src/models/result.rb new file mode 100644 index 0000000..ab92683 --- /dev/null +++ b/src/models/result.rb @@ -0,0 +1,6 @@ +class Result < Sequel::Model + + many_to_one :hardware + many_to_one :benchmarks + +end diff --git a/src/routes/index.rb b/src/routes/index.rb index c92e6c5..9201127 100644 --- a/src/routes/index.rb +++ b/src/routes/index.rb @@ -1,8 +1,10 @@ class GameData < Sinatra::Base get '/' do + results = Result.reverse(:updated_at).limit(10).all() + erb :'index/index', locals: { - title: 'Test!!!', - results: [] + title: 'Dashboard', + results: results } end diff --git a/src/server.rb b/src/server.rb index 9f382d2..73d3cb4 100755 --- a/src/server.rb +++ b/src/server.rb @@ -1,4 +1,11 @@ require 'sinatra/base' +require 'sequel' +require 'sqlite3' + +# Load the Sequel timestamps plugin +Sequel::Model.plugin(:timestamps) +# Initialize Sequel gem for database actions +DB = Sequel.connect(adapter: $conf.get('database.adapter'), database: $conf.get('database.database')) # Base app class GameData < Sinatra::Base @@ -14,3 +21,5 @@ end # Load routes require_relative 'routes/init' +# Load models +require_relative 'models/init' diff --git a/views/partials/navbar.erb b/views/partials/navbar.erb index 1645770..412c6bc 100644 --- a/views/partials/navbar.erb +++ b/views/partials/navbar.erb @@ -4,8 +4,8 @@
  • Dashboard
  • Hardware
  • Benchmarks
  • -
  • Comparisons
  • Results
  • +
  • Comparisons