hooks.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { useEffect } from 'react'
  21. import { statistic } from 'utils/statistic/statistic.dv'
  22. import { IProject } from 'containers/Projects/types'
  23. import { IDisplayFormed } from 'containers/Viz/types'
  24. export const useStatistic = (project: IProject, display: IDisplayFormed) => {
  25. useEffect(() => {
  26. const beforeUnload = () => {
  27. statistic.setDurations(
  28. {
  29. end_time: statistic.getCurrentDateTime()
  30. },
  31. (data) => {
  32. statistic.setPrevDurationRecord(data, () => {
  33. statistic.setDurations({
  34. start_time: statistic.getCurrentDateTime(),
  35. end_time: ''
  36. })
  37. })
  38. }
  39. )
  40. window.addEventListener('beforeunload', beforeUnload, false)
  41. statistic.setDurations({
  42. start_time: statistic.getCurrentDateTime()
  43. })
  44. statistic.startClock()
  45. const interact = () => {
  46. statistic.isTimeout()
  47. }
  48. const onVisibilityChanged = (event) => {
  49. const flag = event.target.webkitHidden
  50. if (flag) {
  51. statistic.setDurations(
  52. {
  53. end_time: statistic.getCurrentDateTime()
  54. },
  55. (data) => {
  56. statistic.sendDuration([data]).then((res) => {
  57. statistic.resetClock()
  58. })
  59. }
  60. )
  61. } else {
  62. statistic.setDurations(
  63. {
  64. start_time: statistic.getCurrentDateTime()
  65. },
  66. (data) => {
  67. statistic.startClock()
  68. }
  69. )
  70. }
  71. }
  72. window.addEventListener('mousemove', interact, false)
  73. window.addEventListener('visibilitychange', onVisibilityChanged, false)
  74. window.addEventListener('keydown', interact, false)
  75. statistic.setOperations(
  76. {
  77. org_id: project.orgId,
  78. project_name: project.name,
  79. project_id: project.id,
  80. viz_type: 'display',
  81. viz_id: display.id,
  82. viz_name: display.name,
  83. create_time: statistic.getCurrentDateTime()
  84. },
  85. (data) => {
  86. const visitRecord = {
  87. ...data,
  88. action: 'visit'
  89. }
  90. statistic.sendOperation(visitRecord)
  91. }
  92. )
  93. return () => {
  94. statistic.setDurations(
  95. {
  96. end_time: statistic.getCurrentDateTime()
  97. },
  98. (data) => {
  99. statistic.sendDuration([data])
  100. }
  101. )
  102. window.removeEventListener('mousemove', interact, false)
  103. window.removeEventListener('keydown', interact, false)
  104. window.removeEventListener(
  105. 'visibilitychange',
  106. onVisibilityChanged,
  107. false
  108. )
  109. statistic.resetClock()
  110. }
  111. }
  112. }, [])
  113. }