# 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 request stop

all:
	make zookeeper-start init run
	sleep 3 # waiting for spring boot application started
	make data-prepare request stop zookeeper-stop

zookeeper-start:
	docker run --name zookeeper --restart unless-stopped -p 2181:2181 -d zookeeper

zookeeper-stop:
	docker stop zookeeper
	docker rm zookeeper

init:
	mysql -uroot -P3306 -h127.0.0.1 -p123456 < src/main/resources/use-shardingsphere-initial.sql

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

data-prepare:
	curl \
    		--location --request POST 'http://localhost:8080/event/full' \
    		--header 'Content-Type: application/json' \
    		-d @src/main/resources/github_events.payload.json \
    		| python3 -m json.tool
	curl \
        		--location --request POST 'http://localhost:8080/repo/full' \
        		--header 'Content-Type: application/json' \
        		-d @src/main/resources/github_repos.payload.json \
        		| python3 -m json.tool

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":2,"repoName":"pingcap/same-name-repo","ownerID":0,"ownerLogin":"pingcap","ownerIsOrg":0}]' \
    	| python3 -m json.tool

stop:
	kill `cat shardingsphere-plan.pid`