Memory usage: ${Math.round(memoryUsage / 1024 / 1024 * 100) / 100}Mb
`, 79 | memoryProgress: `${Math.round(memoryUsage / (this.totalMemory / 100) * 100) / 100}%
82 |${this.calculateHealth(globalData)}%
86 |Visits: ${globalData.visits}
88 |Uptime: ${Math.round((Date.now() - globalData.startDate) / 86400000 * 100) / 100} days
89 |Total errors: ${globalData.errorCase - 1}
`, 90 | browser: this.generateBrowserInfo(globalData), 91 | chartData: this.generateChartData(globalData, outData, keys) 92 | }]); 93 | }); 94 | } 95 | 96 | /** 97 | * @private 98 | * @since 0.3.0 99 | * @version 0.1.0 100 | * @method calculateHealth 101 | * @summary The API health calculation method 102 | * @description A method calculating the health based on the max errors per 100 visits @see config 103 | * @param {Object} globalData An object containing all the values and methods the application needs 104 | * @returns {Number} The current health 105 | */ 106 | calculateHealth(globalData) { 107 | const maxErrors = globalData.visits || 1 / 100 * globalData.config.values.analytics.maxErrorsPer100Visits; 108 | 109 | return Math.round(((maxErrors - (globalData.errorCase - 1)) / (maxErrors / 100)) * 10) / 10; 110 | } 111 | 112 | /** 113 | * @private 114 | * @since 0.3.0 115 | * @version 0.1.0 116 | * @method generateBrowserInfo 117 | * @summary The user browser navigation constructor 118 | * @description This method generates the navigation for all the browsers and browser versions used to visit this API 119 | * @param {Object} globalData An object containing all the values and methods the application needs 120 | * @returns {String} The navigation HTML block 121 | */ 122 | generateBrowserInfo(globalData) { 123 | let out = ""; 124 | if (globalData.config.settings.analytics.showBrowserVisits) { 125 | const data = globalData.browser; 126 | const keys = Object.keys(data); 127 | let innerKeys; 128 | 129 | for (let i in keys) { 130 | out += ` 131 | ${keys[i]}: 132 |${data[keys[i]].visits} visits
133 | 134 | `; 135 | innerKeys = Object.keys(data[keys[i]].versions); 136 | for (let j in innerKeys) { 137 | out += `v${innerKeys[j]}${data[keys[i]].versions[innerKeys[j]]} visits
`; 138 | } 139 | out += ""; 140 | } 141 | } else { 142 | out = "denied"; 143 | } 144 | 145 | return out; 146 | } 147 | 148 | /** 149 | * @private 150 | * @since 0.3.0 151 | * @version 0.1.0 152 | * @method generateChartData 153 | * @summary The chartData generator 154 | * @description This method generates the service response time data in the chart format which can be used by the frontend 155 | * @param {Object} globalData An object containing all the values and methods the application needs 156 | * @param {Number[]} now The current response times 157 | * @param {String[]} keys the graph entry keys which need to be given 158 | * @returns {Object} The chart data object 159 | */ 160 | generateChartData(globalData, now, keys) { 161 | let output = []; 162 | let tempI = 0; 163 | 164 | for (let i in keys) { 165 | if (globalData.config.settings.endpoints 166 | [`enable${globalData.graphEntries[keys[i]].settingsName}Endpoint`]) { 167 | output[tempI] = { 168 | label: keys[i], 169 | data: [], 170 | borderColor: globalData.graphEntries[keys[i]].color 171 | }; 172 | for (let j in globalData.graphEntries[keys[i]].data) { 173 | output[tempI].data[j] = globalData.graphEntries[keys[i]].data[j]; 174 | } 175 | output[tempI].data.push(now[tempI]); 176 | tempI++; 177 | } 178 | } 179 | 180 | return output; 181 | } 182 | } -------------------------------------------------------------------------------- /api/services/analytics/AnalyticsIndexService.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @class 3 | * @public 4 | * @author SMJS 5 | * @name AnalyticsIndexService 6 | * @typedef {Object} AnalyticsIndexService 7 | */ 8 | module.exports = class AnalyticsIndexService { 9 | 10 | /** 11 | * @private 12 | * @type {Object} 13 | * @name fs 14 | */ 15 | fs = require("fs"); 16 | 17 | /** 18 | * @private 19 | * @since 0.3.0 20 | * @version 0.1.0 21 | * @type {[Number, String]} 22 | * @name output 23 | * @summary The saved output after the first use 24 | */ 25 | output; 26 | 27 | /** 28 | * @async 29 | * @public 30 | * @since 0.3.0 31 | * @version 0.1.0 32 | * @method service 33 | * @summary The base analytics constructor service 34 | * @description The constructing method converting the navigation HTML blocks to the full navigation 35 | * @param {Object} globalData An object containing all the values and methods the application needs 36 | * @returns {Promise<[Number, String]>} The analytics page and response status 37 | */ 38 | service(globalData) { 39 | return new Promise((resolve) => { 40 | if (!this.output) { 41 | this.fs.readFile("resources/pages/analytics/base.html", "utf8", (error, content) => { 42 | if (error) { 43 | globalData.errorFunc(error, "analytics", globalData); 44 | resolve([500, "This file wasn't found
%CONTENT%
70 | `; 71 | 72 | /** 73 | * @async 74 | * @public 75 | * @since 0.3.0 76 | * @version 0.1.0 77 | * @method service 78 | * @summary The base service to generate the main body 79 | * @description The main service layer which generates the docs body and sets the navigation highlight to right place 80 | * @param {String[]} params The url navigation string 81 | * @returns {Promise<[Number, String]>} The HTML page and the response status 82 | */ 83 | service(params) { 84 | return new Promise((resolve) => { 85 | let ArrayFile = []; 86 | let fileToRead = this.UrlToRoute.converter(params); 87 | 88 | this.fs.readFile("resources/pages/docs/base.html", "utf8", (error, content) => { 89 | if (error) { 90 | resolve([500, "61 | URL: 62 | 63 | ${this.endpointProperties[key].link} 64 | 65 |
66 |