Skip to content

Commit

Permalink
Merge pull request #9 from isaqueveras/release/refactoring
Browse files Browse the repository at this point in the history
library refactoring
  • Loading branch information
isaqueveras authored Aug 31, 2024
2 parents 6da31fb + d3f731e commit c21d555
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 387 deletions.
101 changes: 34 additions & 67 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,68 @@
package outis

import (
"context"
"errors"
"fmt"
"time"
)

type Script func(*Context)

// Context defines the data structure of the routine context
type Context struct {
Id ID `json:"id,omitempty"`
RoutineID ID `json:"routine_id,omitempty"`
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
Start uint `json:"start,omitempty"`
End uint `json:"end,omitempty"`
Interval time.Duration `json:"interval,omitempty"`
LoadInterval time.Duration `json:"load_interval,omitempty"`
Path string `json:"path,omitempty"`
RunAt time.Time `json:"run_at,omitempty"`
Watcher Watch `json:"-"`

script Script
metadata Metadata
logs []Log

Id ID `json:"id,omitempty"`
RoutineID ID `json:"routine_id,omitempty"`
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
Start uint `json:"start,omitempty"`
End uint `json:"end,omitempty"`
Interval time.Duration `json:"interval,omitempty"`
Path string `json:"path,omitempty"`
RunAt time.Time `json:"run_at,omitempty"`
Watcher Watch `json:"-"`

script func(*Context) error
metadata Metadata
latency time.Duration
histrogram []*histogram
indicator []*indicator
log ILogger `json:"-"`
}

// L define the log layer interface
L Logger `json:"-"`

context.Context
// GetLatency get script execution latency (in seconds)
func (ctx *Context) GetLatency() float64 {
return ctx.latency.Seconds()
}

func (ctx *Context) Error(message string, args ...interface{}) {
ctx.L.Errorf(message, args...)
ctx.logs = append(ctx.logs, Log{
Message: fmt.Sprintf(message, args...),
Level: levelLogError,
Timestamp: time.Now(),
})
// Error creates a new error message
func (ctx *Context) Error(msg string) {
ctx.log.Errorf(msg)
}

func (ctx *Context) Info(message string, args ...interface{}) {
ctx.L.Infof(message, args...)
ctx.logs = append(ctx.logs, Log{
Message: fmt.Sprintf(message, args...),
Level: levelLogInfo,
Timestamp: time.Now(),
})
// Info creates a new info message
func (ctx *Context) Info(msg string) {
ctx.log.Infof(msg)
}

func (ctx *Context) Debug(message string, args ...interface{}) {
ctx.L.Debugf(message, args...)
ctx.logs = append(ctx.logs, Log{
Message: fmt.Sprintf(message, args...),
Level: levelLogDebug,
Timestamp: time.Now(),
})
// Debug creates a new debug message
func (ctx *Context) Debug(msg string) {
ctx.log.Debugf(msg)
}

func (ctx *Context) Panic(message string, args ...interface{}) {
ctx.L.Panicf(message, args...)
ctx.logs = append(ctx.logs, Log{
Message: fmt.Sprintf(message, args...),
Level: levelLogPanic,
Timestamp: time.Now(),
})
// Panic creates a new panic message
func (ctx *Context) Panic(msg string) {
ctx.log.Panicf(msg)
}

// Metadata method for adding data to routine metadata
func (ctx *Context) Metadata(key string, args interface{}) {
ctx.metadata.Set(key, args)
}

func (ctx *Context) reload(ioutis Outis) {
ticker := time.NewTicker(ctx.LoadInterval)
for range ticker.C {
if err := ioutis.Reload(ctx); err != nil {
ctx.L.Errorf(err.Error())
}

ticker.Reset(ctx.LoadInterval)
ctx.Info("script '%s' (rid: %s) has been updated", ctx.Name, ctx.RoutineID)
}
}

func (ctx *Context) metrics(w *Watch, now time.Time) {
w.outis.Event(ctx, EventMetric{
ID: ctx.Id.ToString(),
StartedAt: now,
FinishedAt: time.Now(),
Latency: time.Since(now),
Metadata: ctx.metadata,
Log: ctx.logs,
Indicator: ctx.indicator,
Histogram: ctx.histrogram,
Watcher: WatcherMetric{
Expand All @@ -110,8 +78,7 @@ func (ctx *Context) metrics(w *Watch, now time.Time) {
},
})

ctx.logs, ctx.metadata, ctx.indicator, ctx.histrogram =
[]Log{}, Metadata{}, []*indicator{}, []*histogram{}
ctx.metadata, ctx.indicator, ctx.histrogram = Metadata{}, []*indicator{}, []*histogram{}
}

func (ctx *Context) isTime(hour int) bool {
Expand Down
118 changes: 0 additions & 118 deletions example/README.md

This file was deleted.

60 changes: 0 additions & 60 deletions example/main.go

This file was deleted.

6 changes: 5 additions & 1 deletion indicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ type indicator struct {
CreatedAt time.Time `json:"created_at"`
}

// NewIndicator creates a new indicator
func (ctx *Context) NewIndicator(key string) *indicator {
indicator := &indicator{Key: key, Value: 0, CreatedAt: time.Now()}
ctx.indicator = append(ctx.indicator, indicator)
return indicator
}

func (i *indicator) Inc() { i.Value++ }
// Inc increments the indicator data
func (i *indicator) Inc() { i.Value++ }

// Add add a value to the indicator
func (i *indicator) Add(value float64) { i.Value += value }
13 changes: 8 additions & 5 deletions interface.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package outis

// Event sets the type for the event structure
type Event interface{}

// Outis is the main interface for implementing the outis lib.
type Outis interface {
// IOutis is the main interface for implementing the outis lib.
type IOutis interface {
Go(fn func() error)
Wait() error

Init(ctx *Context) error
Before(ctx *Context) error
After(ctx *Context) error
Reload(ctx *Context) error
Event(ctx *Context, event Event)
}

// Logger methods for logging messages.
type Logger interface {
// ILogger methods for logging messages.
type ILogger interface {
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Expand Down
Loading

0 comments on commit c21d555

Please sign in to comment.