Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed some minor Backend Issues. Adjusted API to Erics proposals

  • Loading branch information...
commit 833b44853adf850ce15c37037c0e19ef757e4fdd 1 parent 92de2f0
Johan Uhle authored
View
36 api.py
@@ -53,7 +53,7 @@ def add_to_track_array(track, track_array):
"""
location_dict = create_location_dict(track.user.location)
- user_dict = {'user_id': track.user.user_id,
+ user_dict = {'id': track.user.user_id,
'permalink': track.user.permalink,
'permalink_url': track.user.permalink_url,
'username': track.user.username,
@@ -61,7 +61,7 @@ def add_to_track_array(track, track_array):
'avatar_url': track.user.avatar_url}
track_array.append({ 'error': False,
- 'track_id': track.track_id,
+ 'id': track.track_id,
'permalink': track.permalink,
'permalink_url': track.permalink_url,
'title': track.title,
@@ -163,15 +163,15 @@ def get(self, track_id=None):
if not location:
error_response(self, 'location_not_found', 'The location with the id %s is not in the datastore.' % self.request.get('location'))
else:
- users = models.User.all().filter('location', location.key()).fetch(limit, offset)
- if users:
- for user in users:
- tracks = models.Track.all().filter('user', user.key()).fetch(limit, offset)
- for track in tracks:
- add_to_track_array(track, track_array)
+ users = db.Query(models.User, keys_only=True).filter('location', location.key()).order('-__key__').fetch(1000)
+ tracks = models.Track.all().filter('user IN', users).order('-entry_created_at').fetch(limit, offset)
+ if tracks:
+ for track in tracks:
+ add_to_track_array(track, track_array)
self.response.out.write(json.dumps(track_array))
else:
- error_response(self, 'no_tracks_in_location', 'There are no tracks at the location %s in the datastore.' % self.request.get('location'))
+ error_response(self, 'no_tracks_in_location', 'There are no tracks at the location %s with limit %i and offset %s in the datastore.' % \
+ (self.request.get('location'), limit, offset))
return
# Processing for api/tracks/?location=location_id&genre={genre_name}
@@ -196,7 +196,7 @@ def get(self, track_id=None):
error_response(self, 'no_tracks_in_location', 'There are no tracks at the location %s in the datastore.' % self.request.get('location'))
return
- # Processing for api/tracks/ and api/tracks/?genre=all
+ # Processing for api/tracks and api/tracks/?genre=all
if (self.request.get('genre') == 'all' or not self.request.get('genre')) and not \
(self.request.get('location') or self.request.get('location_lat') or self.request.get('location_lon')):
tracks = models.Track.all().order('-created_at').fetch(limit, offset)
@@ -222,13 +222,12 @@ def get(self, track_id=None):
class LocationsHandler(webapp.RequestHandler):
"""
- Fetching tracks. Returning json
+ Fetching latest updated locations. Returning json
"""
def get(self):
# initialization
locations_array = []
- genre = self.request.get('genre')
-
+ genre = self.request.get('genre')
if self.request.get('limit'):
limit = int(self.request.get('limit'))
else:
@@ -240,7 +239,8 @@ def get(self):
if self.request.get('location'):
return fetch_location_by_id(self, self.request.get('location'))
- if genre:
+ # Processing latest locations for a certain genre api/locations/?genre={genre_name}
+ if genre and genre != 'all':
if genre not in utils.genres:
error_response(self, 'unknown_genre', 'Sorry, but we do not know the genre %s.' % genre)
return
@@ -251,7 +251,8 @@ def get(self):
self.response.out.write(json.dumps(locations_array))
return
- if not genre:
+ # Processing latest locations for api/locations
+ if not genre or genre == 'all':
locations = models.Location.all().order('-last_time_updated').fetch(limit, offset)
if locations:
for location in locations:
@@ -260,6 +261,7 @@ def get(self):
else:
error_response(self, 'no_locations', 'There are no locations in the datastore.')
return
+
class LocationIDHandler(webapp.RequestHandler):
"""
@@ -275,9 +277,9 @@ def get(self, location_id=None):
def main():
application = webapp.WSGIApplication([(r'/api/tracks/([0-9]{1,64})', TrackIDHandler),
- ('/api/tracks/.*', TracksHandler),
+ ('/api/tracks.*', TracksHandler),
(r'/api/locations/([0-9]{1,64})', LocationIDHandler),
- ('/api/locations/.*', LocationsHandler),], debug=utils.in_development_enviroment())
+ ('/api/locations.*', LocationsHandler),], debug=utils.in_development_enviroment())
run_wsgi_app(application)
if __name__ == '__main__':
View
7 backend_update.py
@@ -29,7 +29,8 @@
import os
import models
-import backend_utils
+import backend_utils
+import settings
def main():
"""
@@ -47,8 +48,8 @@ def main():
counter = 0
for track in tracks:
track['id'] = unicode(track['id'])
- if memcache.add(track['id'], track, time=1800, namespace="backend_update_track"):
- taskqueue.add(url='/backend-update/track', params={'track_id': track['id']})
+ if memcache.add(track['id'], track, time=settings.TRACK_BACKEND_UPDATE_LIFETIME, namespace="backend_update_track"):
+ taskqueue.add(url='/backend-update/track', params={'track_id': track['id'], 'time_track_added_to_queue': str(int(time.time()))})
logging.info("Added track_id %s to memcache and traskqueue." % track['id'])
counter += 1
else:
View
13 backend_update_track.py
@@ -32,7 +32,8 @@
import datetime
import models
-import backend_utils
+import backend_utils
+import settings
class FetchTrackInfo(webapp.RequestHandler):
@@ -45,8 +46,16 @@ def post(self):
logging.info("Working the taskqueue. Fetching a new track.")
try:
+ # check, if track is not already overdue (which may happen, if memcache fails, which happens from time to time)
+ if (self.request.get('time_track_added_to_queue') == '' or \
+ time.time() > (int(self.request.get('time_track_added_to_queue')) + settings.TRACK_BACKEND_UPDATE_LIFETIME * 60)):
+ # track is overdue
+ logging.info("Track with Track ID %s is overdue with time %s." % ((self.request.get('track_id') or ''), (self.request.get('time_track_added_to_queue') or '')))
+ self.response.out.write("done") # finished processing script
+ return # return 200. task gets deleted from task queue
+
# fetch track info from memcache
- track_id = self.request.get('track_id')
+ track_id = self.request.get('track_id')
track = memcache.get(track_id, namespace="backend_update_track")
if track is None:
logging.error("Fetching memcache item %s failed in backend track update" % track_id)
View
5 backend_utils.py
@@ -81,8 +81,9 @@ def update_location_genre_data(track, location):
# determine genre for track
track_genre = None
for genre in utils.genres.iteritems():
- if track['genre'].strip().lower() in genre[1]: track_genre = genre[0]
- # update LocationGenreLastUpdate
+ if track['genre'] and track['genre'].strip().lower() in genre[1]:
+ track_genre = genre[0]
+ # update LocationGenreLastUpdate
if track_genre:
location_genre = models.LocationGenreLastUpdate.all().filter('location', location.key()).filter('genre', track_genre).get()
if location_genre:
View
15 settings.py
@@ -8,12 +8,11 @@
SOUNDCLOUD_TIMEZONE_ADJUSTMENT = 0 # in hours behind server timezone
DURATION_LIMIT = "1200000" # in milliseconds to filter out dj-sets + podcasts
-# How many tracks shall be displayed in FrontEnd?
-FRONTEND_TRACKS_LIMIT = 200
-
-# How old shall the oldest track in the cache be?
-CLEANUP_INTERVAL = 1440 # in minutes
-
-# In which Interval the backend scripts /backend-update is called
-API_QUERY_INTERVAL = 3 # in minutes
+# Frontend
+FRONTEND_TRACKS_LIMIT = 200 # How many tracks shall be displayed as default in FrontEnd?
+
+# Backend
+CLEANUP_INTERVAL = 1440 # How old in minutes shall the oldest track in the cache be?
+API_QUERY_INTERVAL = 3 # In which Interval in minutes is the backend scripts /backend-update called?
+TRACK_BACKEND_UPDATE_LIFETIME = 3600 # How long in minutes shall a track remain in Taskqueue/Memcache before purged if not copmpleted?
Please sign in to comment.
Something went wrong with that request. Please try again.