@@ -3,9 +3,12 @@ package main
3
3
import (
4
4
"common"
5
5
"context"
6
+ "fmt"
6
7
"gdcd/db"
8
+ "gdcd/snooty"
7
9
"gdcd/types"
8
10
"gdcd/utils"
11
+ "time"
9
12
10
13
"github.com/tmc/langchaingo/llms/ollama"
11
14
)
@@ -17,8 +20,13 @@ import (
17
20
func CheckPagesForUpdates (pages []types.PageWrapper , project types.ProjectDetails , llm * ollama.LLM , ctx context.Context , report types.ProjectReport ) {
18
21
incomingPageIdsMatchingExistingPages := make (map [string ]bool )
19
22
incomingDeletedPageCount := 0
20
- var newPageIds []string
21
- var newPages []common.DocsPage
23
+
24
+ // When a page doesn't match one in the DB, it could be either net new or a moved page. Hold it in a temp array
25
+ // for comparison
26
+ var maybeNewPages []types.NewOrMovedPage
27
+ var newPages []types.NewOrMovedPage
28
+ var newPageDBEntries []common.DocsPage
29
+ var movedPages []types.NewOrMovedPage
22
30
var updatedPages []common.DocsPage
23
31
for _ , page := range pages {
24
32
// The Snooty Data API returns pages that may have been deleted. If the page is deleted, we want to check and see
@@ -27,6 +35,7 @@ func CheckPagesForUpdates(pages []types.PageWrapper, project types.ProjectDetail
27
35
if page .Data .Deleted {
28
36
report = HandleDeletedIncomingPages (project .ProjectName , page , report )
29
37
incomingDeletedPageCount ++
38
+ utils .UpdateSecondaryTarget ()
30
39
} else {
31
40
maybeExistingPage := CheckForExistingPage (project .ProjectName , page )
32
41
if maybeExistingPage != nil {
@@ -39,20 +48,72 @@ func CheckPagesForUpdates(pages []types.PageWrapper, project types.ProjectDetail
39
48
if updatedPage != nil {
40
49
updatedPages = append (updatedPages , * updatedPage )
41
50
}
51
+ utils .UpdateSecondaryTarget ()
42
52
} else {
43
- // If there is no existing document in Atlas that matches the page, we need to make a new page
44
- var newPage common. DocsPage
45
- newPage , report = MakeNewPage ( page , project . ProdUrl , report , llm , ctx )
46
- newPageIds = append ( newPageIds , newPage . ID )
47
- newPages = append (newPages , newPage )
53
+ // If there is no existing document in Atlas that matches the page, we need to make a new page. BUT!
54
+ // It might actually be a new or moved page. So store it in a temp `maybeNewPages` slice so we can compare
55
+ // it against removed pages later and potentially call it a "moved" page, instead.
56
+ newOrMovedPage := getNewOrMovedPageDetails ( page . Data )
57
+ maybeNewPages = append (maybeNewPages , newOrMovedPage )
48
58
}
49
59
}
50
- utils .UpdateSecondaryTarget ()
51
60
}
52
61
53
62
// After iterating through the incoming pages from the Snooty Data API, we need to figure out if any of the page IDs
54
- // we had in the DB are not coming in from the incoming response. If so, we should delete those entries.
55
- report = db .HandleMissingPageIds (project .ProjectName , incomingPageIdsMatchingExistingPages , report )
63
+ // we had in the DB are not coming in from the incoming response. If so, those pages are either moved or removed.
64
+ report , newPages , movedPages = db .HandleMissingPageIds (project .ProjectName , incomingPageIdsMatchingExistingPages , maybeNewPages , report )
65
+
66
+ // If we have new pages, create the corresponding DocsPage and increment the project report for them
67
+ if newPages != nil {
68
+ for _ , page := range newPages {
69
+ newPage := MakeNewPage (page .PageData , project .ProjectName , project .ProdUrl , llm , ctx )
70
+ newPageDBEntries = append (newPageDBEntries , newPage )
71
+ report = UpdateProjectReportForNewPage (newPage , report )
72
+ utils .UpdateSecondaryTarget ()
73
+ }
74
+ }
75
+
76
+ // If we have moved pages, handle them
77
+ if movedPages != nil {
78
+ for _ , page := range movedPages {
79
+ var movedPage common.DocsPage
80
+ oldPage := db .GetAtlasPageData (project .ProjectName , page .OldPageId )
81
+
82
+ if oldPage != nil {
83
+ movedPage = * oldPage
84
+ movedPage .ID = page .NewPageId
85
+ newPageUrl := utils .ConvertAtlasPageIdToProductionUrl (page .NewPageId , project .ProdUrl )
86
+ movedPage .DateLastUpdated = time .Now ()
87
+ movedPage .PageURL = newPageUrl
88
+ } else {
89
+ movedPage = MakeNewPage (page .PageData , project .ProjectName , project .ProdUrl , llm , ctx )
90
+ movedPage .DateAdded = page .DateAdded
91
+ }
92
+
93
+ // Remove the old page from the DB
94
+ db .RemovePageFromAtlas (project .ProjectName , page .OldPageId )
95
+
96
+ // Append the "moved" page to the `newPageDBEntries` array. Because the page ID doesn't match the old one,
97
+ // we write it to the DB as a new page. Because we just deleted the old page, it works out to the same count
98
+ // and provides the up-to-date data in the DB.
99
+ newPageDBEntries = append (newPageDBEntries , movedPage )
100
+
101
+ incomingAstCodeNodes , incomingAstLiteralIncludeNodes , incomingAstIoCodeBlockNodes := snooty .GetCodeExamplesFromIncomingData (page .PageData .AST )
102
+ incomingAstCodeNodeCount := len (incomingAstCodeNodes )
103
+ incomingAstLiteralIncludeNodesCount := len (incomingAstLiteralIncludeNodes )
104
+ incomingAstIoCodeBlockNodesCount := len (incomingAstIoCodeBlockNodes )
105
+ // Update the project counts for the "existing" page
106
+ report = IncrementProjectCountsForExistingPage (incomingAstCodeNodeCount , incomingAstLiteralIncludeNodesCount , incomingAstIoCodeBlockNodesCount , movedPage , report )
107
+
108
+ // Report it in the logs as a moved page
109
+ stringMessageForReport := fmt .Sprintf ("Old page ID: %s, new page ID: %s" , page .OldPageId , page .NewPageId )
110
+ report = utils .ReportChanges (types .PageMoved , report , stringMessageForReport )
111
+ if movedPage .CodeNodesTotal != incomingAstCodeNodeCount {
112
+ utils .ReportIssues (types .CodeNodeCountIssue , report , page .NewPageId , page .CodeNodeCount , len (incomingAstCodeNodes ))
113
+ }
114
+ utils .UpdateSecondaryTarget ()
115
+ }
116
+ }
56
117
57
118
// Get the existing "summaries" document from the DB, and update it.
58
119
var summaryDoc common.CollectionReport
@@ -65,5 +126,17 @@ func CheckPagesForUpdates(pages []types.PageWrapper, project types.ProjectDetail
65
126
LogReportForProject (project .ProjectName , report )
66
127
67
128
// At this point, we have all the new and updated pages and an updated summary. Write updates to Atlas.
68
- db .BatchUpdateCollection (project .ProjectName , newPages , updatedPages , summaryDoc )
129
+ db .BatchUpdateCollection (project .ProjectName , newPageDBEntries , updatedPages , summaryDoc )
130
+ }
131
+
132
+ func getNewOrMovedPageDetails (metadata types.PageMetadata ) types.NewOrMovedPage {
133
+ incomingCodeNodes , incomingLiteralIncludeNodes , incomingIoCodeBlockNodes := snooty .GetCodeExamplesFromIncomingData (metadata .AST )
134
+ pageId := utils .ConvertSnootyPageIdToAtlasPageId (metadata .PageID )
135
+ return types.NewOrMovedPage {
136
+ PageId : pageId ,
137
+ CodeNodeCount : len (incomingCodeNodes ),
138
+ LiteralIncludeCount : len (incomingLiteralIncludeNodes ),
139
+ IoCodeBlockCount : len (incomingIoCodeBlockNodes ),
140
+ PageData : metadata ,
141
+ }
69
142
}
0 commit comments