|
| 1 | +package org.hypertrace.graphql.label.application.rules.dao; |
| 2 | + |
| 3 | +import io.grpc.CallCredentials; |
| 4 | +import io.reactivex.rxjava3.core.Single; |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | +import javax.inject.Inject; |
| 7 | +import org.hypertrace.core.graphql.common.request.ContextualRequest; |
| 8 | +import org.hypertrace.core.graphql.utils.grpc.GrpcChannelRegistry; |
| 9 | +import org.hypertrace.core.graphql.utils.grpc.GrpcContextBuilder; |
| 10 | +import org.hypertrace.graphql.config.HypertraceGraphQlServiceConfig; |
| 11 | +import org.hypertrace.graphql.label.application.rules.request.LabelApplicationRuleCreateRequest; |
| 12 | +import org.hypertrace.graphql.label.application.rules.request.LabelApplicationRuleDeleteRequest; |
| 13 | +import org.hypertrace.graphql.label.application.rules.request.LabelApplicationRuleUpdateRequest; |
| 14 | +import org.hypertrace.graphql.label.application.rules.schema.query.LabelApplicationRuleResultSet; |
| 15 | +import org.hypertrace.graphql.label.application.rules.schema.shared.LabelApplicationRule; |
| 16 | +import org.hypertrace.label.application.rule.config.service.v1.GetLabelApplicationRulesRequest; |
| 17 | +import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRuleConfigServiceGrpc; |
| 18 | +import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRuleConfigServiceGrpc.LabelApplicationRuleConfigServiceFutureStub; |
| 19 | + |
| 20 | +class LabelApplicationRuleConfigServiceDao implements LabelApplicationRuleDao { |
| 21 | + |
| 22 | + private final LabelApplicationRuleConfigServiceFutureStub |
| 23 | + labelApplicationRuleConfigServiceFutureStub; |
| 24 | + private final GrpcContextBuilder grpcContextBuilder; |
| 25 | + private final HypertraceGraphQlServiceConfig serviceConfig; |
| 26 | + private final LabelApplicationRuleRequestConverter requestConverter; |
| 27 | + private final LabelApplicationRuleResponseConverter responseConverter; |
| 28 | + |
| 29 | + @Inject |
| 30 | + LabelApplicationRuleConfigServiceDao( |
| 31 | + HypertraceGraphQlServiceConfig serviceConfig, |
| 32 | + CallCredentials credentials, |
| 33 | + GrpcContextBuilder grpcContextBuilder, |
| 34 | + GrpcChannelRegistry grpcChannelRegistry, |
| 35 | + LabelApplicationRuleRequestConverter requestConverter, |
| 36 | + LabelApplicationRuleResponseConverter responseConverter) { |
| 37 | + this.grpcContextBuilder = grpcContextBuilder; |
| 38 | + this.serviceConfig = serviceConfig; |
| 39 | + this.requestConverter = requestConverter; |
| 40 | + this.responseConverter = responseConverter; |
| 41 | + this.labelApplicationRuleConfigServiceFutureStub = |
| 42 | + LabelApplicationRuleConfigServiceGrpc.newFutureStub( |
| 43 | + grpcChannelRegistry.forAddress( |
| 44 | + serviceConfig.getConfigServiceHost(), serviceConfig.getConfigServicePort())) |
| 45 | + .withCallCredentials(credentials); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Single<LabelApplicationRule> createLabelApplicationRule( |
| 50 | + LabelApplicationRuleCreateRequest request) { |
| 51 | + return Single.fromFuture( |
| 52 | + this.grpcContextBuilder |
| 53 | + .build(request.context()) |
| 54 | + .call( |
| 55 | + () -> |
| 56 | + this.labelApplicationRuleConfigServiceFutureStub |
| 57 | + .withDeadlineAfter( |
| 58 | + serviceConfig.getConfigServiceTimeout().toMillis(), |
| 59 | + TimeUnit.MILLISECONDS) |
| 60 | + .createLabelApplicationRule( |
| 61 | + this.requestConverter.convertCreationRequest(request)))) |
| 62 | + .flatMap(this.responseConverter::convertCreateLabelApplicationRuleResponse); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Single<LabelApplicationRuleResultSet> getLabelApplicationRules(ContextualRequest request) { |
| 67 | + return Single.fromFuture( |
| 68 | + this.grpcContextBuilder |
| 69 | + .build(request.context()) |
| 70 | + .call( |
| 71 | + () -> |
| 72 | + this.labelApplicationRuleConfigServiceFutureStub |
| 73 | + .withDeadlineAfter( |
| 74 | + serviceConfig.getConfigServiceTimeout().toMillis(), |
| 75 | + TimeUnit.MILLISECONDS) |
| 76 | + .getLabelApplicationRules( |
| 77 | + GetLabelApplicationRulesRequest.getDefaultInstance()))) |
| 78 | + .flatMap(this.responseConverter::convertGetLabelApplicationsRuleResponse); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Single<LabelApplicationRule> updateLabelApplicationRule( |
| 83 | + LabelApplicationRuleUpdateRequest request) { |
| 84 | + return Single.fromFuture( |
| 85 | + this.grpcContextBuilder |
| 86 | + .build(request.context()) |
| 87 | + .call( |
| 88 | + () -> |
| 89 | + this.labelApplicationRuleConfigServiceFutureStub |
| 90 | + .withDeadlineAfter( |
| 91 | + serviceConfig.getConfigServiceTimeout().toMillis(), |
| 92 | + TimeUnit.MILLISECONDS) |
| 93 | + .updateLabelApplicationRule( |
| 94 | + this.requestConverter.convertUpdateRequest(request)))) |
| 95 | + .flatMap(this.responseConverter::convertUpdateLabelApplicationRuleResponse); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Single<Boolean> deleteLabelApplicationRule(LabelApplicationRuleDeleteRequest request) { |
| 100 | + return Single.fromFuture( |
| 101 | + this.grpcContextBuilder |
| 102 | + .build(request.context()) |
| 103 | + .call( |
| 104 | + () -> |
| 105 | + this.labelApplicationRuleConfigServiceFutureStub |
| 106 | + .withDeadlineAfter( |
| 107 | + serviceConfig.getConfigServiceTimeout().toMillis(), |
| 108 | + TimeUnit.MILLISECONDS) |
| 109 | + .deleteLabelApplicationRule( |
| 110 | + this.requestConverter.convertDeleteRequest(request)))) |
| 111 | + .flatMap( |
| 112 | + unusedResponse -> this.responseConverter.buildDeleteLabelApplicationRuleResponse()); |
| 113 | + } |
| 114 | +} |
0 commit comments