示例#1
0
    def test_get(self):
        request, response = new_mock_request_response(self.m)
        self.m.StubOutWithMock(ArtifactSourceAccessor, "find_artifact_counts")
        self.m.StubOutWithMock(ArtifactSourceAccessor, "find_artifact_counts_newer")
        self.m.StubOutWithMock(memcache, "get_stats")
        self.m.StubOutWithMock(ConfigurationAccessor, "get_or_create")
        
        ArtifactSourceAccessor.find_artifact_counts().AndReturn({})
        ArtifactSourceAccessor.find_artifact_counts_newer(IgnoreArg()).AndReturn({})
        memcache.get_stats().AndReturn({})
        
        # token = "oauth_token_secret=hi&oauth_token=hi"
        # config = MockEntity(key_name="hi", twitter_oauth_enabled=True, twitter_access_token=token, twitter_read_only=False)
        # ConfigurationAccessor.get_or_create().AndReturn(config)

        api = new_mock_twitter_api(self.m)
        api.GetUserTimeline(count=IgnoreArg()).AndReturn(())
        api.GetDirectMessages(since=IgnoreArg()).AndReturn(())
        api.GetReplies(since=IgnoreArg()).AndReturn(())
                    
        handler = StatsHandler()
        handler.initialize(request, response)
        
        self.m.ReplayAll()
        handler.get()
        self.m.VerifyAll()
示例#2
0
  def test_about_page_caches_response(self):
    self.assertEqual(0, memcache.get_stats()['items'])

    response = self.app.get(self.uri_for('about'))
    self.assertEqual(1, memcache.get_stats()['items'])
    self.assertEqual(0, memcache.get_stats()['hits'])
    self.assertEqual(1, memcache.get_stats()['misses'])

    response = self.app.get(self.uri_for('about'))
    self.assertOk(response)
    self.assertEqual(1, memcache.get_stats()['hits'])
示例#3
0
    def test_about_page_caches_response(self):
        self.assertEqual(0, memcache.get_stats()['items'])

        response = self.app.get(self.uri_for('about'))
        self.assertEqual(1, memcache.get_stats()['items'])
        self.assertEqual(0, memcache.get_stats()['hits'])
        self.assertEqual(1, memcache.get_stats()['misses'])

        response = self.app.get(self.uri_for('about'))
        self.assertOk(response)
        self.assertEqual(1, memcache.get_stats()['hits'])
示例#4
0
  def test_home_page_uses_cache(self):
    # Cache should start empty.
    self.assertEqual(0, memcache.get_stats()['items'])

    # After we retrieve the homepage, the cache should be primed
    response = self.app.get(self.uri_for('home'))
    self.assertEqual(1, memcache.get_stats()['items'])

    # If we get the home page again, we should have a single hit
    response = self.app.get(self.uri_for('home'))
    self.assertOk(response)
    self.assertEqual(1, memcache.get_stats()['hits'])
 def testMemcacheDecorator1(self):
     """Tests basic functionality of Memcache Decorator."""
     Story(title="Harry Potter", difficulty="Intermediate").put()
     Story(title="A korhazban", difficulty="Beginner").put()
     
     @memcached(memcache_key="most_recent_stories")
     def most_recent_stories():
         return [s for s in Story.most_recent().run(limit=10)]
     
     stories = most_recent_stories() # miss 1, sets with default key
     
     self.assertEqual(["A korhazban", "Harry Potter"], [s.title for s in stories])
     self.assertEqual(2, len(stories))
     retrieved_stories = memcache.get('most_recent_stories') # hit 1
     self.assertEqual(2, len(retrieved_stories))
     self.assertEqual([stories[i].title for i in xrange(2)], [retrieved_stories[i].title for i in xrange(2)])
     self.assertEqual([stories[i].difficulty for i in xrange(2)],
                     [retrieved_stories[i].difficulty for i in xrange(2)])
     
     for i in xrange(3):
         stories = most_recent_stories() #hit 2, 3, 4
     
     stats = memcache.get_stats()
     self.assertEqual(4, stats['hits'] )
     self.assertEqual(1, stats['misses'])
     
     
     # trying out a new key
     Story(title="A new title", difficulty="Advanced").put()
     Story(title="Yet another story", difficulty="Beginner").put()
     
     stories2 = most_recent_stories(memcache_key="whatever I want") # miss 2, sets with new key
     retrieved_stories2 = memcache.get('whatever I want') # hit 5
     self.assertEqual(4, len(stories2))
     self.assertEqual(4, len(retrieved_stories2))
     
     stats = memcache.get_stats()
     stories3 = memcache.get('most_recent_stories') # hit 6
     self.assertEqual(2, len(stories3))
     self.assertEqual(2, stats['misses'])
     
     s4 = most_recent_stories(memcache_key="whatever I want") # hit 7
     self.assertEqual(4, len(s4))
     self.assertEqual(s4[0].title, "Yet another story")
     
     s5 = most_recent_stories(memcache_key="most_recent_stories", update=True) # hit 8
     s6 = memcache.get('most_recent_stories') # hit 9
     self.assertEqual(4, len(s5))
     self.assertEqual(4, len(s6))
     
     stats = memcache.get_stats()
     self.assertEqual(9, stats['hits'])
     self.assertEqual(2, stats['misses'])
