Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Use copy from #43

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
cloud.google.com/go/cloudsqlconn v1.3.0
github.com/jackc/pgx/v5 v5.4.3
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.1
google.golang.org/grpc v1.59.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/microsoft/go-mssqldb v1.1.0 h1:jsV+tpvcPTbNNKW0o3kiCD69kOHICsfjZ2VcVu2lKYc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
58 changes: 58 additions & 0 deletions postgres-driver/copyfrom.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions postgres-driver/db.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions postgres-driver/postgresdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"net"
"time"

// PQ import is required

"cloud.google.com/go/cloudsqlconn"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
Expand Down
66 changes: 1 addition & 65 deletions postgres-driver/query.sql.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 46 additions & 134 deletions postgres-driver/service_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,155 +4,67 @@ import (
"context"
"time"

"github.com/lib/pq"
"github.com/pokt-foundation/transaction-db/types"
)

const insertServiceRecords = `
INSERT INTO service_record (
node_public_key,
pokt_chain_id,
session_key,
request_id,
portal_region_name,
latency,
tickets,
result,
available,
successes,
failures,
p90_success_latency,
median_success_latency,
weighted_success_latency,
success_rate,
created_at,
updated_at
)
SELECT * FROM unnest(
$1::char(64)[],
$2::char(4)[],
$3::char(44)[],
$4::varchar[],
$5::varchar[],
$6::float[],
$7::integer[],
$8::varchar[],
$9::boolean[],
$10::integer[],
$11::integer[],
$12::float[],
$13::float[],
$14::float[],
$15::float[],
$16::timestamp[],
$17::timestamp[]
) AS t(
session_key,
request_id,
portal_region_name,
latency,
tickets,
result,
available,
successes,
failures,
p90_success_latency,
median_success_latency,
weighted_success_latency,
success_rate,
created_at,
updated_at
)`

