Coder Social home page Coder Social logo

krikeychallenge's Introduction

krikeyChallenge

Part 1: SQL Challenge

Write statements to answer each of the following questions using the following schema:

CREATE TABLE authors ( 
  id serial PRIMARY KEY, 
  name text, 
  date_of_birth timestamp 
); 
CREATE TABLE books ( 
  id serial PRIMARY KEY, 
  author_id integer REFERENCES authors (id), 
  isbn text
); 
CREATE TABLE sale_items ( 
  id serial PRIMARY KEY, 
  book_id integer REFERENCES books (id), 
  customer_name text, 
  item_price money, 
  quantity integer 
); 
  1. Who are the first 10 authors ordered by date_of_birth?
SELECT
  *
FROM authors
ORDER BY date_of_birth
LIMIT 10;

(as shown is ordered from youngest to oldest, update LIMIT 10 to LIMIT 10 ASC for oldest to youngest)

  1. What is the sales total for the author named “Lorelai Gilmore”?
SELECT
  SUM(sale_items.item_price * sale_items.quantity) as total_sales
FROM authors
LEFT JOIN books on books.author_id = authors.id
LEFT JOIN sale_items on sale_items.book_id = books.id
WHERE authors.name LIKE 'Lorelai Gilmore';
  1. What are the top 10 performing authors, ranked by sales revenue?
SELECT
  authors.id,
  authors.name,
  SUM(sale_items.item_price * sale_items.quantity) as total_sales
FROM authors
LEFT JOIN books on books.author_id = authors.id
LEFT JOIN sale_items on sale_items.book_id = books.id
GROUP BY authors.id
ORDER BY sale_total DESC
LIMIT 10;

Part 2A: Write an API Endpoint

In order to start the endpoint locally do the following

  1. Fork then clone the repo to your local computer
  2. Run docker-compose up

You will then be able to query the server using localhost:3000.

  • Use localhost:3000/author for a list of the top 10 best selling authors and their total sales.
  • Use localhost:3000/author?author_name=[author name] for the total sales of an author. Try using "Steven King" or "JK Rowling".

Part 2B: API Performance

In order to optimize performance I added a Node Cache caching system and indexes to the database. These indexes are in the schema.sql file and are automatically added to the database when it is created in step 2 of Part 2A.

  • The cache brings the response time down from around 100ms on the initial request to less than 10ms on subsequent requests.
  • I didn't test response times before adding the indexes to know their effect, but in past projects indexing improved query times dramatically.

Part 3: Build Docker Container and steps to deploy

Before this project I didn't know how to use Docker so this was a learning experience. I wasn't able to create an image that could be shared or deployed, but I came pretty close. I know that with more time I would have been able to create a proper docker image for this database and deploy it on an EC2 instance.

krikeychallenge's People

Contributors

gusfel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.