Skip to content

Commit

Permalink
Merge pull request #37 from ReconfigureIO/feature/sim-reports
Browse files Browse the repository at this point in the history
Add ability to download simulation reports
  • Loading branch information
Max Siegieda authored Nov 19, 2018
2 parents abaa911 + c1411cd commit 1aaa5f0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ var (
},
}

testCmdReport = &cobra.Command{
Use: fmt.Sprintf("report [simulation_ID]"),
Aliases: []string{"reports"},
Short: fmt.Sprintf("View reports for a completed simulation"),
Long: fmt.Sprintf("View reports for a completed simulation, containing area and resource utilisation figures."),
Run: openTestReport,
}

testLogPreRun = func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError("ID required")
Expand Down Expand Up @@ -68,6 +76,7 @@ func init() {
testCmd.AddCommand(testCmdLog)
testCmd.AddCommand(testCmdStop)
testCmd.AddCommand(testCmdStart)
testCmd.AddCommand(testCmdReport)
testCmd.PersistentFlags().StringVar(&project, "project", project, "Project to use. If unset, the active project is used")

RootCmd.AddCommand(testCmd)
Expand Down Expand Up @@ -95,3 +104,16 @@ func startTest(cmd *cobra.Command, args []string) {
status := tool.Test().Status(id)
logger.Std.Println("Simulation ID: " + id + " Status: " + strings.Title(status))
}

func openTestReport(_ *cobra.Command, args []string) {
if len(args) != 1 {
exitWithError("ID required")
}

report, err := tool.Test().(reco.SimulationReporter).Report(args[0]) // TODO campgareth: Anything but this weird form.
if err != nil {
exitWithError(err)
}

logger.Std.Println(report)
}
51 changes: 51 additions & 0 deletions simulation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package reco

import (
"bytes"
"encoding/json"
"errors"
"io"
"strings"
"time"
Expand All @@ -12,6 +15,12 @@ import (

var _ Job = &testJob{}

// SimulationReporter can return simulation reports.
type SimulationReporter interface {
// Report returns a formatted JSON simulation report.
Report(id string) (string, error)
}

type testJob struct {
*clientImpl
}
Expand Down Expand Up @@ -121,3 +130,45 @@ func (t testJob) Stop(id string) error {
func (t testJob) Log(id string, writer io.Writer) error {
return t.clientImpl.logJob("simulation", id)
}

type simulationReport struct {
Report string `json:"report"`
}

func (t testJob) Report(id string) (string, error) {
var req = t.apiRequest(endpoints.simulations.Report())
req.param("id", id)
resp, err := req.Do("GET", nil)
if err != nil {
return "", err
}
switch resp.StatusCode {
case 404:
return "", errors.New("Report not found")
case 204:
return "", errors.New("No report generated. Reports are only generated for COMPLETED simulations")
case 200:
break
default:
return "", errors.New("Unknown error occured")
}

var apiResp struct {
Value struct {
Report string `json:"report"`
} `json:"value"`
}
err = json.NewDecoder(resp.Body).Decode(&apiResp)
if err != nil {
return "", err
}

validJSONReport := strings.Replace(apiResp.Value.Report, "\\", "", -1)
var prettyJSONReport bytes.Buffer
err = json.Indent(&prettyJSONReport, []byte(validJSONReport), "", "\t")
if err != nil {
return "", err
}

return prettyJSONReport.String(), nil
}

0 comments on commit 1aaa5f0

Please sign in to comment.