44
Designing the Call of Cthulhu app with Google App Engine Presented by Chris Bunch at UCSB CS 189A / 172 : Capstone Project / Software Engineering February 10, 2010 http://cs.ucsb.edu/~cgb

Designing the Call of Cthulhu app with Google App Engine

Embed Size (px)

DESCRIPTION

These are slides from a talk I gave at UCSB to the Senior Capstone class on 02/10/10 on how I developed the Call of Cthulhu application using Google App Engine.

Citation preview

Page 1: Designing the Call of Cthulhu app with Google App Engine

Designing the Call of Cthulhu app with

Google App EnginePresented by Chris Bunch at UCSB

CS 189A / 172 : Capstone Project / Software EngineeringFebruary 10, 2010

http://cs.ucsb.edu/~cgb

Page 2: Designing the Call of Cthulhu app with Google App Engine

Motivation

•I am what you would call a “nerd”

•Plays nerdy board games

•Would like to upload stats from games and analyze them

•Could do other cool things as well

Page 3: Designing the Call of Cthulhu app with Google App Engine

Specifically

Page 4: Designing the Call of Cthulhu app with Google App Engine

Arkham Horror

•Lots of variables that can be analyzed

•Co-op game: all v. the board

•Mix and match expansions

•Many different winning / losing conditions

•But fundamentally boils down to:

•Save the world from total destruction

Page 5: Designing the Call of Cthulhu app with Google App Engine

App Requirements

•Must be able to upload game data

•Should be able to predict a win or loss given some preliminary info

•Can experiment with prediction algorithms

•Should be able to summarize game data

Page 6: Designing the Call of Cthulhu app with Google App Engine

App Requirements•Allows users to log in to upload their

data

•Allows users to share data with each other

•Handles users in a sane fashion

•Harder than it sounds

•Display a top score list for users to view

•Game provides scoring metric

Page 7: Designing the Call of Cthulhu app with Google App Engine

Enter the Cloud

•Why go the cloud route?

•Simple answer: You likely don’t have your own hardware to host this app on, so just use somebody else’s

•But cloud computing is confusing and not well defined yet, so...

Page 8: Designing the Call of Cthulhu app with Google App Engine

Defining Cloud Computing

•Three layers, each building on the last

• Infrastructure:

•Programmer gets a virtual machine and / or reliable storage

•Most control of environment

•Must scale manually

•Amazon EC2, Eucalyptus

Page 9: Designing the Call of Cthulhu app with Google App Engine

Defining Cloud Computing

•Three layers, each building on the last

•Platform:

•Programmer gets a scalable API

•Less control of environment

•Scaling done automatically

•Google App Engine, Microsoft Azure

Page 10: Designing the Call of Cthulhu app with Google App Engine

Defining Cloud Computing

•Three layers, each building on the last

•Software:

•Programmer gets to “skin” an existing app

•No control of environment

•Scaling done automatically

•SalesForce, GMail

Page 11: Designing the Call of Cthulhu app with Google App Engine

My Choice•Clearly I want the platform!

•Platform:

•Scalable API = great!

•Less control of environment = ok

•Programability depends on APIs offered

•Have personal exp. w/ Google App Engine

Page 12: Designing the Call of Cthulhu app with Google App Engine

What is Google App Engine?

•Scalable web framework first offered in 2008

•Users program apps in:

•Python

•Java

•Unofficially: JRuby, Groovy, JavaScript

Page 13: Designing the Call of Cthulhu app with Google App Engine

Sandboxed Environment

•Requests limited to 30 seconds

•Only white-listed libraries can be used

•Anything non-trivial is quota-ed

•No reading / writing to the filesystem

•Web communication over HTTP(S) only

Page 14: Designing the Call of Cthulhu app with Google App Engine

Datastore API

•A giant hash table

•Primitives:

•Get, put, delete - same as regular HT

•Query, count - new operations

•Transactions supported (row-level only)

Page 15: Designing the Call of Cthulhu app with Google App Engine

Datastore API

•GQL queries offered - subset of SQL Select statement, no JOINs

•Tip: Queries are the most expensive operation, use sparingly

•If possible, perform it asynchronously

Page 16: Designing the Call of Cthulhu app with Google App Engine

Memcache API

•Equivalent to memcached

•Provides access to low latency LRU cache

•Roughly 100x faster than Datastore

•But unreliable - remember it’s a cache!

•Exposes get, set, delete operations

•Exposes atomic increment / decrement

Page 17: Designing the Call of Cthulhu app with Google App Engine

URL Fetch API•Equivalent to curl

•Used to grab web content from other sites

•Only allows for HTTP(S) data to be read

•Can also be done asynchronously

•Typically grabs images or mashup data

Page 18: Designing the Call of Cthulhu app with Google App Engine

Mail API

•Equivalent to sendmail

•Can send or receive mail asynchronously

•Can also add attachments

•Not all types allowed - no .doc, .xls, or .ppt in Python SDK

Page 19: Designing the Call of Cthulhu app with Google App Engine

XMPP API

•Equivalent to jabber

•Send and receive instant messages to

•appname.appspotchat.com

•App can’t join group chat yet

•Use as a notifier or alternate UI to app

Page 20: Designing the Call of Cthulhu app with Google App Engine

Images API

•Equivalent to python-imaging

•Exposes image rotation, flipping, cropping, and histogram

•Image is stored internally as a blob, then retrieved and manipulated

Page 21: Designing the Call of Cthulhu app with Google App Engine

Users API

•Equivalent to Google Accounts

