Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[simulator][machine] Simulator config, command and improve logic #13

Merged
merged 18 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .ayi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ test:
scripts:
ft: go test ./pkg/...
sm: go test -v ./pkg/simulator -run TestMachineSimulator
f:
- gofmt -d -l -w ./pkg
format:
- gofmt -d -l -w ./pkg
- git status
23 changes: 12 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
*.class
# Mac
.DS_Store

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# Logs
log
*.log
log.txt

# vendor
vendor

# IDE
.idea

# Config

# Generated data
*.dat
63 changes: 61 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ import:
version: 50515b700e02658272117a72bd641b6b7f1222e5
- package: github.com/stretchr/testify
vcs: git
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
- package: github.com/Sirupsen/logrus
vcs: git
version: 0.11.0
30 changes: 29 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import (
"fmt"
"os"

"github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xephonhq/xephon-b/pkg/config"
"github.com/xephonhq/xephon-b/pkg/util"
)

const Version = "0.0.1-dev"
// Short name use in machine simulator package
var log = util.Logger.WithFields(logrus.Fields{
"pkg": "x.cmd",
})

// RootCmd is the top command, other commands should be its child
var RootCmd = &cobra.Command{
Expand All @@ -27,5 +34,26 @@ func Execute() {
}

func init() {
cobra.OnInitialize(initConfig)

RootCmd.PersistentFlags().StringVar(&config.ConfigFile, "config", config.DefaultConfigFile, "config file (default is ./xephon-b.yml)")
RootCmd.PersistentFlags().BoolVar(&config.Debug, "debug", false, "debug")

RootCmd.AddCommand(VersionCmd)
RootCmd.AddCommand(SimulatorCmd)
}

func initConfig() {
if config.Debug {
util.UseVerboseLog()
}
viper.AutomaticEnv()
// TODO: check file existence
viper.SetConfigFile(config.ConfigFile)
err := viper.ReadInConfig()
if err != nil {
log.Warn(err)
} else {
log.Debugf("config file %s is loaded", config.ConfigFile)
}
}
76 changes: 76 additions & 0 deletions pkg/cmd/simulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/xephonhq/xephon-b/pkg/config"
"github.com/xephonhq/xephon-b/pkg/serialize"
"github.com/xephonhq/xephon-b/pkg/simulator/machine"
)

// flags to bind
var (
simulatorType string
simulatorDataEncoding string
simulatorOutput string
simulatorOutputLocation string
)

// SimulatorCmd generate and store simulated time series data
var SimulatorCmd = &cobra.Command{
// TODO: alias is not shown in help
Use: "simulator",
Aliases: []string{"sim"},
Short: "Simulate time series data for different scenario",
Long: "Simulate real word time series data, serialize and store to file",
Run: func(cmd *cobra.Command, args []string) {
log.Debug("triggered simulator command")
// TODO: check if what user passed is not supported
// TODO: simulator config is not even used
if simulatorType != "machine" {
log.Fatalf("we only support machine, no %s", simulatorType)
return
}

c := config.ReadMachineSimulatorConfigFromViper()
sm := machine.NewMachineSimulator(*c)
switch simulatorOutput {
case "stdout":
sm.SetWriter(os.Stdout)
case "file":
// try to open the file
f, err := os.Create(simulatorOutputLocation)
if err != nil {
log.Error("can not create output file")
log.Fatalf(err.Error())
return
}
defer f.Close()
sm.SetWriter(f)
default:
log.Fatalf("unsupported output type %s", simulatorOutput)
return
}
switch simulatorDataEncoding {
case "json":
log.Debug("set encoding to json")
sm.SetSerializer(&serialize.JsonSerializer{})
case "debug":
log.Debug("set encoding to debug")
sm.SetSerializer(&serialize.DebugSerializer{})
default:
log.Fatalf("unsupported encoding %s", simulatorDataEncoding)
return
}
sm.Start()
},
}

func init() {
SimulatorCmd.Flags().StringVar(&simulatorType, "type", "machine", "simluator type")
SimulatorCmd.Flags().StringVar(&simulatorDataEncoding, "encoding", "debug", "serializer encoding")
SimulatorCmd.Flags().StringVar(&simulatorOutput, "output", "stdout", "output type")
SimulatorCmd.Flags().StringVar(&simulatorOutputLocation, "location", "give_me_a_name.dat", "output destination")
}
11 changes: 8 additions & 3 deletions pkg/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package cmd

import (
"github.com/spf13/cobra"
"fmt"

"github.com/spf13/cobra"
)

// Version need to be manually updated
const Version = "0.0.1-dev"

// VersionCmd print the version
var VersionCmd = &cobra.Command{
Use: "version",
Use: "version",
Short: "Show Xephon-B version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(Version)
},
}
}
4 changes: 4 additions & 0 deletions pkg/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Config

This directory contains struct that represent configuration, in order to abstract config from detail config file (whose format and structure
may chage a lot)
8 changes: 8 additions & 0 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

// ConfigFile is the location of the loaded config file
var ConfigFile string
var DefaultConfigFile = "xephon-b.yml"

// Debug indicate whether run in debug mode
var Debug bool
26 changes: 26 additions & 0 deletions pkg/config/machine_simulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
"time"

"github.com/spf13/viper"
)

// MachineSimulatorConfig defines the configurable property for the machine simulator
type MachineSimulatorConfig struct {
Num int
Start time.Time
End time.Time
Step time.Duration
}

// ReadMachineSimulatorConfigFromViper return a config struct using configuration in yml
func ReadMachineSimulatorConfigFromViper() *MachineSimulatorConfig {
c := &MachineSimulatorConfig{}
c.Num = viper.GetInt("simulator.machine.num")
c.Start = viper.GetTime("simulator.machine.start")
c.End = viper.GetTime("simulator.machine.end")
// TODO: may remove the outer time.Duration?
c.Step = time.Duration(time.Duration(viper.GetInt("simulator.machine.step")) * time.Second)
return c
}
12 changes: 12 additions & 0 deletions pkg/config/machine_simulator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import (
"github.com/xephonhq/xephon-b/pkg/util"
"testing"
)

func TestReadMachineSimulatorConfigFromViper(t *testing.T) {
util.ViperReadTestConfig()
c := ReadMachineSimulatorConfigFromViper()
t.Log(c)
}
12 changes: 12 additions & 0 deletions pkg/config/simulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

var SupportedType = []string{"machine"}
var SupportedEncoding = []string{"debug", "json"}
var SupportedOutput = []string{"stdout", "file"}

type SimulatorConfig struct {
Type string
Encoding string
Output string
OutputLocation string
}
51 changes: 0 additions & 51 deletions pkg/generator/constant_generator.go

This file was deleted.

Loading