def get_visitor_details(): """Retrieve detailed visitor information for the frontend.""" result = { "traffic": [], # Historical traffic data "most_unique": ["0000-00-00", 0], # Day with the most unique "most_hits": ["0000-00-00", 0], # Day with the most hits "oldest": None, # Oldest day on record. } # List all the documents. hits = JsonDB.list_docs("traffic/hits") for date in sorted(hits): if date == "total": continue if not result["oldest"]: result["oldest"] = date # Get the DBs. hits_db = JsonDB.get("traffic/hits/{}".format(date), cache=False) uniq_db = JsonDB.get("traffic/unique/{}".format(date), cache=False) # Most we've seen? if hits_db["hits"] > result["most_hits"][1]: result["most_hits"] = [date, hits_db["hits"]] if len(uniq_db.keys()) > result["most_unique"][1]: result["most_unique"] = [date, len(uniq_db.keys())] result["traffic"].append( dict( date=date, hits=hits_db["hits"], unique=len(uniq_db.keys()), )) return result
def list_users(): """Get a sorted list of all users.""" uids = JsonDB.list_docs("users/by-id") users = list() for uid in sorted(map(lambda x: int(x), uids)): db = get_user(uid=uid) if db["role"] == "deleted": continue users.append(db) return users
def rebuild_index(): """Rebuild the index.json if it goes missing.""" index = {} entries = JsonDB.list_docs("blog/entries") for post_id in entries: db = JsonDB.get("blog/entries/{}".format(post_id)) update_index(post_id, db, index, False) JsonDB.commit("blog/index", index) return index
def rebuild_visitor_stats(): """Recalculate the total unique/hits based on daily info.""" total_unique = {} total_hits = 0 # Tally them all up! for date in JsonDB.list_docs("traffic/unique"): if date == "total": continue db = JsonDB.get("traffic/unique/{}".format(date), cache=False) total_unique.update(db) for date in JsonDB.list_docs("traffic/hits"): if date == "total": continue db = JsonDB.get("traffic/hits/{}".format(date), cache=False) total_hits += db.get("hits", 0) # Write the outputs. JsonDB.commit("traffic/unique/total", total_unique) JsonDB.commit("traffic/hits/total", dict(hits=total_hits))
def unsubscribe(thread, email): """Unsubscribe an e-mail address from a thread. If `thread` is `*`, the e-mail is unsubscribed from all threads.""" # Which threads to unsubscribe from? threads = [] if thread == "*": threads = JsonDB.list_docs("comments/subscribers") else: threads = [thread] # Remove them as a subscriber. for thread in threads: if JsonDB.exists("comments/subscribers/{}".format(thread)): logger.info("Unsubscribe e-mail address {} from comment thread {}".format(email, thread)) db = get_subscribers(thread) del db[email] write_subscribers(thread, db)
def unsubscribe(thread, email): """Unsubscribe an e-mail address from a thread. If `thread` is `*`, the e-mail is unsubscribed from all threads.""" # Which threads to unsubscribe from? threads = [] if thread == "*": threads = JsonDB.list_docs("comments/subscribers") else: threads = [thread] # Remove them as a subscriber. for thread in threads: if JsonDB.exists("comments/subscribers/{}".format(thread)): logger.info( "Unsubscribe e-mail address {} from comment thread {}".format( email, thread)) db = get_subscribers(thread) del db[email] write_subscribers(thread, db)
def list_pages(): """Get a list of all existing wiki pages.""" return JsonDB.list_docs("wiki/pages", recursive=True)