diff --git a/Log/AddDevice.go b/Log/AddDevice.go index 30906e2..030347b 100644 --- a/Log/AddDevice.go +++ b/Log/AddDevice.go @@ -16,6 +16,9 @@ func AddLoggingDevice(device Device.Device) { go func() { mutexDevices.Lock() devices.PushBack(newDevice) + + // Let each device know what the project name is: + newDevice.SetProjectName(projectName) mutexDevices.Unlock() }() } diff --git a/Log/Device/Device.go b/Log/Device/Device.go index e826601..69782b8 100644 --- a/Log/Device/Device.go +++ b/Log/Device/Device.go @@ -8,4 +8,5 @@ import ( type Device interface { Log(logEntries []Meta.Entry) Flush() + SetProjectName(projectName string) } diff --git a/Log/DeviceConsole/SetProjectName.go b/Log/DeviceConsole/SetProjectName.go new file mode 100644 index 0000000..16d9f2b --- /dev/null +++ b/Log/DeviceConsole/SetProjectName.go @@ -0,0 +1,6 @@ +package DeviceConsole + +// Set the project name. This function is not necessary for the console logger. +func (dev Console) SetProjectName(projectName string) { + +} diff --git a/Log/DeviceDatabase/ReadCustom.go b/Log/DeviceDatabase/ReadCustom.go index 89927ba..5b0e82b 100644 --- a/Log/DeviceDatabase/ReadCustom.go +++ b/Log/DeviceDatabase/ReadCustom.go @@ -1,18 +1,34 @@ package DeviceDatabase import ( + "github.com/SommerEngineering/Ocean/Log" + LM "github.com/SommerEngineering/Ocean/Log/Meta" "gopkg.in/mgo.v2/bson" + "math" ) -func ReadCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage string) (events []LogDBEntry) { +func ReadCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage string) (events []LogDBEntry, numPages int) { // // TODO => Is currently a stub // // Define the query: - query := logDBCollection.Find(bson.D{}).Sort(`-TimeUTC`).Limit(26) - count := 26 + query := logDBCollection.Find(bson.D{}).Sort(`-TimeUTC`) // TODO: projectName!!! + + // How many record we have all over? + numRecords := loggingViewerPageSize + numPages = 1 + if number, err := query.Count(); err != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityNone, LM.ImpactNone, LM.MessageNameDATABASE, `It was not possible to find the total number of records for the custom logging reader.`, err.Error()) + } else { + numRecords = number + numPages = int(math.Ceil(float64(numRecords) / float64(loggingViewerPageSize))) + } + + // Set now the page's record limit: + query = query.Limit(loggingViewerPageSize) + count := loggingViewerPageSize // Execute the query and count the results: if n, err := query.Count(); err == nil { diff --git a/Log/DeviceDatabase/ReadLatest.go b/Log/DeviceDatabase/ReadLatest.go index a544f78..b65e23b 100644 --- a/Log/DeviceDatabase/ReadLatest.go +++ b/Log/DeviceDatabase/ReadLatest.go @@ -1,14 +1,30 @@ package DeviceDatabase import ( + "github.com/SommerEngineering/Ocean/Log" + LM "github.com/SommerEngineering/Ocean/Log/Meta" "gopkg.in/mgo.v2/bson" + "math" ) // Read the latest logging events from the database. -func ReadLatest() (events []LogDBEntry) { +func ReadLatest() (events []LogDBEntry, numPages int) { // Define the query: - query := logDBCollection.Find(bson.D{}).Sort(`-TimeUTC`).Limit(26) - count := 26 + query := logDBCollection.Find(bson.D{}).Sort(`-TimeUTC`) // TODO: projectName!!! + + // How many record we have all over? + numRecords := loggingViewerPageSize + numPages = 1 + if number, err := query.Count(); err != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityNone, LM.ImpactNone, LM.MessageNameDATABASE, `It was not possible to find the total number of records for the latest logging reader.`, err.Error()) + } else { + numRecords = number + numPages = int(math.Ceil(float64(numRecords) / float64(loggingViewerPageSize))) + } + + // Set now the page's record limit: + query = query.Limit(loggingViewerPageSize) + count := loggingViewerPageSize // Execute the query and count the results: if n, err := query.Count(); err == nil { diff --git a/Log/DeviceDatabase/SetProjectName.go b/Log/DeviceDatabase/SetProjectName.go new file mode 100644 index 0000000..98ea54e --- /dev/null +++ b/Log/DeviceDatabase/SetProjectName.go @@ -0,0 +1,6 @@ +package DeviceDatabase + +// Set the project name. +func (dev Database) SetProjectName(name string) { + projectName = name +} diff --git a/Log/DeviceDatabase/Variables.go b/Log/DeviceDatabase/Variables.go index acd0914..f2b4f1e 100644 --- a/Log/DeviceDatabase/Variables.go +++ b/Log/DeviceDatabase/Variables.go @@ -8,18 +8,20 @@ import ( ) var ( - // This is the name for logging event from this package: - senderName LM.Sender = `System::Logger::Database` - mutexCacheFull sync.Mutex = sync.Mutex{} - mutexCacheSenderNames sync.RWMutex = sync.RWMutex{} - mutexCacheMessageNames sync.RWMutex = sync.RWMutex{} - cache chan LogDBEntry = nil - cacheSizeNumberOfEvents int = 50 - cacheSizeTime2FlushSeconds int = 6 - nameCachesRefreshTimeSeconds int = 300 - cacheSenderNames []Scheme.Sender = nil - cacheMessageNames []Scheme.MessageNames = nil - logDB *mgo.Database = nil - logDBSession *mgo.Session = nil - logDBCollection *mgo.Collection = nil + senderName LM.Sender = `System::Logger::Database` // This is the name for logging event from this package + mutexCacheFull sync.Mutex = sync.Mutex{} // Mutex for the cache full event + mutexCacheSenderNames sync.RWMutex = sync.RWMutex{} // Read/write mutex for the sender names + mutexCacheMessageNames sync.RWMutex = sync.RWMutex{} // Read/write mutex for the messages names + cache chan LogDBEntry = nil // The cache + cacheSizeNumberOfEvents int = 50 // How many events are cached? + cacheSizeTime2FlushSeconds int = 6 // Wait how many seconds before forcing to write events? + nameCachesRefreshTimeSeconds int = 300 // Wait how many seconds until we reload the sender and message names? + cacheSenderNames []Scheme.Sender = nil // Cache for the sender names + cacheMessageNames []Scheme.MessageNames = nil // Cache for the message names + logDB *mgo.Database = nil // The logging database + logDBSession *mgo.Session = nil // The logging database session + logDBCollection *mgo.Collection = nil // The logging collection + loggingViewerPageSize int = 26 // How many records per page for the logging web viewer? + projectName string = `not set` // The project name for the logging + isProjectNameSet bool = false // Status about the project name ) diff --git a/Log/Web/HandlerLog.go b/Log/Web/HandlerLog.go index 65981d9..387c9e4 100644 --- a/Log/Web/HandlerLog.go +++ b/Log/Web/HandlerLog.go @@ -42,7 +42,7 @@ func HandlerWebLog(response http.ResponseWriter, request *http.Request) { // To less parameters? if countParameters < 9 { // Initial view => first page (latest logs) - data.Events = readLatest() + data.Events, lastPageNumber = readLatest() data.SetLiveView = true data.CurrentPage = `1` data.LastPage = fmt.Sprintf("%d", lastPageNumber) @@ -66,7 +66,7 @@ func HandlerWebLog(response http.ResponseWriter, request *http.Request) { currentLiveView := request.FormValue(`LiveView`) // Store the events for the template: - data.Events = readCustom(currentTimeRange, currentLevel, currentCategory, currentImpact, currentSeverity, currentMessageName, currentSender, currentPage) + data.Events, lastPageNumber = readCustom(currentTimeRange, currentLevel, currentCategory, currentImpact, currentSeverity, currentMessageName, currentSender, currentPage) if strings.ToLower(currentLiveView) == `true` { data.SetLiveView = true diff --git a/Log/Web/ReadCustom.go b/Log/Web/ReadCustom.go index 24bd08a..14486f8 100644 --- a/Log/Web/ReadCustom.go +++ b/Log/Web/ReadCustom.go @@ -8,10 +8,10 @@ import ( ) // Read a custom event range from the database. -func readCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage string) (events []Scheme.LogEvent) { +func readCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage string) (events []Scheme.LogEvent, numPages int) { // Get the custom events: - eventsFromDB := DeviceDatabase.ReadCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage) + eventsFromDB, totalNumberPages := DeviceDatabase.ReadCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMessageName, logSender, logPage) count := len(eventsFromDB) // Array with all events, prepared for the website: @@ -32,5 +32,6 @@ func readCustom(timeRange, logLevel, logCategory, logImpact, logSeverity, logMes } } + numPages = totalNumberPages return } diff --git a/Log/Web/ReadLatest.go b/Log/Web/ReadLatest.go index 0e40ce3..ae09f5c 100644 --- a/Log/Web/ReadLatest.go +++ b/Log/Web/ReadLatest.go @@ -8,9 +8,9 @@ import ( ) // Read the latest log events from the database -func readLatest() (events []Scheme.LogEvent) { +func readLatest() (events []Scheme.LogEvent, numPages int) { // Get the latest events from the database - eventsFromDB := DeviceDatabase.ReadLatest() + eventsFromDB, totalNumberPages := DeviceDatabase.ReadLatest() count := len(eventsFromDB) // Array for the log events, prepared for the website: @@ -31,5 +31,6 @@ func readLatest() (events []Scheme.LogEvent) { } } + numPages = totalNumberPages return }