From c62ab03b1a9fe19739ecea7cedaaf3c0634cb4dd Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Mon, 26 Dec 2022 19:06:31 -0600
Subject: [PATCH] wip debug stuff

---
 config_data.yml           | 10 -----
 lib/gat/admin/admin.go    | 42 ++++++++++++++++++-
 lib/util/gatutil/table.go | 66 ++++++++++++++++++++++++++++++
 lib/util/gatutil/type.go  | 86 +++++++++++++++++++++++++++++++++++++++
 scripts/ddos/ddos.go      |  2 +-
 test/docker-compose.yml   |  6 +--
 6 files changed, 196 insertions(+), 16 deletions(-)
 create mode 100644 lib/util/gatutil/table.go
 create mode 100644 lib/util/gatutil/type.go

diff --git a/config_data.yml b/config_data.yml
index 4dacac28..51f3c470 100644
--- a/config_data.yml
+++ b/config_data.yml
@@ -34,11 +34,6 @@ pools:
             role: primary
             username: ENV$PSQL_DB_USER_RW
             password: ENV$PSQL_DB_PASS_RW
-          - host: ENV$PSQL_REP_DB_HOST
-            port: 5432
-            role: replica
-            username: ENV$PSQL_DB_USER_RO
-            password: ENV$PSQL_DB_PASS_RO
   prest:
     pool_mode: transaction
     default_role: primary
@@ -61,11 +56,6 @@ pools:
             role: primary
             username: ENV$PSQL_DB_USER_RW
             password: ENV$PSQL_DB_PASS_RW
-          - host: ENV$PSQL_REP_DB_HOST
-            port: 5432
-            role: replica
-            username: ENV$PSQL_DB_USER_RO
-            password: ENV$PSQL_DB_PASS_RO
 #  poolname OR databasename:
 #    pool_mode: session OR transaction
 #    default_role: primary OR replica
