Skip to content
Permalink
Browse files

progressbar for `fhirbase init` command

  • Loading branch information...
mlapshin committed Sep 19, 2018
1 parent 4dc998a commit 91fe94055c002610655c856c7d501cce0c84a3db
Showing with 38 additions and 8 deletions.
  1. +36 −7 dbinit.go
  2. +2 −1 dbinit_test.go
@@ -2,18 +2,21 @@ package main

import (
"fmt"
"time"

"github.com/gobuffalo/packr"
"github.com/jackc/pgx"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/urfave/cli"

"log"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)

type initProgressCb func(curIdx int, total int64, duration time.Duration)

// PerformInit actually performs init operation
func PerformInit(db *pgx.Conn, fhirVersion string) error {
func PerformInit(db *pgx.Conn, fhirVersion string, cb initProgressCb) error {
var schemaStatements []string
var functionStatements []string

@@ -42,12 +45,18 @@ func PerformInit(db *pgx.Conn, fhirVersion string) error {
return errors.Wrap(err, "Cannot parse function definitions")
}

for _, stmt := range append(schemaStatements, functionStatements...) {
allStmts := append(schemaStatements, functionStatements...)

t := time.Now()
for i, stmt := range allStmts {
_, err = db.Exec(stmt)

if err != nil {
return errors.Wrapf(err, "PG error while executing statement:\n%s\n", stmt)
}

cb(i, int64(len(allStmts)), time.Since(t))
t = time.Now()
}

return nil
@@ -56,15 +65,35 @@ func PerformInit(db *pgx.Conn, fhirVersion string) error {
// InitCommand loads FHIR schema into database
func InitCommand(c *cli.Context) error {
fhirVersion := c.GlobalString("fhir")

db := GetConnection(nil)
err := PerformInit(db, fhirVersion)

bars := mpb.New(
mpb.WithWidth(100),
)

bar := bars.AddBar(int64(1),
mpb.AppendDecorators(
decor.Percentage(decor.WC{W: 3}),
decor.AverageETA(decor.ET_STYLE_MMSS, decor.WC{W: 6}),
),
mpb.PrependDecorators(decor.CountersNoUnit("%d / %d", decor.WC{W: 10})))

err := PerformInit(db, fhirVersion, func(curIdx int, total int64, duration time.Duration) {
if curIdx == 0 {
bar.SetTotal(total, false)
}

bar.IncrBy(1, duration)
})

if err != nil {
bars.Abort(bar, false)
return errors.Wrap(err, "Failed to perform init command. Perhaps target database is not empty?")
}

log.Printf("Database initialized with FHIR schema version '%s'", fhirVersion)
bars.Wait()

fmt.Printf("Database initialized with FHIR schema version '%s'\n", fhirVersion)

return nil
}
@@ -5,6 +5,7 @@ import (
"log"
"os"
"testing"
"time"

"github.com/jackc/pgx"
)
@@ -53,7 +54,7 @@ func TestInitAllSchemas(t *testing.T) {
}

db := GetConnection(&DbConfig)
PerformInit(db, version)
PerformInit(db, version, func(c int, t int64, d time.Duration) {})
db.Close()
}
}

0 comments on commit 91fe940

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