-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in prep for encrypting alerting configs
- Loading branch information
Showing
10 changed files
with
153 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jackc/pgx/v5" | ||
|
||
"github.com/PeerDB-io/peer-flow/generated/protos" | ||
) | ||
|
||
func (h *FlowRequestHandler) GetAlertConfigs(ctx context.Context, req *protos.GetAlertConfigsRequest) (*protos.GetAlertConfigsResponse, error) { | ||
rows, err := h.pool.Query(ctx, "select id, service_type, service_config from peerdb_stats.alerting_config") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
configs, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (*protos.AlertConfig, error) { | ||
config := &protos.AlertConfig{} | ||
err := row.Scan(&config.Id, &config.ServiceType, &config.ServiceConfig) | ||
return config, err | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protos.GetAlertConfigsResponse{Configs: configs}, nil | ||
} | ||
|
||
func (h *FlowRequestHandler) PostAlertConfig(ctx context.Context, req *protos.PostAlertConfigRequest) (*protos.PostAlertConfigResponse, error) { | ||
if req.Id == -1 { | ||
var id int32 | ||
if err := h.pool.QueryRow( | ||
ctx, | ||
"insert into peerdb_stats.alerting_config (service_type, service_config) values ($1, $2) returning id", | ||
req.ServiceType, | ||
req.ServiceConfig, | ||
).Scan(&id); err != nil { | ||
// TODO don't send back json | ||
return nil, fmt.Errorf("cannot add %s: %w", req.ServiceConfig, err) | ||
} | ||
return &protos.PostAlertConfigResponse{Id: id}, nil | ||
} else if _, err := h.pool.Exec( | ||
ctx, | ||
"update peerdb_stats.alerting_config set service_type = $1, service_config = $2 where id = $3", | ||
req.ServiceType, | ||
req.ServiceConfig, | ||
req.Id, | ||
); err != nil { | ||
return nil, err | ||
} | ||
return &protos.PostAlertConfigResponse{Id: req.Id}, nil | ||
} | ||
|
||
func (h *FlowRequestHandler) DeleteAlertConfig( | ||
ctx context.Context, | ||
req *protos.DeleteAlertConfigRequest, | ||
) (*protos.DeleteAlertConfigResponse, error) { | ||
if _, err := h.pool.Exec(ctx, "delete from peerdb_stats.alerting_config where id = $1", req.Id); err != nil { | ||
return nil, err | ||
} | ||
return &protos.DeleteAlertConfigResponse{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,30 @@ | ||
import { alertConfigType } from '@/app/alert-config/validation'; | ||
import prisma from '@/app/utils/prisma'; | ||
import { alerting_config } from '@prisma/client'; | ||
import { GetFlowHttpAddressFromEnv } from '@/rpc/http'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export async function GET() { | ||
const configs: alerting_config[] = await prisma.alerting_config.findMany(); | ||
const serializedConfigs = configs.map((config) => ({ | ||
...config, | ||
id: String(config.id), | ||
})); | ||
return new Response(JSON.stringify(serializedConfigs)); | ||
const flowServiceAddr = GetFlowHttpAddressFromEnv(); | ||
return fetch(`${flowServiceAddr}/v1/alerts/config`, { cache: 'no-store' }); | ||
} | ||
|
||
export async function POST(request: Request) { | ||
const alertConfigReq: alertConfigType = await request.json(); | ||
const createRes = await prisma.alerting_config.create({ | ||
data: { | ||
service_type: alertConfigReq.serviceType, | ||
service_config: alertConfigReq.serviceConfig, | ||
}, | ||
}); | ||
|
||
return new Response(createRes.id ? 'success' : 'error'); | ||
const flowServiceAddr = GetFlowHttpAddressFromEnv(); | ||
return fetch(`${flowServiceAddr}/v1/alerts/config`, { | ||
method: 'POST', | ||
cache: 'no-store', | ||
body: request.body, | ||
duplex: 'half', | ||
} as any); | ||
} | ||
|
||
export async function DELETE(request: Request) { | ||
const configDeleteReq: { id: number } = await request.json(); | ||
const deleteRes = await prisma.alerting_config.delete({ | ||
where: { | ||
id: configDeleteReq.id, | ||
}, | ||
}); | ||
|
||
return new Response(deleteRes.id ? 'success' : 'error'); | ||
} | ||
|
||
export async function PUT(request: Request) { | ||
const alertConfigReq: alertConfigType = await request.json(); | ||
const editRes = await prisma.alerting_config.update({ | ||
data: { | ||
service_type: alertConfigReq.serviceType, | ||
service_config: alertConfigReq.serviceConfig, | ||
}, | ||
where: { | ||
id: alertConfigReq.id, | ||
}, | ||
}); | ||
|
||
return new Response(editRes.id ? 'success' : 'error'); | ||
export async function DELETE(request: NextRequest) { | ||
const flowServiceAddr = GetFlowHttpAddressFromEnv(); | ||
return fetch( | ||
`${flowServiceAddr}/v1/alerts/config/${Number( | ||
request.nextUrl.searchParams.get('id') | ||
)}`, | ||
{ | ||
method: 'DELETE', | ||
cache: 'no-store', | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.