Refactor index-management (maintance refactor)

This commit is contained in:
George Kalampokis 2023-12-14 14:34:39 +02:00
parent e2c352bf00
commit 0b45dd6981
13 changed files with 176 additions and 20 deletions

View File

@ -31,6 +31,7 @@ public class ElasticConfiguration extends AbstractElasticConfiguration {
new DmpUserRoleToShortConverter(), new DmpUserRoleToShortConverter(),
new DescriptionTemplateVersionStatusToShortConverter(), new DescriptionTemplateVersionStatusToShortConverter(),
new DmpStatusToShortConverter(), new DmpStatusToShortConverter(),
new DescriptionStatusToShortConverter(),
new IsActiveToShortConverter(), new IsActiveToShortConverter(),
new DescriptionTemplateVersionStatusToShortConverter(), new DescriptionTemplateVersionStatusToShortConverter(),
new DmpVersionStatusToShortConverter(), new DmpVersionStatusToShortConverter(),

View File

@ -0,0 +1,14 @@
package eu.eudat.elastic.converter;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.enums.DmpStatus;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
@WritingConverter
public class DescriptionStatusToShortConverter implements Converter<DescriptionStatus, Short> {
@Override
public Short convert(DescriptionStatus source) {
return source.getValue();
}
}

View File

@ -30,7 +30,7 @@ public class NestedDescriptionElasticEntity {
private String description; private String description;
public final static String _description = "description"; public final static String _description = "description";
@Field(value = NestedDescriptionElasticEntity._status, type = FieldType.Short) @Field(value = NestedDescriptionElasticEntity._status, type = FieldType.Short )
private DescriptionStatus status; private DescriptionStatus status;
public final static String _status = "status"; public final static String _status = "status";

View File

@ -64,7 +64,9 @@ public class DescriptionElasticBuilder extends BaseElasticBuilder<DescriptionEla
m.setDescription(d.getDescription()); m.setDescription(d.getDescription());
m.setStatus(d.getStatus()); m.setStatus(d.getStatus());
m.setCreatedAt(Date.from(d.getCreatedAt())); m.setCreatedAt(Date.from(d.getCreatedAt()));
m.setFinalizedAt(Date.from(d.getFinalizedAt())); if (d.getFinalizedAt() != null) {
m.setFinalizedAt(Date.from(d.getFinalizedAt()));
}
if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null)); if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null));
if (tagElasticEntityMap != null) m.setTags(tagElasticEntityMap.getOrDefault(d.getId(), null)); if (tagElasticEntityMap != null) m.setTags(tagElasticEntityMap.getOrDefault(d.getId(), null));
if (dmpElasticEntityMap != null) m.setDmp(dmpElasticEntityMap.getOrDefault(d.getDmpId(), null)); if (dmpElasticEntityMap != null) m.setDmp(dmpElasticEntityMap.getOrDefault(d.getDmpId(), null));

View File

@ -60,7 +60,9 @@ public class DmpElasticBuilder extends BaseElasticBuilder<DmpElasticEntity, DmpE
m.setLanguage(d.getLanguage()); m.setLanguage(d.getLanguage());
m.setBlueprintId(d.getBlueprintId()); m.setBlueprintId(d.getBlueprintId());
m.setGroupId(d.getGroupId()); m.setGroupId(d.getGroupId());
m.setFinalizedAt(Date.from(d.getFinalizedAt())); if (d.getFinalizedAt() != null) {
m.setFinalizedAt(Date.from(d.getFinalizedAt()));
}
if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null)); if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null));
if (dmpElasticEntityMap != null) m.setDescriptions(dmpElasticEntityMap.getOrDefault(d.getId(), null)); if (dmpElasticEntityMap != null) m.setDescriptions(dmpElasticEntityMap.getOrDefault(d.getId(), null));
if (collaboratorElasticEntityMap != null) m.setCollaborators(collaboratorElasticEntityMap.getOrDefault(d.getId(), null)); if (collaboratorElasticEntityMap != null) m.setCollaborators(collaboratorElasticEntityMap.getOrDefault(d.getId(), null));

View File

@ -59,7 +59,9 @@ public class NestedDescriptionElasticBuilder extends BaseElasticBuilder<NestedDe
m.setLabel(d.getLabel()); m.setLabel(d.getLabel());
m.setDescription(d.getDescription()); m.setDescription(d.getDescription());
m.setStatus(d.getStatus()); m.setStatus(d.getStatus());
m.setFinalizedAt(Date.from(d.getFinalizedAt())); if (d.getFinalizedAt() != null) {
m.setFinalizedAt(Date.from(d.getFinalizedAt()));
}
if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null)); if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null));
if (tagElasticEntityMap != null) m.setTags(tagElasticEntityMap.getOrDefault(d.getId(), null)); if (tagElasticEntityMap != null) m.setTags(tagElasticEntityMap.getOrDefault(d.getId(), null));
models.add(m); models.add(m);