diff --git a/lib/gat/admin/admin.go b/lib/gat/admin/admin.go
index 289348d3..3c72400a 100644
--- a/lib/gat/admin/admin.go
+++ b/lib/gat/admin/admin.go
@@ -3,6 +3,7 @@ package admin
 import (
 	"context"
 	"errors"
+	"gfx.cafe/gfx/pggat/lib/util/gatutil"
 
 	"gfx.cafe/gfx/pggat/lib/config"
 	"gfx.cafe/gfx/pggat/lib/gat"
@@ -92,8 +93,45 @@ func New(g gat.Gat) *Database {
 	out.r.Register([]string{"show", "clients"}, func(_ gat.Client, _ []string) error {
 		return nil
 	})
-	out.r.Register([]string{"show", "pools"}, func(_ gat.Client, _ []string) error {
-		return nil
+	out.r.Register([]string{"show", "pools"}, func(c gat.Client, _ []string) error {
+		table := gatutil.Table{
+			Header: gatutil.TableHeader{
+				Columns: []gatutil.TableHeaderColumn{
+					{
+						Name: "Table name",
+						Type: gatutil.Text{},
+					},
+					{
+						Name: "Min latency",
+						Type: gatutil.Float64{},
+					},
+					{
+						Name: "Max latency",
+						Type: gatutil.Float64{},
+					},
+					{
+						Name: "Avg latency",
+						Type: gatutil.Float64{},
+					},
+					{
+						Name: "Request Count",
+						Type: gatutil.Int64{},
+					},
+				},
+			},
+			Rows: []gatutil.TableRow{
+				{
+					Columns: []any{
+						"Test",
+						float64(1.0),
+						float64(2.0),
+						float64(3.0),
+						int64(123),
+					},
+				},
+			},
+		}
+		return table.Send(c)
 	})
 	out.r.Register([]string{"show", "lists"}, func(_ gat.Client, _ []string) error {
 		return nil
diff --git a/lib/util/gatutil/table.go b/lib/util/gatutil/table.go
new file mode 100644
index 00000000..aafc2056
--- /dev/null
+++ b/lib/util/gatutil/table.go
@@ -0,0 +1,66 @@
+package gatutil
+
+import (
+	"fmt"
+	"gfx.cafe/gfx/pggat/lib/gat"
+	"gfx.cafe/gfx/pggat/lib/gat/protocol"
+)
+
+type TableHeaderColumn struct {
+	Name string
+	Type Type
+}
+
+func (T *TableHeaderColumn) RowDescription() protocol.FieldsRowDescriptionFields {
+	return protocol.FieldsRowDescriptionFields{
+		Name:         T.Name,
+		DataType:     T.Type.OID(),
+		DataTypeSize: T.Type.Len(),
+		FormatCode:   0,
+	}
+}
+
+type TableHeader struct {
+	Columns []TableHeaderColumn
+}
+
+func (T *TableHeader) RowDescription() (pkt protocol.RowDescription) {
+	for _, col := range T.Columns {
+		pkt.Fields.Fields = append(pkt.Fields.Fields, col.RowDescription())
+	}
+	return
+}
+
+type TableRow struct {
+	Columns []any
+}
+
+func (T *TableRow) DataRow() (pkt protocol.DataRow) {
+	for _, col := range T.Columns {
+		pkt.Fields.Columns = append(pkt.Fields.Columns, protocol.FieldsDataRowColumns{
+			Bytes: []byte(fmt.Sprintf("%v\x00", col)),
+		})
+	}
+	return
+}
+
+type Table struct {
+	Header TableHeader
+	Rows   []TableRow
+}
+
+func (T *Table) Send(client gat.Client) error {
+	rowDescription := T.Header.RowDescription()
+	err := client.Send(&rowDescription)
+	if err != nil {
+		return err
+	}
+	for _, row := range T.Rows {
+		dataRow := row.DataRow()
+		err = client.Send(&dataRow)
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
diff --git a/lib/util/gatutil/type.go b/lib/util/gatutil/type.go
new file mode 100644
index 00000000..72cf2099
--- /dev/null
+++ b/lib/util/gatutil/type.go
@@ -0,0 +1,86 @@
+package gatutil
+
+type Type interface {
+	OID() int32
+	Len() int16
+}
+
+type Text struct{}
+
+func (Text) OID() int32 {
+	return 25
+}
+
+func (Text) Len() int16 {
+	return -1
+}
+
+type Int16 struct{}
+
+func (Int16) OID() int32 {
+	return 21
+}
+
+func (Int16) Len() int16 {
+	return 2
+}
+
+type Int32 struct{}
+
+func (Int32) OID() int32 {
+	return 23
+}
+
+func (Int32) Len() int16 {
+	return 4
+}
+
+type Int64 struct{}
+
+func (Int64) OID() int32 {
+	return 20
+}
+
+func (Int64) Len() int16 {
+	return 8
+}
+
+type Char struct{}
+
+func (Char) OID() int32 {
+	return 18
+}
+
+func (Char) Len() int16 {
+	return 1
+}
+
+type Bool struct{}
+
+func (Bool) OID() int32 {
+	return 16
+}
+
+func (Bool) Len() int16 {
+	return 1
+}
+
+type Float32 struct{}
+
+func (Float32) OID() int32 {
+	return 700
+}
+
+func (Float32) Len() int16 {
+	return 4
+}
+
+type Float64 struct{}
+
+func (Float64) OID() int32 {
+	return 701
+}
+
+func (Float64) Len() int16 {
+	return 8
+}
diff --git a/scripts/ddos/ddos.go b/scripts/ddos/ddos.go
index 02698589..ba0f6e6b 100644
--- a/scripts/ddos/ddos.go
+++ b/scripts/ddos/ddos.go
@@ -24,7 +24,7 @@ const PrestHost = "http://localhost:3000"
 const PostgresHost = "postgres://dev_rw:pGf63Aq0M5ck@pggat-dev.gfx.town:6432/prest"
 
 // ThreadCount how many concurrent spammers to run at once
-const ThreadCount = 4
+const ThreadCount = 1000
 
 // TestTime how long to run the test. Set to 0 to run forever
 const TestTime = 30 * time.Second
diff --git a/test/docker-compose.yml b/test/docker-compose.yml
index d1b41b5a..2d6583db 100644
--- a/test/docker-compose.yml
+++ b/test/docker-compose.yml
@@ -15,10 +15,10 @@ services:
     build: ../
     restart: always
     environment:
+      PSQL_DB_ADMIN_USER: postgres
+      PSQL_DB_ADMIN_PASS: example
       PSQL_DB_USER_RW: postgres
       PSQL_DB_PASS_RW: example
-      PSQL_DB_USER_RO: postgres
-      PSQL_DB_PASS_RO: example
       PSQL_PRI_DB_HOST: db
       PSQL_REP_DB_HOST: db
     ports:
@@ -30,7 +30,7 @@ services:
     image: prest/prest
     restart: always
     environment:
-      PREST_PG_URL: postgres://postgres:example@pggat:6432/prest
+      PREST_PG_URL: postgres://postgres:example@db:5432/prest
       PREST_DEBUG: true
       PREST_SSL_MODE: disable
     ports:
-- 
GitLab