2025-05-17 00:15:00 +05:30
|
|
|
package implapdex
|
2023-07-26 12:27:46 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/errors"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/modules/apdex"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/sqlstore"
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/types"
|
2025-04-04 01:36:47 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
2025-03-06 15:39:45 +05:30
|
|
|
"github.com/uptrace/bun"
|
2023-07-26 12:27:46 +05:30
|
|
|
)
|
|
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
const (
|
|
|
|
|
defaultApdexThreshold float64 = 0.5
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type module struct {
|
|
|
|
|
sqlstore sqlstore.SQLStore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewModule(sqlstore sqlstore.SQLStore) apdex.Module {
|
|
|
|
|
return &module{
|
|
|
|
|
sqlstore: sqlstore,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-26 12:27:46 +05:30
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
func (module *module) Get(ctx context.Context, orgID string, services []string) ([]*types.ApdexSettings, error) {
|
|
|
|
|
var apdexSettings []*types.ApdexSettings
|
2023-07-26 12:27:46 +05:30
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
err := module.
|
|
|
|
|
sqlstore.
|
|
|
|
|
BunDB().
|
|
|
|
|
NewSelect().
|
2025-03-06 15:39:45 +05:30
|
|
|
Model(&apdexSettings).
|
|
|
|
|
Where("org_id = ?", orgID).
|
|
|
|
|
Where("service_name IN (?)", bun.In(services)).
|
|
|
|
|
Scan(ctx)
|
2023-07-26 12:27:46 +05:30
|
|
|
if err != nil {
|
2025-05-17 00:15:00 +05:30
|
|
|
return nil, module.sqlstore.WrapNotFoundErrf(err, errors.CodeNotFound, "apdex settings not found for services %v", services)
|
2023-07-26 12:27:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add default apdex settings for services that don't have any
|
|
|
|
|
for _, service := range services {
|
|
|
|
|
var found bool
|
|
|
|
|
for _, apdexSetting := range apdexSettings {
|
|
|
|
|
if apdexSetting.ServiceName == service {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
2025-05-17 00:15:00 +05:30
|
|
|
apdexSettings = append(apdexSettings, &types.ApdexSettings{
|
2023-07-26 12:27:46 +05:30
|
|
|
ServiceName: service,
|
|
|
|
|
Threshold: defaultApdexThreshold,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apdexSettings, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
func (module *module) Set(ctx context.Context, orgID string, apdexSettings *types.ApdexSettings) error {
|
2025-03-06 15:39:45 +05:30
|
|
|
apdexSettings.OrgID = orgID
|
2025-04-04 01:36:47 +05:30
|
|
|
apdexSettings.Identifiable.ID = valuer.GenerateUUID()
|
2023-07-26 12:27:46 +05:30
|
|
|
|
2025-05-17 00:15:00 +05:30
|
|
|
_, err := module.
|
|
|
|
|
sqlstore.
|
|
|
|
|
BunDB().
|
|
|
|
|
NewInsert().
|
2025-03-06 15:39:45 +05:30
|
|
|
Model(apdexSettings).
|
|
|
|
|
On("CONFLICT (org_id, service_name) DO UPDATE").
|
|
|
|
|
Set("threshold = EXCLUDED.threshold").
|
|
|
|
|
Set("exclude_status_codes = EXCLUDED.exclude_status_codes").
|
|
|
|
|
Exec(ctx)
|
2023-07-26 12:27:46 +05:30
|
|
|
if err != nil {
|
2025-05-17 00:15:00 +05:30
|
|
|
return err
|
2023-07-26 12:27:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|