ApiLogger.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import licenceStore from '../stores/LicenceStore'
  16. type LogType = 'REQUEST' | 'RESPONSE'
  17. type LogOptions = {
  18. url: string,
  19. method: string,
  20. type: LogType,
  21. description?: string,
  22. requestStatus?: number | 'canceled',
  23. requestError?: any,
  24. windowPath?: string,
  25. stack?: string,
  26. }
  27. type RequestLog = LogOptions & {
  28. date: Date,
  29. }
  30. type Log = {
  31. requests: RequestLog[],
  32. userAgent: string,
  33. platform: string,
  34. version: string,
  35. }
  36. const MAX_LOGS = 1000
  37. const validateLog = (logs: RequestLog[]): boolean => {
  38. if (logs.length && !logs[0].windowPath) {
  39. return false
  40. }
  41. return true
  42. }
  43. class Storage {
  44. static NAME = 'apiLog'
  45. static EMPTY = '[]'
  46. static getLogRaw(): string {
  47. return localStorage.getItem(this.NAME) || this.EMPTY
  48. }
  49. static saveLog(options: LogOptions) {
  50. let logs: RequestLog[] = JSON.parse(localStorage.getItem(this.NAME) || this.EMPTY)
  51. if (!validateLog(logs)) {
  52. localStorage.setItem(this.NAME, this.EMPTY)
  53. logs = JSON.parse(this.EMPTY)
  54. }
  55. let newRequest: RequestLog = {
  56. date: new Date(),
  57. windowPath: window.location.href.replace(`${window.location.protocol}//${window.location.host}`, ''),
  58. ...options,
  59. }
  60. if (options.type === 'REQUEST') {
  61. let err = new Error()
  62. newRequest.stack = err.stack
  63. }
  64. if (logs.length > MAX_LOGS) {
  65. logs.splice(0, logs.length - MAX_LOGS)
  66. }
  67. logs.push(newRequest)
  68. localStorage.setItem(this.NAME, JSON.stringify(logs))
  69. }
  70. }
  71. class ApiLogger {
  72. log(options: LogOptions) {
  73. if (options.type === 'REQUEST') {
  74. console.log(`%cSending ${options.method} Request to ${options.url}`, 'color: #F5A623')
  75. } else if (options.requestError) {
  76. console.log(`%cError Response: ${options.url}`, 'color: #D0021B', options.requestError)
  77. } else if (options.requestStatus === 'canceled') {
  78. console.log(`%cRequest Canceled: ${options.url}`, 'color: #0044CA')
  79. } else if (options.requestStatus === 500) {
  80. console.log(`%cError Something happened in setting up the request: ${options.url}`, 'color: #D0021B')
  81. }
  82. if (options.requestError && options.requestError.response && options.requestError.response.data) {
  83. options.requestError = options.requestError.response.data.error
  84. }
  85. Storage.saveLog(options)
  86. }
  87. download() {
  88. let requests: RequestLog[] = JSON.parse(Storage.getLogRaw())
  89. let log: Log = {
  90. requests,
  91. userAgent: window.navigator.userAgent,
  92. platform: window.navigator.platform,
  93. version: licenceStore.version || '-',
  94. }
  95. let href: string = `data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify(log))}`
  96. let downloadAnchorNode = document.createElement('a')
  97. downloadAnchorNode.setAttribute('href', href)
  98. downloadAnchorNode.setAttribute('download', 'coriolis-log.json')
  99. if (document.body) {
  100. document.body.appendChild(downloadAnchorNode) // required for firefox
  101. }
  102. downloadAnchorNode.click()
  103. downloadAnchorNode.remove()
  104. }
  105. }
  106. export default new ApiLogger()