Browse Source

Accept being passed pre-built statements instead of query strings

gek/load-tests
Gabe Kangas 3 years ago
parent
commit
c847faec88
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
  1. 47
      core/chat/persistence.go

47
core/chat/persistence.go

@ -1,6 +1,7 @@
package chat package chat
import ( import (
"database/sql"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -9,6 +10,7 @@ import (
"github.com/owncast/owncast/core/data" "github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/core/user" "github.com/owncast/owncast/core/user"
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -207,9 +209,9 @@ type rowData struct {
userType *string userType *string
} }
func getChat(query string) []interface{} { func getChat(query *sql.Stmt) []interface{} {
history := make([]interface{}, 0) history := make([]interface{}, 0)
rows, err := _datastore.DB.Query(query) rows, err := query.Query()
if err != nil || rows.Err() != nil { if err != nil || rows.Err() != nil {
log.Errorln("error fetching chat history", err) log.Errorln("error fetching chat history", err)
return history return history
@ -271,32 +273,47 @@ func getChat(query string) []interface{} {
var _historyCache *[]interface{} var _historyCache *[]interface{}
var cachedAdminChatHistoryStatement *sql.Stmt
// GetChatModerationHistory will return all the chat messages suitable for moderation purposes. // GetChatModerationHistory will return all the chat messages suitable for moderation purposes.
func GetChatModerationHistory() []interface{} { func GetChatModerationHistory() []interface{} {
if _historyCache != nil { if _historyCache != nil {
return *_historyCache return *_historyCache
} }
if cachedAdminChatHistoryStatement == nil {
stmt, err := _datastore.DB.Prepare(`SELECT messages.id, user_id, body, title, subtitle, image, link, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes, users.type FROM messages INNER JOIN users ON messages.user_id = users.id ORDER BY timestamp DESC`)
if err != nil {
log.Errorln("error preparing chat moderation history statement", err)
return nil
}
cachedAdminChatHistoryStatement = stmt
}
// Get all messages regardless of visibility // Get all messages regardless of visibility
query := "SELECT messages.id, user_id, body, title, subtitle, image, link, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, authenticated_at, scopes, type FROM messages INNER JOIN users ON messages.user_id = users.id ORDER BY timestamp DESC" result := getChat(cachedAdminChatHistoryStatement)
result := getChat(query)
_historyCache = &result _historyCache = &result
return result return result
} }
var cachedChatHistoryStatement *sql.Stmt
// GetChatHistory will return all the chat messages suitable for returning as user-facing chat history. // GetChatHistory will return all the chat messages suitable for returning as user-facing chat history.
func GetChatHistory() []interface{} { func GetChatHistory() []interface{} {
// Get all visible messages if cachedChatHistoryStatement == nil {
query := fmt.Sprintf("SELECT messages.id,messages.user_id, messages.body, messages.title, messages.subtitle, messages.image, messages.link, messages.eventType, messages.hidden_at, messages.timestamp, users.display_name, users.display_color, users.created_at, users.disabled_at, users.previous_names, users.namechanged_at, users.authenticated_at, users.scopes, users.type FROM messages LEFT JOIN users ON messages.user_id = users.id WHERE hidden_at IS NULL AND disabled_at IS NULL ORDER BY timestamp DESC LIMIT %d", maxBacklogNumber) stmt, err := _datastore.DB.Prepare(fmt.Sprintf("SELECT messages.id, messages.user_id, messages.body, messages.title, messages.subtitle, messages.image, messages.link, messages.eventType, messages.hidden_at, messages.timestamp, users.display_name, users.display_color, users.created_at, users.disabled_at, users.previous_names, users.namechanged_at, users.scopes, users.type FROM users JOIN messages ON users.id = messages.user_id WHERE hidden_at IS NULL AND disabled_at IS NULL ORDER BY timestamp DESC LIMIT %d", maxBacklogNumber))
m := getChat(query) if err != nil {
log.Errorln("error preparing chat history statement", err)
// Invert order of messages return nil
for i, j := 0, len(m)-1; i < j; i, j = i+1, j-1 { }
m[i], m[j] = m[j], m[i] cachedChatHistoryStatement = stmt
} }
// Get all visible messages
m := getChat(cachedChatHistoryStatement)
return m return m
} }
@ -309,8 +326,12 @@ func SetMessageVisibilityForUserID(userID string, visible bool) error {
// Get a list of IDs to send to the connected clients to hide // Get a list of IDs to send to the connected clients to hide
ids := make([]string, 0) ids := make([]string, 0)
query := fmt.Sprintf("SELECT messages.id, user_id, body, title, subtitle, image, link, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, authenticated, scopes, type FROM messages INNER JOIN users ON messages.user_id = users.id WHERE user_id IS '%s'", userID) query := fmt.Sprintf("SELECT messages.id, user_id, body, title, subtitle, image, link, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes, type FROM messages INNER JOIN users ON messages.user_id = users.id WHERE user_id IS '%s'", userID)
messages := getChat(query) stmt, err := _datastore.DB.Prepare(query)
if err != nil {
return errors.Wrap(err, "error preparing chat history statement when setting message visibility")
}
messages := getChat(stmt)
if len(messages) == 0 { if len(messages) == 0 {
return nil return nil

Loading…
Cancel
Save