View File

@ -64,7 +64,9 @@ public class NestedDmpElasticBuilder extends BaseElasticBuilder<NestedDmpElastic
m.setGroupId(d.getGroupId()); m.setGroupId(d.getGroupId());
m.setGroupId(d.getGroupId()); m.setGroupId(d.getGroupId());
m.setVersionStatus(d.getVersionStatus()); m.setVersionStatus(d.getVersionStatus());
m.setFinalizedAt(Date.from(d.getFinalizedAt())); if (d.getFinalizedAt() != null) {
m.setFinalizedAt(Date.from(d.getFinalizedAt()));
}
if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null)); if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null));
if (collaboratorElasticEntityMap != null) m.setCollaborators(collaboratorElasticEntityMap.getOrDefault(d.getId(), null)); if (collaboratorElasticEntityMap != null) m.setCollaborators(collaboratorElasticEntityMap.getOrDefault(d.getId(), null));
if (doiElasticEntityMap != null) m.setDois(doiElasticEntityMap.getOrDefault(d.getId(), null)); if (doiElasticEntityMap != null) m.setDois(doiElasticEntityMap.getOrDefault(d.getId(), null));

View File

@ -0,0 +1,52 @@
package eu.eudat.service.maintance;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.data.DmpEntity;
import eu.eudat.query.DmpQuery;
import eu.eudat.service.dmp.DmpService;
import eu.eudat.service.elastic.ElasticService;
import gr.cite.tools.data.query.QueryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
@Service
public class MaintenanceService {
private static final Logger logger = LoggerFactory.getLogger(MaintenanceService.class);
private final QueryFactory queryFactory;
private final ElasticService elasticService;
@Autowired
public MaintenanceService(DmpService dmpService, QueryFactory queryFactory, ElasticService elasticService) {
this.queryFactory = queryFactory;
this.elasticService = elasticService;
}
public void generateElasticIndex() {
List<DmpEntity> dmpEntities = queryFactory.query(DmpQuery.class).isActive(IsActive.Active, IsActive.Inactive).collect();
dmpEntities.forEach(dmpEntity -> {
try {
elasticService.persistDmp(dmpEntity);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
});
}
public void clearElasticIndex() {
List<DmpEntity> dmpEntities = queryFactory.query(DmpQuery.class).isActive(IsActive.Active, IsActive.Inactive).collect();
dmpEntities.forEach(dmpEntity -> {
try {
elasticService.deleteDmp(dmpEntity);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
});
}
}

View File

@ -0,0 +1,51 @@
package eu.eudat.controllers.v2;
import eu.eudat.authorization.Permission;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.service.maintance.MaintenanceService;
import eu.eudat.types.ApiMessageCode;
import gr.cite.commons.web.authz.service.AuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "api/maintenance")
public class MaintenanceController {
private final AuthorizationService authorizationService;
private final MaintenanceService maintenanceService;
@Autowired
public MaintenanceController(AuthorizationService authorizationService, MaintenanceService maintenanceService) {
this.authorizationService = authorizationService;
this.maintenanceService = maintenanceService;
}
/*
* Data Index
* */
@RequestMapping(method = RequestMethod.POST, value = {"/index/elastic"})
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionEntity>> generateIndex() throws Exception {
this.authorizationService.authorizeForce(Permission.ManageElastic);
this.maintenanceService.generateElasticIndex();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionEntity>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Generated").payload(null));
}
@RequestMapping(method = RequestMethod.DELETE, value = {"/index/elastic"})
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionEntity>> clearIndex() throws Exception {
this.authorizationService.authorizeForce(Permission.ManageElastic);
this.maintenanceService.clearElasticIndex();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionEntity>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Cleared").payload(null));
}
}

View File

@ -63,6 +63,7 @@ import { FileUtils } from './services/utilities/file-utils.service';
import { QueryParamsService } from './services/utilities/query-params.service'; import { QueryParamsService } from './services/utilities/query-params.service';
import { LanguageHttpService } from './services/language/language.http.service'; import { LanguageHttpService } from './services/language/language.http.service';
import { DescriptionService } from './services/description/description.service'; import { DescriptionService } from './services/description/description.service';
import { MaintenanceService } from './services/maintenance/maintenance.service';
// //
// //
// This is shared module that provides all the services. Its imported only once on the AppModule. // This is shared module that provides all the services. Its imported only once on the AppModule.
@ -145,7 +146,8 @@ export class CoreServiceModule {
UserService, UserService,
LanguageHttpService, LanguageHttpService,
DmpServiceNew, DmpServiceNew,
DescriptionService DescriptionService,
MaintenanceService
], ],
}; };
} }

View File

