Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import * as electricity from "../domain/Electricity"; import * as gas from "../domain/Gas"; import * as water from "../domain/Water"; import { MySqlClient } from "../infra/MySqlClient"; import { CommonEnv, Env } from "../Env"; import { PrismaClient } from "@prisma/client"; import { GasClient } from "../infra/GasClient"; import { WaterClient } from "../infra/WaterClient"; import Logger from "bunyan"; import { ElectricityFactory } from "../infra/ElectricityClientFactory"; export interface Params { enableElectricity: boolean; enableGas: boolean; enableWater: boolean; } export class FetchApplication { readonly commonEnv: CommonEnv; readonly electricityEnv: Env; readonly gasEnv: Env; readonly waterEnv: Env; readonly params: Params; readonly mysqlClient: MySqlClient; constructor( commonEnv: CommonEnv, electricityEnv: Env, gasEnv: Env, waterEnv: Env, prisma: PrismaClient, params: Params ) { this.commonEnv = commonEnv; this.electricityEnv = electricityEnv; this.gasEnv = gasEnv; this.waterEnv = waterEnv; this.params = params; this.mysqlClient = new MySqlClient( prisma, this.commonEnv.encryptionPassword ); } async run(logger: Logger): Promise<void> { try { await this.mysqlClient.upsertRunning(this.commonEnv.appName, new Date()); let success = true; if (this.params.enableElectricity) { const result = await this.runElectricity(logger); Iif (!result) { success = false; } } else { logger.info("skip electricity"); } if (this.params.enableGas) { const result = await this.runGas(logger); Iif (!result) { success = false; } } else { logger.info("skip gas"); } if (this.params.enableWater) { const result = await this.runWater(logger); Iif (!result) { success = false; } } else { logger.info("skip water"); } if (success) { await this.mysqlClient.upsertStopped( this.commonEnv.appName, new Date() ); } else { await this.mysqlClient.upsertError(this.commonEnv.appName, new Date()); } } catch (err) { await this.mysqlClient.upsertError(this.commonEnv.appName, new Date()); throw err; } } private async runElectricity(parentLogger: Logger): Promise<boolean> { const service = new electricity.UsageService( this.electricityEnv, this.mysqlClient, new ElectricityFactory(this.commonEnv, this.electricityEnv), this.mysqlClient, this.mysqlClient ); const logger = parentLogger.child({ usage_type: "electricity" }); const result = await service.run(logger); return result.failureCount === 0; } private async runGas(parentLogger: Logger): Promise<boolean> { const service = new gas.UsageService( this.gasEnv, new GasClient(this.commonEnv, this.gasEnv), this.mysqlClient, this.mysqlClient, this.mysqlClient ); const logger = parentLogger.child({ usage_type: "gas" }); const result = await service.run(logger); return result.failureCount === 0; } private async runWater(parentLogger: Logger): Promise<boolean> { const service = new water.UsageService( this.waterEnv, new WaterClient(this.commonEnv, this.waterEnv), this.mysqlClient, this.mysqlClient, this.mysqlClient ); const logger = parentLogger.child({ usage_type: "water" }); const result = await service.run(logger); return result.failureCount === 0; } } |