2016-09-28 19:32:58 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
Introduce ability to edit a dashboard cell
* Correct documentation for dashboards
* Exclude .git and use 'make run-dev' in 'make continuous'
* Fix dashboard deletion bug where id serialization was wrong
* Commence creation of overlay technology, add autoRefresh props to DashboardPage
* Enhance overlay magnitude of overlay technology
* Add confirm buttons to overlay technology
* Refactor ResizeContainer to accommodate arbitrary containers
* Refactor ResizeContainer to require explicit ResizeTop and ResizeBottom for clarity
* Add markup and styles for OverlayControls
* CellEditorOverlay needs a larger minimum bottom height to accommodate more things
* Revert Visualization to not use ResizeTop or flex-box
* Remove TODO and move to issue
* Refactor CellEditorOverlay to allow selection of graph type
* Style Overlay controls, move confirm buttons to own stylesheet
* Fix toggle buttons in overlay so active is actually active
* Block user-select on a few UI items
* Update cell query shape to support Visualization and LayoutRenderer
* Code cleanup
* Repair fixture schema; update props for affected components
* Wired up selectedGraphType and activeQueryID in CellEditorOverlay
* Wire up chooseMeasurements in QueryBuilder
Pass queryActions into QueryBuilder so that DataExplorer can provide
actionCreators and CellEditorOverlay can provide functions that
modify its component state
* semicolon cleanup
* Bind all queryModifier actions to component state with a stateReducer
* Overlay Technologies™ can add and delete a query from a cell
* Semicolon cleanup
* Add conversion of InfluxQL to QueryConfig for dashboards
* Update go deps to add influxdb at af72d9b0e4ebe95be30e89b160f43eabaf0529ed
* Updated docs for dashboard query config
* Update CHANGELOG to mention InfluxQL to QueryConfig
* Make reducer’s name more specific for clarity
* Remove 'table' as graphType
* Make graph renaming prettier
* Remove duplicate DashboardQuery in swagger.json
* Fix swagger to include name and links for Cell
* Refactor CellEditorOverlay to enable graph type selection
* Add link.self to all Dashboard cells; add bolt migrations
* Make dash graph names only hover on contents
* Consolidate timeRange format patterns, clean up
* Add cell endpoints to dashboards
* Include Line + Stat in Visualization Type list
* Add cell link to dashboards
* Enable step plot and stacked graph in Visualization
* Overlay Technologies are summonable and dismissable
* OverlayTechnologies saves changes to a cell
* Convert NameableGraph to createClass for state
This was converted from a pure function to encapsulate the state of the
buttons. An attempt was made previously to store this state in Redux,
but it proved too convoluted with the current state of the reducers for
cells and dashboards. Another effort must take place to separate a cell
reducer to manage the state of an individual cell in Redux in order for
this state to be sanely kept in Redux as well.
For the time being, this state is being kept in the component for the
sake of expeditiousness, since this is needed for Dashboards to be
released. A refactor of this will occur later.
* Cells should contain a links key in server response
* Clean up console logs
* Use live data instead of a cellQuery fixture
* Update docs for dashboard creation
* DB and RP are already present in the Command field
* Fix LayoutRenderer’s understanding of query schema
* Return a new object, rather that mutate in place
* Visualization doesn’t use activeQueryID
* Selected is an object, not a string
* QueryBuilder refactored to use query index instead of query id
* CellEditorOverlay refactored to use query index instead of query id
* ConfirmButtons doesn’t need to act on an item
* Rename functions to follow convention
* Queries are no longer guaranteed to have ids
* Omit WHERE and GROUP BY clauses when saving query
* Select new query on add in OverlayTechnologies
* Add click outside to dash graph menu, style menu also
* Change context menu from ... to a caret
More consistent with the rest of the UI, better affordance
* Hide graph context menu in presentation mode
Don’t want people editing a dashboard from presentation mode
* Move graph refreshing spinner so it does not overlap with context menu
* Wire up Cell Menu to Overlay Technologies
* Correct empty dashboard type
* Refactor dashboard spec fixtures
* Test syncDashboardCell reducer
* Remove Delete button from graph dropdown menu (for now)
* Update changelog
2017-03-24 00:12:33 +00:00
|
|
|
"context"
|
2017-12-16 21:32:21 +00:00
|
|
|
"fmt"
|
2017-12-16 09:01:14 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-10-20 17:50:19 +00:00
|
|
|
"path"
|
2016-09-28 19:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
2020-01-21 16:26:23 +00:00
|
|
|
"github.com/influxdata/chronograf/kv"
|
|
|
|
"github.com/influxdata/chronograf/mocks"
|
2020-11-26 08:14:18 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2016-09-28 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
const (
|
|
|
|
// ErrUnableToOpen means we had an issue establishing a connection (or creating the database)
|
|
|
|
ErrUnableToOpen = "Unable to open boltdb; is there a chronograf already running? %v"
|
|
|
|
// ErrUnableToBackup means we couldn't copy the db file into ./backup
|
|
|
|
ErrUnableToBackup = "Unable to backup your database prior to migrations: %v"
|
|
|
|
// ErrUnableToInitialize means we couldn't create missing Buckets (maybe a timeout)
|
|
|
|
ErrUnableToInitialize = "Unable to boot boltdb: %v"
|
2020-01-21 16:26:23 +00:00
|
|
|
// ErrUnableToUpdate means we had an issue changing the db schema
|
|
|
|
ErrUnableToUpdate = "Unable to store new version in boltdb: %v"
|
2020-01-22 19:08:22 +00:00
|
|
|
// Default boltdb path (from server/server.go).
|
|
|
|
defaultBoltPath = "chronograf-v1.db"
|
2017-12-18 20:06:17 +00:00
|
|
|
)
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
var (
|
|
|
|
// Ensure client implements kv.Store interface.
|
2020-01-22 19:08:22 +00:00
|
|
|
_ kv.Store = (*client)(nil)
|
|
|
|
_ kv.Tx = (*Tx)(nil)
|
|
|
|
_ kv.Bucket = (*Bucket)(nil)
|
2020-01-21 16:26:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// client is a client for the boltDB data store.
|
|
|
|
type client struct {
|
|
|
|
buildInfo chronograf.BuildInfo
|
|
|
|
buildStore *buildStore
|
|
|
|
db *bolt.DB
|
|
|
|
isNew bool
|
|
|
|
logger chronograf.Logger
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient initializes bolt client implementing the kv.Store interface.
|
|
|
|
func NewClient(ctx context.Context, opts ...Option) (*client, error) {
|
|
|
|
c := &client{
|
|
|
|
buildInfo: defaultBuildInfo,
|
|
|
|
path: defaultBoltPath,
|
|
|
|
logger: mocks.NewLogger(),
|
2016-10-10 22:00:27 +00:00
|
|
|
}
|
2020-01-21 16:26:23 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if err := opts[i](c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Introduce ability to edit a dashboard cell
* Correct documentation for dashboards
* Exclude .git and use 'make run-dev' in 'make continuous'
* Fix dashboard deletion bug where id serialization was wrong
* Commence creation of overlay technology, add autoRefresh props to DashboardPage
* Enhance overlay magnitude of overlay technology
* Add confirm buttons to overlay technology
* Refactor ResizeContainer to accommodate arbitrary containers
* Refactor ResizeContainer to require explicit ResizeTop and ResizeBottom for clarity
* Add markup and styles for OverlayControls
* CellEditorOverlay needs a larger minimum bottom height to accommodate more things
* Revert Visualization to not use ResizeTop or flex-box
* Remove TODO and move to issue
* Refactor CellEditorOverlay to allow selection of graph type
* Style Overlay controls, move confirm buttons to own stylesheet
* Fix toggle buttons in overlay so active is actually active
* Block user-select on a few UI items
* Update cell query shape to support Visualization and LayoutRenderer
* Code cleanup
* Repair fixture schema; update props for affected components
* Wired up selectedGraphType and activeQueryID in CellEditorOverlay
* Wire up chooseMeasurements in QueryBuilder
Pass queryActions into QueryBuilder so that DataExplorer can provide
actionCreators and CellEditorOverlay can provide functions that
modify its component state
* semicolon cleanup
* Bind all queryModifier actions to component state with a stateReducer
* Overlay Technologies™ can add and delete a query from a cell
* Semicolon cleanup
* Add conversion of InfluxQL to QueryConfig for dashboards
* Update go deps to add influxdb at af72d9b0e4ebe95be30e89b160f43eabaf0529ed
* Updated docs for dashboard query config
* Update CHANGELOG to mention InfluxQL to QueryConfig
* Make reducer’s name more specific for clarity
* Remove 'table' as graphType
* Make graph renaming prettier
* Remove duplicate DashboardQuery in swagger.json
* Fix swagger to include name and links for Cell
* Refactor CellEditorOverlay to enable graph type selection
* Add link.self to all Dashboard cells; add bolt migrations
* Make dash graph names only hover on contents
* Consolidate timeRange format patterns, clean up
* Add cell endpoints to dashboards
* Include Line + Stat in Visualization Type list
* Add cell link to dashboards
* Enable step plot and stacked graph in Visualization
* Overlay Technologies are summonable and dismissable
* OverlayTechnologies saves changes to a cell
* Convert NameableGraph to createClass for state
This was converted from a pure function to encapsulate the state of the
buttons. An attempt was made previously to store this state in Redux,
but it proved too convoluted with the current state of the reducers for
cells and dashboards. Another effort must take place to separate a cell
reducer to manage the state of an individual cell in Redux in order for
this state to be sanely kept in Redux as well.
For the time being, this state is being kept in the component for the
sake of expeditiousness, since this is needed for Dashboards to be
released. A refactor of this will occur later.
* Cells should contain a links key in server response
* Clean up console logs
* Use live data instead of a cellQuery fixture
* Update docs for dashboard creation
* DB and RP are already present in the Command field
* Fix LayoutRenderer’s understanding of query schema
* Return a new object, rather that mutate in place
* Visualization doesn’t use activeQueryID
* Selected is an object, not a string
* QueryBuilder refactored to use query index instead of query id
* CellEditorOverlay refactored to use query index instead of query id
* ConfirmButtons doesn’t need to act on an item
* Rename functions to follow convention
* Queries are no longer guaranteed to have ids
* Omit WHERE and GROUP BY clauses when saving query
* Select new query on add in OverlayTechnologies
* Add click outside to dash graph menu, style menu also
* Change context menu from ... to a caret
More consistent with the rest of the UI, better affordance
* Hide graph context menu in presentation mode
Don’t want people editing a dashboard from presentation mode
* Move graph refreshing spinner so it does not overlap with context menu
* Wire up Cell Menu to Overlay Technologies
* Correct empty dashboard type
* Refactor dashboard spec fixtures
* Test syncDashboardCell reducer
* Remove Delete button from graph dropdown menu (for now)
* Update changelog
2017-03-24 00:12:33 +00:00
|
|
|
}
|
2020-01-21 16:26:23 +00:00
|
|
|
|
|
|
|
c.buildStore = &buildStore{client: c}
|
|
|
|
|
|
|
|
return c, c.open(ctx)
|
2016-09-28 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// Option to change behavior of Open()
|
2020-01-21 16:26:23 +00:00
|
|
|
type Option func(c *client) error
|
|
|
|
|
|
|
|
// WithBuildInfo allows for setting this chronograf's build info.
|
|
|
|
func WithBuildInfo(bi chronograf.BuildInfo) Option {
|
|
|
|
return func(c *client) error {
|
|
|
|
c.buildInfo = bi
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
// WithLogger allows for setting this chronograf's logger.
|
|
|
|
func WithLogger(logger chronograf.Logger) Option {
|
|
|
|
return func(c *client) error {
|
|
|
|
c.logger = logger
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
// WithPath sets the path to the boltdb.
|
|
|
|
func WithPath(path string) Option {
|
|
|
|
return func(c *client) error {
|
|
|
|
c.path = path
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
// Open opens or creates the boltDB file.
|
|
|
|
func (c *client) open(ctx context.Context) error {
|
|
|
|
if _, err := os.Stat(c.path); os.IsNotExist(err) {
|
2017-12-16 21:06:43 +00:00
|
|
|
c.isNew = true
|
2017-12-18 20:06:17 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2017-12-16 21:06:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 19:32:58 +00:00
|
|
|
// Open database file.
|
2020-01-21 16:26:23 +00:00
|
|
|
db, err := bolt.Open(c.path, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
2016-09-28 19:32:58 +00:00
|
|
|
if err != nil {
|
2017-12-18 20:06:17 +00:00
|
|
|
return fmt.Errorf(ErrUnableToOpen, err)
|
2016-09-28 19:32:58 +00:00
|
|
|
}
|
|
|
|
c.db = db
|
2017-12-18 20:06:17 +00:00
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
if err = c.initialize(ctx); err != nil {
|
|
|
|
return fmt.Errorf(ErrUnableToInitialize, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.isNew {
|
|
|
|
if lastBuild, err := c.buildStore.Get(ctx); err == nil && lastBuild.Version != c.buildInfo.Version {
|
|
|
|
if err = c.backup(ctx, lastBuild, c.buildInfo); err != nil {
|
2017-12-18 20:06:17 +00:00
|
|
|
return fmt.Errorf(ErrUnableToBackup, err)
|
|
|
|
}
|
2020-01-21 16:26:23 +00:00
|
|
|
|
|
|
|
if err = c.buildStore.Update(ctx, c.buildInfo); err != nil {
|
|
|
|
return fmt.Errorf(ErrUnableToUpdate, err)
|
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// View opens up a view transaction against the store.
|
|
|
|
func (c *client) View(ctx context.Context, fn func(tx kv.Tx) error) error {
|
|
|
|
return c.db.View(func(tx *bolt.Tx) error {
|
|
|
|
return fn(&Tx{
|
|
|
|
tx: tx,
|
|
|
|
ctx: ctx,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update opens up an update transaction against the store.
|
|
|
|
func (c *client) Update(ctx context.Context, fn func(tx kv.Tx) error) error {
|
|
|
|
return c.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return fn(&Tx{
|
|
|
|
tx: tx,
|
|
|
|
ctx: ctx,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tx is a light wrapper around a boltdb transaction. It implements kv.Tx.
|
|
|
|
type Tx struct {
|
|
|
|
tx *bolt.Tx
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucketIfNotExists creates a bucket with the provided byte slice.
|
|
|
|
func (tx *Tx) CreateBucketIfNotExists(b []byte) (kv.Bucket, error) {
|
|
|
|
bkt, err := tx.tx.CreateBucketIfNotExists(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
2020-01-21 16:26:23 +00:00
|
|
|
return &Bucket{
|
|
|
|
bucket: bkt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bucket retrieves the bucket named b.
|
|
|
|
func (tx *Tx) Bucket(b []byte) kv.Bucket {
|
|
|
|
return &Bucket{
|
|
|
|
bucket: tx.tx.Bucket(b),
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
2020-01-21 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bucket implements kv.Bucket.
|
|
|
|
type Bucket struct {
|
|
|
|
bucket *bolt.Bucket
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves the value at the provided key.
|
|
|
|
func (b *Bucket) Get(key []byte) ([]byte, error) {
|
|
|
|
return b.bucket.Get(key), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put sets the value at the provided key.
|
|
|
|
func (b *Bucket) Put(key []byte, value []byte) error {
|
|
|
|
return b.bucket.Put(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the provided key.
|
|
|
|
func (b *Bucket) Delete(key []byte) error {
|
|
|
|
return b.bucket.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextSequence calls NextSequence on the bolt bucket.
|
|
|
|
func (b *Bucket) NextSequence() (uint64, error) {
|
|
|
|
return b.bucket.NextSequence()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForEach executes a function for each key/value pair in a bucket.
|
|
|
|
// If the provided function returns an error then the iteration is stopped and
|
|
|
|
// the error is returned to the caller. The provided function must not modify
|
|
|
|
// the bucket; this will result in undefined behavior.
|
|
|
|
func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
|
|
|
|
return b.bucket.ForEach(fn)
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// initialize creates Buckets that are missing
|
2020-01-21 16:26:23 +00:00
|
|
|
func (c *client) initialize(ctx context.Context) error {
|
2016-09-28 19:32:58 +00:00
|
|
|
if err := c.db.Update(func(tx *bolt.Tx) error {
|
2017-12-16 20:27:24 +00:00
|
|
|
// Always create Build bucket.
|
2020-01-21 16:26:23 +00:00
|
|
|
if _, err := tx.CreateBucketIfNotExists(buildBucket); err != nil {
|
2018-07-16 15:23:54 +00:00
|
|
|
return err
|
2018-07-06 19:54:35 +00:00
|
|
|
}
|
2016-09-28 19:32:58 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-16 20:27:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-31 23:50:03 +00:00
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
// Close the connection to the bolt database
|
2020-01-21 16:26:23 +00:00
|
|
|
func (c *client) Close() error {
|
2016-09-28 19:32:58 +00:00
|
|
|
if c.db != nil {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-20 17:50:19 +00:00
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
// backup makes a copy of the database to the backup/ directory, if necessary:
|
|
|
|
// - If this is a fresh install, don't create a backup and store the current version
|
|
|
|
// - If we are on the same version, don't create a backup
|
|
|
|
// - If the version has changed, create a backup and store the current version
|
|
|
|
func (c *client) backup(ctx context.Context, lastBuild, build chronograf.BuildInfo) error {
|
|
|
|
// The database was pre-existing, and the version has changed
|
|
|
|
// and so create a backup
|
|
|
|
c.logger.Info("Moving from version ", lastBuild.Version)
|
|
|
|
c.logger.Info("Moving to version ", build.Version)
|
|
|
|
|
|
|
|
return c.copy(ctx, lastBuild.Version)
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// copy creates a copy of the database in toFile
|
2020-01-21 16:26:23 +00:00
|
|
|
func (c *client) copy(ctx context.Context, version string) error {
|
|
|
|
backupDir := path.Join(path.Dir(c.path), "backup")
|
2017-12-18 20:06:17 +00:00
|
|
|
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
|
|
|
|
if err = os.Mkdir(backupDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
fromFile, err := os.Open(c.path)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
defer fromFile.Close()
|
2017-12-16 09:01:14 +00:00
|
|
|
|
2020-01-21 16:26:23 +00:00
|
|
|
toName := fmt.Sprintf("%s.%s", path.Base(c.path), version)
|
2017-12-16 21:32:21 +00:00
|
|
|
toPath := path.Join(backupDir, toName)
|
2017-12-17 22:11:27 +00:00
|
|
|
toFile, err := os.OpenFile(toPath, os.O_RDWR|os.O_CREATE, 0600)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
defer toFile.Close()
|
2017-12-16 09:01:14 +00:00
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
_, err = io.Copy(toFile, fromFile)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
c.logger.Info("Successfully created ", toPath)
|
2017-12-17 22:11:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|