@ -0,0 +1,27 @@
import { BaseService } from "@common/base/base.service";
import { BaseHttpService } from "../http/base-http.service";
import { ConfigurationService } from "../configuration/configuration.service";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
@Injectable()
export class MaintenanceService extends BaseService{
private actionUrl: string;
constructor(private http: BaseHttpService,
private configurationService: ConfigurationService) {
super();
this.actionUrl = `${configurationService.server}maintenance`;
}
public generateElasticIndex(): Observable<any> {
return this.http.post<any>(`${this.actionUrl}/index/elastic`, null);
}
public clearElasticIndex(): Observable<any> {
return this.http.delete<any>(`${this.actionUrl}/index/elastic`);
}
}

View File

@ -3,7 +3,7 @@
<div> <div>
<div style="color: red;">Warning: Danger zone. It might delete Dataset tags if not careful</div> <div style="color: red;">Warning: Danger zone. It might delete Dataset tags if not careful</div>
<button mat-raised-button color="primary" (click)="generateIndex($event)" class="lightblue-btn button">Generate Index</button> <button mat-raised-button color="primary" (click)="generateIndex($event)" class="lightblue-btn button">Generate Index</button>
<!-- <button mat-raised-button color="primary" (click)="clearIndex($event)" class="lightblue-btn button">Clear Index</button> --> <button mat-raised-button color="primary" (click)="clearIndex($event)" class="lightblue-btn button">Clear Index</button>
</div> </div>
</mat-card> </mat-card>
</div> </div>

View File

@ -6,6 +6,7 @@ import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/serv
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { DmpService } from '@app/core/services/dmp/dmp.service'; import { DmpService } from '@app/core/services/dmp/dmp.service';
import { MaintenanceService } from '@app/core/services/maintenance/maintenance.service';
@Component({ @Component({
selector: 'app-index-managment', selector: 'app-index-managment',
@ -19,7 +20,7 @@ export class IndexManagmentComponent extends BaseComponent implements OnInit {
private uiNotificationService: UiNotificationService, private uiNotificationService: UiNotificationService,
private translate: TranslateService, private translate: TranslateService,
private router: Router, private router: Router,
private dmpService: DmpService private maintenanceService: MaintenanceService
) )
{ {
super(); super();
@ -29,48 +30,48 @@ export class IndexManagmentComponent extends BaseComponent implements OnInit {
} }
generateIndex(ev: Event) { generateIndex(ev: Event) {
(ev.srcElement as HTMLButtonElement).disabled = true; (ev.target as HTMLButtonElement).disabled = true;
// this.datasetService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe( // this.datasetService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe(
// response => { // response => {
// (ev.srcElement as HTMLButtonElement).disabled = false; // (ev.target as HTMLButtonElement).disabled = false;
// this.onCallbackSuccess(); // this.onCallbackSuccess();
// }, // },
// error => { // error => {
// (ev.srcElement as HTMLButtonElement).disabled = false; // (ev.target as HTMLButtonElement).disabled = false;
// this.onCallbackError(error); // this.onCallbackError(error);
// } // }
// ); // );
this.dmpService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe( this.maintenanceService.generateElasticIndex().pipe(takeUntil(this._destroyed)).subscribe(
response => { response => {
(ev.srcElement as HTMLButtonElement).disabled = false; (ev.target as HTMLButtonElement).disabled = false;
this.onCallbackSuccess(); this.onCallbackSuccess();
}, },
error => { error => {
(ev.srcElement as HTMLButtonElement).disabled = false; (ev.target as HTMLButtonElement).disabled = false;
this.onCallbackError(error); this.onCallbackError(error);
} }
); );
} }
clearIndex(ev: Event) { clearIndex(ev: Event) {
(ev.srcElement as HTMLButtonElement).disabled = true; (ev.target as HTMLButtonElement).disabled = true;
// this.datasetService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe( // this.datasetService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe(
// response => { // response => {
// (ev.srcElement as HTMLButtonElement).disabled = false; // (ev.target as HTMLButtonElement).disabled = false;
// this.onCallbackSuccess(); // this.onCallbackSuccess();
// }, // },
// error => { // error => {
// (ev.srcElement as HTMLButtonElement).disabled = false; // (ev.target as HTMLButtonElement).disabled = false;
// this.onCallbackError(error); // this.onCallbackError(error);
// } // }
// ); // );
this.dmpService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe( this.maintenanceService.clearElasticIndex().pipe(takeUntil(this._destroyed)).subscribe(
response => { response => {
(ev.srcElement as HTMLButtonElement).disabled = false; (ev.target as HTMLButtonElement).disabled = false;
this.onCallbackSuccess(); this.onCallbackSuccess();
}, },
error => { error => {
(ev.srcElement as HTMLButtonElement).disabled = false; (ev.target as HTMLButtonElement).disabled = false;
this.onCallbackError(error); this.onCallbackError(error);
} }
); );