# Copyright 2022 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.PHONY: all init run vitess-start vitess-stop request stop

all:
	# run `make vitess-start` and waiting for vitess container started
	# maybe need 1 min
	make init run
	sleep 3 # waiting for spring boot container started
	make request stop

init:
	mysql -u root -P 33577 -h 127.0.0.1 < src/main/resources/use-vitess-initial.sql

vitess-start:
	docker run -d \
		--name=vttestserver \
		-p 33577:33577 \
		-e PORT=33574 \
		-e PLANNER_VERSION=gen4fallback \
		-e KEYSPACES=vitess_plan \
		-e NUM_SHARDS=2 \
		-e MYSQL_MAX_CONNECTIONS=70000 \
		-e MYSQL_BIND_HOST=0.0.0.0 \
		--health-cmd="mysqladmin ping -h127.0.0.1 -P33577" \
		--health-interval=5s \
		--health-timeout=2s \
		--health-retries=5 \
		vitess/vttestserver:mysql80

vitess-stop:
	docker stop vttestserver
	docker rm vttestserver

run:
	java -jar target/vitess-plan-0.0.1-SNAPSHOT.jar > vitess-plan.log &

request:
	curl --location --request GET 'http://localhost:8080/event/daily' | python3 -m json.tool
	curl --location --request GET 'http://localhost:8080/personal/language?userID=66930949'  | python3 -m json.tool
	curl \
		--location --request POST 'http://localhost:8080/repo' \
		--header 'Content-Type: application/json' \
		--data-raw '[{"repoID":1,"repoName":"pingcap/same-name-repo","ownerID":0,"ownerLogin":"pingcap","ownerIsOrg":0}]' \
		| python3 -m json.tool
	curl \
		--location --request POST 'http://localhost:8080/repo' \
		--header 'Content-Type: application/json' \
		--data-raw '[{"repoID":81,"repoName":"pingcap/same-name-repo","ownerID":0,"ownerLogin":"pingcap","ownerIsOrg":0}]' \
    	| python3 -m json.tool

stop:
	kill `cat vitess-plan.pid`