Skip to content
Permalink
Browse files

Add demo mode. Put demo message back in.

  • Loading branch information...
upvalue committed Aug 13, 2017
1 parent 641d4c1 commit e01daa7b6854c0419ea0d3a6a0abce1119ab25ed
Showing with 1,275 additions and 1,247 deletions.
  1. +28 βˆ’0 backend/app.go
  2. +4 βˆ’0 backend/backend_test.go
  3. +42 βˆ’12 backend/database.go
  4. +1,191 βˆ’0 backend/database_seed_entries.go
  5. +6 βˆ’5 docs/manual.org
  6. +1 βˆ’1 dokku_run_meditations.sh
  7. +3 βˆ’0 templates/base.htm
  8. +0 βˆ’1,081 utils/gentestdb-journal.txt
  9. +0 βˆ’148 utils/gentestdb.py
@@ -34,6 +34,7 @@ type Configuration struct {
Development bool
// If true, run a database migration before starting
Migrate bool
Demo bool
// Message to be displayed in navbar, used in the demo instance
Message string
// Package path
@@ -57,6 +58,7 @@ var Config = Configuration{
SiteTitle: "meditations",
Development: true,
Migrate: false,
Demo: false,
Message: "",
PackagePath: "",
}
@@ -68,6 +70,7 @@ func loadConfig(c *cli.Context) {
Config.Port = c.Int("port")
Config.Migrate = c.Bool("migrate")
Config.Message = c.String("message")
Config.Demo = c.Bool("demo")
}

// App configures returns a meditations web application
@@ -191,6 +194,10 @@ func Main() {
}

serverflags := append(commonflags, []cli.Flag{
cli.BoolFlag{
Name: "demo",
Usage: "If true, will reset the database every hour",
},
cli.StringFlag{
Name: "message",
Usage: "A message that will be displayed at the top, used for demo deployment",
@@ -247,6 +254,7 @@ func Main() {
loadConfig(c)
Config.DBLog = true
DBOpen()

if Config.Migrate == true {
DBMigrate()
}
@@ -276,6 +284,26 @@ func Main() {
Flags: serverflags,
Action: func(c *cli.Context) {
loadConfig(c)

DBOpen()

if Config.Demo {
ticker := time.NewTicker(time.Second * 10)
// ticker := time.NewTicker(time.Hour)
today := time.Now()

DBSeed(today.Format("2006-01"))

go func() {
for t := range ticker.C {
fmt.Printf("Seeding database at %v!\n", t)
DBSeed(today.Format("2006-01"))
}
}()

}

// DBSeed
log.Printf("running with configuration %+v\n", Config)
log.Printf("starting server")
server := Server()
@@ -16,6 +16,10 @@ func TestSeed(t *testing.T) {
}
}

func TestStatistics(t *testing.T) {

}

func TestMain(m *testing.M) {
Config.DBPath = ":memory:"
DBOpen()
@@ -84,12 +84,15 @@ func DBSeed(seedFrom string) {
seedFrom = "2017-07"
}

log.Printf("Seeding database from %s\n", seedFrom)

day, err := time.Parse("2006-01", seedFrom)
if err != nil {
log.Fatalf("Expected date of format 2006-01 but got %s: %v", seedFrom, err)
}

DB.DropTableIfExists(&Task{}, &Entry{}, &Scope{}, &Comment{})
day = day.AddDate(0, 1, -1)
DB.DropTableIfExists(&Task{}, &Entry{}, &Scope{}, &Comment{}, &Tag{})
DBMigrate()

// deterministic random values
@@ -99,6 +102,7 @@ func DBSeed(seedFrom string) {
tx := DB.Begin()

// Generate 2 years of tasks with some random functions
dayi := day
for i := 0; i < 365*2; i++ {
// Diet will have some calories logged as a comment, and success based on that random number
calories := rand.Intn(500) + 2350
@@ -109,33 +113,59 @@ func DBSeed(seedFrom string) {
status = TaskIncomplete
}

tx.Save(seedTask("Diet", day, ScopeDay, status, fmt.Sprintf("%d calories\n", calories), 0))
tx.Save(seedTask("Exercise", day, ScopeDay, TaskComplete, fmt.Sprintf("ran %d minutes\n", minutes), minutes))
tx.Save(seedTask("Diet", dayi, ScopeDay, status, fmt.Sprintf("%d calories\n", calories), 0))
tx.Save(seedTask("Exercise", dayi, ScopeDay, TaskComplete, fmt.Sprintf("ran %d minutes\n", minutes), minutes))

// Project tasks
if day.Format("Monday") == "Monday" {
tx.Save(seedTask("Drawing practice", day, ScopeDay, TaskComplete, "Monday drawing practice", 60))
if dayi.Format("Monday") == "Monday" {
tx.Save(seedTask("Drawing practice", dayi, ScopeDay, TaskComplete, "Monday drawing practice", 60))
}

if day.Day() == 1 {
if dayi.Day() == 1 {
// Add month tasks on first of day
tx.Save(seedTask("Diet", day, ScopeMonth, status, "Eat less than 2500 calories a day", 0))
tx.Save(seedTask("Exercise", day, ScopeMonth, TaskComplete, "Run every day", 0))
tx.Save(seedTask("Diet", dayi, ScopeMonth, status, "Eat less than 2500 calories a day", 0))
tx.Save(seedTask("Exercise", dayi, ScopeMonth, TaskComplete, "Run every day", 0))
}

if day.AddDate(0, 0, -1).Year() != day.Year() {
tx.Save(seedTask("Diet", day, ScopeYear, status, "Eat less than 2500 calories a day", 0))
tx.Save(seedTask("Exercise", day, ScopeYear, TaskComplete, "Run every day", 0))
if dayi.AddDate(0, 0, -1).Year() != dayi.Year() {
tx.Save(seedTask("Diet", dayi, ScopeYear, status, "Eat less than 2500 calories a day", 0))
tx.Save(seedTask("Exercise", dayi, ScopeYear, TaskComplete, "Run every day", 0))
}

day = day.AddDate(0, 0, -1)
dayi = dayi.AddDate(0, 0, -1)
}

// Generate example project
tx.Save(&Scope{
Name: "Drawing practice",
Pinned: true,
})

// Generate example entries from built-in strings
tag := &Tag{
Name: "enchiridion",
}

tx.Save(tag)

dayi = day
for i, text := range dbseedentries {
tx.Save(&Entry{
Name: fmt.Sprintf("Enchiridion: Paragraph %d", len(dbseedentries)+1-i),
Date: dayi,
Body: text.Text,
LastBody: "",
Tags: []Tag{*tag},
})
dayi = dayi.AddDate(0, 0, -1)
}

tx.Save(&Entry{
Name: "Welcome to the Meditations Journal",
Date: day,
Body: `<p>This is an example instance of meditations, seeded with text from the Enchiridion.</p>`,
})

tx.Commit()
}

Oops, something went wrong.

0 comments on commit e01daa7

Please sign in to comment.
You can’t perform that action at this time.