示例#6
0
 def get(self):
   requestParams = FeedBusterUtils.getRequestParams(self.request.query_string, ["cacheId"])
   if requestParams.has_key("cacheId"):
     result = str(memcache.delete(requestParams["cacheId"]))
     result += "\n"
     result += str(memcache.get_stats())
     return result
   else:
     result = str(memcache.flush_all())
     result += "\n"
     result += str(memcache.get_stats())
     return result
示例#7
0
    def test_home_page_uses_cache(self):
        # Cache should start empty.
        self.assertEqual(0, memcache.get_stats()['items'])

        # After we retrieve the homepage, the cache should be primed
        response = self.app.get(self.uri_for('home'))
        self.assertEqual(1, memcache.get_stats()['items'])

        # If we get the home page again, we should have a single hit
        response = self.app.get(self.uri_for('home'))
        self.assertOk(response)
        self.assertEqual(1, memcache.get_stats()['hits'])
示例#8
0
    def testStatsAfterFlush(self):
        """Asserts that the stats are reset after a flush."""

        self.assertTrue(memcache.set(self.key1, self.value1))
        self.assertTrue(memcache.set(self.key2, self.value2))

        self.assertEqual(self.value1, memcache.get(self.key1))
        self.assertEqual(self.value2, memcache.get(self.key2))
        self.assertLen(memcache.get_multi([self.key1, self.key2]), 2)

        self.assertEqual(None, memcache.get(self.key3))
        self.assertEqual(None, memcache.get('unknown'))
        self.assertEqual(None, memcache.get('another not known'))
        self.assertEmpty(memcache.get_multi(['what', 'who']))

        self._StepClock(7)

        result = memcache.get_stats()

        expected = {
            memcache.STAT_HITS:
            4,
            memcache.STAT_MISSES:
            5,
            memcache.STAT_BYTE_HITS:
            (2 * (len(self.key1) + len(self.value1) + len(self.key2_hash) +
                  len(self.value2))),
            memcache.STAT_ITEMS:
            2,
            memcache.STAT_BYTES: (len(self.key1) + len(self.value1) +
                                  len(self.key2_hash) + len(self.value2)),
            memcache.STAT_OLDEST_ITEM_AGES:
            7,
        }

        self.assertEqual(expected, result)

        self.assertTrue(memcache.flush_all())
        result = memcache.get_stats()
        expected = {
            memcache.STAT_HITS: 0,
            memcache.STAT_MISSES: 0,
            memcache.STAT_BYTE_HITS: 0,
            memcache.STAT_ITEMS: 0,
            memcache.STAT_BYTES: 0,
            memcache.STAT_OLDEST_ITEM_AGES: 0,
        }
        self.assertEqual(expected, result)
示例#9
0
    def get(self):
        now = datetime.datetime.now()
        now_time_t = int(time.mktime(now.timetuple()))
        is_enabled = CapabilitySet('memcache').is_enabled()
        memcache_stats = memcache.get_stats()

        if self.request.get('output') in ('text', 'txt'):
            self.response.out.write(now_time_t)
            self.response.out.write(' up' if is_enabled else ' down')
            self.response.out.write(' h:%(hits)s'
                                    ' m:%(misses)s'
                                    ' bh:%(byte_hits)s'
                                    ' i:%(items)s'
                                    ' b:%(bytes)s'
                                    ' oia:%(oldest_item_age)s'
                                    '\n' % memcache_stats)
            self.response.headers['Content-Type'] = "text/text"
        else:
            template_values = {
                'now': now.ctime(),
                'now_time_t': now_time_t,
                'is_enabled': is_enabled,
                'memcache_stats': memcache_stats,
            }
            self.render_jinja2_template("memcache_stats.html", template_values)
示例#10
0
 def get(self):
     self.sess = sessions.Session(writer="cookie")
     if self.request.get('deleteSession') == "true":
         self.sess.delete()
         print "Location: /cookiesession\n\n"
     elif self.request.get('setflash') == "true":
         self.sess[
             'flash'] = 'You set a flash message! <a href="/cookiesession">Refresh this page</a> and this message is gone!'
         print "Location: /cookiesession\n\n"
     else:
         keyname = 'testKey'
         self.sess[keyname] = "test"
         self.sess[keyname + '2'] = "test2"
         self.sess[3] = "test3"
         if not 'viewCount' in self.sess:
             self.sess['viewCount'] = 1
         else:
             self.sess['viewCount'] = int(self.sess['viewCount']) + 1
         self.sess[u"unicode_key"] = u"unicode_value"
         session_length = len(self.sess)
         self.memcacheStats = memcache.get_stats()
         template_values = {
             'sess': self.sess,
             'sess_str': str(self.sess),
             'session_length': session_length,
             'memcacheStats': self.memcacheStats
         }
         path = os.path.join(os.path.dirname(__file__),
                             'templates/cookie_session-new.html')
         self.response.out.write(template.render(path, template_values))
