-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-users-clean.py
78 lines (61 loc) · 2.75 KB
/
mongo-users-clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Vedi: https://github.com/officina/gamifier-platform/blob/revive-playoff/contributing.md
# Perché funzioni eseguire il seguente:
# ssh -i ~/.ssh/playlyfe-aws.pem -p 22 [email protected] -L 27017:10.20.50.50:27017 -L 9333:10.20.50.50:9333 -L 8085:10.20.50.50:8080 -L 8086:10.20.50.51:8080 -L 8087:10.20.50.52:8080 -L 9042:10.20.50.50:9042
from pymongo import MongoClient
import datetime
import psycopg2
from playoff import Playoff, PlayoffException
import os
from dotenv import load_dotenv, find_dotenv
print("INIZIO " + str(datetime.datetime.now()))
load_dotenv(find_dotenv())
os.environ['NO_PROXY'] = '127.0.0.1'
hostname = "mongodb://localhost:27017"
# env = 'STAGING'
env = 'PRODUCTION'
if env == 'PRODUCTION':
postgres_host = os.environ.get("PRODUCTION_POSTGRES_HOST")
postgres_user = os.getenv("PRODUCTION_POSTGRES_USER")
postgres_password = os.getenv("PRODUCTION_POSTGRES_PASSWORD")
CLIENT_ID = os.getenv("PRODUCTION_PO_CLIENT_ID")
CLIENT_SECRET = os.getenv("PRODUCTION_PO_CLIENT_SECRET")
collection_name = 'production_players'
else:
postgres_host = os.getenv("STAGING_POSTGRES_HOST")
postgres_user = os.getenv("STAGING_POSTGRES_USER")
postgres_password = os.getenv("STAGING_POSTGRES_PASSWORD")
CLIENT_ID = os.getenv("STAGING_PO_CLIENT_ID")
CLIENT_SECRET = os.getenv("STAGING_PO_CLIENT_SECRET")
collection_name = 'staging_players'
mongoDB_uri = f"{hostname}"
client = MongoClient(mongoDB_uri)
pl_client = Playoff(
hostname="playoffgamification.io",
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
type="client",
)
db = client.pl_core
docs_number = db[collection_name].count()
result = db[collection_name].aggregate([{"$group": { "_id": { "alias": "$alias"},"count": { "$sum": 1 }, "docs": { "$push": "$_id" }}},{ "$match": {"count": { "$gt" : 1 }}}])
print(result)
postgres_port = "5432"
postgres_dbname = "nowrbackend"
conn_string = f"host={postgres_host} port={postgres_port} dbname={postgres_dbname} user={postgres_user} password={postgres_password}"
conn = psycopg2.connect(conn_string)
gg = 0
for doc in result:
# print(doc)
# print("*********** " + doc['_id']['alias'] + " *****")
for dd in doc['docs']:
if 'g:nowrprod:l:' in dd:
PO_ID = dd.replace('g:nowrprod:l:','')
cursor = conn.cursor()
cursor.execute(f"select sum(cc) from (select count(*) cc from jhi_user where po_id in ('{PO_ID}') union select count(*) cc from event where po_id in ('{PO_ID}')) as det")
count1 = cursor.fetchone()
if count1[0] == 0:
gg += 1
print(f"{PO_ID} -- {gg}")
pl_client.delete(f"/admin/players/{PO_ID}")
cursor.close()
print("FINE " + str(datetime.datetime.now()))