Skip to content

Commit

Permalink
feat: add last time work
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaliao committed Oct 8, 2024
1 parent 7c8fb46 commit af5e57e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 85 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cac from 'cac'
import { generateReportLastTimeWork } from '~/modules/reportLastTimeWork'

Check failure on line 2 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Cannot find module '~/modules/reportLastTimeWork' or its corresponding type declarations.

Check failure on line 2 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

Cannot find module '~/modules/reportLastTimeWork' or its corresponding type declarations.

Check failure on line 2 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

Cannot find module '~/modules/reportLastTimeWork' or its corresponding type declarations.

Check failure on line 2 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

Cannot find module '~/modules/reportLastTimeWork' or its corresponding type declarations.
import { generateReportLastUpdate } from '~/modules/reportLastUpdate'
import { generateReportNotWorking } from '~/modules/reportNotWorking'
import { exit } from './utils'
Expand All @@ -8,15 +9,18 @@ const cli = cac()
cli
.command('report', 'generate report')
.option('--type <type>', 'Set report type', {
default: 'last-update',
default: 'last-time-work',
})
.action((options) => {
if (options.type === 'not-working') {
generateReportNotWorking()
}
else if (options.type === 'last-update') {
if (options.type === 'last-update') {
generateReportLastUpdate()
}
if (options.type === 'last-time-work') {
generateReportLastTimeWork()
}
})

cli
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions src/modules/reportLastUpdate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function generateReportLastUpdate(): Promise<void> {

consola.info('讀取檔案中')
const nameDevicePrefix = process.env.Name_Device || 'airaConnect.devices'
const devices: Device[] = await readFileContent(`./data/${nameDevicePrefix}.json`)
const devices: Device[] = await readFileContent(nameDevicePrefix)

consola.info(`讀取檔案完成,共花費 ${(cost() / 1000).toFixed(2)} 秒`)

Expand All @@ -19,7 +19,7 @@ export async function generateReportLastUpdate(): Promise<void> {

await writeOutput(result)

consola.success(`已寫入檔案,共花費 ${(cost() / 1000).toFixed(2)} 秒`)
consola.success('已完成!')
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/modules/reportNotWorking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function generateReportNotWorking(): Promise<void> {

const nameMessagePrefix = process.env.Name_Message || 'airaConnect.machineryMessages.test'

const files = await readFolderNames(new URL('./data', import.meta.url))
const files = await readFolderNames()

const filterMessageFiles = files
.filter(file => file.startsWith(nameMessagePrefix!) && file.endsWith('.json'))
Expand All @@ -36,23 +36,22 @@ export async function generateReportNotWorking(): Promise<void> {

consola.info('讀取檔案中')
for (const selectFile of selectedFiles) {
const fileContent = await readFileContent<Payload>(new URL(`./data/${selectFile}.json`, import.meta.url))
const fileContent = await readFileContent<Payload[]>(selectFile)
payload = payload.concat(fileContent)
}
consola.info(`共有 ${payload.length} 筆資料,共花費 ${(cost() / 1000).toFixed(2)} 秒`)

let preprocessedData: serializableData
try {
await fs.access(new URL('./data/preprocessed_data.json', import.meta.url))
await fs.access('./data/preprocessed_data.json')
}
catch {
consola.info('預處理數據不存在,將重新生成')

preprocessData()
}
finally {
const data = await fs.readFile(new URL('./data/preprocessed_data.json', import.meta.url), 'utf-8')
preprocessedData = JSON.parse(data)
preprocessedData = await readFileContent<serializableData>('preprocessed_data')
}

const { signals: transformedPayloads, startTime, endTime } = processPayload(payload)
Expand All @@ -66,7 +65,7 @@ export async function generateReportNotWorking(): Promise<void> {

await writeOutput(result)

consola.success('已寫入檔案,共花費')
consola.success('已完成!')
}

export function processPayload(payload: Payload[]): ProcessedResult {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/reportNotWorking/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export async function preprocessData(): Promise<PreprocessedData> {
const mapName = process.env.Name_Map || 'airaConnect.maps'
const deviceName = process.env.Name_Device || 'airaConnect.devices'

const maps: ConnectMap[] = await readFileContent(new URL(`./data/${mapName}.json`, import.meta.url))
const devices: Device[] = await readFileContent(new URL(`./data/${deviceName}.json`, import.meta.url))
const maps: ConnectMap[] = await readFileContent(mapName)
const devices: Device[] = await readFileContent(deviceName)

const areaMap = preprocessAreaMap(maps)
const devicesMap = preprocessDevicesMap(devices)
Expand Down
15 changes: 4 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,16 @@ export function formatDate(timestamp: number): string {
/**
* 讀取目錄下的檔案名稱
*/
export async function readFolderNames(folderPath: PathLike = 'data'): Promise<string[]> {
export async function readFolderNames(folderPath: PathLike = './data'): Promise<string[]> {
return await fs.readdir(folderPath)
}

/**
* 讀取檔案內容
*/
export async function readFileContent<T>(filePath: PathLike = 'data'): Promise<T[]> {
try {
const fileContent = await fs.readFile(filePath, 'utf-8')
return JSON.parse(fileContent)
}
catch (error) {
consola.error(`讀取檔案 ${filePath} 時發生錯誤:`, error)
exit()
return []
}
export async function readFileContent<T>(fileName: string): Promise<T> {
const fileContent = await fs.readFile(`./data/${fileName}.json`, 'utf-8')
return JSON.parse(fileContent)
}

/**
Expand Down
62 changes: 0 additions & 62 deletions test/airaConnect.machineryMessages.test.json

This file was deleted.

0 comments on commit af5e57e

Please sign in to comment.