示例#11
0
 def get(self):
     self.cache = cache.Cache()
     # test deleting a cache object
     del self.cache["sampleStr"]
     # set a string
     if not "sampleStr" in self.cache:
         self.cache["sampleStr"] = "This is a string passed to the cache"
     # store an object
     if not "sampleObj" in self.cache:
         self.cache["sampleObj"] = [
             "this was set up as a list to test object caching"
         ]
     keyname = 'dynamic' + 'key'
     if not keyname in self.cache:
         self.cache[keyname] = 'this is a dynamically created keyname'
     self.memcacheStats = memcache.get_stats()
     template_values = {
         'cacheItemStr': self.cache["sampleStr"],
         'cacheItemObj': self.cache["sampleObj"],
         'dynamickey': self.cache["dynamickey"],
         'memcacheStats': self.memcacheStats,
     }
     path = os.path.join(os.path.dirname(__file__),
                         'templates/cache-new.html')
     self.response.out.write(template.render(path, template_values))
示例#12
0
 def get(self):
   self.sess = sessions.Session(writer="cookie")
   if self.request.get('deleteSession') == "true":
       self.sess.delete()
       print "Location: /cookiesession\n\n"
   elif self.request.get('setflash') == "true":
       self.sess['flash'] = 'You set a flash message! <a href="/cookiesession">Refresh this page</a> and this message is gone!'
       print "Location: /cookiesession\n\n"
   else:
       keyname = 'testKey'
       self.sess[keyname] = "test"
       self.sess[keyname + '2'] = "test2"
       self.sess[3] = "test3"
       if not 'viewCount' in self.sess:
           self.sess['viewCount'] = 1
       else:
           self.sess['viewCount'] = int(self.sess['viewCount']) + 1
       self.sess[u"unicode_key"] = u"unicode_value"
       session_length = len(self.sess)
       self.memcacheStats = memcache.get_stats()
       template_values = {
           'sess': self.sess,
           'sess_str': str(self.sess),
           'session_length': session_length,
           'memcacheStats': self.memcacheStats
       }
       path = os.path.join(os.path.dirname(__file__), 'templates/cookie_session-new.html')
       self.response.out.write(template.render(path, template_values))
示例#13
0
 def GET(self):
     result = {}
     action = web.input(action = None)['action']
     
     if action=='quota': 
         results = urlfetch.fetch('https://api.stackexchange.com/2.0/info?site=stackoverflow&key=%s' % api_key, headers = {'User-Agent': 'StackPrinter'}, deadline = 10)
         response = simplejson.loads(results.content)
         result['result'] = response
     if action=='quotaauth': 
         results = urlfetch.fetch('https://api.stackexchange.com/2.0/info?site=stackoverflow&key=%s&access_token=%s' % (api_key, TokenManager.get_auth_token()), headers = {'User-Agent': 'StackPrinter'}, deadline = 10)
         response = simplejson.loads(results.content)
         result['result'] = response
     if action=='authkey': 
         result['result'] = TokenManager.get_auth_token()
     elif action =='memcachestats':
         result = memcache.get_stats()        
     elif action =='memcacheflush':
         result['result'] = memcache.flush_all()
     elif action =='normalize':
         deferred.defer(worker.deferred_normalize_printed_question)    
         result['result'] = True
     elif action =='delete':
         service = web.input(service = None)['service']
         question_id = web.input(question_id = None)['question_id']
         result['printed_question_deletion'] = dbquestion.delete_printed_question(question_id,service)
         result['question_deletion'] = dbquestion.delete_question(question_id,service)
         result['answers_deletion'] = dbquestion.delete_answers(question_id,service)
     return render.admin(result)
示例#14
0
    def get(self):

		guestbook_name = self.request.get('guestbook_name')
		
		guestbook_entries = self.get_guestbook_entries(guestbook_name)
		stats = memcache.get_stats()
		
		user = users.get_current_user()
		if user:
		    url = users.create_logout_url(self.request.uri)
		    url_linktext = 'Logout'
		else:
		    url = users.create_login_url(self.request.uri)
		    url_linktext = 'Login'
		
		template_values = {
		    'user': user,
		    'guestbook_entries': guestbook_entries,
		    'guestbook_name': urllib.quote_plus(guestbook_name),
		    'url': url,
		    'url_linktext': url_linktext,
		    'cache_hits' : stats['hits'],
		    'cache_miss' : stats['misses'],
		}
		
		template = JINJA_ENVIRONMENT.get_template('index.html')
		self.response.write(template.render(template_values))
示例#15
0
  def get(self):

    flush_cache = urllib.unquote(cgi.escape(self.request.get('flushcache')).lower()[:50])
    reset_pos = urllib.unquote(cgi.escape(self.request.get('resetpos')).lower()[:50])
    get_stats = urllib.unquote(cgi.escape(self.request.get('getstats')).lower()[:50])

    if flush_cache:
      if not memcache.flush_all():
        logging.error("Error flushing memcache")
    if reset_pos:
      memcache.set("index", 1, 86400)
      SearchPosition(key_name="index", position=1).put()



    if get_stats:
      d = memcache.get_stats()
      #index = memcache.get("index")
      #if index:
        #d['indexmc'] = index
      #else:
        #d['indexmc'] = -1
      index_from_ds = SearchPosition.get_by_key_name("index")
      if index_from_ds:
        d['indexds'] = index_from_ds.position
      else:
        d['indexds'] = -1
      s = json.dumps(d)
      self.response.out.write(s)
    else:
      template_values = {}
      path = os.path.join(os.path.dirname(__file__), 'admin_page.html')
      self.response.out.write(template.render(path, template_values))
示例#16
0
 def get(self):
     stats = memcache.get_stats()
     self.render('memcache-config.html',{
         'stats':stats,
         'success':self.params('success'),
         'where':'memcache-config'
     })
示例#17
0
 def get(self):
   stats = memcache.get_stats()
   self.response.out.write('<html><body>')
   self.response.out.write('<b>Cache Hits:%s</b><br/>' % stats['hits'])
   self.response.out.write('<b>Cache Misses:%s</b><br/>' %
                           stats['misses'])
   self.response.out.write('</body></html')
示例#18
0
 def GET(self):
     result = {}
     action = web.input(action = None)['action']
     
     if action=='quota': 
         results = urlfetch.fetch('https://api.stackexchange.com/2.0/info?site=stackoverflow&key=%s' % api_key, headers = {'User-Agent': 'StackPrinter'}, deadline = 10)
         response = simplejson.loads(results.content)
         result['result'] = response
     if action=='quotaauth': 
         results = urlfetch.fetch('https://api.stackexchange.com/2.0/info?site=stackoverflow&key=%s&access_token=%s' % (api_key, TokenManager.get_auth_token()), headers = {'User-Agent': 'StackPrinter'}, deadline = 10)
         response = simplejson.loads(results.content)
         result['result'] = response
     if action=='authkey': 
         result['result'] = TokenManager.get_auth_token()
     elif action =='memcachestats':
         result = memcache.get_stats()        
     elif action =='memcacheflush':
         result['result'] = memcache.flush_all()
     elif action =='normalize':
         deferred.defer(worker.deferred_normalize_printed_question)    
         result['result'] = True
     elif action =='delete':
         service = web.input(service = None)['service']
         question_id = web.input(question_id = None)['question_id']
         result['printed_question_deletion'] = dbquestion.delete_printed_question(question_id,service)
         result['question_deletion'] = dbquestion.delete_question(question_id,service)
         result['answers_deletion'] = dbquestion.delete_answers(question_id,service)
     return render.admin(result)
示例#19
0
    def get(self):

        flush_cache = urllib.unquote(
            cgi.escape(self.request.get('flushcache')).lower()[:50])
        reset_pos = urllib.unquote(
            cgi.escape(self.request.get('resetpos')).lower()[:50])
        get_stats = urllib.unquote(
            cgi.escape(self.request.get('getstats')).lower()[:50])

        if flush_cache:
            if not memcache.flush_all():
                logging.error("Error flushing memcache")
        if reset_pos:
            memcache.set("index", 1, 86400)
            SearchPosition(key_name="index", position=1).put()

        if get_stats:
            d = memcache.get_stats()
            #index = memcache.get("index")
            #if index:
            #d['indexmc'] = index
            #else:
            #d['indexmc'] = -1
            index_from_ds = SearchPosition.get_by_key_name("index")
            if index_from_ds:
                d['indexds'] = index_from_ds.position
            else:
                d['indexds'] = -1
            s = json.dumps(d)
            self.response.out.write(s)
        else:
            template_values = {}
            template = jinja_environment.get_template('html/admin_page.html')
            self.response.out.write(template.render(template_values))
示例#20
0
    def _TestDeleteTimeout(self, timeout):
        """Executes test of deletion with timeout for testDeleteTimeout_* tests."""

        self.assertTrue(memcache.set(self.key1, self.value1))
        self.assertEqual(self.value1, memcache.get(self.key1))
        self.assertLen(self.stub._lru, 1)

        self.assertEqual(memcache.DELETE_SUCCESSFUL,
                         memcache.delete(self.key1, seconds=timeout))
        self.assertFalse(memcache.add(self.key1, self.value1))
        self.assertFalse(memcache.replace(self.key1, self.value1))
        self.assertEqual(0, memcache.get_stats().get('items'))
        self.assertEqual(None, memcache.get(self.key1))
        self.assertEmpty(self.stub._lru)

        self._StepClock(4)
        self.assertFalse(memcache.add(self.key1, self.value1))
        self.assertFalse(memcache.replace(self.key1, self.value1))
        self.assertEqual(None, memcache.get(self.key1))
        self.assertEmpty(self.stub._lru)

        self._StepClock(2)
        self.assertTrue(memcache.add(self.key1, self.value1))
        self.assertEqual(self.value1, memcache.get(self.key1))
        self.assertTrue(memcache.replace(self.key1, self.value2))
        self.assertEqual(self.value2, memcache.get(self.key1))
        self.assertLen(self.stub._lru, 1)
示例#21
0
 def get(self):
     self.render2(
         'views/admin/status.html', {
             'cache': memcache.get_stats(),
             'current': 'status',
             'environ': os.environ
         })
示例#22
0
 def get(self):
     if is_admin():
         self.response.out.write(header())
         stats = memcache.get_stats()
         self.response.out.write('<div class="col1">')
         self.response.out.write('<h3>Add/Update Text</h3>')
         self.response.out.write('<form action=/admin/savetext method=post>')
         self.response.out.write('<table>')
         self.response.out.write('<tr><td>Label/Title</td><td><input type=text name=label></td></tr>')
         self.response.out.write('<tr><td colspan=2>Text<br><textarea name=text rows=6 cols=60></textarea></td></tr>')
         self.response.out.write('<tr><td>Is This A Blog Post?</td><td>')
         self.response.out.write('<select name=blog><option selected value=n>No</option>')
         self.response.out.write('<option value=y>Yes</option></select></td></tr>')
         self.response.out.write('<tr><td>Language</td><td><input type=text name=language value=en></td></tr>')
         self.response.out.write('<tr><td></td><td><input type=submit value=Save></td></tr>')
         self.response.out.write('</table></form>')
         self.response.out.write('<h3>Texts</h3>')
         results = TextObjects.getall()
         if results is not None:
             self.response.out.write('<table width=100% border=1>')
             self.response.out.write('<tr><td>Label/Title</td><td>Text</td><td>IsBlog</td><td></td></tr>')
             for r in results:
                 self.response.out.write('<tr><td>' + r.label + '</td>')
                 self.response.out.write('<td>' + r.text + '</td>')
                 if r.blog:
                     self.response.out.write('<td>y</td>')
                 else:
                     self.response.out.write('<td>n</td>')
                 self.response.out.write('<td></td></tr>')
             self.response.out.write('</table></div>')
         self.response.out.write(footer())
     else:
         self.redirect('/admin')
示例#23
0
 def assertMemcacheItems(self, items):
     """
     Asserts that the Memcache API contains exactly ``items`` items.
     
     This method checks the statistics for the cache and asserts that the
     there are a certain number of items stored in the cache.
     
     For example::
     
         import unittest
         
         from gaetestbed import MemcacheTestCase
         
         from google.appengine.api import memcache
         
         class MyTestCase(MemcacheTestCase, unittest.TestCase):
             def test_memcache(self):
                 # Nothing has been added to the cache
                 self.assertMemcacheItems(0)
                 
                 # Add something to the cache
                 memcache.set(key='test_item', value='test_content')
                 
                 # Test that the item was added
                 self.assertMemcacheItems(1)
                 
                 # Remove that key
                 memcache.delete('test_item')
                 
                 # Test that the cache has zero items
                 self.assertMemcacheHits(0)
     """
     self.assertEqual(memcache.get_stats()['items'], items)
示例#24
0
 def venues(self, pos, limit, radius, categories):
   cacheId = pos+limit+radius+categories
   print 'cacheId: ' + cacheId
   json_data = memcache.get(cacheId)
   
   if json_data is not None:
     print('From cache!!')
     return json_data
   else:
     json_data = self.getVenues(pos, limit, radius, categories)
     print 'mem stats: ' + str(memcache.get_stats())
     if not memcache.add(cacheId, json_data, 1000):
           print('********** MEM CACHE ERROR *****')
           logging.error('Memcache set failed.')
     print 'mem stats: ' + str(memcache.get_stats())
     return json_data
    def post(self):
        self._require_admin()
        flushed = list()

        if self.request.get("all_keys") == "all_keys":
            memcache.flush_all()
            flushed.append("all memcache values")

        if self.request.get("webcast_keys") == "webcast_keys":
            flushed.append(MemcacheWebcastFlusher.flush())

        if self.request.get('memcache_key') is not "":
            memcache.delete(self.request.get("memcache_key"))
            flushed.append(self.request.get("memcache_key"))

        if self.request.get('return_url') is not "":
            self.redirect("{}?flushed={}".format(self.request.get('return_url'), flushed))
            return

        self.template_values.update({
            "flushed": flushed,
            "memcache_stats": memcache.get_stats(),
        })

        path = os.path.join(os.path.dirname(__file__), '../../templates/admin/memcache_index.html')
        self.response.out.write(template.render(path, self.template_values))
示例#26
0
 def get(self, user):
     user = cgi.escape(user, True)
     self.key = self.setKey(user)
     json_data = self.getJson(user)
     stats = memcache.get_stats()
     self.response.headers['Content-Type'] = 'application/json'
     self.response.out.write(json.encode(json_data))
示例#27
0
    def assertMemcacheItems(self, items):
        """
        Asserts that the memcache API has ``items`` key-value pairs.

        :param items: number of items in memcache
        """
        self.assertEqual(items, memcache.get_stats()['items'])
示例#28
0
def admin_memcache_statistics(request):
    stats = [(k, v) for k, v in memcache.get_stats().iteritems()]
    template_vars = {
        'memcache_statistics': stats,
    }
    return render_to_response(request, "admin/memcache-statistics.html",
                              template_vars)
示例#29
0
 def get(self):
   count = placedlit.PlacedLit.count()
   places = placedlit.PlacedLit.get_newest_places(limit=count)
   stats = memcache.get_stats()
   logging.info('memcache stats: %s' % (stats))
   loc_json = [self.export_place_fields(place) for place in places]
   self.output_json(loc_json)
示例#30
0
    def get(self):
        self.response.out.write('<html><body>')

        # Print out some stats on caching
        stats = memcache.get_stats()
        self.response.write('<b>Cache Hits:{}</b><br>'.format(stats['hits']))
        self.response.write('<b>Cache Misses:{}</b><br><br>'.format(
            stats['misses']))

        user = self.request.get('user')
        ancestor_key = ndb.Key("User", user or "*notitle*")
        # Query the datastore
        photos = User.query_user(ancestor_key).fetch(100)

        self.response.out.write("""
        <form action="/post/default/" enctype="multipart/form-data" method="post">
        <div><textarea name="caption" rows="3" cols="60"></textarea></div>
        <div><label>Photo:</label></div>
        <div><input type="file" name="image"/></div>
        <div>User <input value="default" name="user"></div>
        <div><input type="submit" value="Post"></div>
        </form>
        <hr>
        </body>
        </html>""")
示例#31
0
    def get(self):
        guestbook_name = self.request.get('guestbook_name', DEFAULT_GUESTBOOK)
        greetings = self.get_greetings(guestbook_name)
        stats = memcache.get_stats()

        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'cache_hits': stats['hits'],
            'cache_misses': stats['misses'],
            'greetings': greetings,
            'guestbook_name': guestbook_name,
            'query_string':
            urllib.urlencode({'guestbook_name': guestbook_name}),
            'url': url,
            'url_linktext': url_linktext,
        }

        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render(template_values))
    def get(self):
        self._require_admin()

        self.template_values['memcache_stats'] = memcache.get_stats()
        self.template_values['databasequery_stats'] = {
            'hits': sum(filter(None, [memcache.get(key) for key in DatabaseQuery.DATABASE_HITS_MEMCACHE_KEYS])),
            'misses': sum(filter(None, [memcache.get(key) for key in DatabaseQuery.DATABASE_MISSES_MEMCACHE_KEYS]))
        }

        # Gets the 5 recently created users
        users = Account.query().order(-Account.created).fetch(5)
        self.template_values['users'] = users

        self.template_values['suggestions_count'] = Suggestion.query().filter(
            Suggestion.review_state == Suggestion.REVIEW_PENDING).count()

        # version info
        try:
            fname = os.path.join(os.path.dirname(__file__), '../../version_info.json')

            with open(fname, 'r') as f:
                data = json.loads(f.read().replace('\r\n', '\n'))

            self.template_values['git_branch_name'] = data['git_branch_name']
            self.template_values['build_time'] = data['build_time']

            commit_parts = re.split("[\n]+", data['git_last_commit'])
            self.template_values['commit_hash'] = commit_parts[0].split(" ")
            self.template_values['commit_author'] = commit_parts[1]
            self.template_values['commit_date'] = commit_parts[2]
            self.template_values['commit_msg'] = commit_parts[3]

        except Exception, e:
            logging.warning("version_info.json parsing failed: %s" % e)
            pass
示例#33
0
    def get(self):
        self.response.out.write("<html><body>")

        before = time.time()
        greetings = self.get_greetings()
        after = time.time()
        timeTaken = str(after - before)

        stats = memcache.get_stats()

        self.response.out.write("<b>Time to get Greetings:%s seconds</b><br>" %
                                timeTaken)
        self.response.out.write("<b>Cache Hits:%s</b><br>" % stats['hits'])
        self.response.out.write("<b>Cache Misses:%s</b><br><br>" %
                                stats['misses'])
        self.response.out.write(greetings)
        self.response.out.write("""
          <form action="/sign" enctype="multipart/form-data" method="post">
            <div><label>Message:</label></div>
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><label>Avatar:</label></div>
            <div><input type="file" name="img"/></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")
示例#34
0
 def get(self, user):
     user = cgi.escape(user,True)
     self.key = self.setKey(user)
     json_data = self.getJson(user)
     stats = memcache.get_stats()
     self.response.headers['Content-Type']='application/json'
     self.response.out.write(json.encode(json_data))
示例#35
0
 def post(self):
     action = self.request.get("admin_action")
     
     if (action == 'flush_cache'):
         memcache.flush_all()
     
     self.response.out.write(url_tools.dict_to_s(memcache.get_stats()))
    def get(self):
        self._require_admin()

        self.template_values['memcache_stats'] = memcache.get_stats()

        # Gets the 5 recently created users
        users = Account.query().order(-Account.created).fetch(5)
        self.template_values['users'] = users

        # Retrieves the number of pending suggestions
        video_suggestions = Suggestion.query().filter(Suggestion.review_state == Suggestion.REVIEW_PENDING).count()
        self.template_values['video_suggestions'] = video_suggestions

        # version info
        try:
            fname = os.path.join(os.path.dirname(__file__), '../../version_info.json')

            with open(fname, 'r') as f:
                data = json.loads(f.read().replace('\r\n', '\n'))

            self.template_values['git_branch_name'] = data['git_branch_name']
            self.template_values['build_time'] = data['build_time']

            commit_parts = re.split("[\n]+", data['git_last_commit'])
            self.template_values['commit_hash'] = commit_parts[0].split(" ")
            self.template_values['commit_author'] = commit_parts[1]
            self.template_values['commit_date'] = commit_parts[2]
            self.template_values['commit_msg'] = commit_parts[3]

        except Exception, e:
            logging.warning("version_info.json parsing failed: %s" % e)
            pass
    def post(self):
        self._require_admin()
        flushed = list()

        if self.request.get("all_keys") == "all_keys":
            memcache.flush_all()
            flushed.append("all memcache values")

        if self.request.get("webcast_keys") == "webcast_keys":
            flushed.append(MemcacheWebcastFlusher.flush())

        if self.request.get('memcache_key') is not "":
            memcache.delete(self.request.get("memcache_key"))
            flushed.append(self.request.get("memcache_key"))

        if self.request.get('return_url') is not "":
            self.redirect("{}?flushed={}".format(
                self.request.get('return_url'), flushed))
            return

        self.template_values.update({
            "flushed": flushed,
            "memcache_stats": memcache.get_stats(),
        })

        path = os.path.join(os.path.dirname(__file__),
                            '../../templates/admin/memcache_index.html')
        self.response.out.write(template.render(path, self.template_values))
示例#38
0
def Admin(req, command=None):
    req.Require('admin')
    
    # BUG - Add CSRF required field
    if command:
        logging.info("admin command: %s" % command)
        req.Require('api')

        if req.fJSON:
            return filter.HttpJSON(req, {})
        return HttpResponseRedirect("/admin/")

    try:
        ms = memcache.get_stats()
        mpMem = [{'key':key, 'value':ms[key]} for key in ms]
    except:
        mpMem = [{'key':'message', 'value':'memcache get_stats() failure!'}]

    req.AddToResponse(
          {
           'logout':users.create_logout_url(req.get_full_path()),
           'cUnscopedComments':len(Comment.Unscoped()),
           'MemCache': mpMem
           })
    return filter.RenderResponse('admin.html')
示例#39
0
def getDefaultTemplateValues(self):
	myStoriesURL = '/myStories'
	myStories = 'My Stories'
	loginURL = None
	login = None
	loggedIn = None
	loginURLMain = None
	
	loggedIn = None
	currentUser = users.get_current_user()
	if users.get_current_user():
		loginURL = users.create_logout_url(self.request.uri)
		login = '******'
		loggedIn = True
	else:
		loginURL = '/myStories'
		login = '******'
		loginURLMain = users.create_login_url(self.request.uri)

	stats = memcache.get_stats()
	templateValues = {
		'coverImages': [1, 2, 3, 4],
		'currentUser': currentUser,
		'loggedIn': loggedIn,
		'loginURL': loginURL,
		'loginURLMain': loginURLMain,
		'login': login,
		'myStoriesURL': myStoriesURL,
		'myStories': myStories,
		'title': 'Home',
		'cacheHits': stats['hits'],
		'cacheMisses': stats['misses']
	}
	
	return templateValues
示例#40
0
    def get(self):
        now = datetime.datetime.now()
        now_time_t = int(time.mktime(now.timetuple()))
        is_enabled = CapabilitySet('memcache').is_enabled()
        memcache_stats = memcache.get_stats()

        if self.request.get('output') in ('text', 'txt'):
            self.response.out.write(now_time_t)
            self.response.out.write(' up' if is_enabled else ' down')
            self.response.out.write(' h:%(hits)s'
                                    ' m:%(misses)s'
                                    ' bh:%(byte_hits)s'
                                    ' i:%(items)s'
                                    ' b:%(bytes)s'
                                    ' oia:%(oldest_item_age)s'
                                    '\n' % memcache_stats)
            self.response.headers['Content-Type'] = "text/text"
        else:
            template_values = {
                'now': now.ctime(),
                'now_time_t': now_time_t,
                'is_enabled': is_enabled,
                'memcache_stats': memcache_stats,
            }
            self.render_jinja2_template("memcache_stats.html", template_values)
示例#41
0
文件: base_test.py 项目: Khan/agar
    def assertMemcacheHits(self, hits):
        """
        Asserts that the memcache API has had ``hits`` successful lookups.

        :param hits: number of memcache hits
        """
        self.assertEqual(hits, memcache.get_stats()['hits'])
示例#42
0
 def assertMemcacheItems(self, items):
     """
     Asserts that the Memcache API contains exactly ``items`` items.
     
     This method checks the statistics for the cache and asserts that the
     there are a certain number of items stored in the cache.
     
     For example::
     
         import unittest
         
         from gaetestbed import MemcacheTestCase
         
         from google.appengine.api import memcache
         
         class MyTestCase(MemcacheTestCase, unittest.TestCase):
             def test_memcache(self):
                 # Nothing has been added to the cache
                 self.assertMemcacheItems(0)
                 
                 # Add something to the cache
                 memcache.set(key='test_item', value='test_content')
                 
                 # Test that the item was added
                 self.assertMemcacheItems(1)
                 
                 # Remove that key
                 memcache.delete('test_item')
                 
                 # Test that the cache has zero items
                 self.assertMemcacheHits(0)
     """
     self.assertEqual(memcache.get_stats()['items'], items)
示例#43
0
文件: base_test.py 项目: Khan/agar
    def assertMemcacheItems(self, items):
        """
        Asserts that the memcache API has ``items`` key-value pairs.

        :param items: number of items in memcache
        """
        self.assertEqual(items, memcache.get_stats()['items'])
示例#44
0
    def assertMemcacheHits(self, hits):
        """
        Asserts that the memcache API has had ``hits`` successful lookups.

        :param hits: number of memcache hits
        """
        self.assertEqual(hits, memcache.get_stats()['hits'])
示例#45
0
 def get(self):
     stats = memcache.get_stats()
     context = {
         'stats': stats,
     }        
     output = self.render("admin.html", context)
     self.response.out.write(output)
    def get(self):
        self._require_admin()

        self.template_values['memcache_stats'] = memcache.get_stats()
        self.template_values['databasequery_stats'] = {
            'hits':
            sum(
                filter(None, [
                    memcache.get(key)
                    for key in DatabaseQuery.DATABASE_HITS_MEMCACHE_KEYS
                ])),
            'misses':
            sum(
                filter(None, [
                    memcache.get(key)
                    for key in DatabaseQuery.DATABASE_MISSES_MEMCACHE_KEYS
                ]))
        }

        # Gets the 5 recently created users
        users = Account.query().order(-Account.created).fetch(5)
        self.template_values['users'] = users

        # Retrieves the number of pending suggestions
        video_suggestions = Suggestion.query().filter(
            Suggestion.review_state == Suggestion.REVIEW_PENDING).filter(
                Suggestion.target_model == "match").count()
        self.template_values['video_suggestions'] = video_suggestions

        webcast_suggestions = Suggestion.query().filter(
            Suggestion.review_state == Suggestion.REVIEW_PENDING).filter(
                Suggestion.target_model == "event").count()
        self.template_values['webcast_suggestions'] = webcast_suggestions

        media_suggestions = Suggestion.query().filter(
            Suggestion.review_state == Suggestion.REVIEW_PENDING).filter(
                Suggestion.target_model == "media").count()
        self.template_values['media_suggestions'] = media_suggestions

        # version info
        try:
            fname = os.path.join(os.path.dirname(__file__),
                                 '../../version_info.json')

            with open(fname, 'r') as f:
                data = json.loads(f.read().replace('\r\n', '\n'))

            self.template_values['git_branch_name'] = data['git_branch_name']
            self.template_values['build_time'] = data['build_time']

            commit_parts = re.split("[\n]+", data['git_last_commit'])
            self.template_values['commit_hash'] = commit_parts[0].split(" ")
            self.template_values['commit_author'] = commit_parts[1]
            self.template_values['commit_date'] = commit_parts[2]
            self.template_values['commit_msg'] = commit_parts[3]

        except Exception, e:
            logging.warning("version_info.json parsing failed: %s" % e)
            pass
示例#47
0
 def get(self):
     earthquakes = self.getLastDayFromCache()
     stats = memcache.get_stats()
     logging.debug("<b>Cache Hits:%s</b><br>" % stats['hits'])
     logging.debug("<b>Cache Misses:%s</b><br><br>" %
                           stats['misses'])
     self.response.out.write(earthquakes)
     
示例#48
0
  def testMemcacheUsage(self, mock_func):
    """Tests that memcache is used to help to get predefined schools."""
    self.program.schools = 'mock key'

    # check that schools are not found in cache by the first call
    school_logic.getMappedByCountries(self.program)

    stats = memcache.get_stats()
    self.assertEqual(stats['misses'], 1)
    self.assertEqual(stats['hits'], 0)

    # check that schools are found in cache by the second call
    school_logic.getMappedByCountries(self.program)

    stats = memcache.get_stats()
    self.assertEqual(stats['misses'], 1)
    self.assertEqual(stats['hits'], 1)
示例#49
0
 def stat():
     stat_items = memcache.get_stats().iteritems()
     memlog = ''
     for item in stat_items:
         memlog = memlog + '%s=%d ' % item
     tv = {'memlog': memlog}
     path = os.path.join(orig_path, 'stat.html')
     return template.render(path, tv)
示例#50
0
def printCacheStats():
	cache_stats = memcache.get_stats()
	logging.info("Cache has " + str(cache_stats['items']) + " items")
	logging.info("          " + str(cache_stats['hits']) + " hits")
	logging.info("          " + str(cache_stats['misses']) + " misses")
	logging.info("          " + str(cache_stats['byte_hits']) + " byte_hits")
	logging.info("          " + str(cache_stats['bytes']) + " bytes")
	logging.info("          " + str(cache_stats['oldest_item_age']) + " oldest item age")
 def get(self):
     
     template_values = {
         "memcache_stats": memcache.get_stats(),
     }
     
     path = os.path.join(os.path.dirname(__file__), '../../templates/admin/memcache/index.html')
     self.response.out.write(template.render(path, template_values))
示例#52
0
def healthcheck():
    memcache_stats = memcache.get_stats()
    datastore_stats = stats.GlobalStat.all().get()

    return json.dumps({
        "memcache_stats": memcache_stats,
        "datastore_stats": datastore_stats
    })
示例#53
0
    def testStats(self):
        """Tries to get memcache stats."""

        stats = memcache.get_stats()
        self.assertEqual(
            set([
                'hits', 'items', 'bytes', 'oldest_item_age', 'misses',
                'byte_hits'
            ]), set(stats.keys()))
    def get(self):
        self._require_admin()

        self.template_values.update({
            "memcache_stats": memcache.get_stats(),
        })

        path = os.path.join(os.path.dirname(__file__), '../../templates/admin/memcache_index.html')
        self.response.out.write(template.render(path, self.template_values))
示例#55
0
    def get(self):
        """ Handler for GET request for the memcache statistics. """
        if not self.helper.is_user_cloud_admin():
            response = json.dumps({"error": True, "message": "Not authorized"})
            self.response.out.write(response)
            return

        mem_stats = memcache.get_stats()
        self.response.out.write(json.dumps(mem_stats))