Skip to content

CSCI-GA-2820-FA22-001/recommendations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NYU DevOps Project Template

License Python

Build Status codecov BDD Tests

Development Site : http://169.51.203.42:31001 Production Site : http://169.51.203.42:31002

Prerequisite Software Installation

This project uses Docker and Visual Studio Code with the Remote Containers extension to provide a consistent repeatable disposable development environment for all developers.

You will need the following software installed:

All of these can be installed manually by clicking on the links above or you can use a package manager like Homebrew on Mac of Chocolatey on Windows.

Alternately, you can use Vagrant and VirtualBox to create a consistent development environment in a virtual machine (VM).

You can read more about creating these environments in Professor's article: Creating Reproducable Development Environments

Bring up the development environment

To bring up the development environment you should clone this repo, change into the repo directory:

$ git clone https://github.com/DevOps-Recommendations-Squad/recommendations.git
$ cd recommendations

Depending on which development environment you created, pick from the following:

Start developing with Visual Studio Code and Docker

Open Visual Studio Code using the code . command. VS Code will prompt you to reopen in a container and you should say yes. This will take a while as it builds the Docker image and creates a container from it to develop in.

$ code .

Note that there is a period . after the code command. This tells Visual Studio Code to open the editor and load the current folder of files.

Once the environment is loaded you should be placed at a bash prompt in the /app folder inside of the development container. This folder is mounted to the current working directory of your repository on your computer. This means that any file you edit while inside of the /app folder in the container is actually being edited on your computer. You can then commit your changes to git from either inside or outside of the container.

Using Vagrant and VirtualBox

Bring up the virtual machine using Vagrant.

$ vagrant up
$ vagrant ssh
$ cd /vagrant

This will place you in the virtual machine in the /vagrant folder which has been shared with your computer so that your source files can be edited outside of the VM and run inside of the VM.

Running the tests

As developers we always want to run the tests before we change any code. That way we know if we broke the code or if someone before us did. Always run the test cases first!

Run the tests using nosetests

$ nosetests

Nose is configured via the included setup.cfg file to automatically include the flags --with-spec --spec-color so that red-green-refactor is meaningful. If you are in a command shell that supports colors, passing tests will be green while failing tests will be red.

Nose is also configured to automatically run the coverage tool and you should see a percentage-of-coverage report at the end of your tests. If you want to see what lines of code were not tested use:

$ coverage report -m

This is particularly useful because it reports the line numbers for the code that have not been covered so you know which lines you want to target with new test cases to get higher code coverage.

You can also manually run nosetests with coverage (but setup.cfg does this already)

$ nosetests --with-coverage --cover-package=service

Try and get as close to 100% coverage as you can.

It's also a good idea to make sure that your Python code follows the PEP8 standard. Both flake8 and pylint have been included in the requirements.txt file so that you can check if your code is compliant like this:

$ flake8 . --count --max-complexity=10 --max-line-length=127 --statistics
$ pylint service tests --max-line-length=127

Visual Studio Code is configured to use pylint while you are editing. This catches a lot of errors while you code that would normally be caught at runtime. It's a good idea to always code with pylint active.

Running the service

The project uses honcho which gets it's commands from the Procfile. To start the service simply use:

$ honcho start

You should be able to reach the service at: http://localhost:8080. The port that is used is controlled by an environment variable defined in the .flaskenv file which Flask uses to load it's configuration from the environment by default.

Shutdown development environment

If you are using Visual Studio Code with Docker, simply existing Visual Studio Code will stop the docker containers. They will start up again the next time you need to develop as long as you don't manually delete them.

If you are using Vagrant and VirtualBox, when you are done, you can exit and shut down the vm with:

$ exit
$ vagrant halt

If the VM is no longer needed you can remove it with:

$ vagrant destroy

What's featured in the project?

* app/routes.py -- the main Service routes using Python Flask
* app/models.py -- the data model using SQLAlchemy
* tests/test_routes.py -- test cases against the Recommendations service
* tests/test_models.py -- test cases against the Recommendations model

APIs

All APIs are have common route prefix

http://localhost:8080/recommendations

CREATE

This API creates recommendations.

POST /
Request
{
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
}
Response
{
  "id": 140,
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
}

READ

This API is used to fetch recommendation by id.

GET /:id

where "id" is the id of the recommendation. Here is sample response.

Response
{
  "id": 140,
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
}

UPDATE

This API is used to update recommendations.

PUT /:id

where "id" is the id of the recommendation. Here is sample request and response.

Request
{
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
}
Response
{
  "id": 140,
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
}

In the above API "number_of_likes" is changed in DB.

DELETE

This API is used to delete recommendation by id.

DELETE /:id

where "id" is the id of the recommendation.

LIST

This API is used to fetch all recommendations.

GET /
Response
[{
  "id": 140,
  "name": "prodA",
  "number_of_likes": 3,
  "recommendation_id": 2,
  "recommendation_name": "prodB",
  "type": "UPSELL"
},
{
  "id": 141,
  "name": "prodA",
  "number_of_likes": 4,
  "recommendation_id": 3,
  "recommendation_name": "prodC",
  "type": "UPSELL"
}
]

License

Copyright (c) John Rofrano. All rights reserved.

Licensed under the Apache License. See LICENSE

This repository is part of the NYU masters class: CSCI-GA.2820-001 DevOps and Agile Methodologies created and taught by John Rofrano, Adjunct Instructor, NYU Courant Institute, Graduate Division, Computer Science, and NYU Stern School of Business.