Skip to content

Commit

Permalink
refactor: random playing around with sqlite and docker
Browse files Browse the repository at this point in the history
  • Loading branch information
ecshreve committed Jun 28, 2023
1 parent 4efa6ba commit 2385ecc
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 441 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

FROM golang:1.20

WORKDIR /app
WORKDIR /jepp

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY ./cmd/server ./cmd/server
COPY ./cmd/jepp ./cmd/jepp
COPY ./data/sqlite/jepp.db ./data/sqlite/jepp.db
COPY ./pkg/models ./pkg/models
COPY ./pkg/server ./pkg/server
COPY ./pkg/utils ./pkg/utils
COPY ./docs ./docs
COPY ./static/site ./static/site

RUN go build -o bin/server github.com/ecshreve/jepp/cmd/server
RUN go build -o bin/jepp github.com/ecshreve/jepp/cmd/jepp

EXPOSE 8880

CMD [ "./bin/server" ]
CMD [ "./bin/jepp" ]
8 changes: 4 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JEPP TODOS:
tweaks:
swap columns on homepage
add "info" card above example on homepage
swap columns on homepage @done(23-06-28 23:04)
add "info" card above example on homepage @done(23-06-28 23:03)

features:
pagination:
Expand All @@ -16,12 +16,12 @@ JEPP TODOS:
☐ requests per route

rate-limiting:
add rate limiting to api
add rate limiting to api @done(23-06-28 23:03)
- maybe not needed, rate limiting done via cloudflare

chores:
☐ add test coverage
godoc comments / release package
godoc comments / release package @done(23-06-28 23:03)
- added github action to dump sql and attach to release
☐ give endpoint descriptions a once over
☐ add codecov
Expand Down
61 changes: 7 additions & 54 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ dotenv: ['.env', '{{.ENV}}/.env.', '{{.HOME}}/.env']

includes:
docker: ./taskfiles/docker-tasks.yml
dumper:
jepp:
taskfile: ./taskfiles/go-tasks.yml
vars: {CMD_NAME: dumper}
scraper:
taskfile: ./taskfiles/go-tasks.yml
vars: {CMD_NAME: scrape}
server:
taskfile: ./taskfiles/go-tasks.yml
vars: {CMD_NAME: server}
vars: {CMD_NAME: jepp}

tasks:
default:
Expand All @@ -23,65 +17,24 @@ tasks:
build:
desc: Builds all binaries.
deps:
- task: dumper:build
- task: scraper:build
- task: server:build
- task: jepp:build

test:
desc: Runs all tests.
deps:
- task: dumper:test
- task: scraper:test
- task: server:test
- task: jepp:test
cmds:
- go test ./...

scrape:
desc: Runs the scraper.
generates:
- bin/scraper
sources:
- cmd/scrape/*
- pkg/scrape/*
- pkg/models/*
- pkg/utils/*
cmds:
- task: scraper:run

serve:
desc: Runs the server.
deps:
- task: server:build
- task: jepp:build
sources:
- cmd/server/*
- cmd/jepp/*
- pkg/server/*.go
- pkg/server/templates/**/*
- pkg/models/*
- pkg/utils/*
cmds:
- ./bin/server

genswag:
desc: Generates swagger docs for the API.
generates:
- docs/*
sources:
- cmd/server/*
- pkg/server/*
- pkg/models/*
- pkg/utils/*
cmds:
- swag fmt -d cmd/server,pkg/server,pkg/models,pkg/utils
- swag init -d cmd/server,pkg/server,pkg/models,pkg/utils

sql:dump:
desc: WIP Dumps the database to a file.
cmds:
- mysqldump -u ${DB_USER} -p${DB_PASS} -h ${DB_HOST} -P ${DB_PORT} --skip-comments --column-statistics=0 --databases ${DB_NAME} > data/${DB_NAME}_dump.sql
- gzip data/${DB_NAME}_dump.sql

sql:restore:
desc: WIP Restores the database from a file.
cmds:
- gunzip data/${DB_NAME}_dump.sql.gz
- mysql -u${DB_USER} -p${DB_PASS} -h ${DB_HOST} -P ${DB_PORT} < data/${DB_NAME}_dump.sql
- ./bin/jepp
1 change: 1 addition & 0 deletions _internal/dumper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NOT MAINTAINED
File renamed without changes.
1 change: 1 addition & 0 deletions _internal/scraper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NOT MAINTAINED
File renamed without changes.
7 changes: 3 additions & 4 deletions cmd/scrape/main.go → _internal/scraper/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main
package scraper

import (
"os"

"github.com/ecshreve/jepp/pkg/models"
"github.com/ecshreve/jepp/pkg/scraper"
log "github.com/sirupsen/logrus"
)

Expand All @@ -16,8 +15,8 @@ func main() {
log.Info("Starting Jepp scraper...")

// jdb := models.NewJeppDB("data/jepp.db")
jdb := models.NewJeppDB("data/sqlite/scrape.db")
num := scraper.ScrapeAndFillCluesForGame(jdb, 3109)
jdb := models.NewJeppDB()
num := ScrapeAndFillCluesForGame(jdb, 3109)
log.Infof("inserted %d clues", num)
// Change loop values to scrape different seasons.
// for i := 25; i > 24; i-- {
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 8 additions & 11 deletions cmd/server/main.go → cmd/jepp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"os"

"github.com/ecshreve/jepp/docs"
"github.com/ecshreve/jepp/pkg/models"
"github.com/ecshreve/jepp/pkg/server"
log "github.com/sirupsen/logrus"
)

// @title Jepp API Documentation
// @description This is a simple api to access jeopardy data.
// @version 1.0
// @BasePath /api
// @title Jepp API Documentation
// @description This is a simple api to access jeopardy data.
// @version 1.0
// @BasePath /api
//
// @contact.name shreve
// @contact.name shreve
//
// @license.name MIT License
// @license.url https://github.com/ecshreve/jepp/blob/main/LICENSE
// @license.name MIT License
// @license.url https://github.com/ecshreve/jepp/blob/main/LICENSE
func main() {
if os.Getenv("JEPP_ENV") == "prod" {
docs.SwaggerInfo.Host = "jepp.app"
Expand All @@ -26,9 +25,7 @@ func main() {
log.SetLevel(log.DebugLevel)
log.Info("Starting Jepp API server...")

jdb := models.NewJeppDB("data/sqlite/jepp.db")

srv := server.NewServer(jdb)
srv := server.NewServer()
if err := srv.Router.Run(":8880"); err != nil {
log.Fatal(err)
}
Expand Down
21 changes: 10 additions & 11 deletions pkg/models/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func (c *Category) Dump() []string {
}

// GetCategoryGameCount returns the number of games a category has appeared in.
func (db *JeppDB) GetCategoryGameCount(categoryID int64) (int64, error) {

func GetCategoryGameCount(categoryID int64) (int64, error) {
var count int64

err := db.Get(&count, "SELECT COUNT(DISTINCT game_id) FROM clue WHERE category_id = ?", categoryID)
Expand All @@ -34,7 +33,7 @@ func (db *JeppDB) GetCategoryGameCount(categoryID int64) (int64, error) {
}

// GetCategoryClueCount returns the number of clues a category has appeared in.
func (db *JeppDB) GetCategoryClueCount(categoryID int64) (int64, error) {
func GetCategoryClueCount(categoryID int64) (int64, error) {

var count int64

Expand All @@ -47,7 +46,7 @@ func (db *JeppDB) GetCategoryClueCount(categoryID int64) (int64, error) {
}

// InsertCategory inserts a category into the database.
func (db *JeppDB) InsertCategory(name string) (*Category, error) {
func InsertCategory(name string) (*Category, error) {

if name == "" {
return nil, oops.Errorf("cannot insert empty category")
Expand All @@ -74,7 +73,7 @@ func (db *JeppDB) InsertCategory(name string) (*Category, error) {
}

// UpdateCategory updates a category in the database.
func (db *JeppDB) UpdateCategory(c *Category) error {
func UpdateCategory(c *Category) error {

if c == nil {
return nil
Expand All @@ -95,7 +94,7 @@ func (db *JeppDB) UpdateCategory(c *Category) error {
}

// GetCategories returns all categories in the database.
func (db *JeppDB) GetCategories(limit int64) ([]Category, error) {
func GetCategories(limit int64) ([]Category, error) {
query := fmt.Sprintf("SELECT * FROM category ORDER BY category_id ASC LIMIT %d", limit)

var cc []Category
Expand All @@ -107,7 +106,7 @@ func (db *JeppDB) GetCategories(limit int64) ([]Category, error) {
}

// GetCategory returns the category with the given ID.
func (db *JeppDB) GetCategory(categoryID int64) (*Category, error) {
func GetCategory(categoryID int64) (*Category, error) {
query := fmt.Sprintf("SELECT * FROM category WHERE category_id=%d ORDER BY category_id DESC LIMIT 1", categoryID)

c := Category{}
Expand All @@ -118,7 +117,7 @@ func (db *JeppDB) GetCategory(categoryID int64) (*Category, error) {
return &c, nil
}

func (db *JeppDB) GetCategoryByName(categoryName string) (*Category, error) {
func GetCategoryByName(categoryName string) (*Category, error) {
query := "SELECT * FROM category WHERE name = ? LIMIT 1"

c := Category{}
Expand All @@ -131,7 +130,7 @@ func (db *JeppDB) GetCategoryByName(categoryName string) (*Category, error) {
}

// GetRandomCategory returns a single category from the database.
func (db *JeppDB) GetRandomCategory() (*Category, error) {
func GetRandomCategory() (*Category, error) {
c := Category{}
if err := db.Get(&c, "SELECT * FROM category ORDER BY RAND() LIMIT 1"); err != nil {
return nil, oops.Wrapf(err, "getting random category")
Expand All @@ -141,7 +140,7 @@ func (db *JeppDB) GetRandomCategory() (*Category, error) {
}

// GetRandomCategoryMany returns `limit` random categories from the database.
func (db *JeppDB) GetRandomCategoryMany(limit int64) ([]Category, error) {
func GetRandomCategoryMany(limit int64) ([]Category, error) {
query := fmt.Sprintf("SELECT * FROM category ORDER BY RANDOM() LIMIT %d", limit)

var cc []Category
Expand All @@ -153,7 +152,7 @@ func (db *JeppDB) GetRandomCategoryMany(limit int64) ([]Category, error) {
}

// GetCategoriesForGame returns all categories for a given game.
func (db *JeppDB) GetCategoriesForGame(gameID int64) ([]Category, error) {
func GetCategoriesForGame(gameID int64) ([]Category, error) {
var cc []Category
if err := db.Select(&cc, "SELECT clue.category_id, category.name FROM clue JOIN category ON clue.category_id = category.category_id WHERE game_id=? GROUP BY category_id LIMIT 20", gameID); err != nil {
return nil, oops.Wrapf(err, "could not get categories for game %d", gameID)
Expand Down
14 changes: 7 additions & 7 deletions pkg/models/clue.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Clue) String() string {
}

// InsertClue inserts a clue into the database.
func (db *JeppDB) InsertClue(c *Clue) error {
func InsertClue(c *Clue) error {
if c == nil {
return nil
}
Expand All @@ -70,7 +70,7 @@ func (db *JeppDB) InsertClue(c *Clue) error {
}

// UpdateClue updates a category in the database.
func (db *JeppDB) UpdateClue(c *Clue) error {
func UpdateClue(c *Clue) error {
if c == nil {
return nil
}
Expand Down Expand Up @@ -98,7 +98,7 @@ type CluesParams struct {

// ListClues returns a list of clues in the database, defaults to returning
// values ordered by game id descending.
func (db *JeppDB) GetClues(params CluesParams) ([]Clue, error) {
func GetClues(params CluesParams) ([]Clue, error) {
query := "SELECT * FROM clue"

if params.GameID != 0 {
Expand All @@ -125,7 +125,7 @@ func (db *JeppDB) GetClues(params CluesParams) ([]Clue, error) {
}

// GetClue returns a single clue from the database.
func (db *JeppDB) GetClue(clueID int64) (*Clue, error) {
func GetClue(clueID int64) (*Clue, error) {
query := fmt.Sprintf("SELECT * FROM clue WHERE clue_id=%d ORDER BY clue_id DESC LIMIT 1", clueID)

c := Clue{}
Expand All @@ -137,7 +137,7 @@ func (db *JeppDB) GetClue(clueID int64) (*Clue, error) {
}

// GetClue returns a single clue from the database.
func (db *JeppDB) GetRandomClue() (*Clue, error) {
func GetRandomClue() (*Clue, error) {
c := Clue{}
if err := db.Get(&c, "SELECT * FROM clue ORDER BY RANDOM() LIMIT 1"); err != nil {
return nil, oops.Wrapf(err, "getting random clue")
Expand All @@ -147,7 +147,7 @@ func (db *JeppDB) GetRandomClue() (*Clue, error) {
}

// GetRandomClueMany returns `limit` random clues from the database.
func (db *JeppDB) GetRandomClueMany(limit int64) ([]Clue, error) {
func GetRandomClueMany(limit int64) ([]Clue, error) {
query := fmt.Sprintf("SELECT * FROM clue ORDER BY RANDOM() LIMIT %d", limit)

var cc []Clue
Expand All @@ -159,7 +159,7 @@ func (db *JeppDB) GetRandomClueMany(limit int64) ([]Clue, error) {
}

// CountClues returns the number of clues in the database.
func (db *JeppDB) CountClues() (int64, error) {
func CountClues() (int64, error) {
var count int64
if err := db.Get(&count, "SELECT COUNT(*) FROM clue"); err != nil {
return -1, oops.Wrapf(err, "could not get count of clues")
Expand Down
Binary file added pkg/models/data/jepp.db
Binary file not shown.
Loading

0 comments on commit 2385ecc

Please sign in to comment.