-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reindex GitHub Events into maintainer inactivity data
Signed-off-by: Brandon Shien <bshien@amazon.com>
- Loading branch information
Showing
17 changed files
with
1,195 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Duration, Stack, StackProps } from "aws-cdk-lib"; | ||
import { Rule, Schedule } from "aws-cdk-lib/aws-events"; | ||
import { SfnStateMachine } from "aws-cdk-lib/aws-events-targets"; | ||
import { JsonPath, StateMachine } from "aws-cdk-lib/aws-stepfunctions"; | ||
import { LambdaInvoke } from "aws-cdk-lib/aws-stepfunctions-tasks"; | ||
import { Construct } from 'constructs'; | ||
import { OpenSearchLambda } from "../constructs/lambda"; | ||
import { OpenSearchDomainStack } from "./opensearch"; | ||
import { VpcStack } from "./vpc"; | ||
|
||
export interface OpenSearchMaintainerInactivityWorkflowStackProps extends StackProps { | ||
readonly opensearchDomainStack: OpenSearchDomainStack; | ||
readonly vpcStack: VpcStack; | ||
readonly lambdaPackage: string | ||
} | ||
|
||
export interface WorkflowComponent { | ||
opensearchMaintainerInactivityWorkflowStateMachineName: string | ||
} | ||
|
||
export class OpenSearchMaintainerInactivityWorkflowStack extends Stack { | ||
public readonly workflowComponent: WorkflowComponent; | ||
constructor(scope: Construct, id: string, props: OpenSearchMaintainerInactivityWorkflowStackProps) { | ||
super(scope, id, props); | ||
|
||
const maintainerInactivityTask = this.createMaintainerInactivityTask( | ||
this, | ||
props.opensearchDomainStack, | ||
props.vpcStack, | ||
props.lambdaPackage | ||
); | ||
const opensearchMaintainerInactivityWorkflow = new StateMachine(this, 'OpenSearchMaintainerInactivityWorkflow', { | ||
definition: maintainerInactivityTask, | ||
timeout: Duration.minutes(15), | ||
stateMachineName: 'OpenSearchMaintainerInactivityWorkflow' | ||
}) | ||
|
||
new Rule(this, 'MaintainerInactivityWorkflow-Every-Day', { | ||
schedule: Schedule.expression('cron(15 0 * * ? *)'), | ||
targets: [new SfnStateMachine(opensearchMaintainerInactivityWorkflow)], | ||
}); | ||
|
||
this.workflowComponent = { | ||
opensearchMaintainerInactivityWorkflowStateMachineName: opensearchMaintainerInactivityWorkflow.stateMachineName | ||
} | ||
} | ||
|
||
private createMaintainerInactivityTask(scope: Construct, opensearchDomainStack: OpenSearchDomainStack, | ||
vpcStack: VpcStack, lambdaPackage: string) { | ||
const openSearchDomain = opensearchDomainStack.domain; | ||
const maintainerInactivityLambda = new OpenSearchLambda(scope, "OpenSearchMetricsMaintainerInactivityLambdaFunction", { | ||
lambdaNameBase: "OpenSearchMetricsMaintainerInactivity", | ||
handler: "org.opensearchmetrics.lambda.MaintainerInactivityLambda", | ||
lambdaZipPath: `../../../build/distributions/${lambdaPackage}`, | ||
vpc: vpcStack.vpc, | ||
securityGroup: vpcStack.securityGroup, | ||
role: opensearchDomainStack.openSearchMetricsLambdaRole, | ||
environment: { | ||
OPENSEARCH_DOMAIN_ENDPOINT: openSearchDomain.domainEndpoint, | ||
OPENSEARCH_DOMAIN_REGION: openSearchDomain.env.region, | ||
OPENSEARCH_DOMAIN_ROLE: opensearchDomainStack.fullAccessRole.roleArn, | ||
}, | ||
}).lambda; | ||
return new LambdaInvoke(scope, 'Maintainer Inactivity Lambda', { | ||
lambdaFunction: maintainerInactivityLambda, | ||
resultPath: JsonPath.DISCARD, | ||
timeout: Duration.minutes(15) | ||
}).addRetry(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
infrastructure/test/maintainer-inactivity-workflow-stack.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { App } from "aws-cdk-lib"; | ||
import { Template } from "aws-cdk-lib/assertions"; | ||
import { OpenSearchMetricsWorkflowStack } from "../lib/stacks/metricsWorkflow"; | ||
import Project from "../lib/enums/project"; | ||
import { OpenSearchDomainStack } from "../lib/stacks/opensearch"; | ||
import { VpcStack } from "../lib/stacks/vpc"; | ||
import { ArnPrincipal } from "aws-cdk-lib/aws-iam"; | ||
import {OpenSearchS3} from "../lib/stacks/s3"; | ||
import {OpenSearchMaintainerInactivityWorkflowStack} from "../lib/stacks/maintainerInactivityWorkflow"; | ||
|
||
test('Maintainer Inactivity Workflow Stack Test', () => { | ||
const app = new App(); | ||
const vpcStack = new VpcStack(app, 'Test-OpenSearchHealth-VPC', {}); | ||
const s3Stack = new OpenSearchS3(app, "Test-OpenSearchMetrics-GitHubAutomationAppEvents-S3"); | ||
const openSearchDomainStack = new OpenSearchDomainStack(app, 'OpenSearchHealth-OpenSearch', { | ||
region: "us-east-1", | ||
account: "test-account", | ||
vpcStack: new VpcStack(app, 'OpenSearchHealth-VPC', {}), | ||
enableNginxCognito: true, | ||
jenkinsAccess: { | ||
jenkinsAccountRoles: [ | ||
new ArnPrincipal(Project.JENKINS_MASTER_ROLE), | ||
new ArnPrincipal(Project.JENKINS_AGENT_ROLE) | ||
] | ||
}, | ||
githubAutomationAppAccess: "sample-role-arn", | ||
githubEventsBucket: s3Stack.bucket, | ||
}); | ||
const openSearchMaintainerInactivityWorkflowStack = new OpenSearchMaintainerInactivityWorkflowStack(app, 'Test-OpenSearchMaintainerInactivity-Workflow', { | ||
opensearchDomainStack: openSearchDomainStack, | ||
vpcStack: vpcStack, | ||
lambdaPackage: Project.LAMBDA_PACKAGE, | ||
}); | ||
const template = Template.fromStack(openSearchMaintainerInactivityWorkflowStack); | ||
template.resourceCountIs('AWS::IAM::Role', 2); | ||
template.resourceCountIs('AWS::Lambda::Function', 1); | ||
template.hasResourceProperties('AWS::Lambda::Function', { | ||
"FunctionName": "OpenSearchMetricsMaintainerInactivityLambda", | ||
"Handler": "org.opensearchmetrics.lambda.MaintainerInactivityLambda" | ||
}); | ||
template.resourceCountIs('AWS::StepFunctions::StateMachine', 1); | ||
template.hasResourceProperties('AWS::StepFunctions::StateMachine', { | ||
"DefinitionString": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"{\"StartAt\":\"Maintainer Inactivity Lambda\",\"States\":{\"Maintainer Inactivity Lambda\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"Lambda.ClientExecutionTimeoutException\",\"Lambda.ServiceException\",\"Lambda.AWSLambdaException\",\"Lambda.SdkClientException\"],\"IntervalSeconds\":2,\"MaxAttempts\":6,\"BackoffRate\":2},{\"ErrorEquals\":[\"States.ALL\"]}],\"Type\":\"Task\",\"TimeoutSeconds\":900,\"ResultPath\":null,\"Resource\":\"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"", | ||
{ | ||
"Fn::GetAtt": [ | ||
"OpenSearchMetricsMaintainerInactivityLambdaCB6D4475", | ||
"Arn" | ||
] | ||
}, | ||
"\",\"Payload.$\":\"$\"}}},\"TimeoutSeconds\":900}" | ||
] | ||
] | ||
}, | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"OpenSearchMaintainerInactivityWorkflowRoleF9A5E625", | ||
"Arn" | ||
] | ||
}, | ||
"StateMachineName": "OpenSearchMaintainerInactivityWorkflow" | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/opensearchmetrics/lambda/MaintainerInactivityLambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.opensearchmetrics.lambda; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.search.aggregations.AggregationBuilders; | ||
import org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms; | ||
import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearchmetrics.dagger.DaggerServiceComponent; | ||
import org.opensearchmetrics.dagger.ServiceComponent; | ||
import org.opensearchmetrics.metrics.MetricsCalculation; | ||
import org.opensearchmetrics.util.OpenSearchUtil; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
public class MaintainerInactivityLambda extends AbstractBaseLambda { | ||
private static final ServiceComponent COMPONENT = DaggerServiceComponent.create(); | ||
private final OpenSearchUtil openSearchUtil; | ||
|
||
private final MetricsCalculation metricsCalculation; | ||
|
||
public MaintainerInactivityLambda() { | ||
this(COMPONENT.getOpenSearchUtil(), COMPONENT.getMetricsCalculation()); | ||
} | ||
|
||
@VisibleForTesting | ||
MaintainerInactivityLambda(@NonNull OpenSearchUtil openSearchUtil, @NonNull MetricsCalculation metricsCalculation) { | ||
this.openSearchUtil = openSearchUtil; | ||
this.metricsCalculation = metricsCalculation; | ||
} | ||
|
||
@Override | ||
public Void handleRequest(Void input, Context context) { | ||
SearchRequest searchRequest = new SearchRequest("github_repos"); | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
searchSourceBuilder.size(0); | ||
TermsAggregationBuilder aggregation = AggregationBuilders.terms("repos") | ||
.field("repository.keyword").size(500); | ||
searchSourceBuilder.aggregation(aggregation); | ||
searchRequest.source(searchSourceBuilder); | ||
SearchResponse searchResponse = null; | ||
searchResponse = openSearchUtil.search(searchRequest); | ||
ParsedStringTerms termsAggregation = searchResponse.getAggregations().get("repos"); | ||
List<String> keys = termsAggregation.getBuckets().stream() | ||
.map(bucket -> bucket.getKeyAsString()) | ||
.collect(Collectors.toList()); | ||
try { | ||
metricsCalculation.generateMaintainerMetrics(keys); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error running Maintainer Inactivity Calculation", e); | ||
} | ||
return input; | ||
} | ||
} | ||
|
Oops, something went wrong.