2015-02-06 13:12:45 +00:00
package DeviceDatabase
import (
2015-06-23 05:49:59 +00:00
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
2015-02-06 13:12:45 +00:00
"gopkg.in/mgo.v2/bson"
2015-06-23 05:49:59 +00:00
"math"
2015-02-06 13:12:45 +00:00
)
2015-06-17 15:44:52 +00:00
// Read the latest logging events from the database.
2015-06-23 05:49:59 +00:00
func ReadLatest ( ) ( events [ ] LogDBEntry , numPages int ) {
2015-06-17 15:44:52 +00:00
// Define the query:
2015-06-23 15:22:24 +00:00
query := logDBCollection . Find ( bson . D { { "Project" , projectName } } ) . Sort ( ` -TimeUTC ` )
2015-06-23 05:49:59 +00:00
// 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
2015-02-06 13:12:45 +00:00
2015-06-17 15:44:52 +00:00
// Execute the query and count the results:
2015-02-06 13:12:45 +00:00
if n , err := query . Count ( ) ; err == nil {
count = n
}
2015-06-17 15:44:52 +00:00
// The iterator for the results:
2015-02-06 13:12:45 +00:00
iter := query . Iter ( )
entry := LogDBEntry { }
pos := 0
2015-06-17 15:44:52 +00:00
// Reserve the memory for the results:
2015-02-06 13:12:45 +00:00
events = make ( [ ] LogDBEntry , count )
2015-06-17 15:44:52 +00:00
// Loop over all entries and store it:
2015-02-06 13:12:45 +00:00
for iter . Next ( & entry ) {
2015-06-23 15:22:24 +00:00
// Convert the time instance to UTC:
entry . TimeUTC = entry . TimeUTC . UTC ( )
// Store it:
2015-02-06 13:12:45 +00:00
events [ pos ] = entry
pos ++
}
return
}