Created
February 2, 2017 10:39
-
-
Save nickstenning/378f9c766585e46faca05c3562273aab to your computer and use it in GitHub Desktop.
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
import locale | |
import os | |
import time | |
import psycopg2 | |
import requests | |
DATABASE_URL = os.environ['DATABASE_URL'] | |
WEBHOOK_URL = os.environ['WEBHOOK_URL'] | |
SLEEP_TIME = 60 * 60 | |
GOT_THERE_THRESHOLD = 1000000 | |
GETTING_CLOSE_THRESHOLD = 999000 | |
locale.setlocale(locale.LC_ALL, 'en_US') | |
conn = psycopg2.connect(DATABASE_URL) | |
def getcount(): | |
with conn.cursor() as curs: | |
curs.execute('SELECT count(*) FROM annotation;') | |
count, = curs.fetchone() | |
return count | |
def getmillionthuser(): | |
with conn.cursor() as curs: | |
curs.execute('SELECT userid, target_uri FROM (SELECT row_number() OVER (ORDER BY created) AS rownum, userid, target_uri FROM annotation) AS a WHERE rownum = %s;', (GOT_THERE_THRESHOLD,)) | |
return curs.fetchone() | |
def sendtoslack(msg): | |
requests.post(WEBHOOK_URL, json={"text": msg}) | |
while True: | |
count = getcount() | |
if count >= GOT_THERE_THRESHOLD: | |
sendtoslack(':tada::tada::tada::tada::tada::tada::tada:') | |
sendtoslack('ONE MILLION ANNOTATIONS!') | |
sendtoslack(':tada::tada::tada::tada::tada::tada::tada:') | |
userid, url = getmillionthuser() | |
sendtoslack('Our millionth annotation was made by {} on {}'.format(userid, url)) | |
break | |
elif count >= GETTING_CLOSE_THRESHOLD: | |
numstr = locale.format('%d', count, grouping=True) | |
sendtoslack("{} annotations. We're getting close!".format(numstr)) | |
else: | |
numstr = locale.format('%d', count, grouping=True) | |
sendtoslack("There are now {} annotations in the database.".format(numstr)) | |
time.sleep(SLEEP_TIME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment