Skip to content
Draft
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: 4 additions & 1 deletion backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func main() {
updateFactory := update_factory.NewFactory(boardToPackets)

// <--- logger --->
loggerHandler, subloggers := setUpLogger(config)
loggerHandler, subloggers, err := setUpLogger(config, adj.Commit)
if err != nil {
trace.Fatal().Err(err).Msg("setting up logger")
}

// <-- connections & upgrader -->
connections := make(chan *websocket.Client)
Expand Down
7 changes: 4 additions & 3 deletions backend/cmd/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ func createLookupTables(
createBoardToPackets(podData)
}

func setUpLogger(config config.Config) (*logger.Logger, abstraction.SubloggersMap) {
func setUpLogger(config config.Config, commitHash string) (*logger.Logger, abstraction.SubloggersMap, error) {

var subloggers = abstraction.SubloggersMap{
data_logger.Name: data_logger.NewLogger(),
order_logger.Name: order_logger.NewLogger(),
}

logger.ConfigureLogger(config.Logging.TimeUnit, config.Logging.LoggingPath)
err := logger.ConfigureLogger(config.Logging.TimeUnit, config.Logging.LoggingPath, commitHash)

loggerHandler := logger.NewLogger(subloggers, trace.Logger)

return loggerHandler, subloggers
return loggerHandler, subloggers, err

}
23 changes: 15 additions & 8 deletions backend/pkg/adj/adj.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func getRepoPath() string {
}

func NewADJ(AdjSettings config.Adj) (ADJ, error) {
infoRaw, boardsRaw, err := downloadADJ(AdjSettings)
commitHash, infoRaw, boardsRaw, err := downloadADJ(AdjSettings)
if err != nil {
return ADJ{}, err
}
Expand Down Expand Up @@ -84,31 +84,38 @@ func NewADJ(AdjSettings config.Adj) (ADJ, error) {
adj := ADJ{
Info: info,
Boards: boards,
Commit: commitHash,
}

return adj, nil
}

func downloadADJ(AdjSettings config.Adj) (json.RawMessage, json.RawMessage, error) {
updateRepo(AdjSettings.Branch)
func downloadADJ(AdjSettings config.Adj) (string, json.RawMessage, json.RawMessage, error) {
commitHash, err := updateRepo(AdjSettings.Branch)
if err != nil {
return "local", nil, nil, err
}

// If not downloading use local ADJ
if commitHash == "" {
commitHash = "local"
}

// After downloading adj apply adj validator

if AdjSettings.Validate {

Validate()

}

info, err := os.ReadFile(RepoPath + "general_info.json")
if err != nil {
return nil, nil, err
return commitHash, nil, nil, err
}

boardsList, err := os.ReadFile(RepoPath + "boards.json")
if err != nil {
return nil, nil, err
return commitHash, nil, nil, err
}

return info, boardsList, nil
return commitHash, info, boardsList, nil
}
39 changes: 27 additions & 12 deletions backend/pkg/adj/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
// connectivity). If the remote branch is accessible, the local repository is completely
// removed and replaced with a clean, shallow clone of that branch.
// If the remote is not accessible, the existing local repository is left untouched.
// returns commit an error if any operation fails, otherwise returns nil

func updateRepo(AdjBranch string) error {
func updateRepo(AdjBranch string) (string, error) {
var err error

var commitHash string
if AdjBranch == "" {
// Makes use of user's custom ADJ
trace.Info().Msg("No ADJ branch specified. Using local ADJ.")
return nil
return "", nil
} else {
trace.Info().Msgf("Updating local ADJ repository to match remote branch '%s'", AdjBranch)
cloneOptions := &git.CloneOptions{
Expand All @@ -37,38 +38,52 @@ func updateRepo(AdjBranch string) error {

// Remove previous failed cloning attempts
if err = os.RemoveAll(tempPath); err != nil {
return err
return "", err
}

// Try to import the ADJ to the temp directory
_, err = git.PlainClone(tempPath, false, cloneOptions)
if err != nil {
// If the clone fails, work with the local ADJ
trace.Info().Msgf("Warning: Could not clone ADJ branch '%s' from remote. Working with local ADJ. Error: %v", AdjBranch, err)
return nil

return "", nil
}

// If the clone is succesful, delete the temp files
if err = os.RemoveAll(tempPath); err != nil {
return err
return "", err
}

// After checking that the repo is accessible, clone or update (overwrite) the local ADJ repo
if _, err = os.Stat(RepoPath); os.IsNotExist(err) {
_, err = git.PlainClone(RepoPath, false, cloneOptions)
repo, err := git.PlainClone(RepoPath, false, cloneOptions)
if err != nil {
return "", err
}
// log the commit
ref, err := repo.Head()
if err != nil {
return err
return "", err
}
commitHash = ref.Hash().String()

} else {
if err = os.RemoveAll(RepoPath); err != nil {
return err
return "", err
}
repo, err := git.PlainClone(RepoPath, false, cloneOptions)
if err != nil {
return "", err
}
_, err = git.PlainClone(RepoPath, false, cloneOptions)
// log the commit
ref, err := repo.Head()
if err != nil {
return err
return "", err
}
commitHash = ref.Hash().String()
}
}

return nil
return commitHash, nil
}
1 change: 1 addition & 0 deletions backend/pkg/adj/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type ADJ struct {
Info Info
Boards map[string]Board
Commit string
}

type InfoJSON struct {
Expand Down
73 changes: 56 additions & 17 deletions backend/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package logger

import (
"encoding/json"
"os"
"path"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -41,6 +43,12 @@ var Timestamp = time.Now()
// StartAppTimestamp is the time in which the app was started
var StartAppTimestamp = time.Now()

// CommitHash is the commit hash of the current version of ADJ
var CommitHash string

// TimestampUnit is the unit of time used for the logger timestamps
var TimestampUnit TimeUnit

var BasePath = path.Join("logger", StartAppTimestamp.Format(TimestampFormat))

func (Logger) HandlerName() string { return HandlerName }
Expand Down Expand Up @@ -93,6 +101,13 @@ func (logger *Logger) Start() error {

logger.trace.Info().Msg("started")

// Write logger settings to a JSON file in the sublogger directory
err := WriteLoggerSettings(path.Join(BasePath, Timestamp.Format(TimestampFormat), "logger_settings.json"))
if err != nil {
logger.trace.Warn().Stack().Err(err).Msg("write logger settings")
return err
}

if logger.onStart != nil {
logger.onStart()
}
Expand Down Expand Up @@ -127,22 +142,6 @@ func (logger *Logger) PullRecord(request abstraction.LoggerRequest) (abstraction

panic("PullRecord")

// logger.trace.
// Trace().
// Type("request", request).
// Msg("request")

// loggerChecked, ok := logger.subloggers[request.Name()]
// if !ok {
// logger.trace.
// Warn().
// Type("request", request).
// Str("name", string(request.Name())).
// Msg("no subloggger found for request")

// return nil, ErrLoggerNotFound{request.Name()}
// }
// return loggerChecked.PullRecord(request)
}

func (logger *Logger) Stop() error {
Expand Down Expand Up @@ -173,11 +172,51 @@ func (logger *Logger) Stop() error {
}

// ConfigureLogger configures the logger attributes before initializing it.
func ConfigureLogger(unit TimeUnit, basePath string) {
func ConfigureLogger(unit TimeUnit, basePath string, commitHash string) error {

// Start the sublogger
SetFormatTimestamp(unit)

// Set unit for logger timestamps
TimestampUnit = unit

// Set commit hash
CommitHash = commitHash

// Update base Path
BasePath = path.Join(basePath, "logger", StartAppTimestamp.Format(TimestampFormat))

err := WriteLoggerSettings(path.Join(BasePath, "others", "logger_settings.json"))
if err != nil {
return err
}

return nil

}

/******************
* Logger Settings *
*******************/

// JSON with adj commit and unit of time for logger settings
type LoggerSettings struct {
AdjCommitHash string `json:"adj_commit_hash"`
TimeUnit TimeUnit `json:"time_unit"`
}

// WriteLoggerSettings writes the logger settings to a JSON file in the logger directory
func WriteLoggerSettings(path string) error {
settings := LoggerSettings{
AdjCommitHash: CommitHash,
TimeUnit: TimestampUnit,
}

settingsBytes, err := json.Marshal(settings)
if err != nil {
return err
}

return os.WriteFile(path, settingsBytes, 0644)

}
Loading