2020-11-29 06:09:35 +05:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2020-12-23 13:37:52 +05:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-12-23 13:27:17 +05:00
|
|
|
"go.dev.pztrn.name/metricator/internal/models"
|
2020-11-29 06:09:35 +05:00
|
|
|
)
|
|
|
|
|
2020-12-23 13:27:17 +05:00
|
|
|
// Responds with needed data. First parameter is a type of data needed (like metric name),
|
|
|
|
// second parameter is actual metric name. Second parameter also can be empty.
|
|
|
|
func (a *Application) respond(rInfo *models.RequestInfo) string {
|
2020-12-23 13:37:52 +05:00
|
|
|
// If metric was requested - return only it.
|
|
|
|
if rInfo.Metric != "" {
|
|
|
|
metric, err := a.storage.Get(rInfo.Metric)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return metric.GetValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we should get all metrics as slice and return them as string.
|
|
|
|
// This is needed for metrics autodiscovery.
|
|
|
|
metrics := a.storage.GetAsSlice()
|
|
|
|
|
|
|
|
metricsBytes, err := json.Marshal(metrics)
|
2020-12-23 13:27:17 +05:00
|
|
|
if err != nil {
|
2020-12-23 13:37:52 +05:00
|
|
|
// ToDo: log error
|
2020-12-23 13:27:17 +05:00
|
|
|
return ""
|
|
|
|
}
|
2020-11-29 06:09:35 +05:00
|
|
|
|
2020-12-23 13:37:52 +05:00
|
|
|
return string(metricsBytes)
|
2020-11-29 06:09:35 +05:00
|
|
|
}
|