Spaces:
Running
Running
Pedoelski commited on
Commit ·
c7a2d84
1
Parent(s): 84fcc2e
Add update_tags.py and Dockerfile with psycopg
Browse files- Dockerfile +7 -0
- update_tags.py +67 -0
Dockerfile
CHANGED
|
@@ -1 +1,8 @@
|
|
| 1 |
FROM n8nio/n8n:latest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
FROM n8nio/n8n:latest
|
| 2 |
+
|
| 3 |
+
USER root
|
| 4 |
+
RUN apk add --no-cache python3 py3-pip \
|
| 5 |
+
&& pip3 install --no-cache-dir "psycopg[binary]"
|
| 6 |
+
USER node
|
| 7 |
+
|
| 8 |
+
COPY update_tags.py /home/node/update_tags.py
|
update_tags.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, sys, json
|
| 2 |
+
import psycopg
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def main():
|
| 6 |
+
raw = sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()
|
| 7 |
+
payload = json.loads(raw)
|
| 8 |
+
|
| 9 |
+
action = payload.get("action")
|
| 10 |
+
cohort_name = payload.get("parameters", {}).get("mixpanel_cohort_name")
|
| 11 |
+
members = payload.get("parameters", {}).get("members", [])
|
| 12 |
+
|
| 13 |
+
if not (action and cohort_name and members):
|
| 14 |
+
print(json.dumps({"status": "skip", "reason": "missing fields"}))
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
conn = psycopg.connect(
|
| 18 |
+
host=os.getenv("PGHOST", "postgres"),
|
| 19 |
+
port=os.getenv("PGPORT", "5432"),
|
| 20 |
+
dbname=os.getenv("PGDATABASE", "n8n_db"),
|
| 21 |
+
user=os.getenv("PGUSER", "n8n_user"),
|
| 22 |
+
password=os.getenv("PGPASSWORD", "n8n_pass"),
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
results = []
|
| 26 |
+
with conn:
|
| 27 |
+
with conn.cursor() as cur:
|
| 28 |
+
for m in members:
|
| 29 |
+
uuid = m.get("$distinct_id") or m.get("mixpanel_distinct_id")
|
| 30 |
+
if not uuid:
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
+
if action == "add_members":
|
| 34 |
+
cur.execute("""
|
| 35 |
+
UPDATE users u
|
| 36 |
+
SET tags = CASE
|
| 37 |
+
WHEN NOT (%s = ANY(u.tags))
|
| 38 |
+
THEN array_append(u.tags, %s)
|
| 39 |
+
ELSE u.tags
|
| 40 |
+
END,
|
| 41 |
+
updated_at = now()
|
| 42 |
+
WHERE u.uuid = %s
|
| 43 |
+
RETURNING u.uuid, u.tags;
|
| 44 |
+
""", (cohort_name, cohort_name, uuid))
|
| 45 |
+
elif action == "remove_members":
|
| 46 |
+
cur.execute("""
|
| 47 |
+
UPDATE users u
|
| 48 |
+
SET tags = CASE
|
| 49 |
+
WHEN %s = ANY(u.tags)
|
| 50 |
+
THEN array_remove(u.tags, %s)
|
| 51 |
+
ELSE u.tags
|
| 52 |
+
END,
|
| 53 |
+
updated_at = now()
|
| 54 |
+
WHERE u.uuid = %s
|
| 55 |
+
RETURNING u.uuid, u.tags;
|
| 56 |
+
""", (cohort_name, cohort_name, uuid))
|
| 57 |
+
|
| 58 |
+
row = cur.fetchone()
|
| 59 |
+
if row:
|
| 60 |
+
results.append({"uuid": row[0], "tags": row[1]})
|
| 61 |
+
|
| 62 |
+
conn.close()
|
| 63 |
+
print(json.dumps({"status": "ok", "count": len(results), "results": results}))
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
main()
|