Skip to content

Commit 091b89d

Browse files
bharadwaj-adityaAditya Bharadwaj
and
Aditya Bharadwaj
authored
app assessment integration initial commit (#1044)
* app assessment integration initial commit * added stored procs and changes to code report --------- Co-authored-by: Aditya Bharadwaj <[email protected]>
1 parent 688f5a1 commit 091b89d

File tree

8 files changed

+338
-35
lines changed

8 files changed

+338
-35
lines changed

assessment/assessment_engine.go

+72-9
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,34 @@
1515
package assessment
1616

1717
import (
18+
"context"
19+
"fmt"
20+
1821
assessment "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors"
1922
"github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils"
2023
"github.com/GoogleCloudPlatform/spanner-migration-tool/internal"
2124
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
2225
"github.com/GoogleCloudPlatform/spanner-migration-tool/profiles"
26+
"go.uber.org/zap"
2327
)
2428

2529
type assessmentCollectors struct {
26-
sampleCollector assessment.SampleCollector
27-
infoSchemaCollector assessment.InfoSchemaCollector
30+
sampleCollector *assessment.SampleCollector
31+
infoSchemaCollector *assessment.InfoSchemaCollector
32+
appAssessmentCollector *assessment.MigrationSummarizer
2833
}
2934

30-
func PerformAssessment(conv *internal.Conv, sourceProfile profiles.SourceProfile) (utils.AssessmentOutput, error) {
35+
func PerformAssessment(conv *internal.Conv, sourceProfile profiles.SourceProfile, assessmentConfig map[string]string, projectId string) (utils.AssessmentOutput, error) {
3136

3237
logger.Log.Info("performing assessment")
38+
logger.Log.Info(fmt.Sprintf("assessment config %+v", assessmentConfig))
39+
logger.Log.Info(fmt.Sprintf("project id %+v", projectId))
40+
41+
ctx := context.Background()
3342

3443
output := utils.AssessmentOutput{}
3544
// Initialize collectors
36-
c, err := initializeCollectors(conv, sourceProfile)
45+
c, err := initializeCollectors(conv, sourceProfile, assessmentConfig, projectId, ctx)
3746
if err != nil {
3847
logger.Log.Error("unable to initialize collectors")
3948
return output, err
@@ -45,32 +54,86 @@ func PerformAssessment(conv *internal.Conv, sourceProfile profiles.SourceProfile
4554
// Select the highest confidence output for each attribute
4655
// Populate assessment struct
4756

48-
output.SchemaAssessment, err = performSchemaAssessment(c)
57+
output.SchemaAssessment, err = performSchemaAssessment(ctx, c)
58+
4959
return output, err
5060
}
5161

5262
// Initilize collectors. Take a decision here on which collectors are mandatory and which are optional
53-
func initializeCollectors(conv *internal.Conv, sourceProfile profiles.SourceProfile) (assessmentCollectors, error) {
63+
func initializeCollectors(conv *internal.Conv, sourceProfile profiles.SourceProfile, assessmentConfig map[string]string, projectId string, ctx context.Context) (assessmentCollectors, error) {
5464
c := assessmentCollectors{}
5565
sampleCollector, err := assessment.CreateSampleCollector()
5666
if err != nil {
5767
return c, err
5868
}
59-
c.sampleCollector = sampleCollector
69+
c.sampleCollector = &sampleCollector
6070
infoSchemaCollector, err := assessment.CreateInfoSchemaCollector(conv, sourceProfile)
6171
if infoSchemaCollector.IsEmpty() {
6272
return c, err
6373
}
64-
c.infoSchemaCollector = infoSchemaCollector
74+
c.infoSchemaCollector = &infoSchemaCollector
75+
76+
//Initiialize App Assessment Collector
77+
78+
codeDirectory, exists := assessmentConfig["codeDirectory"]
79+
if exists {
80+
logger.Log.Info("initializing app collector")
81+
82+
mysqlSchema := "" // TODO fetch from conv
83+
spannerSchema := "" // TODO fetch from conv
84+
mysqlSchemaPath, exists := assessmentConfig["mysqlSchemaPath"]
85+
if exists {
86+
logger.Log.Info(fmt.Sprintf("overriding mysql schema from file %s", mysqlSchemaPath))
87+
mysqlSchema, err := utils.ReadFile(mysqlSchemaPath)
88+
if err != nil {
89+
logger.Log.Debug("error reading MySQL schema file:", zap.Error(err))
90+
}
91+
logger.Log.Debug(mysqlSchema)
92+
}
93+
94+
spannerSchemaPath, exists := assessmentConfig["spannerSchemaPath"]
95+
if exists {
96+
logger.Log.Info(fmt.Sprintf("overriding spanner schema from file %s", spannerSchemaPath))
97+
spannerSchema, err := utils.ReadFile(spannerSchemaPath)
98+
if err != nil {
99+
logger.Log.Debug("error reading Spanner schema file:", zap.Error(err))
100+
}
101+
logger.Log.Info(spannerSchema)
102+
}
103+
104+
summarizer, err := assessment.NewMigrationSummarizer(ctx, nil, projectId, assessmentConfig["location"], mysqlSchema, spannerSchema, codeDirectory)
105+
if err != nil {
106+
logger.Log.Error("error initiating migration summarizer")
107+
return c, err
108+
}
109+
c.appAssessmentCollector = summarizer
110+
logger.Log.Info("initialized app collector")
111+
} else {
112+
logger.Log.Info("app code info unavailable")
113+
}
114+
65115
return c, err
66116
}
67117

68-
func performSchemaAssessment(collectors assessmentCollectors) (utils.SchemaAssessmentOutput, error) {
118+
func performSchemaAssessment(ctx context.Context, collectors assessmentCollectors) (utils.SchemaAssessmentOutput, error) {
69119
schemaOut := utils.SchemaAssessmentOutput{}
70120
schemaOut.SourceTableDefs, schemaOut.SpannerTableDefs = collectors.infoSchemaCollector.ListTables()
71121
schemaOut.SourceIndexDef, schemaOut.SpannerIndexDef = collectors.infoSchemaCollector.ListIndexes()
72122
schemaOut.Triggers = collectors.infoSchemaCollector.ListTriggers()
73123
schemaOut.SourceColDefs, schemaOut.SpannerColDefs = collectors.infoSchemaCollector.ListColumnDefinitions()
74124
schemaOut.StoredProcedureAssessmentOutput = collectors.infoSchemaCollector.ListStoredProcedures()
125+
126+
if collectors.appAssessmentCollector != nil {
127+
logger.Log.Info("adding app assessment details")
128+
codeAssessment, err := collectors.appAssessmentCollector.AnalyzeProject(ctx)
129+
130+
if err != nil {
131+
logger.Log.Error("error analyzing project", zap.Error(err))
132+
return schemaOut, err
133+
}
134+
135+
logger.Log.Info(fmt.Sprintf("snippets %+v", codeAssessment.Snippets))
136+
schemaOut.CodeSnippets = &codeAssessment.Snippets
137+
}
75138
return schemaOut, nil
76139
}

assessment/collectors/app_migration_analysis.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package assessment
1818

1919
import (
20-
"bufio"
2120
"context"
2221
"encoding/json"
2322
"fmt"
@@ -28,6 +27,7 @@ import (
2827
assessment "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors/embeddings"
2928
parser "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors/parser"
3029
dependencyAnalyzer "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors/project_analyzer"
30+
"github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils"
3131
. "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils"
3232
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
3333
"go.uber.org/zap"
@@ -266,7 +266,7 @@ func ParseJSONWithRetries(model *genai.GenerativeModel, originalPrompt string, o
266266
func (m *MigrationSummarizer) fetchFileContent(filepath string) (string, error) {
267267

268268
// Read file content if not provided
269-
content, err := readFile(filepath)
269+
content, err := utils.ReadFile(filepath)
270270
if err != nil {
271271
logger.Log.Fatal("Failed read file: ", zap.Error(err))
272272
return "", err
@@ -517,24 +517,6 @@ func getPromptForNonDAOClass(content, filepath string, methodChanges *string) st
517517
%s`, filepath, content, *methodChanges)
518518
}
519519

520-
func readFile(filepath string) (string, error) {
521-
file, err := os.Open(filepath)
522-
if err != nil {
523-
return "", err
524-
}
525-
defer file.Close()
526-
527-
scanner := bufio.NewScanner(file)
528-
var content string
529-
for scanner.Scan() {
530-
content += scanner.Text() + "\n"
531-
}
532-
if err := scanner.Err(); err != nil {
533-
return "", err
534-
}
535-
return content, nil
536-
}
537-
538520
func getPromptForDAOClass(content, filepath string, methodChanges, oldSchema, newSchema *string) string {
539521
//TODO: Move prompts to promt file.
540522
return fmt.Sprintf(`

assessment/collectors/project_analyzer/file_dependency_analyzer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package assessment
1616

1717
import (
18+
"fmt"
1819
"strings"
1920

2021
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
@@ -47,8 +48,7 @@ func (g *GoDependencyAnalyzer) getDependencyGraph(directory string) map[string]m
4748
Dir: (directory),
4849
}
4950

50-
logger.Log.Debug(directory)
51-
51+
logger.Log.Debug(fmt.Sprintf("loading packages from directory: %s", directory))
5252
pkgs, err := packages.Load(cfg, "./...")
5353

5454
if err != nil {

0 commit comments

Comments
 (0)