1
1
import { HttpException , Injectable , Logger } from '@nestjs/common' ;
2
+ import { isNull , flatten , concat } from 'lodash' ;
3
+ import { RecommendationService } from 'src/modules/recommendation/recommendation.service' ;
2
4
import { catchAclError } from 'src/utils' ;
5
+ import { ONE_NODE_RECOMMENDATIONS } from 'src/constants' ;
3
6
import { DatabaseAnalyzer } from 'src/modules/database-analysis/providers/database-analyzer' ;
4
7
import { plainToClass } from 'class-transformer' ;
5
8
import { DatabaseAnalysis , ShortDatabaseAnalysis } from 'src/modules/database-analysis/models' ;
6
9
import { DatabaseAnalysisProvider } from 'src/modules/database-analysis/providers/database-analysis.provider' ;
7
- import { CreateDatabaseAnalysisDto } from 'src/modules/database-analysis/dto' ;
10
+ import { CreateDatabaseAnalysisDto , RecommendationVoteDto } from 'src/modules/database-analysis/dto' ;
8
11
import { KeysScanner } from 'src/modules/database-analysis/scanner/keys-scanner' ;
9
12
import { DatabaseConnectionService } from 'src/modules/database/database-connection.service' ;
10
13
import { ClientMetadata } from 'src/common/models' ;
@@ -15,6 +18,7 @@ export class DatabaseAnalysisService {
15
18
16
19
constructor (
17
20
private readonly databaseConnectionService : DatabaseConnectionService ,
21
+ private readonly recommendationService : RecommendationService ,
18
22
private readonly analyzer : DatabaseAnalyzer ,
19
23
private readonly databaseAnalysisProvider : DatabaseAnalysisProvider ,
20
24
private readonly scanner : KeysScanner ,
@@ -50,11 +54,34 @@ export class DatabaseAnalysisService {
50
54
progress . total += nodeResult . progress . total ;
51
55
} ) ;
52
56
57
+ let recommendationToExclude = [ ] ;
58
+
59
+ const recommendations = await scanResults . reduce ( async ( previousPromise , nodeResult , idx ) => {
60
+ const jobsArray = await previousPromise ;
61
+ const nodeRecommendations = await this . recommendationService . getRecommendations ( {
62
+ client : nodeResult . client ,
63
+ keys : nodeResult . keys ,
64
+ total : progress . total ,
65
+ globalClient : client ,
66
+ exclude : recommendationToExclude ,
67
+ } ) ;
68
+ if ( idx === 0 ) {
69
+ recommendationToExclude = concat ( recommendationToExclude , ONE_NODE_RECOMMENDATIONS ) ;
70
+ }
71
+ const foundedRecommendations = nodeRecommendations . filter ( ( recommendation ) => ! isNull ( recommendation ) ) ;
72
+ const foundedRecommendationNames = foundedRecommendations . map ( ( { name } ) => name ) ;
73
+ recommendationToExclude = concat ( recommendationToExclude , foundedRecommendationNames ) ;
74
+ recommendationToExclude . push ( ...foundedRecommendationNames ) ;
75
+ jobsArray . push ( foundedRecommendations ) ;
76
+ return flatten ( jobsArray ) ;
77
+ } , Promise . resolve ( [ ] ) ) ;
78
+
53
79
const analysis = plainToClass ( DatabaseAnalysis , await this . analyzer . analyze ( {
54
80
databaseId : clientMetadata . databaseId ,
55
81
db : client ?. options ?. db || 0 ,
56
82
...dto ,
57
83
progress,
84
+ recommendations,
58
85
} , [ ] . concat ( ...scanResults . map ( ( nodeResult ) => nodeResult . keys ) ) ) ) ;
59
86
60
87
client . disconnect ( ) ;
@@ -86,4 +113,13 @@ export class DatabaseAnalysisService {
86
113
async list ( databaseId : string ) : Promise < ShortDatabaseAnalysis [ ] > {
87
114
return this . databaseAnalysisProvider . list ( databaseId ) ;
88
115
}
116
+
117
+ /**
118
+ * Set user vote for recommendation
119
+ * @param id
120
+ * @param recommendation
121
+ */
122
+ async vote ( id : string , recommendation : RecommendationVoteDto ) : Promise < DatabaseAnalysis > {
123
+ return this . databaseAnalysisProvider . recommendationVote ( id , recommendation ) ;
124
+ }
89
125
}
0 commit comments