2015-02-26 17:29:42 +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-26 17:29:42 +00:00
"gopkg.in/mgo.v2/bson"
2015-06-23 05:49:59 +00:00
"math"
2015-06-23 15:22:24 +00:00
"time"
2015-02-26 17:29:42 +00:00
)
2015-06-23 15:22:24 +00:00
func ReadCustom ( timeRange , logLevel , logCategory , logImpact , logSeverity , logMessageName , logSender string , logPage int ) ( events [ ] LogDBEntry , numPages int ) {
2015-02-26 17:29:42 +00:00
2015-06-23 15:22:24 +00:00
// The base query:
selection := bson . D { { "Project" , projectName } }
//
// Build the selection statement regarding the admin's choice:
2015-02-26 17:29:42 +00:00
//
2015-06-23 15:22:24 +00:00
// IMPORTANT: The order of the arguments e.g. Project->TimeUTC->Sender...
// is very important to enable the database to use the indexes!
2015-02-26 17:29:42 +00:00
//
2015-06-23 15:22:24 +00:00
if timeRange != ` * ` {
nowUTC := time . Now ( ) . UTC ( )
switch timeRange {
case ` last5min ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Minute * - 5 ) } } } )
case ` last30min ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Minute * - 30 ) } } } )
case ` last60min ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Minute * - 60 ) } } } )
case ` last24h ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Hour * - 24 ) } } } )
case ` last7d ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Hour * - 24 * 7 ) } } } )
case ` lastMonth ` :
selection = append ( selection , bson . DocElem { "TimeUTC" , bson . D { { "$gte" , nowUTC . Add ( time . Hour * - 24 * 31 ) } } } )
}
}
if logSender != ` * ` {
selection = append ( selection , bson . DocElem { "Sender" , logSender } )
}
if logMessageName != ` * ` {
selection = append ( selection , bson . DocElem { "MessageName" , logMessageName } )
}
if logLevel != ` * ` {
value := ` L: ` + logLevel
selection = append ( selection , bson . DocElem { "Level" , value } )
}
if logCategory != ` * ` {
value := ` C: ` + logCategory
selection = append ( selection , bson . DocElem { "Category" , value } )
}
2015-06-23 05:49:59 +00:00
2015-06-23 15:22:24 +00:00
if logImpact != ` * ` {
value := ` I: ` + logImpact
selection = append ( selection , bson . DocElem { "Impact" , value } )
}
if logSeverity != ` * ` {
value := ` S: ` + logSeverity
selection = append ( selection , bson . DocElem { "Severity" , value } )
}
// Build the query:
query := logDBCollection . Find ( selection )
// How many record we have all over for this project?
2015-06-23 05:49:59 +00:00
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 ) ) )
}
2015-06-23 15:22:24 +00:00
// Sort all results:
query = query . Sort ( ` -TimeUTC ` )
2015-06-23 05:49:59 +00:00
// Set now the page's record limit:
2015-06-23 15:22:24 +00:00
query = query . Skip ( ( logPage - 1 ) * loggingViewerPageSize ) . Limit ( loggingViewerPageSize )
2015-06-23 05:49:59 +00:00
count := loggingViewerPageSize
2015-02-26 17:29:42 +00:00
2015-06-17 15:44:52 +00:00
// Execute the query and count the results:
2015-02-26 17:29:42 +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-26 17:29:42 +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-26 17:29:42 +00:00
events = make ( [ ] LogDBEntry , count )
2015-06-17 15:44:52 +00:00
// Loop over all entries and store it:
2015-02-26 17:29:42 +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-26 17:29:42 +00:00
events [ pos ] = entry
pos ++
}
return
}