def country_count(cls): cache_key = "country_count" def get_from_database(): session = DBSession() q = session.query(cls.country, func.count('*').label('count')).group_by(cls.country).all() return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def count_nonkang(cls): cache_key = "count_nonkang" def get_from_database(): session = DBSession() q = session.query(func.count(cls.id)).filter(cls.kang == False).one()[0] return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def count_last_day(cls): cache_key = "count_last_day" def get_from_database(): timestamp = datetime.datetime.now() - datetime.timedelta(hours=24) session = DBSession() q = session.query(cls).filter(cls.date_added > timestamp).count() return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def count_nonkang(cls): cache_key = "count_nonkang" def get_from_database(): session = DBSession() q = session.query(func.count( cls.id)).filter(cls.kang == False).one()[0] return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def country_count(cls): cache_key = "country_count" def get_from_database(): session = DBSession() q = session.query(cls.country, func.count('*').label('count')).group_by( cls.country).all() return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def device_count(cls): cache_key = "device_count" def get_from_database(): session = DBSession() q = session.query(func.count(cls.name), cls.name) \ .group_by(cls.name).all() q = sorted(q, key=lambda x: x[0], reverse=True) return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result
def version_count(cls): cache_key = "version_count" def get_from_database(): session = DBSession() q = session.query(func.count(cls.version), cls.version) \ .filter(cls.kang == False) \ .group_by(cls.version).all() q = sorted(q, key=lambda x: x[0], reverse=True) return q result = cache.get(cache_key) if result is None: result = cache.set(cache_key, get_from_database()) return result