Developing lightweight computation at the DSG edge

Commit b21f178b authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[monitor-dump] Save timestamped data


Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent f2783624
<div id="assign"></div>
<script src="./svg.min.js"></script>
<script src="/tmp/out.json"></script>
<script src="/tmp/monitor-dump/last.json"></script>
<script>
setTimeout(function(){
......
......@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"time"
"uc-monitor-go-test/cnml"
"uc-monitor-go-test/goclidote"
......@@ -13,17 +14,42 @@ import (
"github.com/golang/glog"
)
type JSONDeviceMonitorAssignation struct {
type JSONDeviceToMonitorsAssignation struct {
Device string `json:"device"`
Monitors []string `json:"monitors"`
}
type JSONMonitorToDevices struct {
Monitor string `json:"monitor"`
Devices []string `json:"devices"`
}
type JSONAssignations struct {
DeviceToMonitors []JSONDeviceToMonitorsAssignation `json:"deviceToMonitors"`
MonitorToDevices []JSONMonitorToDevices `json:"monitorToDevices"`
}
type JSONStats struct {
Assignations uint `json:"assignations"`
MinMonsPerDev int `json:"minMonsPerDev"`
MaxMonsPerDev int `json:"maxMonsPerDev"`
AvgMonsPerDev float32 `json:"avgMonsPerDev"`
MinDevsPerMon int `json:"minDevsPerMon"`
MaxDevsPerMon int `json:"maxDevsPerMon"`
AvgDevsPerMon float32 `json:"avgDevsPerMon"`
}
type JSONAllData struct {
Devices []string `json:"devices"`
Monitors []string `json:"monitors"`
Assignations []JSONDeviceMonitorAssignation `json:"assignations"`
Devices []string `json:"devices"`
Monitors []string `json:"monitors"`
Assignations JSONAssignations `json:"assignations"`
Stats JSONStats `json:"stats"`
}
//Default settings and descriptions
var outDir = "/tmp/monitor-dump/"
var outDirHelp = "Directory name where the output files will be saved"
// Define error levels
// FATAL output code
......@@ -57,6 +83,35 @@ var globalAssign []cnml.DeviceMonitorAssignation
var monitorsList []string
var devicesList []string
// The initialize() function manages the execution parameters, sets timers, etc.
func initialize() {
fmt.Println("Initializing...")
outDirPtr := flag.String("out_dir", outDir, outDirHelp)
flag.Parse()
outDir = *outDirPtr
// Add a trailing slash to outDir, if missing
if outDir[len(outDir)-1:] != "/" {
outDir = strings.Join([]string{outDir, "/"}, "")
}
// Create the output directory
err := os.MkdirAll(outDir, 0755)
errCheck(err, FATAL, fmt.Sprintf("Error creating working dir %s:", outDir))
// Manage command line flags and execution settings
initializeFlags()
// Initialize periodic timers
initializeTimers()
// The application is now ininitialized; entering the loop
fmt.Println("Initialization done. Entering infinite loop...")
}
func main() {
//Initialize the service (read the configuration flags, etc.)
initialize()
......@@ -75,21 +130,6 @@ func main() {
fmt.Println("\nBye!")
}
// The initialize() function manages the execution parameters, sets timers, etc.
func initialize() {
fmt.Println("Initializing...")
// Manage command line flags and execution settings
initializeFlags()
// Initialize periodic timers
initializeTimers()
// The application is now ininitialized; entering the loop
fmt.Println("Initialization done. Entering infinite loop...")
}
func initializeTimers() {
// Initialize the local assignation check timer
startGlobalAssignTimer()
......@@ -134,37 +174,98 @@ func startGlobalAssignTimer() {
globalAssignTicker := time.NewTicker(time.Duration(globalAssignCheckInterval) * time.Second)
go func() {
for range globalAssignTicker.C {
fmt.Println("Getting monitors list...")
dateTime := fmt.Sprint(time.Now().Format("2006-01-02_15-04-05"))
fmt.Println(dateTime)
// fmt.Println("Getting monitors list...")
monitorsList = getCurrentMonitorsList()
fmt.Println("The current monitors list contains", len(monitorsList), "monitors")
// fmt.Println("The current monitors list contains", len(monitorsList), "monitors")
fmt.Println("Getting devices list...")
// fmt.Println("Getting devices list...")
devicesList = getCurrentDevicesList()
fmt.Println("The current devices list contains", len(devicesList), "devices")
// fmt.Println("The current devices list contains", len(devicesList), "devices")
fmt.Println("Getting global assignation list...")
// fmt.Println("Getting global assignation list...")
globalAssign = getGlobalAssignationList()
fmt.Println("The global assignation list contains", len(globalAssign), "devices")
// fmt.Println("The global assignation list contains", len(globalAssign), "devices")
var dataToExport JSONAllData
dataToExport.Devices = devicesList
dataToExport.Monitors = monitorsList
for _, v := range globalAssign {
var newAssg JSONDeviceMonitorAssignation
newAssg.Device = fmt.Sprintf("%d", v.Device.ID)
newAssg.Monitors = v.MonitorID
dataToExport.Assignations = append(dataToExport.Assignations, newAssg)
for k, v := range globalAssign {
// fmt.Println(v.Device.ID, ":", v.MonitorID)
var newDTMA JSONDeviceToMonitorsAssignation
newDTMA.Device = fmt.Sprintf("%d", v.Device.ID)
newDTMA.Monitors = v.MonitorID
dataToExport.Assignations.DeviceToMonitors = append(dataToExport.Assignations.DeviceToMonitors, newDTMA)
monAdded := false
for _, w := range v.MonitorID {
for m, x := range dataToExport.Assignations.MonitorToDevices {
if x.Monitor == w {
dataToExport.Assignations.MonitorToDevices[m].Devices = append(x.Devices, fmt.Sprintf("%d", v.Device.ID))
monAdded = true
}
}
if !monAdded {
var thisMon JSONMonitorToDevices
thisMon.Monitor = w
thisMon.Devices = append(thisMon.Devices, fmt.Sprintf("%d", v.Device.ID))
dataToExport.Assignations.MonitorToDevices = append(dataToExport.Assignations.MonitorToDevices, thisMon)
}
}
dataToExport.Stats.Assignations = dataToExport.Stats.Assignations + uint(len(newDTMA.Monitors))
if k == 0 || len(newDTMA.Monitors) > dataToExport.Stats.MaxMonsPerDev {
dataToExport.Stats.MaxMonsPerDev = len(newDTMA.Monitors)
}
if k == 0 || len(newDTMA.Monitors) < dataToExport.Stats.MinMonsPerDev {
dataToExport.Stats.MinMonsPerDev = len(newDTMA.Monitors)
}
}
outFile, _ := os.Create("/tmp/out.json")
defer outFile.Close()
for k, v := range dataToExport.Assignations.MonitorToDevices {
if k == 0 || len(v.Devices) > dataToExport.Stats.MaxDevsPerMon {
dataToExport.Stats.MaxDevsPerMon = len(v.Devices)
}
if k == 0 || len(v.Devices) < dataToExport.Stats.MinDevsPerMon {
dataToExport.Stats.MinDevsPerMon = len(v.Devices)
}
}
dataToExport.Stats.AvgMonsPerDev = float32(dataToExport.Stats.Assignations) / float32(len(dataToExport.Devices))
dataToExport.Stats.AvgDevsPerMon = float32(dataToExport.Stats.Assignations) / float32(len(dataToExport.Monitors))
lastOutFile, _ := os.Create(outDir + "last.json")
timeOutFile, _ := os.Create(outDir + dateTime + ".json")
defer lastOutFile.Close()
defer timeOutFile.Close()
jnodes, _ := json.Marshal(dataToExport)
//fmt.Println(string(jnode))
//outFile.WriteString(string(jnodes))
outFile.WriteString(fmt.Sprintf("data = %s", string(jnodes)))
lastOutFile.WriteString(string(jnodes))
timeOutFile.WriteString(fmt.Sprintf("data = %s", string(jnodes)))
lastOutFile.Close()
timeOutFile.Close()
fmt.Println("Devices:", len(dataToExport.Devices))
fmt.Println("Monitors:", len(dataToExport.Monitors))
fmt.Println("Assignations:", dataToExport.Stats.Assignations)
fmt.Println("MinMonsPerDev:", dataToExport.Stats.MinMonsPerDev)
fmt.Println("MaxMonsPerDev:", dataToExport.Stats.MaxMonsPerDev)
fmt.Println("AvgMonsPerDev:", dataToExport.Stats.AvgMonsPerDev)
fmt.Println("MinDevsPerMon:", dataToExport.Stats.MinDevsPerMon)
fmt.Println("MaxDevsPerMon:", dataToExport.Stats.MaxDevsPerMon)
fmt.Println("AvgDevsPerMon:", dataToExport.Stats.AvgDevsPerMon)
fmt.Println("")
}
}()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment