Skip to content

Commit

Permalink
Feature/28 rest endpoints (#29)
Browse files Browse the repository at this point in the history
* #28 Same status on REST as on socket

* #28 Functional style for reuse in REST api

* Corrected paths for distribution

* Serving static resources in the right places.

* Less silly

* Added memory stats to status endpoint

* CPU info in json

* Endpoint accepting commands like the websocket

* This file will be included in the client

* This file is included in the client

* This file is included in the client
  • Loading branch information
ingesolvoll authored and hplatou committed Nov 23, 2018
1 parent e118a3c commit 5df3119
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 59 deletions.
4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
name = "github.com/gorilla/websocket"
version = "1.4.0"

[[constraint]]
name = "github.com/shirou/gopsutil"
version = "2.18.10"

65 changes: 40 additions & 25 deletions cse/cse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"cse-server-go/metadata"
"cse-server-go/structs"
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -232,43 +234,56 @@ func getModuleNames(metaData *structs.MetaData) []string {
return modules
}

func getModuleData(status *structs.SimulationStatus, metaData *structs.MetaData, observer *C.cse_observer) (module structs.Module) {
func getModuleData(observer *C.cse_observer, fmu structs.FMU) (module structs.Module) {
realSignals := observerGetReals(observer, fmu)
intSignals := observerGetIntegers(observer, fmu)
var signals []structs.Signal
signals = append(signals, realSignals...)
signals = append(signals, intSignals...)
module.Name = fmu.Name
module.Signals = signals
return
}

func findModuleData(status *structs.SimulationStatus, metaData *structs.MetaData, observer *C.cse_observer) (module structs.Module) {
if len(status.Module.Name) > 0 {
for _, fmu := range metaData.FMUs {
if fmu.Name == status.Module.Name {
realSignals := observerGetReals(observer, fmu)
intSignals := observerGetIntegers(observer, fmu)
var signals []structs.Signal
signals = append(signals, realSignals...)
signals = append(signals, intSignals...)
module.Name = fmu.Name
module.Signals = signals
module = getModuleData(observer, fmu)
}
}
}
return
}

func SimulationStatus(simulationStatus *structs.SimulationStatus, sim *Simulation) structs.JsonResponse {
virtualMemoryStats, _ := mem.VirtualMemory()
cpuInfo, _ := cpu.PerfInfo()
if simulationStatus.Loaded {
return structs.JsonResponse{
SimulationTime: getSimulationTime(sim.Execution),
Modules: getModuleNames(&sim.MetaData),
Module: findModuleData(simulationStatus, &sim.MetaData, sim.Observer),
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
ConfigDir: simulationStatus.ConfigDir,
TrendSignals: simulationStatus.TrendSignals,
Memory: virtualMemoryStats,
Cpu: cpuInfo,
}
} else {
return structs.JsonResponse{
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
}
}

return module
}

func StateUpdateLoop(state chan structs.JsonResponse, simulationStatus *structs.SimulationStatus, sim *Simulation) {

for {
if simulationStatus.Loaded {
state <- structs.JsonResponse{
SimulationTime: getSimulationTime(sim.Execution),
Modules: getModuleNames(&sim.MetaData),
Module: getModuleData(simulationStatus, &sim.MetaData, sim.Observer),
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
ConfigDir: simulationStatus.ConfigDir,
TrendSignals: simulationStatus.TrendSignals,
}
} else {
state <- structs.JsonResponse{
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
}
}
state <- SimulationStatus(simulationStatus, sim)
time.Sleep(1000 * time.Millisecond)
}
}
Expand Down
4 changes: 0 additions & 4 deletions layout.html

This file was deleted.

10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ func main() {
cmd := make(chan []string, 10)
state := make(chan structs.JsonResponse, 10)

simulationStatus := &structs.SimulationStatus{
simulationStatus := structs.SimulationStatus{
Loaded: false,
Status: "stopped",
TrendSignals: []structs.TrendSignal{},
}

// Passing the channel to the go routine
go cse.StateUpdateLoop(state, simulationStatus, &sim)
go cse.CommandLoop(&sim, cmd, simulationStatus)
go cse.TrendLoop(&sim, simulationStatus)
go cse.StateUpdateLoop(state, &simulationStatus, &sim)
go cse.CommandLoop(&sim, cmd, &simulationStatus)
go cse.TrendLoop(&sim, &simulationStatus)

//Passing the channel to the server
server.Server(cmd, state)
server.Server(cmd, state, &simulationStatus, &sim)
close(cmd)
}
38 changes: 19 additions & 19 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package server

import (
"cse-server-go/cse"
"cse-server-go/structs"
"html/template"
"net/http"
"encoding/json"
"github.com/gobuffalo/packr"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"github.com/gobuffalo/packr"
"net/http"
)

type PageData struct {
PageTitle string
CseAnswer string
}

var data = PageData{
PageTitle: "CSE Hello World",
CseAnswer: "",
}

func Server(command chan []string, state chan structs.JsonResponse) {
func Server(command chan []string, state chan structs.JsonResponse, simulationStatus *structs.SimulationStatus, sim *cse.Simulation) {
router := mux.NewRouter()
box := packr.NewBox("./resources/public")
tmpl := template.Must(template.ParseFiles("layout.html"))
box := packr.NewBox("../resources/public")

router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, data)
router.Handle("/", http.FileServer(box))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(box)))

router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cse.SimulationStatus(simulationStatus, sim))
})

router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(box)))
router.HandleFunc("/command", func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
commandRequest := []string{}
json.Unmarshal(body, &commandRequest)
command <- commandRequest
}).Methods("PUT")

router.HandleFunc("/ws", WebsocketHandler(command, state))

Expand Down
19 changes: 13 additions & 6 deletions structs/structs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package structs

import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
)

type Signal struct {
Name string `json:"name"`
Causality string `json:"causality"`
Expand All @@ -13,12 +18,14 @@ type Module struct {
}

type JsonResponse struct {
Loaded bool `json:"loaded"`
SimulationTime float64 `json:"time"`
ConfigDir string `json:"configDir,omitempty"`
Status string `json:"status,omitempty"`
Modules []string `json:"modules"`
Module Module `json:"module,omitempty"`
Loaded bool `json:"loaded"`
SimulationTime float64 `json:"time"`
ConfigDir string `json:"configDir,omitempty"`
Status string `json:"status,omitempty"`
Modules []string `json:"modules"`
Module Module `json:"module,omitempty"`
Memory *mem.VirtualMemoryStat
Cpu []cpu.Win32_PerfFormattedData_Counters_ProcessorInformation
TrendSignals []TrendSignal `json:"trendSignals,omitempty"`
}

Expand Down

0 comments on commit 5df3119

Please sign in to comment.