• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BurialPointRequestBody } from './SpStatisticsHttpBean.js';
17
18export class SpStatisticsHttpUtil {
19  static requestServerInfo: string = '';
20  static serverTime: number = 0;
21  static timeDiff: number = 0;
22  static retryCount: number = 0;
23  static retryMaxCount: number = 5;
24  static pauseRetry: boolean = false;
25  static retryRestTimeOut: boolean = false;
26
27  static initStatisticsServerConfig() {
28    if (SpStatisticsHttpUtil.requestServerInfo === '') {
29      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
30    }
31    if (SpStatisticsHttpUtil.serverTime == 0) {
32      SpStatisticsHttpUtil.getServerTime();
33    }
34  }
35
36  static getRequestServerInfo(): string {
37    let req = new XMLHttpRequest();
38    req.open(
39      'GET',
40      `${window.location.protocol}//${window.location.host.split(':')[0]}:${
41        window.location.port
42      }/application/serverInfo`,
43      false
44    );
45    req.send(null);
46    if (req.status == 200) {
47      let requestInfo = req.getResponseHeader('request_info');
48      if (requestInfo && requestInfo.length > 0) {
49        return requestInfo;
50      }
51    }
52    return '';
53  }
54
55  static getServerTime() {
56    if (SpStatisticsHttpUtil.requestServerInfo === '') {
57      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
58    }
59    if (SpStatisticsHttpUtil.pauseRetry) {
60      return;
61    }
62    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/serverTime`)
63      .then((resp) => {
64        resp.text().then((it) => {
65          if (it && it.length > 0) {
66            SpStatisticsHttpUtil.serverTime = Number(it);
67            SpStatisticsHttpUtil.timeDiff = SpStatisticsHttpUtil.serverTime - Date.now();
68          }
69        });
70      })
71      .catch((e) => {
72        this.handleRequestException();
73      });
74  }
75
76  private static handleRequestException() {
77    if (SpStatisticsHttpUtil.retryCount >= SpStatisticsHttpUtil.retryMaxCount) {
78      SpStatisticsHttpUtil.pauseRetry = true;
79      if (SpStatisticsHttpUtil.retryRestTimeOut) {
80        return;
81      }
82      SpStatisticsHttpUtil.retryRestTimeOut = true;
83      setTimeout(() => {
84        SpStatisticsHttpUtil.retryCount = 0;
85        SpStatisticsHttpUtil.pauseRetry = false;
86        SpStatisticsHttpUtil.retryRestTimeOut = false;
87      }, 600000);
88    }
89    ++SpStatisticsHttpUtil.retryCount;
90  }
91
92  static addUserVisitAction(requestUrl: string) {
93    // @ts-ignore
94    if (window.useWb) {
95      return;
96    }
97    if (SpStatisticsHttpUtil.requestServerInfo === '') {
98      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
99    }
100    if (SpStatisticsHttpUtil.pauseRetry) {
101      return;
102    }
103    let visitId = 0;
104    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, {
105      method: 'post',
106      headers: {
107        'Content-Type': 'application/json',
108      },
109    })
110      .then((resp) => {
111        resp.text().then((it) => {
112          let res = JSON.parse(it);
113          if (res && res.data) {
114            visitId = res.data.accessId;
115          }
116        });
117      })
118      .catch((err) => {});
119    setTimeout(() => {
120      fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, {
121        method: 'post',
122        headers: {
123          'Content-Type': 'application/json',
124        },
125        body: JSON.stringify({
126          effectiveAccess: true,
127          visitId: visitId,
128        }),
129      })
130        .catch((err) => {})
131        .then((resp) => {});
132    }, 1800000);
133  }
134
135  static addOrdinaryVisitAction(requestBody: BurialPointRequestBody) {
136    // @ts-ignore
137    if (window.useWb) {
138      return;
139    }
140    if (SpStatisticsHttpUtil.requestServerInfo === '') {
141      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
142    }
143    if (SpStatisticsHttpUtil.pauseRetry) {
144      return;
145    }
146    requestBody.ts = SpStatisticsHttpUtil.getCorrectRequestTime();
147    if (SpStatisticsHttpUtil.serverTime === 0) {
148      return;
149    }
150    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/record`, {
151      method: 'post',
152      headers: {
153        'Content-Type': 'application/json',
154      },
155      body: JSON.stringify(requestBody),
156    })
157      .catch((err) => {
158        this.handleRequestException();
159      })
160      .then((resp) => {});
161  }
162
163  static getCorrectRequestTime(): number {
164    if (SpStatisticsHttpUtil.serverTime === 0) {
165      SpStatisticsHttpUtil.getServerTime();
166    }
167    return Date.now() + SpStatisticsHttpUtil.timeDiff;
168  }
169}
170