-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sfn and cron rule for daily refresh of schedules/flights
- Loading branch information
Showing
4 changed files
with
224 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Construct } from 'constructs'; | ||
import { IBucket } from 'aws-cdk-lib/aws-s3'; | ||
import { IFunction } from 'aws-cdk-lib/aws-lambda'; | ||
import { DefinitionBody, IStateMachine, StateMachine, TaskInput, JsonPath } from 'aws-cdk-lib/aws-stepfunctions'; | ||
import { LambdaInvoke } from 'aws-cdk-lib/aws-stepfunctions-tasks'; | ||
|
||
export interface SfnConstructProps { | ||
dataBucket: IBucket; | ||
cronLambda: IFunction; | ||
} | ||
|
||
export class SfnConstruct extends Construct { | ||
readonly flightSchedules: IStateMachine; | ||
|
||
constructor(scope: Construct, id: string, props: SfnConstructProps) { | ||
super(scope, id); | ||
|
||
const definition = new LambdaInvoke(this, 'LoadSchedulesTask', { | ||
lambdaFunction: props.cronLambda, | ||
payload: TaskInput.fromObject({ | ||
'action': 'cron', | ||
'params': { | ||
'loadFlightSchedules': { | ||
'outputBucket': props.dataBucket.bucketName, | ||
'outputPrefix': 'raw/LH_Public_Data/flightschedules/', | ||
'schedule': JsonPath.stringAt('$.schedule'), | ||
}, | ||
}, | ||
}), | ||
payloadResponseOnly: true, | ||
resultPath: '$.loadSchedulesResponse', | ||
retryOnServiceExceptions: true, | ||
}) | ||
.next(new LambdaInvoke(this, 'ConvertSchedulesTask', { | ||
lambdaFunction: props.cronLambda, | ||
payload: TaskInput.fromObject({ | ||
'action': 'convert_flight_schedules', | ||
'params': { | ||
'inputBucket': JsonPath.stringAt('$.loadSchedulesResponse.loadFlightSchedules.input.outputBucket'), | ||
'inputPrefix': JsonPath.stringAt('$.loadSchedulesResponse.loadFlightSchedules.input.outputPrefix'), | ||
'outputBucket': props.dataBucket.bucketName, | ||
'outputPrefix': 'processed/flights/', | ||
'dateRanges': JsonPath.objectAt('$.loadSchedulesResponse.loadFlightSchedules.input.dateRanges'), | ||
}, | ||
}), | ||
payloadResponseOnly: true, | ||
resultPath: '$.convertSchedulesResponse', | ||
retryOnServiceExceptions: true, | ||
})) | ||
|
||
this.flightSchedules = new StateMachine(this, 'FlightSchedules', { | ||
definitionBody: DefinitionBody.fromChainable(definition), | ||
tracingEnabled: false, | ||
}); | ||
} | ||
} |
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,129 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"github.com/explore-flights/monorepo/go/common" | ||
"time" | ||
) | ||
|
||
type CronParams struct { | ||
LoadFlightSchedules *struct { | ||
OutputBucket string `json:"outputBucket"` | ||
OutputPrefix string `json:"outputPrefix"` | ||
Schedule string `json:"schedule"` | ||
} `json:"loadFlightSchedules,omitempty"` | ||
ConvertFlightSchedules *struct { | ||
InputBucket string `json:"inputBucket"` | ||
InputPrefix string `json:"inputPrefix"` | ||
OutputBucket string `json:"outputBucket"` | ||
OutputPrefix string `json:"outputPrefix"` | ||
Schedule string `json:"schedule"` | ||
} `json:"convertFlightSchedules,omitempty"` | ||
} | ||
|
||
type CronOutput struct { | ||
LoadFlightSchedules *InputOutput[LoadFlightSchedulesParams, LoadFlightSchedulesOutput] `json:"loadFlightSchedules,omitempty"` | ||
ConvertFlightSchedules *InputOutput[ConvertFlightSchedulesParams, ConvertFlightSchedulesOutput] `json:"convertFlightSchedules,omitempty"` | ||
} | ||
|
||
type InputOutput[IN any, OUT any] struct { | ||
Input IN `json:"input"` | ||
Output OUT `json:"output"` | ||
} | ||
|
||
type cronAction struct { | ||
lfsA Action[LoadFlightSchedulesParams, LoadFlightSchedulesOutput] | ||
cfsA Action[ConvertFlightSchedulesParams, ConvertFlightSchedulesOutput] | ||
} | ||
|
||
func NewCronAction(lfsA Action[LoadFlightSchedulesParams, LoadFlightSchedulesOutput], cfsA Action[ConvertFlightSchedulesParams, ConvertFlightSchedulesOutput]) Action[CronParams, CronOutput] { | ||
return &cronAction{ | ||
lfsA: lfsA, | ||
cfsA: cfsA, | ||
} | ||
} | ||
|
||
func (c *cronAction) Handle(ctx context.Context, params CronParams) (CronOutput, error) { | ||
var output CronOutput | ||
var err error | ||
|
||
if params.LoadFlightSchedules != nil { | ||
lfsInOut := InputOutput[LoadFlightSchedulesParams, LoadFlightSchedulesOutput]{ | ||
Input: LoadFlightSchedulesParams{ | ||
OutputBucket: params.LoadFlightSchedules.OutputBucket, | ||
OutputPrefix: params.LoadFlightSchedules.OutputPrefix, | ||
DateRanges: nil, | ||
}, | ||
} | ||
|
||
switch params.LoadFlightSchedules.Schedule { | ||
case "daily": | ||
start := time.Now() | ||
end := start.AddDate(0, 0, 7) | ||
|
||
lfsInOut.Input.DateRanges = append(lfsInOut.Input.DateRanges, [2]common.LocalDate{ | ||
common.NewLocalDate(start), | ||
common.NewLocalDate(end), | ||
}) | ||
|
||
default: | ||
return output, errors.New("invalid schedule") | ||
} | ||
|
||
if lfsInOut.Output, err = c.lfsA.Handle(ctx, lfsInOut.Input); err != nil { | ||
return output, err | ||
} | ||
|
||
output.LoadFlightSchedules = &lfsInOut | ||
} | ||
|
||
if params.ConvertFlightSchedules != nil { | ||
cfsInOut := InputOutput[ConvertFlightSchedulesParams, ConvertFlightSchedulesOutput]{ | ||
Input: ConvertFlightSchedulesParams{ | ||
InputBucket: params.ConvertFlightSchedules.InputBucket, | ||
InputPrefix: params.ConvertFlightSchedules.InputPrefix, | ||
OutputBucket: params.ConvertFlightSchedules.OutputBucket, | ||
OutputPrefix: params.ConvertFlightSchedules.OutputPrefix, | ||
DateRanges: nil, | ||
}, | ||
} | ||
|
||
switch params.LoadFlightSchedules.Schedule { | ||
case "daily": | ||
start := time.Now() | ||
end := start.AddDate(0, 0, 7) | ||
|
||
cfsInOut.Input.DateRanges = append(cfsInOut.Input.DateRanges, [2]common.LocalDate{ | ||
common.NewLocalDate(start), | ||
common.NewLocalDate(end), | ||
}) | ||
|
||
default: | ||
return output, errors.New("invalid schedule") | ||
} | ||
|
||
if cfsInOut.Output, err = c.cfsA.Handle(ctx, cfsInOut.Input); err != nil { | ||
return output, err | ||
} | ||
|
||
output.ConvertFlightSchedules = &cfsInOut | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func handle[IN any, OUT any](ctx context.Context, act Action[IN, OUT], params json.RawMessage) ([]byte, error) { | ||
var input IN | ||
if err := json.Unmarshal(params, &input); err != nil { | ||
return nil, err | ||
} | ||
|
||
output, err := act.Handle(ctx, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return json.Marshal(output) | ||
} |
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