Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare document data between Algolia and PostgreSQL #341

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions internal/api/approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,58 @@ func ApprovalHandler(
"path", r.URL.Path,
)

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
var algoDoc map[string]any
err = ar.Docs.GetObject(docID, &algoDoc)
if err != nil {
l.Error("error getting Algolia object for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
// Get document from database.
dbDoc := models.Document{
GoogleFileID: docID,
}
if err := dbDoc.Get(db); err != nil {
l.Error("error getting document from database for data comparison",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docID,
)
return
}
// Get all reviews for the document.
var reviews models.DocumentReviews
if err := reviews.Find(db, models.DocumentReview{
Document: models.Document{
GoogleFileID: docID,
},
}); err != nil {
l.Error("error getting all reviews for document for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
if err := compareAlgoliaAndDatabaseDocument(
algoDoc, dbDoc, reviews, cfg.DocumentTypes.DocumentType,
); err != nil {
l.Warn("inconsistencies detected between Algolia and database docs",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
}

case "POST":
// Validate request.
docID, err := parseResourceIDFromURL(r.URL.Path, "approvals")
Expand Down Expand Up @@ -439,6 +491,58 @@ func ApprovalHandler(
"path", r.URL.Path,
)

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
var algoDoc map[string]any
err = ar.Docs.GetObject(docID, &algoDoc)
if err != nil {
l.Error("error getting Algolia object for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
// Get document from database.
dbDoc := models.Document{
GoogleFileID: docID,
}
if err := dbDoc.Get(db); err != nil {
l.Error("error getting document from database for data comparison",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docID,
)
return
}
// Get all reviews for the document.
var reviews models.DocumentReviews
if err := reviews.Find(db, models.DocumentReview{
Document: models.Document{
GoogleFileID: docID,
},
}); err != nil {
l.Error("error getting all reviews for document for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
if err := compareAlgoliaAndDatabaseDocument(
algoDoc, dbDoc, reviews, cfg.DocumentTypes.DocumentType,
); err != nil {
l.Warn("inconsistencies detected between Algolia and database docs",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
}

default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
Expand Down
105 changes: 104 additions & 1 deletion internal/api/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,58 @@ func DocumentHandler(
"doc_id", docID,
)

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
var algoDoc map[string]any
err = ar.Docs.GetObject(docID, &algoDoc)
if err != nil {
l.Error("error getting Algolia object for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
// Get document from database.
dbDoc := models.Document{
GoogleFileID: docID,
}
if err := dbDoc.Get(db); err != nil {
l.Error("error getting document from database for data comparison",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docID,
)
return
}
// Get all reviews for the document.
var reviews models.DocumentReviews
if err := reviews.Find(db, models.DocumentReview{
Document: models.Document{
GoogleFileID: docID,
},
}); err != nil {
l.Error("error getting all reviews for document for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
if err := compareAlgoliaAndDatabaseDocument(
algoDoc, dbDoc, reviews, cfg.DocumentTypes.DocumentType,
); err != nil {
l.Warn("inconsistencies detected between Algolia and database docs",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
}

case "PATCH":
// Authorize request (only the owner can PATCH the doc).
userEmail := r.Context().Value("userEmail").(string)
Expand Down Expand Up @@ -688,7 +740,58 @@ Hermes

w.WriteHeader(http.StatusOK)
l.Info("patched document", "doc_id", docID)
return

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
var algoDoc map[string]any
err = ar.Docs.GetObject(docID, &algoDoc)
if err != nil {
l.Error("error getting Algolia object for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
// Get document from database.
dbDoc := models.Document{
GoogleFileID: docID,
}
if err := dbDoc.Get(db); err != nil {
l.Error("error getting document from database for data comparison",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docID,
)
return
}
// Get all reviews for the document.
var reviews models.DocumentReviews
if err := reviews.Find(db, models.DocumentReview{
Document: models.Document{
GoogleFileID: docID,
},
}); err != nil {
l.Error("error getting all reviews for document for data comparison",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
return
}
if err := compareAlgoliaAndDatabaseDocument(
algoDoc, dbDoc, reviews, cfg.DocumentTypes.DocumentType,
); err != nil {
l.Warn("inconsistencies detected between Algolia and database docs",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
}

default:
w.WriteHeader(http.StatusMethodNotAllowed)
Expand Down
Loading