/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getThreadPoolTraceBufferCacheKey } from './database/SqlLite';
export enum TraceMode {
NORMAL,
LONG_TRACE,
DISTRIBUTED,
}
export const applicationHtml: string = `
`;
export function readTraceFileBuffer(): Promise {
return new Promise((resolve) => {
caches.match(getThreadPoolTraceBufferCacheKey('1')).then((res) => {
if (res) {
res.arrayBuffer().then((buffer) => {
resolve(buffer);
});
} else {
resolve(undefined);
}
});
});
}
export function clearTraceFileCache(): void {
caches.keys().then((keys) => {
keys.forEach((key) => {
if (key === getThreadPoolTraceBufferCacheKey('1')) {
caches.delete(key).then();
} else if (key.includes('/') && key.includes('-')) {
let splits = key.split('/');
let keyStr = splits[splits.length - 1];
let time = keyStr.split('-')[0];
let fileDate = new Date(parseInt(time));
if (fileDate.toLocaleDateString() !== new Date().toLocaleDateString()) {
//如果不是当天的缓存则删去缓存文件
caches.delete(key).then();
}
} else {
caches.delete(key).then();
}
});
});
}
export function postLog(filename: string, fileSize: string): void {
fetch(`https://${window.location.host.split(':')[0]}:${window.location.port}/logger`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
fileName: filename,
fileSize: fileSize,
}),
})
.then((response) => response.json())
.then((data) => { })
.catch((error) => { });
}
export function indexedDataToBufferData(sourceData: unknown): ArrayBuffer {
let uintArrayLength = 0;
//@ts-ignore
let uintDataList = sourceData.map((item: unknown) => {
//@ts-ignore
let currentBufData = new Uint8Array(item.buf);
uintArrayLength += currentBufData.length;
return currentBufData;
});
let resultArrayBuffer = new ArrayBuffer(uintArrayLength);
let resultUintArray = new Uint8Array(resultArrayBuffer);
let offset = 0;
uintDataList.forEach((currentArray: Uint8Array) => {
resultUintArray.set(currentArray, offset);
offset += currentArray.length;
});
return resultArrayBuffer;
}
export function findFreeSizeAlgorithm(numbers: Array, freeSize: number): Array {
let closestSize = 0;
let currentSize = 0;
let finalIndex: Array = [];
let currentSelectIndex: Array = [];
function reBackFind(index: number): void {
if (index === numbers.length) {
const sumDifference = Math.abs(currentSize - freeSize);
if (currentSize <= freeSize && sumDifference < Math.abs(closestSize - freeSize)) {
closestSize = currentSize;
finalIndex = [...currentSelectIndex];
}
return;
}
currentSize += numbers[index];
currentSelectIndex.push(index);
reBackFind(index + 1);
currentSize -= numbers[index];
currentSelectIndex.pop();
reBackFind(index + 1);
}
reBackFind(0);
return finalIndex;
}
export function getCurrentDataTime(): string[] {
let current = new Date();
let year = '' + current.getFullYear();
let month = ('0' + (current.getMonth() + 1)).slice(-2);
let day = ('0' + current.getDate()).slice(-2);
let hours = ('0' + current.getHours()).slice(-2);
let minutes = ('0' + current.getMinutes()).slice(-2);
let seconds = ('0' + current.getSeconds()).slice(-2);
return [year, month, day, hours, minutes, seconds];
}