feta/database/imconnector.go

95 lines
2.7 KiB
Go
Raw Normal View History

2020-03-27 23:02:36 +00:00
package database
import (
2020-03-27 23:46:47 +00:00
"git.eeqj.de/sneak/feta/instance"
2020-03-28 01:17:52 +00:00
"github.com/rs/zerolog/log"
2020-03-27 23:02:36 +00:00
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
2020-03-28 01:17:52 +00:00
func (m *Manager) SaveInstance(i *instance.Instance) error {
i.Lock()
defer i.Unlock()
var x APInstance
if m.db.Where("UUID = ?", i.UUID).First(&x).RecordNotFound() {
log.Info().
Str("hostname", i.Hostname).
Msg("instance not in db, inserting")
// item does not exist in db yet, must insert
ni := APInstance{
UUID: i.UUID,
Disabled: i.Disabled,
ErrorCount: i.ErrorCount,
FSMState: i.Status(),
Fetching: i.Fetching,
HighestID: i.HighestID,
Hostname: i.Hostname,
Identified: i.Identified,
2020-03-28 02:57:58 +00:00
Implementation: i.Implementation,
2020-03-28 01:17:52 +00:00
NextFetch: i.NextFetch,
2020-04-05 01:16:37 +00:00
LastError: i.LastError,
2020-03-28 01:17:52 +00:00
NodeInfoURL: i.NodeInfoURL,
ServerImplementationString: i.ServerImplementationString,
ServerVersionString: i.ServerVersionString,
SuccessCount: i.SuccessCount,
}
r := m.db.Create(&ni)
return r.Error
} else {
log.Info().
Str("hostname", i.Hostname).
Str("id", i.UUID.String()).
Msg("instance found in db, updating")
// exists in db, update db
var ei APInstance
// EI EI uh-oh
m.db.Where("UUID = ?", i.UUID).First(&ei)
ei.Disabled = i.Disabled
ei.ErrorCount = i.ErrorCount
ei.FSMState = i.Status()
ei.Fetching = i.Fetching
ei.HighestID = i.HighestID
ei.Hostname = i.Hostname
2020-04-05 01:16:37 +00:00
ei.LastError = i.LastError
2020-03-28 01:17:52 +00:00
ei.Identified = i.Identified
ei.Implementation = string(i.Implementation)
ei.NextFetch = i.NextFetch
ei.NodeInfoURL = i.NodeInfoURL
ei.ServerImplementationString = i.ServerImplementationString
ei.ServerVersionString = i.ServerVersionString
ei.SuccessCount = i.SuccessCount
r := m.db.Save(&ei)
return r.Error
}
2020-03-27 23:02:36 +00:00
}
2020-03-27 23:46:47 +00:00
func (m *Manager) ListInstances() ([]*instance.Instance, error) {
output := make([]*instance.Instance, 0)
2020-03-28 02:57:58 +00:00
var results []APInstance
m.db.Find(&results)
for _, i := range results {
newinst := instance.New(func(x *instance.Instance) {
x.UUID = i.UUID
x.Disabled = i.Disabled
x.ErrorCount = i.ErrorCount
x.InitialFSMState = i.FSMState
x.Fetching = i.Fetching
x.HighestID = i.HighestID
x.Hostname = i.Hostname
2020-04-05 01:16:37 +00:00
x.LastError = i.LastError
2020-03-28 02:57:58 +00:00
x.Identified = i.Identified
x.Implementation = i.Implementation
x.NextFetch = i.NextFetch
x.NodeInfoURL = i.NodeInfoURL
x.ServerImplementationString = i.ServerImplementationString
x.ServerVersionString = i.ServerVersionString
x.SuccessCount = i.SuccessCount
})
output = append(output, newinst)
}
2020-03-27 23:46:47 +00:00
return output, nil
2020-03-27 23:02:36 +00:00
}