mirror of
https://github.com/SigNoz/signoz.git
synced 2025-12-21 01:16:57 +00:00
101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
|
|
package sqlalertmanagerstore
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
|
||
|
|
"go.signoz.io/signoz/pkg/errors"
|
||
|
|
"go.signoz.io/signoz/pkg/sqlstore"
|
||
|
|
"go.signoz.io/signoz/pkg/types/alertmanagertypes"
|
||
|
|
)
|
||
|
|
|
||
|
|
type config struct {
|
||
|
|
sqlstore sqlstore.SQLStore
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewConfigStore(sqlstore sqlstore.SQLStore) alertmanagertypes.ConfigStore {
|
||
|
|
return &config{sqlstore: sqlstore}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get implements alertmanagertypes.ConfigStore.
|
||
|
|
func (store *config) Get(ctx context.Context, orgID string) (*alertmanagertypes.Config, error) {
|
||
|
|
storeableConfig := new(alertmanagertypes.StoreableConfig)
|
||
|
|
|
||
|
|
err := store.
|
||
|
|
sqlstore.
|
||
|
|
BunDB().
|
||
|
|
NewSelect().
|
||
|
|
Model(storeableConfig).
|
||
|
|
Where("org_id = ?", orgID).
|
||
|
|
Scan(ctx)
|
||
|
|
if err != nil {
|
||
|
|
if err == sql.ErrNoRows {
|
||
|
|
return nil, errors.Newf(errors.TypeNotFound, alertmanagertypes.ErrCodeAlertmanagerConfigNotFound, "cannot find alertmanager config for orgID %s", orgID)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
cfg, err := alertmanagertypes.NewConfigFromStoreableConfig(storeableConfig)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return cfg, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Set implements alertmanagertypes.ConfigStore.
|
||
|
|
func (store *config) Set(ctx context.Context, config *alertmanagertypes.Config) error {
|
||
|
|
tx, err := store.sqlstore.BunDB().BeginTx(ctx, nil)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
defer tx.Rollback() //nolint:errcheck
|
||
|
|
|
||
|
|
if _, err = tx.
|
||
|
|
NewInsert().
|
||
|
|
Model(config.StoreableConfig()).
|
||
|
|
On("CONFLICT (org_id) DO UPDATE").
|
||
|
|
Set("config = ?", string(config.StoreableConfig().Config)).
|
||
|
|
Set("updated_at = ?", config.StoreableConfig().UpdatedAt).
|
||
|
|
Exec(ctx); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
channels := config.Channels()
|
||
|
|
if len(channels) != 0 {
|
||
|
|
if _, err = tx.NewInsert().
|
||
|
|
Model(&channels).
|
||
|
|
On("CONFLICT (name) DO UPDATE").
|
||
|
|
Set("data = EXCLUDED.data").
|
||
|
|
Set("updated_at = EXCLUDED.updated_at").
|
||
|
|
Exec(ctx); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if err = tx.Commit(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (store *config) ListOrgs(ctx context.Context) ([]string, error) {
|
||
|
|
var orgIDs []string
|
||
|
|
|
||
|
|
err := store.
|
||
|
|
sqlstore.
|
||
|
|
BunDB().
|
||
|
|
NewSelect().
|
||
|
|
Table("organizations").
|
||
|
|
ColumnExpr("id").
|
||
|
|
Scan(ctx, &orgIDs)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return orgIDs, nil
|
||
|
|
}
|