From 25963566cae286685263cbe496f5f185d5549406 Mon Sep 17 00:00:00 2001
From: mrfry <mr.fry@tutanota.com>
Date: Wed, 11 Aug 2021 12:40:19 +0200
Subject: [PATCH 1/2] Docker fixes

---
 scripts/runDocker.sh | 10 ++++++----
 src/server.ts        |  4 ++--
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/scripts/runDocker.sh b/scripts/runDocker.sh
index e51fab1..d178e8c 100755
--- a/scripts/runDocker.sh
+++ b/scripts/runDocker.sh
@@ -26,11 +26,13 @@ elif [ "$1" == "start" ]; then
 elif [ "$1" == "run" ]; then
   # --entrypoint bash \
   scripts/setup.sh
+
   docker run -it \
-    -p 8080:80 \
-    --mount src="$(pwd)/stats",target=/server/stats,type=bind \
-    --mount src="$(pwd)/data",target=/server/data,type=bind \
-    --mount src="$(pwd)/publicDirs",target=/server/publicDirs,type=bind \
+    --network host \
+    --mount src="$(pwd)/stats",target="/server/stats",type=bind \
+    --mount src="$(pwd)/data",target="/server/data",type=bind \
+    --mount src="$(pwd)/publicDirs",target="/server/publicDirs",type=bind \
+    --mount src="/etc/letsencrypt",target="/etc/letsencrypt",type=bind \
     qminer/mrfrys-node-server
 elif [ "$1" == "bash" ]; then
   docker exec -it $(getContainerId) /bin/bash
diff --git a/src/server.ts b/src/server.ts
index 70491e2..79f3aef 100755
--- a/src/server.ts
+++ b/src/server.ts
@@ -24,8 +24,8 @@ console.log('Current working directory: ' + process.cwd())
 const startHTTPS = true
 const isRoot = process.getuid && process.getuid() === 0
 
-const port = isRoot ? 80 : 8080
-const httpsport = isRoot ? 443 : 5001
+const port = 8080
+const httpsport = 5001
 
 // import os from 'os'
 // os.setPriority(10)

From e17760a8db0c8e6b1957dcfe955c100d018c37de Mon Sep 17 00:00:00 2001
From: mrfry <mr.fry@tutanota.com>
Date: Wed, 22 Sep 2021 14:41:09 +0200
Subject: [PATCH 2/2] Docker and stat script changes

---
 .dockerignore          |   1 +
 scripts/make.sh        |  12 +-
 scripts/runDocker.sh   |   4 +-
 scripts/serverStats.js | 294 ++++++++++++++++++++++-------------------
 scripts/start.sh       |   2 +-
 5 files changed, 165 insertions(+), 148 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index 75af1a9..203a62c 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -4,5 +4,6 @@ dist
 stats
 data
 publicDirs
+nextStatic
 devel
 .git
diff --git a/scripts/make.sh b/scripts/make.sh
index 44ee1b7..056dec1 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -1,7 +1,9 @@
 #!/bin/bash
 
 hr() {
-  printf '\033[0;32m%*s\033[0m\n' "$(tput cols)" '' | tr ' ' '='
+  if [ -t 0 ]; then
+    printf '\033[0;32m%*s\033[0m\n' "$(tput cols)" '' | tr ' ' '='
+  fi
 }
 
 log() {
@@ -75,16 +77,14 @@ touch publicDirs/qminingPublic/version
 touch publicDirs/qminingPublic/motd
 
 if [ -z "$(ls -A ./data/dbs)" ]; then
+  log "Making DB-s"
   pushd src/standaloneUtils
   NS_LOGLEVEL=2 node dbSetup.js
   mv *.db ../../data/dbs
   popd
 fi
 
+log "Disabling next telemetry"
 npx --yes next telemetry disable
 
-hr
-echo "Done!"
-echo "npm start {loglevel}"
-echo "To start server"
-hr
+log "Done!"
diff --git a/scripts/runDocker.sh b/scripts/runDocker.sh
index d178e8c..73cd7fa 100755
--- a/scripts/runDocker.sh
+++ b/scripts/runDocker.sh
@@ -22,12 +22,12 @@ elif [ "$1" == "stop" ]; then
 elif [ "$1" == "start" ]; then
   # --entrypoint bash \
   scripts/setup.sh
-  docker start -i $(getContainerId)
+  docker start $(getContainerId)
 elif [ "$1" == "run" ]; then
   # --entrypoint bash \
   scripts/setup.sh
 
-  docker run -it \
+  docker run \
     --network host \
     --mount src="$(pwd)/stats",target="/server/stats",type=bind \
     --mount src="$(pwd)/data",target="/server/data",type=bind \
diff --git a/scripts/serverStats.js b/scripts/serverStats.js
index 0eb907d..671df7c 100644
--- a/scripts/serverStats.js
+++ b/scripts/serverStats.js
@@ -74,7 +74,8 @@ const filterFromDailyStats = [
   '.xml',
   '.aspx',
   '/questionDbs/',
-  '/chatFiles/'
+  '/chatFiles/',
+  'rss'
 ]
 
 // -----------------------------------------------------------------
@@ -291,165 +292,180 @@ function pCols(cols, rowTitles, colorNames, firstRowColor) {
 
 // ------------------------------------------------------------------------------
 printHeader('Daily stats')
-
-const dailyStats = readJSON(`${dir}stats/vstats`)
-function preProcessDailyStats(obj) {
-  const formatted = Object.keys(obj).reduce((acc, key) => {
-    const includes = filterFromDailyStats.some((keyword) => {
-      return key.toLowerCase().includes(keyword.toLowerCase())
-    })
-    if (!includes) {
-      acc.push({ name: key.replace(/\.html/g, ''), val: obj[key] })
-    }
-    return acc
-  }, [])
-
-  const merged = formatted.reduce((acc, x) => {
-    const index = acc.findIndex((y) => {
-      return x.name === y.name
-    })
-
-    if (index !== -1) {
-      acc = acc.map((z, i) => {
-        if (i === index) {
-          return {
-            ...x,
-            val: z.val + x.val,
-          }
-        }
-        return z
+try {
+  const dailyStats = readJSON(`${dir}stats/vstats`)
+  function preProcessDailyStats(obj) {
+    const formatted = Object.keys(obj).reduce((acc, key) => {
+      const includes = filterFromDailyStats.some((keyword) => {
+        return key.toLowerCase().includes(keyword.toLowerCase())
       })
-    } else {
-      acc.push(x)
-    }
+      if (!includes) {
+        acc.push({ name: key.replace(/\.html/g, ''), val: obj[key] })
+      }
+      return acc
+    }, [])
 
-    return acc
-  }, [])
+    const merged = formatted.reduce((acc, x) => {
+      const index = acc.findIndex((y) => {
+        return x.name === y.name
+      })
 
-  return merged.sort((a, b) => {
-    if (a.name > b.name) {
-      return 1
-    } else if (a.name < b.name) {
-      return -1
-    } else {
-      return 0
-    }
-  })
+      if (index !== -1) {
+        acc = acc.map((z, i) => {
+          if (i === index) {
+            return {
+              ...x,
+              val: z.val + x.val,
+            }
+          }
+          return z
+        })
+      } else {
+        acc.push(x)
+      }
+
+      return acc
+    }, [])
+
+    return merged.sort((a, b) => {
+      if (a.name > b.name) {
+        return 1
+      } else if (a.name < b.name) {
+        return -1
+      } else {
+        return 0
+      }
+    })
+  }
+  function getDailyStat(day) {
+    return preProcessDailyStats(dailyStats[getDayIndex(day)])
+  }
+
+  pCols(
+    [
+      ...[...Array(colsToPrint).keys()].map((x) => {
+        return getDailyStat(-x)
+      }),
+    ],
+    null,
+    true
+  )
+} catch (e) {
+  console.error(e)
 }
-function getDailyStat(day) {
-  return preProcessDailyStats(dailyStats[getDayIndex(day)])
-}
-
-pCols(
-  [
-    ...[...Array(colsToPrint).keys()].map((x) => {
-      return getDailyStat(-x)
-    }),
-  ],
-  null,
-  true
-)
 
 // ------------------------------------------------------------------------------
 printHeader('User id test solving')
-const userIdTestSolving = readJSON(`${dir}stats/idvstats`)
-function preProcessUIdTestSolving(obj, minLength) {
-  if (!obj) {
-    return '0'
+try {
+  const userIdTestSolving = readJSON(`${dir}stats/idvstats`)
+  function preProcessUIdTestSolving(obj, minLength) {
+    if (!obj) {
+      return '0'
+    }
+    if (minLength) {
+      return Object.keys(obj)
+        .filter((key) => {
+          return obj[key] > minLength
+        })
+        .length.toString()
+    } else {
+      return Object.keys(obj).length.toString()
+    }
   }
-  if (minLength) {
-    return Object.keys(obj)
-      .filter((key) => {
-        return obj[key] > minLength
-      })
-      .length.toString()
-  } else {
-    return Object.keys(obj).length.toString()
+  function getUserIdTestSolving(day) {
+    return [
+      getDayName(day),
+      preProcessUIdTestSolving(userIdTestSolving[getDayIndex(day)]),
+    ]
   }
-}
-function getUserIdTestSolving(day) {
-  return [
-    getDayName(day),
-    preProcessUIdTestSolving(userIdTestSolving[getDayIndex(day)]),
-  ]
-}
 
-pCols(
-  [
-    ...[...Array(colsToPrint).keys()].map((x) => {
-      return getUserIdTestSolving(-x)
-    }),
-  ],
-  null,
-  false,
-  'green'
-)
+  pCols(
+    [
+      ...[...Array(colsToPrint).keys()].map((x) => {
+        return getUserIdTestSolving(-x)
+      }),
+    ],
+    null,
+    false,
+    'green'
+  )
+} catch(e) {
+  console.error(e)
+}
 
 // ------------------------------------------------------------------------------
 printHeader('User id requests')
-const clientIdTestSolving = readJSON(`${dir}stats/uvstats`)
-function getUserIdRequests(day) {
-  return [
-    getDayName(day),
-    preProcessUIdTestSolving(clientIdTestSolving[getDayIndex(day)]),
-    preProcessUIdTestSolving(clientIdTestSolving[getDayIndex(day)], 5),
-  ]
-}
+try {
+  const clientIdTestSolving = readJSON(`${dir}stats/uvstats`)
+  function getUserIdRequests(day) {
+    return [
+      getDayName(day),
+      preProcessUIdTestSolving(clientIdTestSolving[getDayIndex(day)]),
+      preProcessUIdTestSolving(clientIdTestSolving[getDayIndex(day)], 5),
+    ]
+  }
 
-pCols(
-  [
-    ...[...Array(colsToPrint).keys()].map((x) => {
-      return getUserIdRequests(-x)
-    }),
-  ],
-  ['', 'All', 'More than 5'],
-  false,
-  'green'
-)
+  pCols(
+    [
+      ...[...Array(colsToPrint).keys()].map((x) => {
+        return getUserIdRequests(-x)
+      }),
+    ],
+    ['', 'All', 'More than 5'],
+    false,
+    'green'
+  )
 
-// ------------------------------------------------------------------------------
-printHeader('Daily data count')
-const dailyDataCount = readFile(`${dir}stats/dailyDataCount`)
-function getDailyDataCount(count) {
-  return [...Array(count).keys()].map((x) => {
-    return JSON.parse(head(tail(dailyDataCount, x + 1), 1))
-  })
-}
+  // ------------------------------------------------------------------------------
+  printHeader('Daily data count')
+  const dailyDataCount = readFile(`${dir}stats/dailyDataCount`)
+  function getDailyDataCount(count) {
+    return [...Array(count).keys()].map((x) => {
+      return JSON.parse(head(tail(dailyDataCount, x + 1), 1))
+    })
+  }
 
-printLastDataCount(getDailyDataCount(colsToPrint))
+  printLastDataCount(getDailyDataCount(colsToPrint))
 
-function printLastDataCount(data) {
-  const res = [...Array(colsToPrint).keys()].map((x) => {
-    return [getDayName(-x)]
-  })
-  data.forEach((dataCount, i) => {
-    res[i].push(dataCount.userCount.toString())
-    res[i].push(dataCount.subjectCount.toString())
-    res[i].push(dataCount.questionCount.toString())
-  })
+  function printLastDataCount(data) {
+    const res = [...Array(colsToPrint).keys()].map((x) => {
+      return [getDayName(-x)]
+    })
+    data.forEach((dataCount, i) => {
+      res[i].push(dataCount.userCount.toString())
+      res[i].push(dataCount.subjectCount.toString())
+      res[i].push(dataCount.questionCount.toString())
+    })
 
-  pCols(res, ['', 'Users', 'Subjects', 'Questions'], false, 'green')
+    pCols(res, ['', 'Users', 'Subjects', 'Questions'], false, 'green')
+  }
+} catch(e) {
+  console.error(e)
 }
 // ------------------------------------------------------------------------------
 printHeader('Daily script install / update check count')
-function getDailyScriptStat(day) {
-  const log =
-    !day || day === 0
-      ? readFile(`${dir}stats/vlogs/log`)
-      : readFile(`${dir}stats/vlogs/${getDayIndex(day)}`)
+try {
+  function getDailyScriptStat(day) {
+    const log =
+      !day || day === 0
+        ? readFile(`${dir}stats/vlogs/log`)
+        : readFile(`${dir}stats/vlogs/${getDayIndex(day)}`)
 
-  if (!log) {
-    return [getDayName(day), 0, 0]
+    if (!log) {
+      return [getDayName(day), 0, 0]
+    }
+    return [
+      getDayName(day),
+      countLinesMatching(log, '?install').toString(),
+      countLinesMatching(log, '?up').toString(),
+    ]
   }
-  return [
-    getDayName(day),
-    countLinesMatching(log, '?install').toString(),
-    countLinesMatching(log, '?up').toString(),
-  ]
+
+  const installs = [...Array(colsToPrint).keys()].map((x) => {
+    return getDailyScriptStat(-x)
+  })
+
+  pCols(installs, ['', 'Installs', 'Updates'], false, 'green')
+} catch(e) {
+  console.error(e)
 }
-
-const installs = [...Array(colsToPrint).keys()].map((x) => {
-  return getDailyScriptStat(-x)
-})
-
-pCols(installs, ['', 'Installs', 'Updates'], false, 'green')
diff --git a/scripts/start.sh b/scripts/start.sh
index 492ba6a..3cca89e 100755
--- a/scripts/start.sh
+++ b/scripts/start.sh
@@ -1,4 +1,4 @@
 #!/bin/bash
 
 scripts/make.sh
-npm run start
+npm run start > /dev/null