•Provides for simple authentication

•Allows app to force login or admin on certain pages

•Also exposes URL to optionally login to

Page 22: Designing the Call of Cthulhu app with Google App Engine

Blobstore API

•Allows for large file uploads (<50 MB)

•Users can upload videos, datasets, or large music files for later retrieval

•Upload must be done via form post

Page 23: Designing the Call of Cthulhu app with Google App Engine

Cron API

•Equivalent to cron

•User specifies how often a web page should be accessed (e.g., daily, every Monday)

•Task is then run in the background

•Useful for running expensive data analysis

Page 24: Designing the Call of Cthulhu app with Google App Engine

Task Queue API

•Spawns a new thread in the background

•But still must visit your app

•Configuration file specifies queues

•Useful for short to medium length tasks

Page 25: Designing the Call of Cthulhu app with Google App Engine

To summarize:

•Many, many APIs available for what we need to do!

•Use Datastore to store / retrieve user data

•Use Users to authenticate users

•Gets around a lot of boilerplate authentication code

Page 26: Designing the Call of Cthulhu app with Google App Engine

To summarize:

•Use Mail to e-mail users if their score is beat

•Use XMPP to allow users to add new data

•Use Cron to run compute-intensive code in the background

Page 27: Designing the Call of Cthulhu app with Google App Engine

To summarize:

•Think about paid functionality:

•Maybe use Blobstore to allow users to upload videos explaining gameplay

•Handle greater traffic if site becomes popular

Page 28: Designing the Call of Cthulhu app with Google App Engine

Let’s Get to Business

•Data Modeling

•Upload Stats Page

•Game Stats Page

•Game Prediction Page

Page 29: Designing the Call of Cthulhu app with Google App Engine

Data Modeling•class Game(db.Model):

•player = db.UserProperty()

•goo = db.StringProperty()

•comments = dbStringProperty(multiline=True)

•date = db.DateTimeProperty(auto_now_add=True)

Page 30: Designing the Call of Cthulhu app with Google App Engine

Data Modeling

•num_of_players = db.IntegerProperty()

•won_game = db.BooleanProperty()

•doom_counters = db.IntegerProperty()

•terror_level = db.IntegerProperty()

•expansions_used = dbListProperty(str)

Page 31: Designing the Call of Cthulhu app with Google App Engine

Uploading Stats• class AddGames(webapp.RequestHandler):

• def get(self):

• user = users.get_current_user()

• if user: # means they’re logged in

• # write html form for getting data

• else:

• self.redirect(users.create_login_url(self.request.uri))

Page 32: Designing the Call of Cthulhu app with Google App Engine

Uploading Stats•def post(self):

•new_goo = self.request.get(‘goo’)

•# validate goo, make sure data type is right

•game = Game()

•game.goo = new_goo

•game.put()

Page 33: Designing the Call of Cthulhu app with Google App Engine

Game Stats

•games = db.GqlQuery (“SELECT * FROM Game ORDER BY date desc”)

•for game in games:

•# find the goo w/ highest player win percentage

•# can also just display the info here

Page 34: Designing the Call of Cthulhu app with Google App Engine

Game Prediction

•def get(self):

•# user specifies the game they will play

•def post(self):

•# calculate how likely they are to win

•# right now we only care about the goo

Page 35: Designing the Call of Cthulhu app with Google App Engine

Routing

•application = webapp.WSGIApplication([

•(‘/upload’, AddGames),

•(‘/stats’, GameStats),

•(‘/predict’, PredictGame)])

Page 36: Designing the Call of Cthulhu app with Google App Engine

Thankfully•Most requirements have very little

state shared between URLs (all saved in DB)

•Thus each class is one of two forms:

•get / post: Grab data from user and do something with it

•get: Just display or synthesize some data

Page 37: Designing the Call of Cthulhu app with Google App Engine

Rendering Web Pages

•Two main options available:

•self.response.out.write(“Hello World!”)

•self.response.out.write(template.render(‘index.html’, {‘my_title: ‘baz’, ‘data’: some_variable}))

•Use the second for any non-trivial app

Page 38: Designing the Call of Cthulhu app with Google App Engine

Rendering Web Pages

•Now in your index.html:

•<title> {{ my_title }} </title>

•<div> {{ data }} </div>

Page 39: Designing the Call of Cthulhu app with Google App Engine

Tips for Your Programs

•Separate out your code into files as needed

•app.yaml specifies this:

•handlers:

• - url: /one

• script: one.py

• - url: /two/(.*)/(.*)

• script: /two/\2/\1.py

Page 40: Designing the Call of Cthulhu app with Google App Engine

Tips for Your Programs

•Make pages with sensitive data ‘secure’

•All access done over HTTPS

•Optional - HTTP and HTTPS work

•Never - HTTPS redirects to HTTP

•Always - HTTP redirects to HTTPS

Page 41: Designing the Call of Cthulhu app with Google App Engine

Tips for Your Programs

•Specify static files in app.yaml

•- url: /images

• static_dir: static/images

•Can also tag ‘secure’ if desired

Page 42: Designing the Call of Cthulhu app with Google App Engine

Tips for Your Programs

•Be creative!

•The webapp framework supports:

•get, post, head, options, put, delete, trace

•Map user pages to ~name?

Page 43: Designing the Call of Cthulhu app with Google App Engine

Summary

•Google App Engine is more than flexible enough to handle your web app desires

•And it’s only getting better!

•More than enough possibilities to create a web app: you just need a good idea!

Page 44: Designing the Call of Cthulhu app with Google App Engine