mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +00:00
feat: implement date formatting function and enhance PowerClampService with space-based data retrieval
This commit is contained in:
14
libs/common/src/helper/date-format.ts
Normal file
14
libs/common/src/helper/date-format.ts
Normal file
@ -0,0 +1,14 @@
|
||||
export function toDDMMYYYY(dateString?: string | null): string | null {
|
||||
if (!dateString) return null;
|
||||
|
||||
// Ensure dateString is valid format YYYY-MM-DD
|
||||
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
||||
if (!regex.test(dateString)) {
|
||||
throw new Error(
|
||||
`Invalid date format: ${dateString}. Expected format is YYYY-MM-DD`,
|
||||
);
|
||||
}
|
||||
|
||||
const [year, month, day] = dateString.split('-');
|
||||
return `${day}-${month}-${year}`;
|
||||
}
|
@ -46,16 +46,15 @@ export class PowerClampService {
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<void> {
|
||||
const query = this.loadQuery(procedureFileName);
|
||||
const query = this.loadQuery(
|
||||
'fact_device_energy_consumed',
|
||||
procedureFileName,
|
||||
);
|
||||
await this.dataSource.query(query, params);
|
||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||
}
|
||||
|
||||
private loadQuery(fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(
|
||||
'fact_device_energy_consumed',
|
||||
fileName,
|
||||
SQL_PROCEDURES_PATH,
|
||||
);
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($2, ''), 'DD-MM-YYYY') AS start_date,
|
||||
TO_DATE(NULLIF($3, ''), 'DD-MM-YYYY') AS end_date,
|
||||
string_to_array(NULLIF($4, ''), ',') AS device_ids
|
||||
TO_DATE(NULLIF($1, ''), 'DD-MM-YYYY') AS start_date,
|
||||
TO_DATE(NULLIF($2, ''), 'DD-MM-YYYY') AS end_date,
|
||||
string_to_array(NULLIF($3, ''), ',') AS device_ids
|
||||
)
|
||||
|
||||
SELECT TO_CHAR(A.date, 'MM-YYYY') AS month,
|
||||
|
Reference in New Issue
Block a user