Initial commit of Ocean's local development
This commit is contained in:
1 parent
9944a9e1df
commit
86451938ec
116 files changed
+3320
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
|
||||
func ActivateLoggingDevice() {
|
||||
Log.AddLoggingDevice(Database{})
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/*
|
||||
This is the database logging device. The logging events will be cached and after a time span or a maximum amount of
|
||||
events, the logging entries are written to the database.
|
||||
*/
|
||||
package DeviceDatabase
|
||||
@@ -0,0 +1,14 @@
|
||||
package DeviceDatabase
|
||||
|
||||
func (dev Database) Flush() {
|
||||
mutexCacheFull.Lock()
|
||||
defer mutexCacheFull.Unlock()
|
||||
|
||||
amount := len(cache)
|
||||
for counter := 0; counter < amount; counter++ {
|
||||
write2Database(<-cache)
|
||||
}
|
||||
|
||||
logDB.Logout()
|
||||
logDBSession.Close()
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "strconv"
|
||||
import "fmt"
|
||||
import "github.com/SommerEngineering/Ocean/ConfigurationDB"
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
func init() {
|
||||
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting now the database logging.`)
|
||||
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the database logging done.`)
|
||||
|
||||
initDatabase()
|
||||
if value, err := strconv.Atoi(ConfigurationDB.Read(`LogDBCacheSizeNumberOfEvents`)); err != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityHigh, LM.ImpactUnknown, LM.MessageNameCONFIGURATION, `It was not possible to read the LogDBCacheSizeNumberOfEvents configuration.`, `The default value will be used.`, fmt.Sprintf(`Default value is %d.`, cacheSizeNumberOfEvents))
|
||||
} else {
|
||||
cacheSizeNumberOfEvents = value
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, `Configuration LogDBCacheSizeNumberOfEvents was loaded.`, fmt.Sprintf(`The value is %d.`, cacheSizeNumberOfEvents))
|
||||
}
|
||||
|
||||
if value, err := strconv.Atoi(ConfigurationDB.Read(`LogDBCacheSizeTime2FlushSeconds`)); err != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityHigh, LM.ImpactUnknown, LM.MessageNameCONFIGURATION, `It was not possible to read the LogDBCacheSizeTime2FlushSeconds configuration.`, `The default value will be used.`, fmt.Sprintf(`Default value is %d.`, cacheSizeTime2FlushSeconds))
|
||||
} else {
|
||||
cacheSizeTime2FlushSeconds = value
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, `Configuration LogDBCacheSizeTime2FlushSeconds was loaded.`, fmt.Sprintf(`The value is %d.`, cacheSizeTime2FlushSeconds))
|
||||
}
|
||||
|
||||
cache = make(chan LogDBEntry, cacheSizeNumberOfEvents)
|
||||
initTimeout()
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "labix.org/v2/mgo"
|
||||
import "github.com/SommerEngineering/Ocean/ConfigurationDB"
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
func initDatabase() {
|
||||
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Checking and init the logging database collection.`)
|
||||
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Checking and init the logging database collection done.`)
|
||||
|
||||
databaseHost := ConfigurationDB.Read(`LogDBHost`)
|
||||
databaseDB := ConfigurationDB.Read(`LogDBDatabase`)
|
||||
databaseUsername := ConfigurationDB.Read(`LogDBUsername`)
|
||||
databasePassword := ConfigurationDB.Read(`LogDBPassword`)
|
||||
|
||||
// Connect to MongoDB:
|
||||
if newSession, errDial := mgo.Dial(databaseHost); errDial != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+databaseHost, errDial.Error())
|
||||
return
|
||||
} else {
|
||||
logDBSession = newSession
|
||||
}
|
||||
|
||||
// Use the correct database:
|
||||
logDB = logDBSession.DB(databaseDB)
|
||||
|
||||
// Login:
|
||||
if errLogin := logDB.Login(databaseUsername, databasePassword); errLogin != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+databaseUsername, errLogin.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Get the collection:
|
||||
logDBCollection = logDB.C(`Logbook`)
|
||||
|
||||
//
|
||||
// Take care about all the indexes:
|
||||
//
|
||||
indexTimeUTC := mgo.Index{}
|
||||
indexTimeUTC.Key = []string{`TimeUTC`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTC)
|
||||
|
||||
indexProject := mgo.Index{}
|
||||
indexProject.Key = []string{`Project`}
|
||||
logDBCollection.EnsureIndex(indexProject)
|
||||
|
||||
indexSender := mgo.Index{}
|
||||
indexSender.Key = []string{`Sender`}
|
||||
logDBCollection.EnsureIndex(indexSender)
|
||||
|
||||
indexCategory := mgo.Index{}
|
||||
indexCategory.Key = []string{`Category`}
|
||||
logDBCollection.EnsureIndex(indexCategory)
|
||||
|
||||
indexLevel := mgo.Index{}
|
||||
indexLevel.Key = []string{`Level`}
|
||||
logDBCollection.EnsureIndex(indexLevel)
|
||||
|
||||
indexSeverity := mgo.Index{}
|
||||
indexSeverity.Key = []string{`Severity`}
|
||||
logDBCollection.EnsureIndex(indexSeverity)
|
||||
|
||||
indexImpact := mgo.Index{}
|
||||
indexImpact.Key = []string{`Impact`}
|
||||
logDBCollection.EnsureIndex(indexImpact)
|
||||
|
||||
indexMessageName := mgo.Index{}
|
||||
indexMessageName.Key = []string{`MessageName`}
|
||||
logDBCollection.EnsureIndex(indexMessageName)
|
||||
|
||||
indexMessageDescription := mgo.Index{}
|
||||
indexMessageDescription.Key = []string{`MessageDescription`}
|
||||
logDBCollection.EnsureIndex(indexMessageDescription)
|
||||
|
||||
indexProjectTimeUTC := mgo.Index{}
|
||||
indexProjectTimeUTC.Key = []string{`Project`, `TimeUTC`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTC)
|
||||
|
||||
indexProjectSender := mgo.Index{}
|
||||
indexProjectSender.Key = []string{`Project`, `Sender`}
|
||||
logDBCollection.EnsureIndex(indexProjectSender)
|
||||
|
||||
indexProjectCategory := mgo.Index{}
|
||||
indexProjectCategory.Key = []string{`Project`, `Category`}
|
||||
logDBCollection.EnsureIndex(indexProjectCategory)
|
||||
|
||||
indexProjectLevel := mgo.Index{}
|
||||
indexProjectLevel.Key = []string{`Project`, `Level`}
|
||||
logDBCollection.EnsureIndex(indexProjectLevel)
|
||||
|
||||
indexProjectSeverity := mgo.Index{}
|
||||
indexProjectSeverity.Key = []string{`Project`, `Severity`}
|
||||
logDBCollection.EnsureIndex(indexProjectSeverity)
|
||||
|
||||
indexProjectImpact := mgo.Index{}
|
||||
indexProjectImpact.Key = []string{`Project`, `Impact`}
|
||||
logDBCollection.EnsureIndex(indexProjectImpact)
|
||||
|
||||
indexProjectMessageName := mgo.Index{}
|
||||
indexProjectMessageName.Key = []string{`Project`, `MessageName`}
|
||||
logDBCollection.EnsureIndex(indexProjectMessageName)
|
||||
|
||||
indexProjectMessageDescription := mgo.Index{}
|
||||
indexProjectMessageDescription.Key = []string{`Project`, `MessageDescription`}
|
||||
logDBCollection.EnsureIndex(indexProjectMessageDescription)
|
||||
|
||||
indexProjectTimeUTCSender := mgo.Index{}
|
||||
indexProjectTimeUTCSender.Key = []string{`Project`, `TimeUTC`, `Sender`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCSender)
|
||||
|
||||
indexProjectTimeUTCCategory := mgo.Index{}
|
||||
indexProjectTimeUTCCategory.Key = []string{`Project`, `TimeUTC`, `Category`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCCategory)
|
||||
|
||||
indexProjectTimeUTCLevel := mgo.Index{}
|
||||
indexProjectTimeUTCLevel.Key = []string{`Project`, `TimeUTC`, `Level`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCLevel)
|
||||
|
||||
indexProjectTimeUTCSeverity := mgo.Index{}
|
||||
indexProjectTimeUTCSeverity.Key = []string{`Project`, `TimeUTC`, `Severity`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCSeverity)
|
||||
|
||||
indexProjectTimeUTCImpact := mgo.Index{}
|
||||
indexProjectTimeUTCImpact.Key = []string{`Project`, `TimeUTC`, `Impact`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCImpact)
|
||||
|
||||
indexProjectTimeUTCMessageName := mgo.Index{}
|
||||
indexProjectTimeUTCMessageName.Key = []string{`Project`, `TimeUTC`, `MessageName`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCMessageName)
|
||||
|
||||
indexProjectTimeUTCMessageDescription := mgo.Index{}
|
||||
indexProjectTimeUTCMessageDescription.Key = []string{`Project`, `TimeUTC`, `MessageDescription`}
|
||||
logDBCollection.EnsureIndex(indexProjectTimeUTCMessageDescription)
|
||||
|
||||
indexTimeUTCSender := mgo.Index{}
|
||||
indexTimeUTCSender.Key = []string{`TimeUTC`, `Sender`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCSender)
|
||||
|
||||
indexTimeUTCCategory := mgo.Index{}
|
||||
indexTimeUTCCategory.Key = []string{`TimeUTC`, `Category`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCCategory)
|
||||
|
||||
indexTimeUTCLevel := mgo.Index{}
|
||||
indexTimeUTCLevel.Key = []string{`TimeUTC`, `Level`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCLevel)
|
||||
|
||||
indexTimeUTCSeverity := mgo.Index{}
|
||||
indexTimeUTCSeverity.Key = []string{`TimeUTC`, `Severity`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCSeverity)
|
||||
|
||||
indexTimeUTCImpact := mgo.Index{}
|
||||
indexTimeUTCImpact.Key = []string{`TimeUTC`, `Impact`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCImpact)
|
||||
|
||||
indexTimeUTCMessageName := mgo.Index{}
|
||||
indexTimeUTCMessageName.Key = []string{`TimeUTC`, `MessageName`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCMessageName)
|
||||
|
||||
indexTimeUTCMessageDescription := mgo.Index{}
|
||||
indexProjectTimeUTCMessageDescription.Key = []string{`TimeUTC`, `MessageDescription`}
|
||||
logDBCollection.EnsureIndex(indexTimeUTCMessageDescription)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "time"
|
||||
import "github.com/SommerEngineering/Ocean/Shutdown"
|
||||
|
||||
// Case: The cache is full
|
||||
func cacheFull() {
|
||||
mutexCacheFull.Lock()
|
||||
defer mutexCacheFull.Unlock()
|
||||
|
||||
if len(cache) < cacheSizeNumberOfEvents {
|
||||
return
|
||||
}
|
||||
|
||||
for counter := 0; counter < cacheSizeNumberOfEvents; counter++ {
|
||||
write2Database(<-cache)
|
||||
}
|
||||
}
|
||||
|
||||
// Case: Time out
|
||||
func initTimeout() {
|
||||
|
||||
go func() {
|
||||
for {
|
||||
|
||||
if Shutdown.IsDown() {
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(cacheSizeTime2FlushSeconds) * time.Second)
|
||||
mutexCacheFull.Lock()
|
||||
amount := len(cache)
|
||||
for counter := 0; counter < amount; counter++ {
|
||||
write2Database(<-cache)
|
||||
}
|
||||
mutexCacheFull.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
type Database struct {
|
||||
}
|
||||
|
||||
func (dev Database) Log(entries []Meta.Entry) {
|
||||
|
||||
//
|
||||
// Can not log here to prevent endless loop (consumer is also producer)
|
||||
//
|
||||
|
||||
write2Cache(entries)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "time"
|
||||
|
||||
type LogDBEntry struct {
|
||||
TimeUTC time.Time `bson:"TimeUTC"`
|
||||
Project string `bson:"Project"`
|
||||
Sender string `bson:"Sender"`
|
||||
Category string `bson:"Category"`
|
||||
Level string `bson:"Level"`
|
||||
Severity string `bson:"Severity"`
|
||||
Impact string `bson:"Impact"`
|
||||
MessageName string `bson:"MessageName"`
|
||||
MessageDescription string `bson:"MessageDescription"`
|
||||
Parameters []string `bson:"Parameters"`
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "sync"
|
||||
import "labix.org/v2/mgo"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
var (
|
||||
senderName LM.Sender = `System::Logger::Database`
|
||||
mutexCacheFull sync.Mutex = sync.Mutex{}
|
||||
cache chan LogDBEntry = nil
|
||||
cacheSizeNumberOfEvents int = 50
|
||||
cacheSizeTime2FlushSeconds int = 6
|
||||
logDB *mgo.Database = nil
|
||||
logDBSession *mgo.Session = nil
|
||||
logDBCollection *mgo.Collection = nil
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
package DeviceDatabase
|
||||
|
||||
import "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
func write2Cache(entries []Meta.Entry) {
|
||||
for _, entry := range entries {
|
||||
if len(cache) == cacheSizeNumberOfEvents {
|
||||
go cacheFull()
|
||||
}
|
||||
|
||||
logDBentry := LogDBEntry{}
|
||||
logDBentry.Category = Meta.FormatCategory(entry.Category)
|
||||
logDBentry.Impact = Meta.FormatImpact(entry.Impact)
|
||||
logDBentry.Level = Meta.FormatLevel(entry.Level)
|
||||
logDBentry.MessageDescription = entry.MessageDescription
|
||||
logDBentry.MessageName = string(entry.MessageName)
|
||||
logDBentry.Parameters = entry.Parameters
|
||||
logDBentry.Project = entry.Project
|
||||
logDBentry.Sender = string(entry.Sender)
|
||||
logDBentry.Severity = Meta.FormatSeverity(entry.Severity)
|
||||
logDBentry.TimeUTC = entry.Time
|
||||
cache <- logDBentry
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package DeviceDatabase
|
||||
|
||||
func write2Database(entry LogDBEntry) {
|
||||
if err := logDBCollection.Insert(entry); err != nil {
|
||||
// Can not log here to prevent endless loop (consumer is also producer)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user