From bd3340f2e7fa5825da7f313622faef686cda17ab Mon Sep 17 00:00:00 2001
From: Leonardo Arena <rnalrd@alpinelinux.org>
Date: Wed, 25 Jul 2018 09:49:12 +0000
Subject: [PATCH] non-free/unifi: add DB cron job

(disabled by default)
---
 non-free/unifi/APKBUILD            | 12 ++++-
 non-free/unifi/mongo_prune.js      | 87 ++++++++++++++++++++++++++++++
 non-free/unifi/unifi_db_prune.cron |  2 +
 3 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 non-free/unifi/mongo_prune.js
 create mode 100755 non-free/unifi/unifi_db_prune.cron

diff --git a/non-free/unifi/APKBUILD b/non-free/unifi/APKBUILD
index 25fab6f580b1..2664bf5feb98 100644
--- a/non-free/unifi/APKBUILD
+++ b/non-free/unifi/APKBUILD
@@ -4,7 +4,7 @@ pkgname=unifi
 _pkgname=UniFi
 pkgver=5.8.23
 _pkgver=d5a5bbfda4
-pkgrel=0
+pkgrel=1
 pkgdesc="The Ubiquiti UniFi network controller"
 url="http://wiki.ubnt.com/UniFi_FAQ"
 arch="noarch"
@@ -19,6 +19,8 @@ source="$pkgname-$pkgver.zip::https://dl.ubnt.com/unifi/$pkgver-$_pkgver/$_pkgna
 	$pkgname.initd
 	$pkgname.confd
 	$pkgname.logrotated
+	mongo_prune.js
+	unifi_db_prune.cron
 	"
 builddir="$srcdir/$_pkgname"
 
@@ -79,9 +81,15 @@ package() {
 
 	# tell search engines to do not index
 	echo -e "User-Agent:*\nDisallow:/" > "$webapps"/$pkgname/ROOT/robots.txt
+
+	# install purging DB job (disabled by default)
+	install -Dm644 "$srcdir"/mongo_prune.js "$pkgdir"/etc/unifi/mongo_prune.js
+	install -Dm755 "$srcdir"/unifi_db_prune.cron "$pkgdir"/etc/periodic/weekly/unifi_db_prune
 }
 
 sha512sums="2b42df2a84574ac9e282c7ff59eb86ce29170ea7ef5957690dec8772c90acda3c08c633b3bceeac08bb73ff1460143207a64d2405de19436faf20a042849231b  unifi-5.8.23.zip
 b19a7d684ef2ec7c4159417c21185ccd8ce498da25405b69014fdb32e346a0077f7edc5dfc994481d12936aa8dbf22e6baf29571fd0003aaad19609d24c549f4  unifi.initd
 d339555a91de7488badbedf8a4c85cff878e7d0720a8cf6a8340f51f3666dcf4878b47a1fff4c9c2846d7af140d11e48e898f8c4dba1f81c1004b76a81f0821e  unifi.confd
-9e54d9e1c720b8e50c9af9363105f6ea9ff2cffff7dc67477a7701aacf21ba977424fe9fbaba6a00d5f64310a2e2517e9328a7acb91a4bc06ed237139a8e0d9b  unifi.logrotated"
+9e54d9e1c720b8e50c9af9363105f6ea9ff2cffff7dc67477a7701aacf21ba977424fe9fbaba6a00d5f64310a2e2517e9328a7acb91a4bc06ed237139a8e0d9b  unifi.logrotated
+3eec528077af9b32aa065610b4ffc50537a1c851c84624eaa63287787d853c0f8c953a97bd58bc6c2cab7f9f26eaff8c8b2815a84454169f7e0d96b94dfc91ee  mongo_prune.js
+25d66b144403676bcb6142f4942b62c6f969a6b359ccb4dd4fcb826b1e57f902179d01182369704970b2d6094b8b3789fd375c17cec908cac53f7abf01fb2e1b  unifi_db_prune.cron"
diff --git a/non-free/unifi/mongo_prune.js b/non-free/unifi/mongo_prune.js
new file mode 100644
index 000000000000..421c3f1438a5
--- /dev/null
+++ b/non-free/unifi/mongo_prune.js
@@ -0,0 +1,87 @@
+// keep N-day worth of data
+var days=14;
+
+// change to false to have the script to really exclude old records
+// from the database. While true, no change at all will be made to the DB
+var dryrun=true;
+
+var now = new Date().getTime(),
+	time_criteria = now - days * 86400 * 1000,
+	time_criteria_in_seconds = time_criteria / 1000;
+
+print((dryrun ? "[dryrun] " : "") + "pruning data older than " + days + " days (" + time_criteria + ")... ");
+
+use ace;
+var collectionNames = db.getCollectionNames();
+for (i=0; i<collectionNames.length; i++) {
+	var name = collectionNames[i];
+	var query = null;
+
+	if (name === 'event' || name === 'alarm') {
+		query = {time: {$lt:time_criteria}};
+	}
+
+	// rogue ap
+	if (name === 'rogue') {
+		query = {last_seen: {$lt:time_criteria_in_seconds}};
+	}
+
+	// removes vouchers expired more than '$days' ago
+	// active and unused vouchers are NOT touched
+	if (name === 'voucher') {
+		query = {end_time: {$lt:time_criteria_in_seconds}};
+	}
+
+	// guest authorization
+	if (name === 'guest') {
+		query = {end: {$lt:time_criteria_in_seconds}};
+	}
+
+	// if an user was only seen ONCE, $last_seen will not be defined
+	// so, if $last_seen not defined, lets use $first_seen instead
+	// also check if $blocked or $use_fixedip is set. If true, do NOT purge the
+	// entry no matter how old it is. We want blocked/fixed_ip users to continue
+	// blocked/fixed_ip
+	if (name === 'user') {
+		query = { blocked: { $ne: true}, use_fixedip: { $ne: true}, $or: [
+				{last_seen: {$lt:time_criteria_in_seconds} },
+				{last_seen: {$exists: false}, first_seen: {$lt:time_criteria_in_seconds} }
+			]
+		};
+	}
+
+	if (query) {
+		count1 = db.getCollection(name).count();
+		count2 = db.getCollection(name).find(query).count();
+		print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... ");
+		if (!dryrun) {
+			db.getCollection(name).remove(query);
+			db.runCommand({ compact: name });
+		}
+	}
+}
+
+use ace_stat;
+var collectionNames = db.getCollectionNames();
+for (i=0; i<collectionNames.length; i++) {
+	var name = collectionNames[i];
+	var query = null;
+
+	// historical stats (stat.*)
+	if (name.indexOf('stat')==0) {
+		query = {time: {$lt:time_criteria}};
+	}
+
+	if (query) {
+		count1 = db.getCollection(name).count();
+		count2 = db.getCollection(name).find(query).count();
+		print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... ");
+		if (!dryrun) {
+			db.getCollection(name).remove(query);
+			db.runCommand({ compact: name });
+		}
+	}
+}
+
+if (!dryrun) db.repairDatabase();
+
diff --git a/non-free/unifi/unifi_db_prune.cron b/non-free/unifi/unifi_db_prune.cron
new file mode 100755
index 000000000000..d5338835a552
--- /dev/null
+++ b/non-free/unifi/unifi_db_prune.cron
@@ -0,0 +1,2 @@
+#!/bin/sh
+mongo --port 27117 < /etc/unifi/mongo_prune.js
-- 
GitLab