fix functions with changed names

This commit is contained in:
Sergio Moura 2023-09-12 15:56:05 -04:00
parent a8360efa67
commit 7638751bd6
3 changed files with 26 additions and 33 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/ddworken/hishtory/shared" "github.com/ddworken/hishtory/shared"
"github.com/jackc/pgx/v4/stdlib" "github.com/jackc/pgx/v4/stdlib"
_ "github.com/lib/pq" _ "github.com/lib/pq"

View File

@ -3,13 +3,14 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ddworken/hishtory/shared"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"html" "html"
"io" "io"
"math" "math"
"net/http" "net/http"
"time" "time"
"github.com/ddworken/hishtory/shared"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
) )
func (s *Server) apiSubmitHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
@ -43,7 +44,7 @@ func (s *Server) apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
} }
fmt.Printf("apiSubmitHandler: Found %d devices\n", len(devices)) fmt.Printf("apiSubmitHandler: Found %d devices\n", len(devices))
err = s.db.DeviceEntriesCreateChunk(r.Context(), devices, entries, 1000) err = s.db.AddHistoryEntriesForAllDevices(r.Context(), devices, entries)
if err != nil { if err != nil {
panic(fmt.Errorf("failed to execute transaction to add entries to DB: %w", err)) panic(fmt.Errorf("failed to execute transaction to add entries to DB: %w", err))
} }
@ -66,7 +67,7 @@ func (s *Server) apiBootstrapHandler(w http.ResponseWriter, r *http.Request) {
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false); err != nil { if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false); err != nil {
fmt.Printf("updateUsageData: %v\n", err) fmt.Printf("updateUsageData: %v\n", err)
} }
historyEntries, err := s.db.EncHistoryEntriesForUser(r.Context(), userId) historyEntries, err := s.db.AllHistoryEntriesForUser(r.Context(), userId)
checkGormError(err) checkGormError(err)
fmt.Printf("apiBootstrapHandler: Found %d entries\n", len(historyEntries)) fmt.Printf("apiBootstrapHandler: Found %d entries\n", len(historyEntries))
if err := json.NewEncoder(w).Encode(historyEntries); err != nil { if err := json.NewEncoder(w).Encode(historyEntries); err != nil {
@ -96,7 +97,7 @@ func (s *Server) apiQueryHandler(w http.ResponseWriter, r *http.Request) {
} }
// Then retrieve // Then retrieve
historyEntries, err := s.db.EncHistoryEntriesForDevice(r.Context(), deviceId, 5) historyEntries, err := s.db.HistoryEntriesForDevice(r.Context(), deviceId, 5)
checkGormError(err) checkGormError(err)
fmt.Printf("apiQueryHandler: Found %d entries for %s\n", len(historyEntries), r.URL) fmt.Printf("apiQueryHandler: Found %d entries for %s\n", len(historyEntries), r.URL)
if err := json.NewEncoder(w).Encode(historyEntries); err != nil { if err := json.NewEncoder(w).Encode(historyEntries); err != nil {
@ -108,11 +109,11 @@ func (s *Server) apiQueryHandler(w http.ResponseWriter, r *http.Request) {
if s.isProductionEnvironment { if s.isProductionEnvironment {
go func() { go func() {
span, ctx := tracer.StartSpanFromContext(ctx, "apiQueryHandler.incrementReadCount") span, ctx := tracer.StartSpanFromContext(ctx, "apiQueryHandler.incrementReadCount")
err := s.db.DeviceIncrementReadCounts(ctx, deviceId) err := s.db.IncrementEntryReadCountsForDevice(ctx, deviceId)
span.Finish(tracer.WithError(err)) span.Finish(tracer.WithError(err))
}() }()
} else { } else {
err := s.db.DeviceIncrementReadCounts(ctx, deviceId) err := s.db.IncrementEntryReadCountsForDevice(ctx, deviceId)
if err != nil { if err != nil {
panic("failed to increment read counts") panic("failed to increment read counts")
} }
@ -146,7 +147,7 @@ func (s *Server) apiSubmitDumpHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
err = s.db.EncHistoryCreateMulti(r.Context(), entries...) err = s.db.AddHistoryEntries(r.Context(), entries...)
checkGormError(err) checkGormError(err)
err = s.db.DumpRequestDeleteForUserAndDevice(r.Context(), userId, requestingDeviceId) err = s.db.DumpRequestDeleteForUserAndDevice(r.Context(), userId, requestingDeviceId)
checkGormError(err) checkGormError(err)
@ -209,10 +210,10 @@ func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
userId := getRequiredQueryParam(r, "user_id") userId := getRequiredQueryParam(r, "user_id")
deviceId := getRequiredQueryParam(r, "device_id") deviceId := getRequiredQueryParam(r, "device_id")
existingDevicesCount, err := s.db.DevicesCountForUser(r.Context(), userId) existingDevicesCount, err := s.db.CountDevicesForUser(r.Context(), userId)
checkGormError(err) checkGormError(err)
fmt.Printf("apiRegisterHandler: existingDevicesCount=%d\n", existingDevicesCount) fmt.Printf("apiRegisterHandler: existingDevicesCount=%d\n", existingDevicesCount)
if err := s.db.DeviceCreate(r.Context(), &shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: getRemoteAddr(r), RegistrationDate: time.Now()}); err != nil { if err := s.db.CreateDevice(r.Context(), &shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: getRemoteAddr(r), RegistrationDate: time.Now()}); err != nil {
checkGormError(err) checkGormError(err)
} }

View File

@ -219,28 +219,19 @@ func (s *Server) feedbackHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
if s.isProductionEnvironment { if s.isProductionEnvironment {
// Check that we have a reasonable looking set of devices/entries in the DB encHistoryEntryCount, err := s.db.CountHistoryEntries(r.Context())
//rows, err := s.db.Raw("SELECT true FROM enc_history_entries LIMIT 1 OFFSET 1000").Rows()
//if err != nil {
// panic(fmt.Sprintf("failed to count entries in DB: %v", err))
//}
//defer rows.Close()
//if !rows.Next() {
// panic("Suspiciously few enc history entries!")
//}
encHistoryEntryCount, err := s.db.EncHistoryEntryCount(r.Context())
checkGormError(err) checkGormError(err)
if encHistoryEntryCount < 1000 { if encHistoryEntryCount < 1000 {
panic("Suspiciously few enc history entries!") panic("Suspiciously few enc history entries!")
} }
deviceCount, err := s.db.DevicesCount(r.Context()) deviceCount, err := s.db.CountAllDevices(r.Context())
checkGormError(err) checkGormError(err)
if deviceCount < 100 { if deviceCount < 100 {
panic("Suspiciously few devices!") panic("Suspiciously few devices!")
} }
// Check that we can write to the DB. This entry will get written and then eventually cleaned by the cron. // Check that we can write to the DB. This entry will get written and then eventually cleaned by the cron.
err = s.db.EncHistoryCreate(r.Context(), &shared.EncHistoryEntry{ err = s.db.AddHistoryEntries(r.Context(), &shared.EncHistoryEntry{
EncryptedData: []byte("data"), EncryptedData: []byte("data"),
Nonce: []byte("nonce"), Nonce: []byte("nonce"),
DeviceId: "healthcheck_device_id", DeviceId: "healthcheck_device_id",
@ -285,23 +276,23 @@ func (s *Server) usageStatsHandler(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) statsHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) statsHandler(w http.ResponseWriter, r *http.Request) {
numDevices, err := s.db.DevicesCount(r.Context()) numDevices, err := s.db.CountAllDevices(r.Context())
checkGormError(err) checkGormError(err)
numEntriesProcessed, err := s.db.UsageDataTotal(r.Context()) numEntriesProcessed, err := s.db.UsageDataTotal(r.Context())
checkGormError(err) checkGormError(err)
numDbEntries, err := s.db.EncHistoryEntryCount(r.Context()) numDbEntries, err := s.db.CountHistoryEntries(r.Context())
checkGormError(err) checkGormError(err)
oneWeek := time.Hour * 24 * 7 oneWeek := time.Hour * 24 * 7
weeklyActiveInstalls, err := s.db.WeeklyActiveInstalls(r.Context(), oneWeek) weeklyActiveInstalls, err := s.db.CountActiveInstalls(r.Context(), oneWeek)
checkGormError(err) checkGormError(err)
weeklyQueryUsers, err := s.db.WeeklyQueryUsers(r.Context(), oneWeek) weeklyQueryUsers, err := s.db.CountQueryUsers(r.Context(), oneWeek)
checkGormError(err) checkGormError(err)
lastRegistration, err := s.db.LastRegistration(r.Context()) lastRegistration, err := s.db.DateOfLastRegistration(r.Context())
checkGormError(err) checkGormError(err)
_, _ = fmt.Fprintf(w, "Num devices: %d\n", numDevices) _, _ = fmt.Fprintf(w, "Num devices: %d\n", numDevices)
@ -320,7 +311,7 @@ func (s *Server) wipeDbEntriesHandler(w http.ResponseWriter, r *http.Request) {
panic("refusing to wipe the DB non-test environment") panic("refusing to wipe the DB non-test environment")
} }
err := s.db.EncHistoryClear(r.Context()) err := s.db.Unsafe_DeleteAllHistoryEntries(r.Context())
checkGormError(err) checkGormError(err)
w.Header().Set("Content-Length", "0") w.Header().Set("Content-Length", "0")
@ -343,7 +334,7 @@ func (s *Server) updateUsageData(ctx context.Context, version string, remoteAddr
return fmt.Errorf("db.UsageDataFindByUserAndDevice: %w", err) return fmt.Errorf("db.UsageDataFindByUserAndDevice: %w", err)
} }
if len(usageData) == 0 { if len(usageData) == 0 {
err := s.db.UsageDataCreate( err := s.db.CreateUsageData(
ctx, ctx,
&shared.UsageData{ &shared.UsageData{
UserId: userId, UserId: userId,
@ -359,22 +350,22 @@ func (s *Server) updateUsageData(ctx context.Context, version string, remoteAddr
} else { } else {
usage := usageData[0] usage := usageData[0]
if err := s.db.UsageDataUpdate(ctx, userId, deviceId, time.Now(), remoteAddr); err != nil { if err := s.db.UpdateUsageData(ctx, userId, deviceId, time.Now(), remoteAddr); err != nil {
return fmt.Errorf("db.UsageDataUpdate: %w", err) return fmt.Errorf("db.UsageDataUpdate: %w", err)
} }
if numEntriesHandled > 0 { if numEntriesHandled > 0 {
if err := s.db.UsageDataUpdateNumEntriesHandled(ctx, userId, deviceId, numEntriesHandled); err != nil { if err := s.db.UpdateUsageDataForNumEntriesHandled(ctx, userId, deviceId, numEntriesHandled); err != nil {
return fmt.Errorf("db.UsageDataUpdateNumEntriesHandled: %w", err) return fmt.Errorf("db.UsageDataUpdateNumEntriesHandled: %w", err)
} }
} }
if usage.Version != version { if usage.Version != version {
if err := s.db.UsageDataUpdateVersion(ctx, userId, deviceId, version); err != nil { if err := s.db.UpdateUsageDataClientVersion(ctx, userId, deviceId, version); err != nil {
return fmt.Errorf("db.UsageDataUpdateVersion: %w", err) return fmt.Errorf("db.UsageDataUpdateVersion: %w", err)
} }
} }
} }
if isQuery { if isQuery {
if err := s.db.UsageDataUpdateNumQueries(ctx, userId, deviceId); err != nil { if err := s.db.UpdateUsageDataNumberQueries(ctx, userId, deviceId); err != nil {
return fmt.Errorf("db.UsageDataUpdateNumQueries: %w", err) return fmt.Errorf("db.UsageDataUpdateNumQueries: %w", err)
} }
} }