Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
ejercicio2-framework-back
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Josue
ejercicio2-framework-back
Commits
e74bdaf7
Commit
e74bdaf7
authored
Dec 23, 2021
by
Roberto Loayza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proceso de eliminación BDC
parent
33929e84
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
142 additions
and
12 deletions
+142
-12
AgentController.java
...n/java/com/bytesw/bytebot/controller/AgentController.java
+13
-0
BucketService.java
src/main/java/com/bytesw/bytebot/service/BucketService.java
+45
-1
KnowledgeService.java
...ain/java/com/bytesw/bytebot/service/KnowledgeService.java
+57
-8
OrquestadorService.java
...n/java/com/bytesw/bytebot/service/OrquestadorService.java
+27
-3
No files found.
src/main/java/com/bytesw/bytebot/controller/AgentController.java
View file @
e74bdaf7
...
...
@@ -192,6 +192,19 @@ public class AgentController {
}
}
@DeleteMapping
(
"/file-upload/{uuid}"
)
public
ResponseEntity
<
String
>
deleteBdc
(
@ApiParam
(
value
=
"uuid"
,
required
=
true
)
@PathVariable
(
"uuid"
)
String
uuid
)
{
Gson
gson
=
gsonBuilder
.
create
();
try
{
orquestadorService
.
deleteBcd
(
uuid
);
String
response
=
"Todo Ok"
;
return
new
ResponseEntity
<>(
gson
.
toJson
(
response
),
HttpStatus
.
OK
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Error detectado: "
,
e
);
return
new
ResponseEntity
<>(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
}
}
@GetMapping
(
"/synchronize/{id}"
)
public
ResponseEntity
<
String
>
synchronizeFiles
(
@PathVariable
(
"id"
)
Long
id
,
@RequestParam
(
"user"
)
String
user
)
{
...
...
src/main/java/com/bytesw/bytebot/service/BucketService.java
View file @
e74bdaf7
package
com
.
bytesw
.
bytebot
.
service
;
import
com.amazonaws.AmazonServiceException
;
import
com.amazonaws.SdkClientException
;
import
com.amazonaws.services.s3.model.Bucket
;
import
com.amazonaws.services.s3.model.DeleteObjectRequest
;
import
com.amazonaws.services.s3.model.ObjectMetadata
;
import
com.bytesw.bytebot.http.FileValidationResponse
;
import
com.bytesw.bytebot.model.BusinessParameter
;
import
com.bytesw.bytebot.model.QuestionFile
;
import
com.bytesw.bytebot.repository.BusinessParameterRepository
;
import
com.bytesw.bytebot.repository.QuestionFileRepository
;
import
com.bytesw.xdf.exception.NotFoundException
;
import
lombok.extern.log4j.Log4j2
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
com.amazonaws.regions.Regions
;
import
com.amazonaws.services.s3.AmazonS3
;
import
com.amazonaws.services.s3.AmazonS3ClientBuilder
;
import
com.amazonaws.services.s3.model.PutObjectRequest
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.util.List
;
import
java.util.Optional
;
...
...
@@ -32,6 +39,11 @@ public class BucketService {
@Value
(
"${application.bucket.fileBucket}"
)
private
String
fileDir
;
@Value
(
"${application.bucket.modelBucket}"
)
private
String
modelDir
;
@Autowired
private
QuestionFileRepository
questionFileRepository
;
@Autowired
private
BusinessParameterRepository
businessParameterRepository
;
...
...
@@ -56,6 +68,8 @@ public class BucketService {
PutObjectRequest
request
=
new
PutObjectRequest
(
bucketName
,
newFile
,
mainFile
);
AmazonS3
amazonS3
=
getS3Client
();
amazonS3
.
putObject
(
request
);
String
modelFile
=
String
.
format
(
"%s/%s/1.0/%s"
,
tenant
,
response
.
getUuid
(),
modelDir
);
createFolder
(
bucketName
,
modelFile
,
amazonS3
);
mainFile
.
delete
();
result
=
true
;
}
catch
(
Exception
e
)
{
...
...
@@ -64,6 +78,36 @@ public class BucketService {
return
result
;
}
public
static
void
createFolder
(
String
bucketName
,
String
folderName
,
AmazonS3
client
)
{
ObjectMetadata
metadata
=
new
ObjectMetadata
();
metadata
.
setContentLength
(
0
);
InputStream
emptyContent
=
new
ByteArrayInputStream
(
new
byte
[
0
]);
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName
,
folderName
+
"/"
,
emptyContent
,
metadata
);
client
.
putObject
(
putObjectRequest
);
}
public
void
delete
(
String
uuid
)
{
Optional
<
BusinessParameter
>
businessParameter
=
businessParameterRepository
.
findByKey
(
"Bucket"
);
if
(!
businessParameter
.
isPresent
())
{
throw
new
NotFoundException
(
"No existe el parametro de negocio del bucket."
);
}
String
bucketName
=
businessParameter
.
get
().
getDefaultValue
();
try
{
AmazonS3
s3Client
=
getS3Client
();
Optional
<
QuestionFile
>
questionFile
=
questionFileRepository
.
findByUuid
(
uuid
);
if
(!
questionFile
.
isPresent
())
{
log
.
error
(
"El uuid no se encuentra enlazado con un file"
);
}
String
keyName
=
String
.
format
(
"%s/%s/1.0/%s/%s"
,
tenant
,
uuid
,
fileDir
,
questionFile
.
get
().
getName
());
s3Client
.
deleteObject
(
new
DeleteObjectRequest
(
bucketName
,
keyName
));
}
catch
(
AmazonServiceException
e
)
{
e
.
printStackTrace
();
}
catch
(
SdkClientException
e
)
{
e
.
printStackTrace
();
}
}
public
void
list
()
{
AmazonS3
s3
=
getS3Client
();
List
<
Bucket
>
buckets
=
s3
.
listBuckets
();
...
...
src/main/java/com/bytesw/bytebot/service/KnowledgeService.java
View file @
e74bdaf7
package
com
.
bytesw
.
bytebot
.
service
;
import
com.bytesw.bytebot.http.FileValidationResponse
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.log4j.Log4j2
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.ClientProtocolException
;
import
org.apache.http.client.ResponseHandler
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpDelete
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
@Service
@Log4j2
...
...
@@ -22,20 +30,61 @@ public class KnowledgeService {
@Value
(
"${application.knowledge.url}"
)
private
String
url
;
public
void
generate
(
FileValidationResponse
response
)
throws
IOException
{
public
boolean
generate
(
FileValidationResponse
response
)
throws
IOException
{
boolean
result
=
false
;
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpPost
httppost
=
new
HttpPost
(
String
.
format
(
"%s/clients/%s/versions/1.0"
,
url
,
tenant
));
String
json
=
String
.
format
(
"{\"filename_csv\": \"%s\", \"index_name\": \"%s\"}"
,
response
.
getFileName
(),
response
.
getUuid
());
StringEntity
entity
=
new
StringEntity
(
json
);
httppost
.
setEntity
(
entity
);
httppost
.
setHeader
(
"Accept"
,
"application/json"
);
httppost
.
setHeader
(
"Content-type"
,
"application/json"
);
CloseableHttpResponse
res
=
httpclient
.
execute
(
httppost
);
httpclient
.
close
();
try
{
httppost
.
setEntity
(
entity
);
httppost
.
setHeader
(
"Accept"
,
"application/json"
);
httppost
.
setHeader
(
"Content-type"
,
"application/json"
);
ResponseHandler
<
String
>
responseHandler
=
resp
->
{
int
status
=
resp
.
getStatusLine
().
getStatusCode
();
if
(
status
>=
200
&&
status
<
300
)
{
HttpEntity
entityRes
=
resp
.
getEntity
();
return
entityRes
!=
null
?
EntityUtils
.
toString
(
entityRes
)
:
null
;
}
else
{
throw
new
ClientProtocolException
(
"Error inesperado: "
+
status
);
}
};
String
res
=
httpclient
.
execute
(
httppost
,
responseHandler
);
Map
<
String
,
Object
>
mapJsonResult
=
new
ObjectMapper
().
readValue
(
res
,
HashMap
.
class
);
if
(
mapJsonResult
.
get
(
"status"
).
equals
(
"success"
)){
result
=
true
;
}
}
catch
(
IOException
e
)
{
log
.
error
(
e
.
getMessage
());
}
return
result
;
}
public
void
delete
()
{
HttpPost
httpPost
=
new
HttpPost
();
public
boolean
delete
(
String
uuid
)
{
boolean
result
=
false
;
try
{
HttpDelete
httpDelete
=
new
HttpDelete
(
String
.
format
(
"%s/clients/%s/indexes/%s/versions/1.0"
,
url
,
tenant
,
uuid
));
System
.
out
.
println
(
"Executing request "
+
httpDelete
.
getRequestLine
());
ResponseHandler
<
String
>
responseHandler
=
response
->
{
int
status
=
response
.
getStatusLine
().
getStatusCode
();
if
(
status
>=
200
&&
status
<
300
)
{
HttpEntity
entity
=
response
.
getEntity
();
return
entity
!=
null
?
EntityUtils
.
toString
(
entity
)
:
null
;
}
else
{
throw
new
ClientProtocolException
(
"Error inesperado: "
+
status
);
}
};
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
String
responseBody
=
httpclient
.
execute
(
httpDelete
,
responseHandler
);
Map
<
String
,
Object
>
mapJsonResult
=
new
ObjectMapper
().
readValue
(
String
.
valueOf
(
responseBody
),
HashMap
.
class
);
if
(
mapJsonResult
.
get
(
"status"
).
equals
(
"success"
)){
result
=
true
;
}
httpclient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
}
src/main/java/com/bytesw/bytebot/service/OrquestadorService.java
View file @
e74bdaf7
...
...
@@ -67,9 +67,11 @@ public class OrquestadorService {
model
=
bdcControlRepository
.
save
(
model
);
//Generacion del modelo
//Actualizar
knowledgeService
.
generate
(
response
);
bdcControlRepository
.
delete
(
model
);
response
.
setStatus
(
FrequentQuestionStatusEnum
.
CORRECTO
.
getName
());
model
.
setStatus
(
StatusBcdEnum
.
INDEXADO
.
getName
());
model
=
bdcControlRepository
.
save
(
model
);
}
catch
(
Exception
e
)
{
response
.
setStatus
(
FrequentQuestionStatusEnum
.
ERROR
.
getName
());
}
...
...
@@ -96,11 +98,16 @@ public class OrquestadorService {
}
model
.
setStatus
(
StatusBcdEnum
.
INDEXANDO
.
getName
());
model
=
bdcControlRepository
.
save
(
model
);
step
=
false
;
}
if
(
model
.
getStatus
().
equals
(
StatusBcdEnum
.
INDEXANDO
.
getName
()))
{
knowledgeService
.
generate
(
response
);
step
=
knowledgeService
.
generate
(
response
);
if
(!
step
)
{
throw
new
NotFoundException
(
"Error a cargar knowledge."
);
}
}
bdcControlRepository
.
delete
(
model
);
model
.
setStatus
(
StatusBcdEnum
.
INDEXADO
.
getName
());
model
=
bdcControlRepository
.
save
(
model
);
response
.
setStatus
(
FrequentQuestionStatusEnum
.
CORRECTO
.
getName
());
}
catch
(
IOException
e
)
{
response
.
setStatus
(
FrequentQuestionStatusEnum
.
ERROR
.
getName
());
...
...
@@ -109,7 +116,24 @@ public class OrquestadorService {
}
public
void
deleteBcd
(
String
uuid
)
{
Optional
<
BdcControl
>
bdcControl
=
bdcControlRepository
.
findByUuid
(
uuid
);
if
(!
bdcControl
.
isPresent
())
{
throw
new
NotFoundException
(
"Control no encontrado."
);
}
Optional
<
QuestionFile
>
questionFile
=
questionFileRepository
.
findByUuid
(
uuid
);
if
(
questionFile
.
isPresent
())
{
questionFileRepository
.
deleteById
(
bdcControl
.
get
().
getId
());
bdcControl
.
get
().
setStatus
(
StatusBcdEnum
.
INDEXANDO
.
getName
());
}
if
(
bdcControl
.
get
().
getStatus
().
equals
(
StatusBcdEnum
.
INDEXANDO
.
getName
()))
{
bucketService
.
delete
(
uuid
);
bdcControl
.
get
().
setStatus
(
StatusBcdEnum
.
INDEXADO
.
getName
());
}
if
(
bdcControl
.
get
().
getStatus
().
equals
(
StatusBcdEnum
.
INDEXADO
.
getName
()))
{
knowledgeService
.
delete
(
uuid
);
}
bdcControlRepository
.
deleteById
(
bdcControl
.
get
().
getId
());
}
private
FileValidationResponse
modelToFileValidation
(
BdcControl
model
){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment