• 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 */
15import relationalStore from '@ohos.data.relationalStore';
16import database from './DatabaseUtils';
17import BundleManager from '../utils/BundleMangerUtils';
18import { dateFormat } from '../utils/TimeUtils';
19import { ReportItem } from '../entity/LocalConfigEntity';
20import {
21  sql_t_general_info,
22  dbVersion,
23  dbName,
24  sql_t_index_info,
25} from '../constant/ConstantSQL';
26import { AppFileRealDir } from '../constant/ConstantsPath';
27import SPLogger from '../utils/SPLogger';
28
29const TAG = 'LocalRepository';
30
31export function initDbIndex(): void {
32  try {
33    const STORE_CONFIG: relationalStore.StoreConfig = {
34      name: globalThis.dbTime + '.db',
35      securityLevel: relationalStore.SecurityLevel.S1,
36    };
37    relationalStore
38      .getRdbStore(globalThis.abilityContext, STORE_CONFIG)
39      .then((rdbStore) => {
40        rdbStore.executeSql(sql_t_index_info, null);
41        return rdbStore;
42      });
43  } catch (err) {
44    SPLogger.DEBUG(
45      TAG,
46      '--> createTable start execute sql_t_index_info:' + sql_t_index_info
47    );
48  }
49}
50
51export function initDb(): void {
52  try {
53    const STORE_CONFIG: relationalStore.StoreConfig = {
54      name: dbName,
55      securityLevel: relationalStore.SecurityLevel.S1,
56    };
57    relationalStore
58      .getRdbStore(globalThis.abilityContext, STORE_CONFIG)
59      .then((rdbStore) => {
60        rdbStore.executeSql(sql_t_general_info, null);
61        return rdbStore;
62      });
63  } catch (err) {
64    SPLogger.DEBUG(
65      TAG,
66      '--> createTable start execute sql_t_genneral_info catch:' +
67        sql_t_general_info
68    );
69  }
70
71  getReportListDb()
72    .then((res) => {
73      globalThis.reportList = res;
74      let bundleNameArr = [];
75      for (let reportItemKey in globalThis.reportList) {
76        bundleNameArr.push(globalThis.reportList[reportItemKey].packageName);
77      }
78
79      BundleManager.getIconByBundleName(bundleNameArr).then((map) => {
80        globalThis.iconMap = map;
81      });
82
83      let resReport: Array<ReportItem> = res;
84
85      globalThis.sumTest = resReport.length;
86      globalThis.sumTestTime = 0;
87
88      let sumTestAppMap = new Map();
89      for (let resReportKey in resReport) {
90        sumTestAppMap.set(resReport[resReportKey].appName, '');
91        globalThis.sumTestTime += Number(
92          resReport[resReportKey].testDuration
93        ).valueOf();
94      }
95      globalThis.sumTestApp = sumTestAppMap.size;
96    })
97    .catch((err) => {
98      SPLogger.DEBUG(TAG, 'getReportListDb ERR:' + err);
99    });
100}
101
102export async function getReportListDb(): Promise<Array<ReportItem>> {
103  let result = Array<ReportItem>();
104  await database.queryGeneralData().then((generals) => {
105    for (let i = 0; i < generals.length; i++) {
106      let curGeneralInfo = generals[i];
107      result.push(
108        new ReportItem(
109          curGeneralInfo.taskId.toString(),
110          AppFileRealDir + curGeneralInfo.sessionId.toString(),
111          curGeneralInfo.packageName,
112          '',
113          curGeneralInfo.taskName,
114          curGeneralInfo.appName,
115          dateFormat(curGeneralInfo.startTime),
116          curGeneralInfo.testDuration.toString(),
117          false
118        )
119      );
120    }
121  });
122  return result;
123}
124