From 11bdffc7f54d08a098bc8e9f6b063b0cc1d11835 Mon Sep 17 00:00:00 2001 From: Nabil Akir Date: Fri, 5 Apr 2024 02:14:41 +0200 Subject: [PATCH] add postgres connection --- README.md | 4 ++-- go.mod | 1 + go.sum | 2 ++ homeserver_dendrite.go | 7 ++++++- homeserver_synapse.go | 11 +++++++++-- main.go | 3 ++- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9037c8f..5ac296c 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ To add new tests, crib exiting files in the `tests` directory. # Deployment using docker image Set the environment variables for the go image - * `PANOPTICON_DB_DRIVER` (eg, mysql or sqlite) - * `PANOPTICON_DB` (go mysql connection string or filename for sqlite) + * `PANOPTICON_DB_DRIVER` (eg, mysql, sqlite or postgres) + * `PANOPTICON_DB` (go mysql/postgres connection string or filename for sqlite) * `PANOPTICON_PORT` (http port to expose panopticon on) Set the environment variables for the python image diff --git a/go.mod b/go.mod index 605fc76..1c885e9 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.18 require ( github.com/go-sql-driver/mysql v1.6.0 + github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.12 ) diff --git a/go.sum b/go.sum index b7dedd4..02e5b3a 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= diff --git a/homeserver_dendrite.go b/homeserver_dendrite.go index 7252cf9..277eb79 100644 --- a/homeserver_dendrite.go +++ b/homeserver_dendrite.go @@ -38,11 +38,16 @@ type ReportStatsDendrite struct { func createTableDendrite(db *sql.DB) error { autoincrement := "AUTOINCREMENT" + primaryKeyType := "INTEGER" + if *dbDriver == "mysql" { autoincrement = "AUTO_INCREMENT" + } else if *dbDriver == "postgres" { + autoincrement = "" + primaryKeyType = "SERIAL" } _, err := db.Exec(`CREATE TABLE IF NOT EXISTS dendrite_stats( - id INTEGER NOT NULL PRIMARY KEY ` + autoincrement + ` , + id ` + primaryKeyType + ` NOT NULL PRIMARY KEY ` + autoincrement + ` , homeserver VARCHAR(256), local_timestamp BIGINT, remote_timestamp BIGINT, diff --git a/homeserver_synapse.go b/homeserver_synapse.go index ba5588c..faa164f 100644 --- a/homeserver_synapse.go +++ b/homeserver_synapse.go @@ -31,11 +31,18 @@ type ReportStatsSynapse struct { func createTableSynapse(db *sql.DB) error { autoincrement := "AUTOINCREMENT" + primaryKeyType := "INTEGER" + doubleType := "DOUBLE" + if *dbDriver == "mysql" { autoincrement = "AUTO_INCREMENT" + } else if *dbDriver == "postgres" { + autoincrement = "" + primaryKeyType = "SERIAL" + doubleType = "DOUBLE PRECISION" } _, err := db.Exec(`CREATE TABLE IF NOT EXISTS stats( - id INTEGER NOT NULL PRIMARY KEY ` + autoincrement + ` , + id ` + primaryKeyType + ` NOT NULL PRIMARY KEY ` + autoincrement + ` , homeserver VARCHAR(256), local_timestamp BIGINT, remote_timestamp BIGINT, @@ -65,7 +72,7 @@ func createTableSynapse(db *sql.DB) error { r30v2_users_web BIGINT, cpu_average BIGINT, memory_rss BIGINT, - cache_factor DOUBLE, + cache_factor ` + doubleType + `, event_cache_size BIGINT, user_agent TEXT, daily_user_type_native BIGINT, diff --git a/main.go b/main.go index 566563c..abc53fa 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -// panopticon collects statistics posted to it, and records them in a sqlite3 or mysql database. +// panopticon collects statistics posted to it, and records them in a sqlite3, mysql or postgres database. package main import ( @@ -32,6 +32,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" + _ "github.com/lib/pq" ) var (