Skip to content

Commit

Permalink
feat: Improve advanced mode in checker
Browse files Browse the repository at this point in the history
Datatype that stores metrics for check changed to multiple datatypes
that handle different operations on data. Fetcher interface changed to
return new implemented datatypes

Relates #428
  • Loading branch information
litleleprikon committed Dec 11, 2019
1 parent 6b2e022 commit b3eb635
Show file tree
Hide file tree
Showing 32 changed files with 1,441 additions and 1,365 deletions.
54 changes: 21 additions & 33 deletions api/controller/trigger_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@ import (
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/dto"
"github.com/moira-alert/moira/database"
"github.com/moira-alert/moira/metric_source"
metricSource "github.com/moira-alert/moira/metric_source"
)

// GetTriggerEvaluationResult evaluates every target in trigger and returns
// result, separated on main and additional targets metrics
func GetTriggerEvaluationResult(dataBase moira.Database, metricSourceProvider *metricSource.SourceProvider, from, to int64, triggerID string, fetchRealtimeData bool) (*metricSource.TriggerMetricsData, *moira.Trigger, error) {
func GetTriggerEvaluationResult(dataBase moira.Database, metricSourceProvider *metricSource.SourceProvider, from, to int64, triggerID string, fetchRealtimeData bool) (metricSource.FetchedMetrics, *moira.Trigger, error) {
trigger, err := dataBase.GetTrigger(triggerID)
if err != nil {
return nil, nil, err
}
triggerMetrics := metricSource.NewTriggerMetricsData()
triggerMetrics := metricSource.NewFetchedMetricsWithCapacity(0)
metricsSource, err := metricSourceProvider.GetTriggerMetricSource(&trigger)
if err != nil {
return nil, &trigger, err
}
for i, tar := range trigger.Targets {
fetchResult, err := metricsSource.Fetch(tar, from, to, fetchRealtimeData)
for i, target := range trigger.Targets {
fetchResult, err := metricsSource.Fetch(target, from, to, fetchRealtimeData)
if err != nil {
return nil, &trigger, err
}
metricData := fetchResult.GetMetricsData()
if i == 0 {
triggerMetrics.Main = metricData
} else {
triggerMetrics.Additional = append(triggerMetrics.Additional, metricData...)
}

triggerMetrics.AddMetrics(i, metricData)
}
return triggerMetrics, &trigger, nil
}
Expand All @@ -57,31 +54,22 @@ func GetTriggerMetrics(dataBase moira.Database, metricSourceProvider *metricSour
}
return nil, api.ErrorInternalServer(err)
}
triggerMetrics := dto.TriggerMetrics{
Main: make(map[string][]*moira.MetricValue),
Additional: make(map[string][]*moira.MetricValue),
}
for _, timeSeries := range tts.Main {
values := make([]*moira.MetricValue, 0)
for i := 0; i < len(timeSeries.Values); i++ {
timestamp := timeSeries.StartTime + int64(i)*timeSeries.StepTime
value := timeSeries.GetTimestampValue(timestamp)
if moira.IsValidFloat64(value) {
values = append(values, &moira.MetricValue{Value: value, Timestamp: timestamp})
}
}
triggerMetrics.Main[timeSeries.Name] = values
}
for _, timeSeries := range tts.Additional {
values := make([]*moira.MetricValue, 0)
for i := 0; i < len(timeSeries.Values); i++ {
timestamp := timeSeries.StartTime + int64(i)*timeSeries.StepTime
value := timeSeries.GetTimestampValue(timestamp)
if moira.IsValidFloat64(value) {
values = append(values, &moira.MetricValue{Value: value, Timestamp: timestamp})
triggerMetrics := make(dto.TriggerMetrics)

for targetName, target := range tts {
targetMetrics := make(map[string][]moira.MetricValue)
for _, timeSeries := range target {
values := make([]moira.MetricValue, 0)
for i, l := 0, len(timeSeries.Values); i < l; i++ {
timestamp := timeSeries.StartTime + int64(i)*timeSeries.StepTime
value := timeSeries.GetTimestampValue(timestamp)
if moira.IsValidFloat64(value) {
values = append(values, moira.MetricValue{Value: value, Timestamp: timestamp})
}
}
targetMetrics[timeSeries.Name] = values
}
triggerMetrics.Additional[timeSeries.Name] = values
triggerMetrics[targetName] = targetMetrics
}
return &triggerMetrics, nil
}
Expand Down
7 changes: 2 additions & 5 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/middleware"
"github.com/moira-alert/moira/expression"
"github.com/moira-alert/moira/metric_source"
metricSource "github.com/moira-alert/moira/metric_source"
)

type TriggersList struct {
Expand Down Expand Up @@ -305,10 +305,7 @@ func (*SaveTriggerResponse) Render(w http.ResponseWriter, r *http.Request) error
return nil
}

type TriggerMetrics struct {
Main map[string][]*moira.MetricValue `json:"main"`
Additional map[string][]*moira.MetricValue `json:"additional,omitempty"`
}
type TriggerMetrics map[string]map[string][]moira.MetricValue

func (*TriggerMetrics) Render(w http.ResponseWriter, r *http.Request) error {
return nil
Expand Down
13 changes: 7 additions & 6 deletions api/handler/trigger_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/controller"
"github.com/moira-alert/moira/api/middleware"
"github.com/moira-alert/moira/metric_source"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira/plotting"
)

Expand Down Expand Up @@ -65,18 +65,19 @@ func getEvaluationParameters(request *http.Request) (sourceProvider *metricSourc
return
}

func evaluateTriggerMetrics(metricSourceProvider *metricSource.SourceProvider, from, to int64, triggerID string, fetchRealtimeData bool) ([]*metricSource.MetricData, *moira.Trigger, error) {
func evaluateTriggerMetrics(metricSourceProvider *metricSource.SourceProvider, from, to int64, triggerID string, fetchRealtimeData bool) ([]metricSource.MetricData, *moira.Trigger, error) {
tts, trigger, err := controller.GetTriggerEvaluationResult(database, metricSourceProvider, from, to, triggerID, fetchRealtimeData)
if err != nil {
return nil, trigger, err
}
var metricsData = make([]*metricSource.MetricData, 0, len(tts.Main)+len(tts.Additional))
metricsData = append(metricsData, tts.Main...)
metricsData = append(metricsData, tts.Additional...)
var metricsData = make([]metricSource.MetricData, 0, len(tts))
for _, metrics := range tts {
metricsData = append(metricsData, metrics...)
}
return metricsData, trigger, err
}

func buildRenderable(request *http.Request, trigger *moira.Trigger, metricsData []*metricSource.MetricData) (*chart.Chart, error) {
func buildRenderable(request *http.Request, trigger *moira.Trigger, metricsData []metricSource.MetricData) (*chart.Chart, error) {
timezone := request.URL.Query().Get("timezone")
location, err := time.LoadLocation(timezone)
if err != nil {
Expand Down
Loading

0 comments on commit b3eb635

Please sign in to comment.