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

@ -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
}