mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 14:22:19 +00:00
Initial Commit
This commit is contained in:
commit
4e9199bf3e
12 changed files with 267 additions and 0 deletions
32
internal/gomastodonstats/consts.go
Normal file
32
internal/gomastodonstats/consts.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package gomastodonstats
|
||||
|
||||
// Host information
|
||||
var (
|
||||
POSTGRESQL_HOST string
|
||||
POSTGRESQL_USER string
|
||||
POSTGRESQL_PASS string
|
||||
POSTGRESQL_STATS_DB string
|
||||
POSTGRESQL_STATS_TABLE = "statsdb"
|
||||
|
||||
TIMEZONE string
|
||||
|
||||
// Pixelfed
|
||||
PIXELFED_DB_SCHEMA string
|
||||
PIXELFED_USER_QUERY = "SELECT count(*) FROM users WHERE status IS NULL;"
|
||||
|
||||
// Matrix
|
||||
MATRIX_DB_SCHEMA string
|
||||
MATRIX_USER_QUERY = "SELECT count(*) FROM users WHERE deactivated = 0;"
|
||||
|
||||
// Mastodon
|
||||
MASTODON_DB_SCHEMA string
|
||||
MASTODON_USER_QUERY = "SELECT count(*) FROM users WHERE disabled = False;"
|
||||
|
||||
// Mobilizon
|
||||
MOBILIZON_DB_SCHEMA string
|
||||
MOBILIZON_USER_QUERY = "SELECT count(*) FROM users WHERE disabled = False;"
|
||||
|
||||
// Peertube
|
||||
PEERTUBE_DB_SCHEMA string
|
||||
PEERTUBE_USER_QUERY = "SELECT count(*) FROM \"user\" WHERE blocked = False;"
|
||||
)
|
18
internal/gomastodonstats/main.go
Normal file
18
internal/gomastodonstats/main.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package gomastodonstats
|
||||
|
||||
import "log"
|
||||
|
||||
func Run() {
|
||||
log.Println("Fetching counts")
|
||||
|
||||
// Get Counts
|
||||
metrics, err := getUserCounts()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Write to DB
|
||||
persistMetrics(metrics)
|
||||
|
||||
// Output example
|
||||
}
|
1
internal/gomastodonstats/mastodon.go
Normal file
1
internal/gomastodonstats/mastodon.go
Normal file
|
@ -0,0 +1 @@
|
|||
package gomastodonstats
|
47
internal/gomastodonstats/metrics.go
Normal file
47
internal/gomastodonstats/metrics.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Stores out metric/row data
|
||||
type metric struct {
|
||||
Service string `json:"service"`
|
||||
MetricName string `json:"metric_name"`
|
||||
MetricTime time.Time `json:"metric_time"`
|
||||
MetricValue int `json:"metric_value"`
|
||||
}
|
||||
|
||||
func persistMetrics(metrics []metric) {
|
||||
startOfDay := getStartofDay()
|
||||
for _, v := range metrics {
|
||||
v.MetricTime = startOfDay
|
||||
err := insertValues(v)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getUserCounts() ([]metric, error) {
|
||||
var metrics []metric
|
||||
|
||||
if PIXELFED_DB_SCHEMA != "" {
|
||||
pfUsers, err := runIntQuery(PIXELFED_DB_SCHEMA, PIXELFED_USER_QUERY)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
pfMetric := metric{
|
||||
Service: "pixelfed",
|
||||
MetricName: "userCount",
|
||||
MetricValue: pfUsers,
|
||||
}
|
||||
log.Printf("Pixelfed user count: %d", pfUsers)
|
||||
metrics = append(metrics, pfMetric)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return metrics, nil
|
||||
}
|
59
internal/gomastodonstats/postgresql.go
Normal file
59
internal/gomastodonstats/postgresql.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// Returns valid DSL for PSQL
|
||||
func getConnectionString(schema string) string {
|
||||
return fmt.Sprintf(
|
||||
"postgresql://%s:%s@%s/%s?sslmode=disable",
|
||||
POSTGRESQL_USER,
|
||||
POSTGRESQL_PASS,
|
||||
POSTGRESQL_HOST,
|
||||
schema,
|
||||
)
|
||||
}
|
||||
|
||||
// Returns DB connection
|
||||
func getConnection(schema string) (*sql.DB, error) {
|
||||
return sql.Open("postgres", getConnectionString(schema))
|
||||
}
|
||||
|
||||
// insertMetric to stats DB
|
||||
func insertValues(m metric) error {
|
||||
db, err := getConnection(POSTGRESQL_STATS_DB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec(fmt.Sprintf(
|
||||
"INSERT into %s (service,metric_name,metric_time,metric_value) "+
|
||||
"VALUES ($1, $2, $3, $4)",
|
||||
POSTGRESQL_STATS_TABLE,
|
||||
), m.Service, m.MetricName, m.MetricTime, m.MetricValue)
|
||||
return err
|
||||
}
|
||||
|
||||
func runIntQuery(schema string, q string) (int, error) {
|
||||
var res int
|
||||
db, err := getConnection(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
|
||||
}
|
16
internal/gomastodonstats/utils.go
Normal file
16
internal/gomastodonstats/utils.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getStartofDay() time.Time {
|
||||
localTime, err := time.LoadLocation(TIMEZONE)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
t := time.Now().In(localTime)
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue