|
| 1 | +const { computed, h, Dict, map, dictToCollection, onceTrue } = require("mutant"); |
| 2 | +const nest = require("depnest"); |
| 3 | +const renderProgress = require("../../../../progress/html/render"); |
| 4 | +const fs = require("fs") |
| 5 | +const Path = require("path") |
| 6 | +const electron = require("electron") |
| 7 | + |
| 8 | +exports.needs = nest({ |
| 9 | + "sbot.pull.stream": "first", |
| 10 | + "sbot.obs.connection": "first", |
| 11 | + "config.sync.load": "first", |
| 12 | + "progress.obs": { |
| 13 | + indexes: "first", |
| 14 | + plugins: "first", |
| 15 | + replicate: "first", |
| 16 | + migration: "first", |
| 17 | + peer: "first", |
| 18 | + }, |
| 19 | + "intl.sync.i18n": "first", |
| 20 | +}); |
| 21 | + |
| 22 | +exports.gives = nest("page.html.render"); |
| 23 | + |
| 24 | +exports.create = function (api) { |
| 25 | + return nest("page.html.render", function channel(path) { |
| 26 | + const indexes = api.progress.obs.indexes(); |
| 27 | + const indexProgress = computed(indexes, calcProgress); |
| 28 | + const pluginIndexes = api.progress.obs.plugins(); |
| 29 | + const peer = api.progress.obs.peer(); |
| 30 | + const indexesJson = computed( |
| 31 | + [indexes, pluginIndexes], |
| 32 | + (indexes, plugins) => { |
| 33 | + return JSON.stringify({ indexes, plugins }, null, 4); |
| 34 | + }, |
| 35 | + ); |
| 36 | + const pluginProgress = computed( |
| 37 | + [indexes, pluginIndexes], |
| 38 | + (indexes, plugins) => { |
| 39 | + const keys = Object.keys(plugins); |
| 40 | + const result = []; |
| 41 | + keys.forEach((k) => { |
| 42 | + const obj = { |
| 43 | + target: indexes.target, |
| 44 | + start: indexes.start, |
| 45 | + current: plugins[k], |
| 46 | + }; |
| 47 | + result.push([k, calcProgress(obj)]); |
| 48 | + }); |
| 49 | + return result; |
| 50 | + }, |
| 51 | + ); |
| 52 | + const statusObj = computed([peer], (peer) => { |
| 53 | + return JSON.stringify(peer, null, 4); |
| 54 | + }); |
| 55 | + const replicateProgress = api.progress.obs.replicate(); |
| 56 | + const migration = api.progress.obs.migration(); |
| 57 | + const migrationProgress = computed(migration, calcProgress); |
| 58 | + const config = api.config.sync.load() |
| 59 | + |
| 60 | + function clamp(value) { |
| 61 | + return Math.min(1, Math.max(0, value)) || 0; |
| 62 | + } |
| 63 | + |
| 64 | + function calcProgress(progress) { |
| 65 | + const range = progress.target - progress.start; |
| 66 | + if (range) { |
| 67 | + return (progress.current - progress.start) / range; |
| 68 | + } else { |
| 69 | + return 1; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function rebuildAllIndexes() { |
| 74 | + onceTrue(api.sbot.obs.connection, (ssb) => { |
| 75 | + ssb.rebuild((err) => { |
| 76 | + console.log(err) |
| 77 | + }) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + function deletePrivateIndex() { |
| 82 | + console.dir(config) |
| 83 | + const flumePath = Path.join(config.path, "flume") |
| 84 | + const toUrlFriendly = require('base64-url').escape |
| 85 | + const key = `private-${toUrlFriendly(config.keys.public.slice(0, 9))}` |
| 86 | + const indexPath = Path.join(flumePath, key) |
| 87 | + console.log(indexPath) |
| 88 | + if (fs.existsSync(indexPath)) { |
| 89 | + fs.rmSync(indexPath, {recursive: true, force: true}) |
| 90 | + } |
| 91 | + electron.ipcRenderer.send("relaunch-app") |
| 92 | + } |
| 93 | + |
| 94 | + if (path !== "/troubleshooting-tools") return; |
| 95 | + const i18n = api.intl.sync.i18n; |
| 96 | + |
| 97 | + |
| 98 | + const prepend = [ |
| 99 | + h("PageHeading", [ |
| 100 | + h("h1", [ |
| 101 | + h("strong", i18n("Troubleshooting Tools")), |
| 102 | + ]), |
| 103 | + ]), |
| 104 | + ]; |
| 105 | + |
| 106 | + return h("Scroller", { style: { overflow: "auto" } }, [ |
| 107 | + h("div.wrapper", [ |
| 108 | + h("section.prepend", prepend), |
| 109 | + h("section.content", [ |
| 110 | + h("TroubleshootingTools", [ |
| 111 | + // TOOLS |
| 112 | + h("div.col", [ |
| 113 | + h("section", [ |
| 114 | + h("h2", "Indexing Tools"), |
| 115 | + // rebuild all indexes |
| 116 | + h("button", { |
| 117 | + "ev-click": rebuildAllIndexes |
| 118 | + }, "Rebuild all indexes"), |
| 119 | + h("span", "This can take a very long time."), |
| 120 | + // delete private index |
| 121 | + h("button", { |
| 122 | + "ev-click": deletePrivateIndex |
| 123 | + }, "Delete private index"), |
| 124 | + h("span", "This will relaunch Patchwork.") |
| 125 | + ]) |
| 126 | + ]), |
| 127 | + // REPORT ON INDEXES |
| 128 | + h("div.col", [ |
| 129 | + h("h2", i18n("Indexing Report")), |
| 130 | + h("IndexingReport", [ |
| 131 | + h("ReportItem", [ |
| 132 | + h("span.info", "Index"), |
| 133 | + h("meter", { |
| 134 | + style: { "margin-left": "10px" }, |
| 135 | + min: 0, |
| 136 | + max: 1, |
| 137 | + low: 0.3, |
| 138 | + high: 0.6, |
| 139 | + optimum: 0.9, |
| 140 | + value: indexProgress, |
| 141 | + }), |
| 142 | + ]), |
| 143 | + |
| 144 | + map(pluginProgress, (item) => { |
| 145 | + return h("ReportItem", [ |
| 146 | + h("span.info", item[0]), |
| 147 | + h("meter", { |
| 148 | + style: { "margin-left": "10px" }, |
| 149 | + min: 0, |
| 150 | + max: 1, |
| 151 | + low: 0.5, |
| 152 | + high: 0.9, |
| 153 | + optimum: 0.97, |
| 154 | + value: item[1], |
| 155 | + }), |
| 156 | + ]); |
| 157 | + }), |
| 158 | + ]), |
| 159 | + h("h2", "Raw data"), |
| 160 | + h("pre", [indexesJson]), |
| 161 | + h("h2", i18n("Extra Statuses")), |
| 162 | + h("pre", [statusObj]), |
| 163 | + ]), |
| 164 | + ]), |
| 165 | + ]), |
| 166 | + ]), |
| 167 | + ]); |
| 168 | + }); |
| 169 | +}; |
0 commit comments