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
}

View file

@ -0,0 +1,23 @@
package status
import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
// Distillery holds status and analytical data about a distillery
type Distillery struct {
Time time.Time // Time when this information was built
// Configuration of the distillery
Config *config.Config
// number of instances
TotalCount int
RunningCount int
StoppedCount int
Backups []models.Export // list of backups
}

View file

@ -0,0 +1,117 @@
package status
import (
"fmt"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
)
// Information provides information about a single WissKI
type Information struct {
Time time.Time // Time this info was built
Slug string // slug
URL string // complete URL, including http(s)
Locked bool // Is this instance currently locked?
// Information about the running instance
Running bool
LastRebuild time.Time
LastUpdate time.Time
LastCron time.Time
// Statistics of the WissKI
Statistics Statistics
// List of backups made
Snapshots []models.Export
// List of SSH Keys
SSHKeys []string
// WissKI content information
NoPrefixes bool // TODO: Move this into the database
Prefixes []string // list of prefixes
Pathbuilders map[string]string // all the pathbuilders
}
// Statistics holds statistics generated by the WissKI module
type Statistics struct {
Activity struct {
MostVisited string `json:"mostVisited"`
PageVisits []struct {
URL string `json:"url"`
Visits int `json:"visits"`
} `json:"pageVisits"`
TotalEditsLastWeek int `json:"totalEditsLastWeek"`
} `json:"activity"`
Bundles BundleStatistics `json:"bundles"`
Triplestore struct {
Graphs []struct {
URI string `json:"uri"`
Count int `json:"triples"`
} `json:"graphStatistics"`
Total int `json:"totalTriples"`
} `json:"triplestore"`
Users struct {
LastLogin string `json:"lastLogin"`
TotalUsers int `json:"totalUsers"`
} `json:"users"`
}
type BundleStatistics struct {
Bundles []struct {
Label string `json:"label"`
MachineName string `json:"machineName"`
Count int `json:"entities"`
LastEdit phpx.TimeInt `json:"lastEdit"`
MainBundle phpx.PHPBoolean `json:"mainBundle"`
} `json:"bundleStatistics"`
TotalBundles int `json:"totalBundles"`
TotalMainBundles int `json:"totalMainBundles"`
}
type LastEdit struct {
Time time.Time
Valid bool
}
// LastEdit returns the last time any bundle was edited, and if any edit was bigger than the reference time
func (bs BundleStatistics) LastEdit() (le LastEdit) {
for _, bundle := range bs.Bundles {
time := bundle.LastEdit.Time()
if time.After(le.Time) {
le.Valid = true
le.Time = time
}
}
return
}
func (bs BundleStatistics) Summary() string {
var totalCount int
for _, bundle := range bs.Bundles {
totalCount += bundle.Count
}
if totalCount == 0 {
return ""
}
entitySubject := "Entities"
if totalCount == 1 {
entitySubject = "Entity"
}
bundleSubject := "Bundles"
if len(bs.Bundles) == 1 {
bundleSubject = "Bundle"
}
return fmt.Sprintf("%d %s in %d %s", totalCount, entitySubject, len(bs.Bundles), bundleSubject)
}

View file

@ -0,0 +1,2 @@
// Package status provides status information
package status

View file

@ -5,6 +5,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/locker"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/mstore"
@ -69,7 +70,7 @@ type LastRebuildFetcher struct {
Barrel *Barrel
}
func (lbr *LastRebuildFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (lbr *LastRebuildFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
info.LastRebuild, _ = lbr.Barrel.LastRebuild()
return
}

View file

@ -4,6 +4,7 @@ import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/stream"
@ -43,7 +44,7 @@ type LastCronFetcher struct {
Drush *Drush
}
func (lbr *LastCronFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (lbr *LastCronFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
if flags.Quick {
return
}

View file

@ -4,6 +4,7 @@ import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/mstore"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -54,7 +55,7 @@ type LastUpdateFetcher struct {
Drush *Drush
}
func (lbr *LastUpdateFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (lbr *LastUpdateFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
info.LastUpdate, err = lbr.Drush.LastUpdate()
return
}

View file

@ -1,6 +1,7 @@
package barrel
import (
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/tkw1536/goprogram/stream"
)
@ -20,7 +21,7 @@ type RunningFetcher struct {
Barrel *Barrel
}
func (rf *RunningFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (rf *RunningFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
info.Running, err = rf.Barrel.Running()
return
}

View file

@ -3,6 +3,7 @@ package ssh
import (
"io"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/barrel"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -32,7 +33,7 @@ func (ssh *SSH) Keys() ([]ssh.PublicKey, error) {
return sshx.ParseAllKeys(bytes), nil
}
func (sshx *SSH) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) error {
func (sshx *SSH) Fetch(flags ingredient.FetcherFlags, info *status.Information) error {
if flags.Quick {
return nil
}

View file

@ -1,132 +1,20 @@
package ingredient
import (
"fmt"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
)
// Fetcher is an ingredient with a fetch method
type Fetcher interface {
type WissKIFetcher interface {
Ingredient
// Fetch fetchs information with the given information and writes it into info.
// Distinct Fetchers must write into distinct fields.
Fetch(flags FetchFlags, info *Information) error
// Fetch fetches information from this ingredient and writes it into target.
// Distinct WissKIFetchers must write into distinct fields.
Fetch(flags FetcherFlags, target *status.Information) error
}
// FetchFlags specifies what information to fetch
type FetchFlags struct {
// FetcherFlags describes options for a WissKIFetcher
type FetcherFlags struct {
Quick bool
Server *phpx.Server
}
// Information represents fetched information about a WissKI
type Information struct {
Time time.Time // Time this info was built
// Generic Information
Slug string // slug
URL string // complete URL, including http(s)
Locked bool // Is this instance currently locked?
// Information about the running instance
Running bool
LastRebuild time.Time
LastUpdate time.Time
LastCron time.Time
// Statistics of the wisski (TODO: fix me)
Statistics Statistics
// List of backups made
Snapshots []models.Export
// List of SSH Keys
SSHKeys []string
// WissKI content information
NoPrefixes bool // TODO: Move this into the database
Prefixes []string // list of prefixes
Pathbuilders map[string]string // all the pathbuilders
}
type Statistics struct {
Activity struct {
MostVisited string `json:"mostVisited"`
PageVisits []struct {
URL string `json:"url"`
Visits int `json:"visits"`
} `json:"pageVisits"`
TotalEditsLastWeek int `json:"totalEditsLastWeek"`
} `json:"activity"`
Bundles BundleStatistics `json:"bundles"`
Triplestore struct {
Graphs []struct {
URI string `json:"uri"`
Count int `json:"triples"`
} `json:"graphStatistics"`
Total int `json:"totalTriples"`
} `json:"triplestore"`
Users struct {
LastLogin string `json:"lastLogin"`
TotalUsers int `json:"totalUsers"`
} `json:"users"`
}
type BundleStatistics struct {
Bundles []struct {
Label string `json:"label"`
MachineName string `json:"machineName"`
Count int `json:"entities"`
LastEdit phpx.TimeInt `json:"lastEdit"`
MainBundle phpx.PHPBoolean `json:"mainBundle"`
} `json:"bundleStatistics"`
TotalBundles int `json:"totalBundles"`
TotalMainBundles int `json:"totalMainBundles"`
}
type LastEdit struct {
Time time.Time
Valid bool
}
// LastEdit returns the last time any bundle was edited, and if any edit was bigger than the reference time
func (bs BundleStatistics) LastEdit() (le LastEdit) {
for _, bundle := range bs.Bundles {
time := bundle.LastEdit.Time()
if time.After(le.Time) {
le.Valid = true
le.Time = time
}
}
return
}
func (bs BundleStatistics) Summary() string {
var totalCount int
for _, bundle := range bs.Bundles {
totalCount += bundle.Count
}
if totalCount == 0 {
return ""
}
entitySubject := "Entities"
if totalCount == 1 {
entitySubject = "Entity"
}
bundleSubject := "Bundles"
if len(bs.Bundles) == 1 {
bundleSubject = "Bundle"
}
return fmt.Sprintf("%d %s in %d %s", totalCount, entitySubject, len(bs.Bundles), bundleSubject)
}

View file

@ -3,6 +3,7 @@ package info
import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
@ -13,16 +14,16 @@ type Info struct {
ingredient.Base
PHP *php.PHP
Fetchers []ingredient.Fetcher
Fetchers []ingredient.WissKIFetcher
Analytics *lazy.PoolAnalytics
}
// Information fetches information about this WissKI.
// TODO: Rework this to be able to determine what kind of information is available.
func (wisski *Info) Information(quick bool) (info ingredient.Information, err error) {
func (wisski *Info) Information(quick bool) (info status.Information, err error) {
// setup flags
flags := ingredient.FetchFlags{
flags := ingredient.FetcherFlags{
Quick: quick,
}
@ -47,7 +48,7 @@ func (wisski *Info) Information(quick bool) (info ingredient.Information, err er
return
}
func (wisski *Info) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) error {
func (wisski *Info) Fetch(flags ingredient.FetcherFlags, info *status.Information) error {
info.Time = time.Now().UTC()
info.Slug = wisski.Slug
info.URL = wisski.URL().String()

View file

@ -1,6 +1,9 @@
package info
import "github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
import (
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
type SnapshotsFetcher struct {
ingredient.Base
@ -8,7 +11,7 @@ type SnapshotsFetcher struct {
Info *Info
}
func (lbr *SnapshotsFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (lbr *SnapshotsFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
if flags.Quick {
return
}

View file

@ -2,6 +2,7 @@ package locker
import (
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
@ -17,7 +18,7 @@ func (lock *Locker) Locked() (locked bool) {
return
}
func (locker *Locker) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (locker *Locker) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
info.Locked = locker.Locked()
return
}

View file

@ -4,6 +4,7 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
"golang.org/x/exp/slices"
@ -44,7 +45,7 @@ func (pathbuilder *Pathbuilder) GetAll(server *phpx.Server) (pathbuilders map[st
return
}
func (pathbuilder *Pathbuilder) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (pathbuilder *Pathbuilder) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
if flags.Quick {
return
}

View file

@ -6,6 +6,7 @@ import (
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/mstore"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
@ -155,7 +156,7 @@ func (wisski *Prefixes) Update() error {
return prefix.SetAll(wisski.MStore, prefixes...)
}
func (prefixes *Prefixes) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (prefixes *Prefixes) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
info.NoPrefixes = prefixes.NoPrefix()
if flags.Quick {
// quick mode: grab only the cached prefixes

View file

@ -5,6 +5,7 @@ import (
"log"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
)
@ -19,7 +20,7 @@ type Stats struct {
var statsPHP string
// Get fetches all statistics from the server
func (stats *Stats) Get(server *phpx.Server) (data ingredient.Statistics, err error) {
func (stats *Stats) Get(server *phpx.Server) (data status.Statistics, err error) {
err = stats.PHP.ExecScript(server, &data, statsPHP, "export_statistics")
if err != nil {
log.Println(err)
@ -27,7 +28,7 @@ func (stats *Stats) Get(server *phpx.Server) (data ingredient.Statistics, err er
return
}
func (stats *Stats) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
func (stats *Stats) Fetch(flags ingredient.FetcherFlags, info *status.Information) (err error) {
if flags.Quick {
return
}

View file

@ -171,6 +171,5 @@ func ExportComponent[Component any, InitParams any, ConcreteComponentType any](
}
}
var component ConcreteComponentType
return component
panic("ExportComponent: Attempted to export unregistered component")
}