mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 14:22:19 +00:00
Test adding MySQL
This commit is contained in:
parent
44a6a2b634
commit
ee2ab2b7a2
9 changed files with 111 additions and 11 deletions
|
@ -8,6 +8,10 @@ var (
|
|||
POSTGRESQL_STATS_DB string
|
||||
POSTGRESQL_STATS_TABLE = "statsdb"
|
||||
|
||||
MYSQL_HOST string
|
||||
MYSQL_USER string
|
||||
MYSQL_PASS string
|
||||
|
||||
TIMEZONE string
|
||||
|
||||
MATRIX_WEBHOOK_URL string
|
||||
|
@ -34,6 +38,7 @@ var (
|
|||
PEERTUBE_IDENTIFIER: "https://peertube.nz",
|
||||
BOOKWYRM_IDENTIFIER: "https://bookworm.nz",
|
||||
CALCKEY_IDENTIFIER: "https://calckey.nz",
|
||||
WRITEAS_IDENTIFIER: "https://write.nz",
|
||||
}
|
||||
|
||||
// Pixelfed
|
||||
|
@ -71,4 +76,9 @@ var (
|
|||
CALCKEY_DB_SCHEMA string
|
||||
CALCKEY_USER_QUERY = `SELECT count(*) FROM "user" WHERE "isDeleted" = False and "host" IS NULL;`
|
||||
CALCKEY_IDENTIFIER = "calckey"
|
||||
|
||||
// Write.as
|
||||
WRIETAS_DB_SCHEMA string
|
||||
WRITEAS_USER_QUERY = `SELECT count(*) FROM users WHERE status = 0`
|
||||
WRITEAS_IDENTIFIER = "writeas"
|
||||
)
|
||||
|
|
|
@ -172,6 +172,23 @@ func getUserCounts() ([]metric, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if WRIETAS_DB_SCHEMA != "" {
|
||||
userCount, err := runMySqlIntQuery(WRIETAS_DB_SCHEMA, WRITEAS_USER_QUERY)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: WRITEAS_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(WRITEAS_IDENTIFIER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(WRITEAS_IDENTIFIER),
|
||||
}
|
||||
log.Printf("%s user count: %d", WRITEAS_IDENTIFIER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
}
|
||||
}
|
||||
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
|
|
44
internal/gomastodonstats/mysql.go
Normal file
44
internal/gomastodonstats/mysql.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// Returns valid DSL for PSQL
|
||||
func getMySQLConnectionString(schema string) string {
|
||||
return fmt.Sprintf(
|
||||
"%s:%s@%s/%s",
|
||||
MYSQL_USER,
|
||||
MYSQL_PASS,
|
||||
MYSQL_HOST,
|
||||
schema,
|
||||
)
|
||||
}
|
||||
|
||||
// Returns DB connection
|
||||
func getMySQLConnection(schema string) (*sql.DB, error) {
|
||||
return sql.Open("mysql", getMySQLConnectionString(schema))
|
||||
}
|
||||
|
||||
func runMySqlIntQuery(schema string, q string) (int, error) {
|
||||
var res int
|
||||
db, err := getMySQLConnection(schema)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
rows, err := db.Query(q)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
rows.Scan(&res)
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
|
@ -9,7 +9,7 @@ import (
|
|||
)
|
||||
|
||||
// Returns valid DSL for PSQL
|
||||
func getConnectionString(schema string) string {
|
||||
func getPostgresConnectionString(schema string) string {
|
||||
return fmt.Sprintf(
|
||||
"postgresql://%s:%s@%s/%s?sslmode=disable",
|
||||
POSTGRESQL_USER,
|
||||
|
@ -20,13 +20,13 @@ func getConnectionString(schema string) string {
|
|||
}
|
||||
|
||||
// Returns DB connection
|
||||
func getConnection(schema string) (*sql.DB, error) {
|
||||
return sql.Open("postgres", getConnectionString(schema))
|
||||
func getPostgresConnection(schema string) (*sql.DB, error) {
|
||||
return sql.Open("postgres", getPostgresConnectionString(schema))
|
||||
}
|
||||
|
||||
// insertMetric to stats DB
|
||||
func insertValues(m metric) error {
|
||||
db, err := getConnection(POSTGRESQL_STATS_DB)
|
||||
db, err := getPostgresConnection(POSTGRESQL_STATS_DB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func insertValues(m metric) error {
|
|||
|
||||
func runIntQuery(schema string, q string) (int, error) {
|
||||
var res int
|
||||
db, err := getConnection(schema)
|
||||
db, err := getPostgresConnection(schema)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func runIntQuery(schema string, q string) (int, error) {
|
|||
|
||||
func runIntQueryWithTime(schema string, q string, t time.Time) (int, error) {
|
||||
var res int
|
||||
db, err := getConnection(schema)
|
||||
db, err := getPostgresConnection(schema)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue