Skip to content

Instantly share code, notes, and snippets.

@gallir
Created October 13, 2015 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gallir/74cf8fba0b62290f4fe5 to your computer and use it in GitHub Desktop.
Save gallir/74cf8fba0b62290f4fe5 to your computer and use it in GitHub Desktop.
Update counters per day and hostname with transactions in Google NDB
from google.appengine.ext import ndb
from datetime import datetime, timedelta, date
#
#
#
class HostConnection(ndb.Model):
date = ndb.DateProperty()
host = ndb.StringProperty()
connections = ndb.IntegerProperty(default=0)
@classmethod
@ndb.transactional()
def update_count(cls, host, count):
today = date.today()
key_name = "{}:{}".format(today, host)
entry = cls.get_by_id(key_name)
if entry is None:
entry = cls(id=key_name, date=today, host=host, connections=count)
else:
entry.connections += count
entry.put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment