diff --git a/facebook/arangodb.py b/facebook/arangodb.py index 1e6db3d..12b82e2 100644 --- a/facebook/arangodb.py +++ b/facebook/arangodb.py @@ -1,6 +1,7 @@ from datetime import datetime from getpass import getpass from time import sleep +import json import nacl.secret import nacl.utils @@ -21,6 +22,29 @@ def checked_members(): return members_checked +def count_docs(col): + cursor = db.aql.execute( + """ + FOR doc IN @@col + COLLECT WITH COUNT INTO length + RETURN length + """, + bind_vars={"@col": col} + ) + return cursor.next() + +def write_report(users, pictures): + now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + db.insert_document({ + '_id':f'reports/{now}', + 'users': [user.username for user in users], + 'members': count_docs('members'), + 'total_picture_reactions':count_docs('picture_reactions'), + 'pictures':count_docs('pictures'), + 'new_pictures': pictures + }) + + def get_profile(): """ Hämtar profil om det inte gjorts förut """ cursor = db.aql.execute( @@ -73,5 +97,22 @@ for i in range(0, 6, 1): print("Fel lösenord.") sleep(1) - + +def backup(db): + """Skapar en json-backup för specificerade collections. + + Args: + db: databaskoppling till aktuell databas + """ + d = {} + for col in ['members', 'pictures', 'picture_reactions', 'profiles']: + l = [] + for doc in db.collection(col).all(): + l.append(doc) + d[col] = l + with open('data/backup.json', 'w') as f: + json.dump(d, f) + print(f'Senaste backup: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}') + + db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd)