From 93346d54ef27c0e6e27535f1400844c74b618012 Mon Sep 17 00:00:00 2001
From: Pierre Ozoux <pierre@ozoux.net>
Date: Mon, 24 Apr 2023 15:32:12 +0200
Subject: [PATCH] feat: Adds a way to count users

---
 nextcloud/manifests/cron-count-users.yml  | 43 +++++++++++++++++++++
 rocketchat/manifests/cron-count-users.yml | 47 +++++++++++++++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 nextcloud/manifests/cron-count-users.yml
 create mode 100644 rocketchat/manifests/cron-count-users.yml

diff --git a/nextcloud/manifests/cron-count-users.yml b/nextcloud/manifests/cron-count-users.yml
new file mode 100644
index 0000000..fabf1fa
--- /dev/null
+++ b/nextcloud/manifests/cron-count-users.yml
@@ -0,0 +1,43 @@
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  labels:
+    app: nextcloud
+    component: count-active-users
+  name: ${FQDN}-count
+  namespace: ${NS}
+spec:
+  concurrencyPolicy: Forbid
+  failedJobsHistoryLimit: 1
+  schedule: '15 15 * * *'
+  jobTemplate:
+    spec:
+      template:
+        metadata:
+          labels:
+            app: nextcloud
+            component: count-active-users
+        spec:
+          restartPolicy: Never
+          containers:
+            - name: psql
+              image: postgres:12-alpine
+              command:
+                - sh
+                - -c
+                - >
+                    echo {\"30days\":`psql -t -c "select count(*) from preferences where configkey = 'lastLogin' and to_timestamp("configvalue"::integer) > ('now'::timestamp - '1 month'::interval);" | xargs`,\"90days\":`psql -t -c "select count(*) from preferences where configkey = 'lastLogin' and to_timestamp("configvalue"::integer) > ('now'::timestamp - '3 month'::interval);" | xargs`}
+              env:
+              - name: PGHOST
+                value: pg-${NS}
+              - name: PGDATABASE
+                value: nextcloud
+              - name: PGUSER
+                value: postgres
+              - name: PGPASSWORD
+                valueFrom:
+                  secretKeyRef:
+                    key: password
+                    name: postgres.pg-${PG_DOMAIN}.credentials
+#psql -h pg-desami-es -U postgres nextcloud -t -c "select count(*) from preferences where configkey = 'lastLogin' and to_timestamp("configvalue"::integer) > ('now'::timestamp - '1 month'::interval);"
+#psql -t -c "select count(*) from preferences where configkey = 'lastLogin' and to_timestamp("configvalue"::integer) > ('now'::timestamp - '1 month'::interval);"
diff --git a/rocketchat/manifests/cron-count-users.yml b/rocketchat/manifests/cron-count-users.yml
new file mode 100644
index 0000000..a88746f
--- /dev/null
+++ b/rocketchat/manifests/cron-count-users.yml
@@ -0,0 +1,47 @@
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  labels:
+    app: Rocketchat
+    component: count-active-users
+  name: ${FQDN}-count
+  namespace: ${NS}
+spec:
+  concurrencyPolicy: Forbid
+  failedJobsHistoryLimit: 1
+  schedule: '10 15 * * *'
+  jobTemplate:
+    spec:
+      template:
+        metadata:
+          labels:
+            app: Rocketchat
+            component: count-active-users
+        spec:
+          restartPolicy: Never
+          containers:
+            - name: wrench
+              image: libresh/wrench
+              args:
+                - rc
+                - user
+                - count
+                - --mongo-url=$(MONGO_URL)
+                - --active
+                - --last-login=30
+              env:
+                - name: MONGO_URL
+                  valueFrom:
+                    secretKeyRef:
+                      key: MONGO_URL
+                      name: ${FQDN}-app
+                - name: ADMIN_USER
+                  valueFrom:
+                    configMapKeyRef:
+                      key: ADMIN_USERNAME
+                      name: ${FQDN}-config
+                - name: ADMIN_PASS
+                  valueFrom:
+                    secretKeyRef:
+                      key: ADMIN_PASS
+                      name: ${FQDN}-app
-- 
GitLab