Added Sequel ORM for database handling; added navbar

This commit is contained in:
Gregory Ballantine 2023-07-05 18:04:31 -04:00
parent 6f680e72e5
commit 6c1c8bca0a
16 changed files with 122 additions and 4 deletions

3
.gitignore vendored
View File

@ -56,3 +56,6 @@ build-iPhoneSimulator/
# Used by RuboCop. Remote config files pulled in from inherit_from directive. # Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--* # .rubocop-https?--*
# Ignore local data
data/

View File

@ -4,8 +4,10 @@ gem 'sinatra', '~> 3.0'
gem 'sinatra-contrib', '~> 3.0' gem 'sinatra-contrib', '~> 3.0'
gem 'puma', '~> 6.3' gem 'puma', '~> 6.3'
gem 'sequel', '~> 5.70'
gem 'sqlite3', '~> 1.6'
group :development, :test do group :development, :test do
gem 'rerun' gem 'rerun'
gem 'wdm', '>= 0.1.0' if Gem.win_platform? gem 'wdm', '>= 0.1.0' if Gem.win_platform?
end end

View File

@ -20,6 +20,7 @@ GEM
rerun (0.14.0) rerun (0.14.0)
listen (~> 3.0) listen (~> 3.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
sequel (5.70.0)
sinatra (3.0.6) sinatra (3.0.6)
mustermann (~> 3.0) mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4) rack (~> 2.2, >= 2.2.4)
@ -31,6 +32,7 @@ GEM
rack-protection (= 3.0.6) rack-protection (= 3.0.6)
sinatra (= 3.0.6) sinatra (= 3.0.6)
tilt (~> 2.0) tilt (~> 2.0)
sqlite3 (1.6.3-x86_64-linux)
tilt (2.2.0) tilt (2.2.0)
PLATFORMS PLATFORMS
@ -39,8 +41,10 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
puma (~> 6.3) puma (~> 6.3)
rerun rerun
sequel (~> 5.70)
sinatra (~> 3.0) sinatra (~> 3.0)
sinatra-contrib (~> 3.0) sinatra-contrib (~> 3.0)
sqlite3 (~> 1.6)
BUNDLED WITH BUNDLED WITH
2.3.5 2.3.5

View File

@ -7,6 +7,12 @@ task :setup do
system("bundle install") system("bundle install")
end end
namespace :db do
task :migrate do
%x{sequel -m 'db/migrations/' 'sqlite://data/gamedata.db'}
end
end
namespace :server do namespace :server do
task :start do task :start do
ENV['APP_ENV'] = 'production' ENV['APP_ENV'] = 'production'

View File

@ -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__) root = ::File.dirname(__FILE__)
require ::File.join( root, 'src', 'server' ) require ::File.join( root, 'src', 'server' )
run GameData.new run GameData.new

3
config/defaults.yaml Normal file
View File

@ -0,0 +1,3 @@
database:
adapter: 'sqlite'
database: 'data/gamedata.db'

0
data/.gitkeep Normal file
View File

View File

@ -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

26
src/config.rb Normal file
View File

@ -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

5
src/models/benchmark.rb Normal file
View File

@ -0,0 +1,5 @@
class Benchmark < Sequel::Model
one_to_many :results
end

5
src/models/hardware.rb Normal file
View File

@ -0,0 +1,5 @@
class Hardware < Sequel::Model(:hardware)
one_to_many :results
end

3
src/models/init.rb Normal file
View File

@ -0,0 +1,3 @@
require_relative 'hardware'
require_relative 'benchmark'
require_relative 'result'

6
src/models/result.rb Normal file
View File

@ -0,0 +1,6 @@
class Result < Sequel::Model
many_to_one :hardware
many_to_one :benchmarks
end

View File

@ -1,8 +1,10 @@
class GameData < Sinatra::Base class GameData < Sinatra::Base
get '/' do get '/' do
results = Result.reverse(:updated_at).limit(10).all()
erb :'index/index', locals: { erb :'index/index', locals: {
title: 'Test!!!', title: 'Dashboard',
results: [] results: results
} }
end end

View File

@ -1,4 +1,11 @@
require 'sinatra/base' 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 # Base app
class GameData < Sinatra::Base class GameData < Sinatra::Base
@ -14,3 +21,5 @@ end
# Load routes # Load routes
require_relative 'routes/init' require_relative 'routes/init'
# Load models
require_relative 'models/init'

View File

@ -4,8 +4,8 @@
<li><a href="/">Dashboard</a></li> <li><a href="/">Dashboard</a></li>
<li><a href="/hardware">Hardware</a></li> <li><a href="/hardware">Hardware</a></li>
<li><a href="/benchmarks">Benchmarks</a></li> <li><a href="/benchmarks">Benchmarks</a></li>
<li><a href="/comparisons">Comparisons</a></li>
<li><a href="/results">Results</a></li> <li><a href="/results">Results</a></li>
<li><a href="/comparisons">Comparisons</a></li>
</ul> </ul>
</div> </div>
</div> </div>