From 405ccb04d5767a014910142fbd0577971d7926cc Mon Sep 17 00:00:00 2001 From: Mishra Date: Mon, 11 May 2020 00:53:00 +0530 Subject: [PATCH 1/6] commiting code for find angle service --- app.yaml | 6 +++++ find_angle.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ test_find_angle.py | 51 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 app.yaml create mode 100644 find_angle.py create mode 100644 requirements.txt create mode 100644 test_find_angle.py diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..84bd0c6 --- /dev/null +++ b/app.yaml @@ -0,0 +1,6 @@ +runtime: python +env: flex +entrypoint: gunicorn -b :$PORT:app + +runtime_config: + python_version :3 \ No newline at end of file diff --git a/find_angle.py b/find_angle.py new file mode 100644 index 0000000..673f390 --- /dev/null +++ b/find_angle.py @@ -0,0 +1,60 @@ +from flask import Flask, jsonify +import time + +app = Flask(__name__) + + +@app.route('/clock_angle/', methods=['GET']) +def get_clock_angle(data): + if is_valid_time(data): + return jsonify(context="Angle for time {} is {} degree.".format(data, cal_culculate_angle(data))) + + return jsonify(context="Please enter the valid format i.e. This time function will accept time b/w 00:00 To 23:59.") + + +def is_valid_time(value): + """ This time function will accept time b/w 00:00 to 23:59""" + + try: + time.strptime(value, '%H:%M') + return True + except ValueError: + return False + + +def cal_culculate_angle(data): + """Explanation: + Hour Calculation : + ------------------ + A analog clock is divided up into 12 sectors, based on the numbers 1–12. One sector represents + 30 degrees (360/12 = 30). + so 1 hour = 30 degree + one minute = 0.5 degree + + Min Calculation : + ----------------- + a clock represent 360 degree so + 1 min = 360 /60 = 6 degree + """ + + CONST_PER_HOUR_DEGREE = 30 + CONST_PER_MIN_DEGREE = 0.5 + CONST_PER_MIN_DEG_FOR_MIN = 6 + + _hour, _min = data.split(":") + _hour = 12 if int(_hour) == 0 else int(_hour) + _min = int(_min) + + if int(_hour) > 12: + _hour = int(_hour) - 12 + + # Position of Hour + h = _hour * CONST_PER_HOUR_DEGREE + _min * CONST_PER_MIN_DEGREE + # Position of Min + m = _min * CONST_PER_MIN_DEG_FOR_MIN + + return abs(h - m) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c35bd3a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Flask==1.1.2 +gunicorn==19.6.0 \ No newline at end of file diff --git a/test_find_angle.py b/test_find_angle.py new file mode 100644 index 0000000..7a51788 --- /dev/null +++ b/test_find_angle.py @@ -0,0 +1,51 @@ +import unittest + +from find_angle import app, get_clock_angle + + +class testClock(unittest.TestCase): + + def setUp(self): + # self.app = app.test_client() + self.app_context = app.app_context() + self.app_context.push() + + def test_get_clock_angle_for_time_fail_testing(self): + """Test case for fail testing """ + data = get_clock_angle("as:aa") + expected_data = "Please enter the valid format i.e. This time function will accept time b/w 00:00 To 23:59." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("as:11") + expected_data = "Please enter the valid format i.e. This time function will accept time b/w 00:00 To 23:59." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("111:12") + expected_data = "Please enter the valid format i.e. This time function will accept time b/w 00:00 To 23:59." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("24:60") + expected_data = "Please enter the valid format i.e. This time function will accept time b/w 00:00 To 23:59." + self.assertEqual(expected_data, data.json['context']) + + def test_get_clock_angle_for_time_pass_testing(self): + """Test case for pass testing """ + data = get_clock_angle("00:59") + expected_data = "Angle for time 00:59 is 35.5 degree." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("12:59") + expected_data = "Angle for time 12:59 is 35.5 degree." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("01:00") + expected_data = "Angle for time 01:00 is 30.0 degree." + self.assertEqual(expected_data, data.json['context']) + + data = get_clock_angle("03:20") + expected_data = "Angle for time 03:20 is 20.0 degree." + self.assertEqual(expected_data, data.json['context']) + + +if __name__ == '__main__': + unittest.find_angle() From 39c27df7c0eb1a1e8ff791f119f4d0033391b326 Mon Sep 17 00:00:00 2001 From: Mishra Date: Mon, 11 May 2020 04:01:08 +0530 Subject: [PATCH 2/6] making small changes --- README.md | 80 +++++++++++++++++++++++++++++------------------- app.yaml | 10 ++++-- dsfs | 0 requirements.txt | 2 +- 4 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 dsfs diff --git a/README.md b/README.md index 4a77c09..b5c73f1 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,74 @@ -# Clock Exercise -We are interested in running code of course, but even more in your development process and understanding of Software Development Lifecycle Management. -**Fork this repo, then get to work.** Remember that this is a DevOps team, so make sure your repo reflects that. Spend however much time you feel is reasonable. It doesn’t matter if the project is ‘done’, nothing ever is. **When you’re ready push your changes back to Github and put in a pull request back to the base repo.** +## Product Backlog Item (Sprint Story) -This exercise is not meant to take an excessive amount of time. It is an opportunity for you to demonstrate your skills without the stress of an interview. If you start to run out of time, it’s ok to leave an imaginary team member a TODO list that details all the things you didn’t quite have time to do in order for your solution to go to prod. +Here is the story that is in the backlog. -If you need clarification, or would like to request additional information, pease reach out to the interviewer by email. +As with all stories, the team may have been optimistic with how much can be done in the time permitted. It's ok to meet some of the acceptance criteria by documenting what you would do in the next sprint! Prioritize your time and make sure you have some technical content to deliver. -## Scenario +### Description:- -You have just joined a DevOps team. This team lives by DevOps principles and you want to let them know you mean business! This particular team is developing a product that is deployed in a Google Cloud Project. +As a team
+We need a serivce that we can send a time value to and have it return or store an angle value
+So that we can use it in downstream processing -This sprint, the team has been asked to work on a new feature that depends on being able to calculate the angle between the hands on a clock face. They’ve asked you to write some code to help out with that. This is an IOT project, and they have sensors emitting times at a pretty low frequency (about 10 a minute), and for some reason they need to be processed and stored as angles. +URL : +Output JSON : -You may need to make some assupmtions, that's OK, just document what they are and move on. +### Detail:- -The team loves innovation, so you can use whatever languages and technologies you like to complete this. Approach this problem as if your code will go to production. Whilst we don’t expect the code to be perfect, we are not looking for a hacked together script. +We need to calculate the angle between the hands on a clock face. For example input 03:00 would yield 90 degrees. -Your solution should offer the rest of the team a way to submit a time and receive an angle in return or store it somewhere. They are little fuzzy on the best way to get this low frequency data to your service, so if you can offer them any hints on that, they’d be really happy. +### Acceptance Criteria:- -## How to proceed +1) Code to perform the calculation + #### URL : http://127.0.0.1:5000//clock_angle/3:15 + #### Output : { + "context": "Angle for time 3:15 is 7.5 degree." + } -**Fork this repo, then get to work.** Remember that this is a DevOps team, so make sure your repo reflects that. Spend however much time you feel is reasonable. It doesn’t matter if the project is ‘done’, nothing ever is. **When you’re ready push your changes back to Github and put in a pull request back to the base repo.** +1) How will you deploy this solution (in code or as a todo list if time is limited). i.e. how and where will this run? +--------------------------------- +## Now Deploy the application on GCP +--------------------------------- -Be sure to add in instructions for how to deploy your solution, and document things in a way that the rest of the team can pick this up and run with it. Remember you have all the tools in the GCP arsenal at your disposal. +-On google cloud +---------------- + --create project -We are looking for you to demonstrate your abilities in software practices and DevOps, including reusability, portability, reliability, ease of maintenance etc. +-go to GC console generate ssh key +---------------------------------- + ---command to generate ssh key: ssh-keygen -t rsa -b 4096 -C "abhilash" + ---cat .ssh/id_rsa.pub -Think about how this will actually be deployed and maintained in the future as you build on it and expand it. You don’t have to implement deployment practices if you don’t have the time or resources, its ok to just document those. +-Add pub key to git hub account +------------------------------- ---- +-GC console: go into your project clone the repo +------------------------------------------------ +--run : git clone https://github.com/githubabhilash/clocks.git -## Product Backlog Item (Sprint Story) -Here is the story that is in the backlog. - -As with all stories, the team may have been optimistic with how much can be done in the time permitted. It's ok to meet some of the acceptance criteria by documenting what you would do in the next sprint! Prioritize your time and make sure you have some technical content to deliver. +-GC console:Now go to project folder +------------------------------------ +---run : gcloud app deploy +Note :It will ask region enter the numric value. -### Description:- -As a team
-We need a serivce that we can send a time value to and have it return or store an angle value
-So that we can use it in downstream processing +2) How will you manage any infrastructure needed? -### Detail:- +## Infrastructure needed +---------------------- + We will require GC resources based on the requirements + i. e. + - To run no of instances we will require no of CPU i. e. for now instances 1 , 1 cpu will be required. + - Memory will be required based on resource i.e memory_gb: 0.5 + - disk_size_gb: 10 -We need to calculate the angle between the hands on a clock face. For example input 03:00 would yield 90 degrees. -### Acceptance Criteria:- +3) Delivered as a feature branch in the repo fork + ## -- featureabhilash branch is created -1) Code to perform the calculation -1) How will you deploy this solution (in code or as a todo list if time is limited). i.e. how and where will this run? -1) How will you manage any infrastructure needed? -1) Delivered as a feature branch in the repo fork 1) Bonus points for a working deployed solution in GCP that you can demo at the "sprint review" (ie interview) + -- Deployed this service to on GCP and its running. 1) Any DevOps/Cicd components that would support this feature in a production setting diff --git a/app.yaml b/app.yaml index 84bd0c6..34740c7 100644 --- a/app.yaml +++ b/app.yaml @@ -1,6 +1,10 @@ -runtime: python +runtime: python37 env: flex entrypoint: gunicorn -b :$PORT:app -runtime_config: - python_version :3 \ No newline at end of file +#manual_scaling: +# instances: 1 +#resources: +# cpu: 1 +# memory_gb: 0.5 +# disk_size_gb: 10 \ No newline at end of file diff --git a/dsfs b/dsfs new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index c35bd3a..561834d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ Flask==1.1.2 -gunicorn==19.6.0 \ No newline at end of file +gunicorn==20.0.4 \ No newline at end of file From 02a91b5bfa033fbe9332c82061e64c074f905540 Mon Sep 17 00:00:00 2001 From: Mishra Date: Mon, 11 May 2020 04:04:46 +0530 Subject: [PATCH 3/6] removed unused file --- dsfs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 dsfs diff --git a/dsfs b/dsfs deleted file mode 100644 index e69de29..0000000 From 596b0cf16161ecf6a76b16553e6f3ff452053bd1 Mon Sep 17 00:00:00 2001 From: githubabhilash <31386175+githubabhilash@users.noreply.github.com> Date: Mon, 11 May 2020 04:08:08 +0530 Subject: [PATCH 4/6] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5c73f1..cc10499 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ We need to calculate the angle between the hands on a clock face. For example in ### Acceptance Criteria:- 1) Code to perform the calculation + #### URL : http://127.0.0.1:5000//clock_angle/3:15 #### Output : { "context": "Angle for time 3:15 is 7.5 degree." @@ -59,7 +60,8 @@ Note :It will ask region enter the numric value. ## Infrastructure needed ---------------------- - We will require GC resources based on the requirements + - Implemented this service by using flask framework so we will be required to install flask. + -We will require GC resources based on the requirements i. e. - To run no of instances we will require no of CPU i. e. for now instances 1 , 1 cpu will be required. - Memory will be required based on resource i.e memory_gb: 0.5 From cfbeeaf2412b5e7130abb3011b8d8b61aa2d899d Mon Sep 17 00:00:00 2001 From: githubabhilash <31386175+githubabhilash@users.noreply.github.com> Date: Mon, 11 May 2020 04:24:09 +0530 Subject: [PATCH 5/6] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cc10499..5198bf0 100644 --- a/README.md +++ b/README.md @@ -30,27 +30,27 @@ We need to calculate the angle between the hands on a clock face. For example in 1) How will you deploy this solution (in code or as a todo list if time is limited). i.e. how and where will this run? --------------------------------- -## Now Deploy the application on GCP +- Now Deploy the application on GCP --------------------------------- --On google cloud +- On google cloud ---------------- --create project --go to GC console generate ssh key +- go to GC console generate ssh key ---------------------------------- ---command to generate ssh key: ssh-keygen -t rsa -b 4096 -C "abhilash" ---cat .ssh/id_rsa.pub --Add pub key to git hub account +- Add pub key to git hub account ------------------------------- --GC console: go into your project clone the repo +- GC console: go into your project clone the repo ------------------------------------------------ --run : git clone https://github.com/githubabhilash/clocks.git --GC console:Now go to project folder +- GC console:Now go to project folder ------------------------------------ ---run : gcloud app deploy Note :It will ask region enter the numric value. @@ -58,7 +58,7 @@ Note :It will ask region enter the numric value. 2) How will you manage any infrastructure needed? -## Infrastructure needed +- Infrastructure needed ---------------------- - Implemented this service by using flask framework so we will be required to install flask. -We will require GC resources based on the requirements @@ -69,7 +69,7 @@ Note :It will ask region enter the numric value. 3) Delivered as a feature branch in the repo fork - ## -- featureabhilash branch is created + -- featureabhilash branch is created 1) Bonus points for a working deployed solution in GCP that you can demo at the "sprint review" (ie interview) -- Deployed this service to on GCP and its running. From 6ddba4b0b904ed74183bf28a1e09293ce0e37307 Mon Sep 17 00:00:00 2001 From: githubabhilash <31386175+githubabhilash@users.noreply.github.com> Date: Mon, 11 May 2020 04:36:41 +0530 Subject: [PATCH 6/6] Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5198bf0..244dd73 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ We need to calculate the angle between the hands on a clock face. For example in ### Acceptance Criteria:- 1) Code to perform the calculation - + #### URL : http://127.0.0.1:5000//clock_angle/3:15 #### Output : { "context": "Angle for time 3:15 is 7.5 degree." @@ -32,6 +32,9 @@ We need to calculate the angle between the hands on a clock face. For example in --------------------------------- - Now Deploy the application on GCP --------------------------------- +File description + ------------------------ + app.yaml is used to deploy application on GCP - On google cloud ---------------- @@ -60,6 +63,11 @@ Note :It will ask region enter the numric value. - Infrastructure needed ---------------------- + - Technology Stack + + 1. Python 3.7 + 2. Flask==1.1.2 + 3. GCP - Implemented this service by using flask framework so we will be required to install flask. -We will require GC resources based on the requirements i. e.