func (d *PostgresDriver) WriteServiceRecord(ctx context.Context, serviceRecord types.ServiceRecord) error {
now := time.Now()
createdAt := time.Now()

return d.InsertServiceRecord(ctx, InsertServiceRecordParams{
NodePublicKey: serviceRecord.NodePublicKey,
PoktChainID: serviceRecord.PoktChainID,
SessionKey: serviceRecord.SessionKey,
RequestID: serviceRecord.RequestID,
PortalRegionName: serviceRecord.PortalRegionName,
Latency: serviceRecord.Latency,
Tickets: int32(serviceRecord.Tickets),
Result: serviceRecord.Result,
Available: serviceRecord.Available,
Successes: int32(serviceRecord.Successes),
Failures: int32(serviceRecord.Failures),
P90SuccessLatency: serviceRecord.P90SuccessLatency,
MedianSuccessLatency: serviceRecord.MedianSuccessLatency,
WeightedSuccessLatency: serviceRecord.WeightedSuccessLatency,
SuccessRate: serviceRecord.SuccessRate,
CreatedAt: newTimestamp(now),
UpdatedAt: newTimestamp(now),
_, err := d.InsertServiceRecords(ctx, []InsertServiceRecordsParams{
{
NodePublicKey: serviceRecord.NodePublicKey,
PoktChainID: serviceRecord.PoktChainID,
SessionKey: serviceRecord.SessionKey,
RequestID: serviceRecord.RequestID,
PortalRegionName: serviceRecord.PortalRegionName,
Latency: serviceRecord.Latency,
Tickets: int32(serviceRecord.Tickets),
Result: serviceRecord.Result,
Available: serviceRecord.Available,
Successes: int32(serviceRecord.Successes),
Failures: int32(serviceRecord.Failures),
P90SuccessLatency: serviceRecord.P90SuccessLatency,
MedianSuccessLatency: serviceRecord.MedianSuccessLatency,
WeightedSuccessLatency: serviceRecord.WeightedSuccessLatency,
SuccessRate: serviceRecord.SuccessRate,
CreatedAt: newTimestamp(createdAt),
UpdatedAt: newTimestamp(createdAt),
},
})

return err
}

func (d *PostgresDriver) WriteServiceRecords(ctx context.Context, serviceRecords []*types.ServiceRecord) error {
now := time.Now()
createdAt := time.Now()

var (
nodePublicKeys []string
poktChainIDs []string
sessionKeys []string
requestIDs []string
portalRegionNames []string
latencies []float64
tickets []int32
results []string
availables []bool
successes []int32
failures []int32
p90SuccessLatencies []float64
medianSuccessLatencies []float64
weightedSuccessLatencies []float64
successRates []float64
createdTimes []time.Time
updatedTimes []time.Time
)
serviceRecordParams := make([]InsertServiceRecordsParams, 0, len(serviceRecords))

for _, serviceRecord := range serviceRecords {
nodePublicKeys = append(nodePublicKeys, serviceRecord.NodePublicKey)
poktChainIDs = append(poktChainIDs, serviceRecord.PoktChainID)
sessionKeys = append(sessionKeys, serviceRecord.SessionKey)
requestIDs = append(requestIDs, serviceRecord.RequestID)
portalRegionNames = append(portalRegionNames, serviceRecord.PortalRegionName)
latencies = append(latencies, serviceRecord.Latency)
tickets = append(tickets, int32(serviceRecord.Tickets))
results = append(results, serviceRecord.Result)
availables = append(availables, serviceRecord.Available)
successes = append(successes, int32(serviceRecord.Successes))
failures = append(failures, int32(serviceRecord.Failures))
p90SuccessLatencies = append(p90SuccessLatencies, serviceRecord.P90SuccessLatency)
medianSuccessLatencies = append(medianSuccessLatencies, serviceRecord.MedianSuccessLatency)
weightedSuccessLatencies = append(weightedSuccessLatencies, serviceRecord.WeightedSuccessLatency)
successRates = append(successRates, serviceRecord.SuccessRate)
createdTimes = append(createdTimes, now)
updatedTimes = append(updatedTimes, now)
serviceRecordParams = append(serviceRecordParams, InsertServiceRecordsParams{
NodePublicKey: serviceRecord.NodePublicKey,
PoktChainID: serviceRecord.PoktChainID,
SessionKey: serviceRecord.SessionKey,
RequestID: serviceRecord.RequestID,
PortalRegionName: serviceRecord.PortalRegionName,
Latency: serviceRecord.Latency,
Tickets: int32(serviceRecord.Tickets),
Result: serviceRecord.Result,
Available: serviceRecord.Available,
Successes: int32(serviceRecord.Successes),
Failures: int32(serviceRecord.Failures),
P90SuccessLatency: serviceRecord.P90SuccessLatency,
MedianSuccessLatency: serviceRecord.MedianSuccessLatency,
WeightedSuccessLatency: serviceRecord.WeightedSuccessLatency,
SuccessRate: serviceRecord.SuccessRate,
CreatedAt: newTimestamp(createdAt),
UpdatedAt: newTimestamp(createdAt),
})
}

_, err := d.db.Exec(context.TODO(), insertServiceRecords, pq.StringArray(nodePublicKeys),
pq.StringArray(poktChainIDs),
pq.StringArray(sessionKeys),
pq.StringArray(requestIDs),
pq.StringArray(portalRegionNames),
pq.Float64Array(latencies),
pq.Int32Array(tickets),
pq.StringArray(results),
pq.BoolArray(availables),
pq.Int32Array(successes),
pq.Int32Array(failures),
pq.Float64Array(p90SuccessLatencies),
pq.Float64Array(medianSuccessLatencies),
pq.Float64Array(weightedSuccessLatencies),
pq.Float64Array(successRates),
pq.Array(createdTimes),
pq.Array(updatedTimes))
if err != nil {
return err
}
_, err := d.InsertServiceRecords(ctx, serviceRecordParams)

return nil
return err
}

func (d *PostgresDriver) ReadServiceRecord(ctx context.Context, serviceRecordID int) (types.ServiceRecord, error) {
Expand Down
2 changes: 1 addition & 1 deletion postgres-driver/sqlc/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ VALUES (
$30,
$31
);
-- name: InsertServiceRecord :exec
-- name: InsertServiceRecords :copyfrom
INSERT INTO service_record (
node_public_key,
pokt_chain_id,
Expand Down
Loading