Skip to content

Commit

Permalink
fix: filter read export jsons
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvanes committed May 13, 2024
1 parent 2864f9c commit 5abc840
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ libs/types/examples/
NOTES.md

.nx/cache

0_electric_exports
22 changes: 18 additions & 4 deletions apps/server/src/energyusage/energyusage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
UseGuards,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { fstatSync, openSync } from "fs";
import { readFile, readdir } from "fs/promises";
import got from "got";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
Expand Down Expand Up @@ -148,13 +149,17 @@ const getDayUsage = (
}
};

// exportJsonToRow
const ELECTRIC_EXPORTS_ROOT_DIR = "./0_electric_exports";

const exportJsonToRow =
(usePerDayTotals: GetDomoticzUsePerDayResponse["result"]) =>
async (
fileInDir: string
): Promise<EnergyUsageGetElectricExportsResponse[0] | undefined> => {
const fileContents = await readFile(`./tmp/${fileInDir}`, "utf-8");
const fileContents = await readFile(
`${ELECTRIC_EXPORTS_ROOT_DIR}/${fileInDir}`,
"utf-8"
);
try {
const fileJson: GetDomoticzJsonExport = JSON.parse(fileContents);
const firstItem = fileJson.result[0];
Expand Down Expand Up @@ -365,7 +370,16 @@ export class EnergyUsageController {
);

try {
const filesInDir = await readdir("./tmp");
const filesInDir = await readdir(ELECTRIC_EXPORTS_ROOT_DIR);

const filteredFilesInDir = filesInDir.filter((file) => {
const fileRef = openSync(
`${ELECTRIC_EXPORTS_ROOT_DIR}/${file}`,
"r"
);
const stats = fstatSync(fileRef);
return stats.isFile() && file.endsWith("_electra.json");
});

this.logger.verbose(`[${req.user.name}] start get counter`);

Expand Down Expand Up @@ -395,7 +409,7 @@ export class EnergyUsageController {

const usagePerDay: EnergyUsageGetElectricExportsResponse =
await Promise.all(
filesInDir.map<
filteredFilesInDir.map<
Promise<EnergyUsageGetElectricExportsResponse[0]>
>(exportJsonToRow(usePerDayTotals))
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homeremote",
"version": "3.6.2",
"version": "3.6.3",
"license": "MIT",
"scripts": {
"writeGitInfo": "ts-node --project ./tsconfig.node.json writeGitInfo.ts",
Expand Down

0 comments on commit 5abc840

Please sign in to comment.