<s> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> import datetime <EOL> import logging <EOL> from google . appengine . api import memcache <EOL> from google . appengine . api import users <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import template <EOL> from google . appengine . ext . webapp import util <EOL> class Component ( db . Model ) : <EOL> name = db . StringProperty ( ) <EOL> ip = db . StringProperty ( ) <EOL> class Log ( db . Model ) : <EOL> component = db . ReferenceProperty ( Component ) <EOL> text = db . TextProperty ( ) <EOL> timestamp = db . FloatProperty ( ) <EOL> class LogHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> if not users . is_current_user_admin ( ) : <EOL> response = json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . response . out . write ( response ) <EOL> return <EOL> str_payload = self . request . get ( '<STR_LIT>' ) <EOL> payload = json . loads ( str_payload ) <EOL> name = payload [ '<STR_LIT:name>' ] <EOL> ip = payload [ '<STR_LIT>' ] <EOL> name_and_ip = name + ip <EOL> earliest_time = <NUM_LIT:0.0> <EOL> if '<STR_LIT>' in payload : <EOL> earliest_time = payload [ '<STR_LIT>' ] <EOL> key = name + ip + str ( earliest_time ) <EOL> memcache_data = memcache . get ( key ) <EOL> if memcache_data is not None : <EOL> self . response . out . write ( memcache_data ) <EOL> return <EOL> component = Component . get_by_key_name ( name_and_ip ) <EOL> logs_to_return = [ ] <EOL> logs = db . GqlQuery ( "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> component , earliest_time ) <EOL> for log in logs : <EOL> item = { '<STR_LIT:text>' : log . text , <EOL> '<STR_LIT>' : log . timestamp } <EOL> logs_to_return . append ( item ) <EOL> last_log_index = len ( logs_to_return ) - <NUM_LIT:1> <EOL> log_data = { '<STR_LIT:success>' : True , '<STR_LIT>' : logs_to_return } <EOL> if last_log_index < <NUM_LIT:0> : <EOL> log_data [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> else : <EOL> log_data [ '<STR_LIT>' ] = logs_to_return [ last_log_index ] [ '<STR_LIT>' ] <EOL> dumped_log_data = json . dumps ( log_data ) <EOL> memcache . set ( key , dumped_log_data , <NUM_LIT:30> ) <EOL> self . response . out . write ( dumped_log_data ) <EOL> def post ( self ) : <EOL> str_payload = self . request . get ( '<STR_LIT>' ) <EOL> payload = json . loads ( str_payload ) <EOL> name = payload [ '<STR_LIT:name>' ] <EOL> ip = payload [ '<STR_LIT>' ] <EOL> name_and_ip = name + ip <EOL> component = Component . get_by_key_name ( name_and_ip ) <EOL> logs = payload [ '<STR_LIT>' ] <EOL> for log in logs : <EOL> new_log = Log ( component = component , <EOL> text = log [ '<STR_LIT:text>' ] , <EOL> timestamp = float ( log [ '<STR_LIT>' ] ) ) <EOL> new_log . put ( ) <EOL> response = { "<STR_LIT:success>" : True } <EOL> self . response . out . write ( json . dumps ( response ) ) <EOL> class ComponentHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> if not users . is_current_user_admin ( ) : <EOL> response = json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . response . out . write ( response ) <EOL> return <EOL> component_list = [ ] <EOL> components = db . GqlQuery ( "<STR_LIT>" ) <EOL> for component in components : <EOL> item = { '<STR_LIT:success>' : True , "<STR_LIT:name>" : component . name , "<STR_LIT>" : component . ip } <EOL> component_list . append ( item ) <EOL> self . response . out . write ( json . dumps ( component_list ) ) <EOL> def post ( self ) : <EOL> str_payload = self . request . get ( '<STR_LIT>' ) <EOL> payload = json . loads ( str_payload ) <EOL> components = payload [ '<STR_LIT>' ] <EOL> for component in components : <EOL> name = component [ '<STR_LIT:name>' ] <EOL> ip = component [ '<STR_LIT>' ] <EOL> name_and_ip = name + ip <EOL> new_component = Component ( key_name = name_and_ip ) <EOL> new_component . name = name <EOL> new_component . ip = ip <EOL> new_component . put ( ) <EOL> response = { "<STR_LIT:success>" : True } <EOL> self . response . out . write ( json . dumps ( response ) ) <EOL> class UserInfoHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> response = { } <EOL> user = users . get_current_user ( ) <EOL> if user : <EOL> response = { '<STR_LIT:user>' : user . nickname ( ) , '<STR_LIT>' : True } <EOL> else : <EOL> response = { '<STR_LIT:user>' : None , '<STR_LIT>' : False } <EOL> self . response . out . write ( json . dumps ( response ) ) <EOL> class IndexPage ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> if not users . is_current_user_admin ( ) : <EOL> response = json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . response . out . write ( response ) <EOL> return <EOL> self . response . out . write ( template . render ( '<STR_LIT>' , { } ) ) <EOL> def main ( ) : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> application = webapp . WSGIApplication ( [ ( '<STR_LIT>' , LogHandler ) , <EOL> ( '<STR_LIT>' , ComponentHandler ) , <EOL> ( '<STR_LIT>' , UserInfoHandler ) , <EOL> ( '<STR_LIT:/>' , IndexPage ) , <EOL> ] , <EOL> debug = True ) <EOL> util . run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
