LIMIT ?= 1000
SHIFT ?= 3
NUM_USERS ?= 10
SPAWN_RATE ?= 5
RUN_TIME ?= 60s
DATE ?= $(shell date +"%Y-%m-%d")


CONFIG_DIR ?= config
LOCUST_DIR ?= locust
REPORT_DIR ?= report
SAMPLE_DIR ?= samples

AWS_PROFILE    ?= gdb-infra-dev
AWS_ACCOUNT_ID ?= 483193324480
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= locust
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

#   docker/login - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login
docker/login:
	@aws ecr get-login \
		--profile ${AWS_PROFILE} \
		--no-include-email \
		--region ${AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${AWS_PROFILE} \
			--region ${AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(ECR_REGISTRY)


.PHONY: docker/run/local
docker/run/local: docker/login
	docker build -t core-load-tester .
	mkdir -p $(abspath ${SAMPLE_DIR})/
	mkdir -p $(abspath ${REPORT_DIR})/
	docker run -it --env-file .env -e SAMPLE_FILE=${DATE}.csv\
	 --mount type=bind,source=$(shell pwd)/${SAMPLE_DIR},target="/home/locust/samples" \
	 --mount type=bind,source=${HOME}/.aws,target="/home/locust/.aws" \
	core-load-tester "python3 selectors/aws/athena.py -l ${LIMIT} -o samples/${DATE}.csv -d ${DATE} -s ${SHIFT}"
	docker run -it --env-file .env -e SAMPLE_FILE=/home/locust/samples/${DATE}.csv\
	 --mount type=bind,source=$(shell pwd)/${SAMPLE_DIR},target="/home/locust/samples" \
	 --mount type=bind,source=$(shell pwd)/${REPORT_DIR},target="/home/locust/report" \
	 --mount type=bind,source=$(shell pwd)/locust,target="/home/locust/locust" \
	 --mount type=bind,source=${HOME}/.aws,target="/home/locust/.aws" \
	 locustio/locust \
	-f "/home/locust/locust/locustfile.py" \
	--headless -u ${NUM_USERS} -r ${SPAWN_RATE} -t ${RUN_TIME} \
	-H "https://${HOST}" \
	--csv="/home/locust/report/${DATE}.csv" \
	--html "/home/locust/report/report-${DATE}.html"


.PHONY: docker/run/sample/aws
docker/run/sample/aws: docker/login
	docker build -t core-load-tester .
	mkdir -p $(abspath ${SAMPLE_DIR})/
	docker run -it --env-file .env -e SAMPLE_FILE=${DATE}.csv\
	 --mount type=bind,source=$(shell pwd)/${SAMPLE_DIR},target="/home/locust/samples" \
	 --mount type=bind,source=${HOME}/.aws,target="/home/locust/.aws" \
	core-load-tester "python3 selectors/aws/athena.py -l ${LIMIT} -o samples/${DATE}.csv -d ${DATE} -s ${SHIFT}"


.PHONY: docker/run/sample/datadog
docker/run/sample/datadog: docker/login
	docker build -t core-load-tester .
	mkdir -p $(abspath ${SAMPLE_DIR})/
	docker run -it --env-file .env -e SAMPLE_FILE=${DATE}.csv\
	 --mount type=bind,source=$(shell pwd)/${SAMPLE_DIR},target="/home/locust/samples" \
	 --mount type=bind,source=${HOME}/.aws,target="/home/locust/.aws" \
	core-load-tester "python3 selectors/datadog/selector.py -o samples/${DATE}.csv -d ${DATE} -s ${SHIFT}"


.PHONY: docker/run/locust
docker/run/locust: docker/login
	docker build -t core-load-tester .
	mkdir -p $(abspath ${REPORT_DIR})/
	docker run -it --env-file .env -e SAMPLE_FILE="/home/locust/samples/${CSV_FILE_NAME}.csv"\
	 --mount type=bind,source=${CSV_FOLDER},target="/home/locust/samples" \
	 --mount type=bind,source=$(shell pwd)/${REPORT_DIR},target="/home/locust/report" \
	 --mount type=bind,source=$(shell pwd)/locust,target="/home/locust/locust" \
	 --mount type=bind,source=${HOME}/.aws,target="/home/locust/.aws" \
	 --platform linux/amd64 \
	 core-load-tester \
	-f "locust -f /home/locust/locust/locustfile.py --headless -u ${NUM_USERS} -r ${SPAWN_RATE} -t ${RUN_TIME} -H https://${HOST} --csv=/home/locust/report/${CSV_FILE_NAME}.csv --html /home/locust/report/report-${CSV_FILE_NAME}.html"


.PHONY: clean
clean:
	-rm -r ${REPORT_DIR} ${SAMPLE_DIR}
