Created
October 13, 2015 20:52
-
-
Save gallir/74cf8fba0b62290f4fe5 to your computer and use it in GitHub Desktop.
Update counters per day and hostname with transactions in Google NDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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