Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free Disk Space #794

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Database {
// Change to WAL
await R.exec("PRAGMA journal_mode = WAL");
await R.exec("PRAGMA cache_size = -12000");
await R.exec("PRAGMA auto_vacuum = FULL");

console.log("SQLite config:");
console.log(await R.getAll("PRAGMA journal_mode"));
Expand Down Expand Up @@ -374,6 +375,17 @@ class Database {
console.log("Nothing to restore");
}
}

static getSize() {
debug("Database.getSize()");
let stats = fs.statSync(Database.path);
debug(stats);
return stats.size;
}

static async shrink() {
await R.exec("VACUUM");
}
}

module.exports = Database;
2 changes: 2 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module.exports.io = io;
// Must be after io instantiation
const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo } = require("./client");
const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler");
const databaseSocketHandler = require("./socket-handlers/database-socket-handler");

app.use(express.json());

Expand Down Expand Up @@ -1295,6 +1296,7 @@ exports.entryPage = "dashboard";

// Status Page Socket Handler for admin only
statusPageSocketHandler(socket);
databaseSocketHandler(socket);

debug("added all socket handlers");

Expand Down
37 changes: 37 additions & 0 deletions server/socket-handlers/database-socket-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { checkLogin } = require("../util-server");
const Database = require("../database");

module.exports = (socket) => {

// Post or edit incident
socket.on("getDatabaseSize", async (callback) => {
try {
checkLogin(socket);
callback({
ok: true,
size: Database.getSize(),
});
} catch (error) {
callback({
ok: false,
msg: error.message,
});
}
});

socket.on("shrinkDatabase", async (callback) => {
try {
checkLogin(socket);
Database.shrink();
callback({
ok: true,
});
} catch (error) {
callback({
ok: false,
msg: error.message,
});
}
});

};
41 changes: 40 additions & 1 deletion src/pages/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,15 @@
{{ importAlert }}
</div>

<!-- Advanced -->
<h2 class="mt-5 mb-2">{{ $t("Advanced") }}</h2>

<div class="mb-3">
<button v-if="settings.disableAuth" class="btn btn-outline-primary me-2 mb-2" @click="enableAuth">{{ $t("Enable Auth") }}</button>
<button v-if="! settings.disableAuth" class="btn btn-primary me-2 mb-2" @click="confirmDisableAuth">{{ $t("Disable Auth") }}</button>
<button v-if="! settings.disableAuth" class="btn btn-danger me-2 mb-2" @click="$root.logout">{{ $t("Logout") }}</button>
<button class="btn btn-outline-danger me-1 mb-1" @click="confirmClearStatistics">{{ $t("Clear all statistics") }}</button>
<button class="btn btn-outline-danger me-2 mb-2" @click="confirmClearStatistics">{{ $t("Clear all statistics") }}</button>
<button class="btn btn-info me-2 mb-2" @click="shrinkDatabase">{{ $t("Shrink Database") }} ({{ databaseSizeDisplay }})</button>
</div>
</template>
</div>
Expand Down Expand Up @@ -418,6 +420,7 @@ dayjs.extend(timezone);

import { timezoneList, setPageLocale } from "../util-frontend";
import { useToast } from "vue-toastification";
import { debug } from "../util.ts";

const toast = useToast();

Expand All @@ -427,6 +430,7 @@ export default {
TwoFADialog,
Confirm,
},

data() {
return {
timezoneList: timezoneList(),
Expand All @@ -445,8 +449,16 @@ export default {
importAlert: null,
importHandle: "skip",
processing: false,
databaseSize: 0,
};
},

computed: {
databaseSizeDisplay() {
return Math.round(this.databaseSize / 1024 / 1024 * 10) / 10 + " MB";
}
},

watch: {
"password.repeatNewPassword"() {
this.invalidPassword = false;
Expand All @@ -460,6 +472,7 @@ export default {

mounted() {
this.loadSettings();
this.loadDatabaseSize();
},

methods: {
Expand Down Expand Up @@ -592,7 +605,33 @@ export default {

autoGetPrimaryBaseURL() {
this.settings.primaryBaseURL = location.protocol + "//" + location.host;
},

shrinkDatabase() {
this.$root.getSocket().emit("shrinkDatabase", (res) => {
if (res.ok) {
this.loadDatabaseSize();
toast.success("Done");
} else {
debug(res);
}
});
},

loadDatabaseSize() {
debug("load database size");
this.$root.getSocket().emit("getDatabaseSize", (res) => {

if (res.ok) {
this.databaseSize = res.size;
debug("database size: " + res.size);
} else {
debug(res);
}

});
}

},
};
</script>
Expand Down