Skip to content

Commit

Permalink
update cost estimate endpoint and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
hippo-an committed Oct 30, 2024
1 parent 10cd57b commit 0590935
Show file tree
Hide file tree
Showing 20 changed files with 1,721 additions and 1,666 deletions.
610 changes: 307 additions & 303 deletions api/docs.go

Large diffs are not rendered by default.

610 changes: 307 additions & 303 deletions api/swagger.json

Large diffs are not rendered by default.

476 changes: 235 additions & 241 deletions api/swagger.yaml

Large diffs are not rendered by default.

74 changes: 62 additions & 12 deletions cmd/cm-ant/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package main

import (
"log"
"fmt"

"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/cloud-barista/cm-ant/internal/app"
"github.com/cloud-barista/cm-ant/internal/config"
"github.com/cloud-barista/cm-ant/internal/utils"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// InitRouter initializes the routing for CM-ANT API server.
Expand All @@ -17,55 +23,99 @@ import (
// @version 0.2.2
// @description CM-ANT REST API swagger document.
// @basePath /ant

type CallerHook struct{}

func (h CallerHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
if pc, file, line, ok := runtime.Caller(3); ok {
shortFile := file[strings.LastIndex(file, "/")+1:]
e.Str("file", fmt.Sprintf("%s:%d", shortFile, line))
funcName := strings.Replace(runtime.FuncForPC(pc).Name(), "github.com/cloud-barista/", "", 1)
e.Str("func", funcName)
}
}

func main() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
level := strings.ToUpper(fmt.Sprintf("%s", i))
switch level {
case "DEBUG":
return fmt.Sprintf("\033[36m| %-6s|\033[0m", level) // Cyan
case "INFO":
return fmt.Sprintf("\033[32m| %-6s|\033[0m", level) // Green
case "WARN":
return fmt.Sprintf("\033[33m| %-6s|\033[0m", level) // Yellow
case "ERROR":
return fmt.Sprintf("\033[31m| %-6s|\033[0m", level) // Red
case "FATAL":
return fmt.Sprintf("\033[35m| %-6s|\033[0m", level) // Magenta
default:
return fmt.Sprintf("| %-6s|", level) // Default color
}
}
output.FormatMessage = func(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("message: \033[1m%s\033[0m", i)
}

output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
output.FormatFieldValue = func(i interface{}) string {
return fmt.Sprintf("\033[1m%s\033[0m", i)
}

log.Logger = zerolog.New(output).With().Timestamp().Logger().Hook(CallerHook{})

err := utils.Script(utils.JoinRootPathWith("/script/install_required_utils.sh"), []string{})
if err != nil {
log.Fatal("required tool can not install")
log.Fatal().Msg("required tool can not install")
}
utils.LogInfo("Starting CM-Ant server initialization...")

// Initialize the configuration for CM-Ant server
err = config.InitConfig()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server config error: %v", err)
log.Fatal().Msgf("CM-Ant server config error: %v", err)
}

// Create a new instance of the CM-Ant server
s, err := app.NewAntServer()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server creation error: %v", err)
log.Fatal().Msgf("CM-Ant server creation error: %v", err)
}

// Initialize the router for the CM-Ant server
err = s.InitRouter()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server init router error: %v", err)
log.Fatal().Msgf("CM-Ant server init router error: %v", err)
}

utils.LogInfo("CM-Ant server initialization completed successfully.")
log.Info().Msgf("CM-Ant server initialization completed successfully.")

// Create a channel to listen for OS signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

utils.LogInfo("Starting the CM-Ant server...")
log.Info().Msgf("Starting the CM-Ant server...")
go func() {
if err := s.Start(); err != nil {
log.Fatalf("[ERROR] CM-Ant start server error: %v", err)
log.Fatal().Msgf("CM-Ant start server error: %v", err)
}
}()

utils.LogInfo("CM-Ant server started successfully. Waiting for termination signal...")
log.Info().Msgf("CM-Ant server started successfully. Waiting for termination signal...")

// Wait for termination signal
<-stop

utils.LogInfo("Shutting down CM-Ant server...")
log.Info().Msgf("Shutting down CM-Ant server...")

// Perform any necessary cleanup actions here, such as closing connections or saving state.
// Optionally wait for pending operations to complete gracefully.

utils.LogInfo("CM-Ant server stopped gracefully.")
log.Info().Msgf("CM-Ant server stopped gracefully.")
os.Exit(0)
}
5 changes: 2 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ tumblebug:

cost:
estimation:
forecast:
priceUpdateInterval: 7d
updateInterval: "168h"

load:
retry: 2
jmeter:
dir: "/opt/ant/jmeter"
version: 5.6

logging:
log:
level: info

database:
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
restart: unless-stopped

ant-postgres:
container_name: ant-postgres
image: timescale/timescaledb:latest-pg16
ports:
- "5432:5432"
Expand All @@ -50,7 +51,7 @@ services:
restart: unless-stopped

cb-tumblebug:
image: cloudbaristaorg/cb-tumblebug:0.9.13
image: cloudbaristaorg/cb-tumblebug:0.9.21
container_name: cb-tumblebug
platform: linux/amd64
ports:
Expand Down Expand Up @@ -147,7 +148,7 @@ services:
restart: unless-stopped

cb-spider:
image: cloudbaristaorg/cb-spider:0.9.4
image: cloudbaristaorg/cb-spider:0.9.8
container_name: cb-spider
platform: linux/amd64
networks:
Expand Down
Loading

0 comments on commit 0590935

Please sign in to comment.