Move status into a seperate package

This commit is contained in:
Tom Wiesing 2022-11-18 08:40:44 +01:00
parent 4752c0fcec
commit 3fada6ad38
No known key found for this signature in database
23 changed files with 220 additions and 197 deletions

View file

@ -8,7 +8,7 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
"github.com/tkw1536/goprogram/stream"
"golang.org/x/sync/errgroup"
@ -65,7 +65,7 @@ func (home *Home) homeRender() ([]byte, error) {
if err != nil {
return nil, err
}
context.Instances = make([]ingredient.Information, len(wissKIs))
context.Instances = make([]status.Information, len(wissKIs))
// determine their infos
var eg errgroup.Group
@ -86,7 +86,7 @@ func (home *Home) homeRender() ([]byte, error) {
}
type HomeContext struct {
Instances []ingredient.Information
Instances []status.Information
Time time.Time

View file

@ -8,7 +8,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"golang.org/x/sync/errgroup"
)
@ -20,18 +20,18 @@ var indexTemplate = static.AssetsControlIndex.MustParseShared(
)
type indexContext struct {
component.Observation
Instances []ingredient.Information
status.Distillery
Instances []status.Information
}
func (info *Info) index(r *http.Request) (idx indexContext, err error) {
idx.Observation, idx.Instances, err = info.Status(true)
idx.Distillery, idx.Instances, err = info.Status(true)
return
}
// Status produces a new observation of the distillery, and a new information of all instances
// The information on all instances is passed the given quick flag.
func (info *Info) Status(QuickInformation bool) (observation component.Observation, information []ingredient.Information, err error) {
func (info *Info) Status(QuickInformation bool) (target status.Distillery, information []status.Information, err error) {
var group errgroup.Group
group.Go(func() error {
@ -42,7 +42,7 @@ func (info *Info) Status(QuickInformation bool) (observation component.Observati
}
// get all of their info!
information = make([]ingredient.Information, len(all))
information = make([]status.Information, len(all))
for i, instance := range all {
{
i := i
@ -59,34 +59,34 @@ func (info *Info) Status(QuickInformation bool) (observation component.Observati
})
// gather all the observations
var flags component.ObservationFlags
for _, o := range info.Obervers {
var flags component.FetcherFlags
for _, o := range info.Fetchers {
o := o
group.Go(func() error {
return o.Observe(flags, &observation)
return o.Fetch(flags, &target)
})
}
// wait for all the observes to finish
// wait for all the fetchers to finish
if err := group.Wait(); err != nil {
return component.Observation{}, nil, err
return status.Distillery{}, nil, err
}
// count overall instances
for _, i := range information {
if i.Running {
observation.RunningCount++
target.RunningCount++
} else {
observation.StoppedCount++
target.StoppedCount++
}
}
observation.TotalCount = len(information)
target.TotalCount = len(information)
return
}
func (nfo *Info) Observe(flags component.ObservationFlags, observation *component.Observation) error {
observation.Time = time.Now().UTC()
observation.Config = nfo.Config
func (nfo *Info) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
target.Time = time.Now().UTC()
target.Config = nfo.Config
return nil
}

View file

@ -18,7 +18,7 @@ type Info struct {
component.Base
Analytics *lazy.PoolAnalytics
Obervers []component.Observer
Fetchers []component.DistilleryFetcher
Exporter *exporter.Exporter
Instances *instances.Instances

View file

@ -9,7 +9,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
)
@ -24,7 +24,7 @@ type instanceContext struct {
Time time.Time
Instance models.Instance
Info ingredient.Information
Info status.Information
}
func (info *Info) instance(r *http.Request) (is instanceContext, err error) {

View file

@ -4,6 +4,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/tkw1536/goprogram/lib/collection"
)
@ -77,8 +78,8 @@ func (log *Logger) Add(export models.Export) error {
return nil
}
// Observe writes the SnapshotLog into the given observation
func (logger *Logger) Observe(flags component.ObservationFlags, observation *component.Observation) (err error) {
observation.Backups, err = logger.For("")
// Fetch writes the SnapshotLog into the given observation
func (logger *Logger) Fetch(flags component.FetcherFlags, target *status.Distillery) (err error) {
target.Backups, err = logger.For("")
return
}

View file

@ -0,0 +1,14 @@
package component
import "github.com/FAU-CDI/wisski-distillery/internal/status"
type DistilleryFetcher interface {
Component
// Fetch fetches information from this component and writes it into target.
// Distinct DistilleryFetchers must write into distinct fields.
Fetch(flags FetcherFlags, target *status.Distillery) error
}
// FetcherFlags describes options for a DistilleryFetcher
type FetcherFlags struct{}

View file

@ -1,34 +0,0 @@
package component
import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
// Observer is a component with an Observe method
type Observer interface {
Component
// Observe observes this distillery component and writes the result into observation
// Distinct Observers must write into distinct fields.
Observe(flags ObservationFlags, observation *Observation) error
}
type ObservationFlags struct{}
// Observation represents fetched information about the distillery
type Observation struct {
Time time.Time // Time this obervation was built
// Configuration of the distillery
Config *config.Config
// number of instances
TotalCount int
RunningCount int
StoppedCount int
Backups []models.Export // list of backups
}