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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import Logger from "bunyan"; import { Env } from "../Env"; import { SecretString } from "lib"; export interface FetchSettingModel { id: bigint; siteId: bigint; userName: string; password: SecretString; } export interface MonthlyUsageModel { fetchSettingId: bigint; year: number; month: number; dayCount: number; kwh: number; yen: number; } export interface DailyUsageModel { fetchSettingId: bigint; year: number; month: number; date: number; amount: number; } export interface FetcherFactory { create(siteId: bigint): UsageFetcher; } export interface UsageFetcher { fetchMonthly( logger: Logger, setting: FetchSettingModel ): Promise<MonthlyUsageModel[]>; fetchDaily( logger: Logger, setting: FetchSettingModel ): Promise<DailyUsageModel[]>; } export interface FetchSettingRepository { findAllElectricityFetchSettings(): Promise<FetchSettingModel[]>; } export interface UsageRepository { saveElectricityMonthlyUsages( usages: MonthlyUsageModel[], now: Date ): Promise<void>; saveElectricityDailyUsages( usages: DailyUsageModel[], now: Date ): Promise<void>; } export interface FetchStatusRepository { upsertElectricityFetchStatusSuccess( fetchSettingId: bigint, now: Date ): Promise<void>; upsertElectricityFetchStatusFailure( fetchSettingId: bigint, now: Date ): Promise<void>; } export interface RunResult { successfulCount: number; failureCount: number; } export class UsageService { readonly env: Env; readonly fetchSettingRepo: FetchSettingRepository; readonly fetcherFactory: FetcherFactory; readonly usageRepo: UsageRepository; readonly fetchStatusRepo: FetchStatusRepository; constructor( env: Env, fetchSettingRepo: FetchSettingRepository, fetcherFactory: FetcherFactory, usageRepo: UsageRepository, fetchStatusRepo: FetchStatusRepository ) { this.env = env; this.fetchSettingRepo = fetchSettingRepo; this.fetcherFactory = fetcherFactory; this.usageRepo = usageRepo; this.fetchStatusRepo = fetchStatusRepo; } async run(parentLogger: Logger): Promise<RunResult> { const now = new Date(); const settings = await this.fetchSettingRepo.findAllElectricityFetchSettings(); parentLogger.info( "loaded electricity_fetch_settings, count %d", settings.length ); const result = { successfulCount: 0, failureCount: 0, }; for (const setting of settings) { const logger = parentLogger.child({ setting_id: setting.id.toString(), user_name: setting.userName, }); logger.info( "fetch electricity start by setting[%s, %s]", setting.id.toString(), setting.userName ); try { await this.fetchAndSave(logger, now, setting); result.successfulCount++; } catch (err) { logger.error({ err }, "fetch electricity failed"); result.failureCount++; } finally { logger.info( "fetch electricity end by setting[%s, %s]", setting.id.toString(), setting.userName ); } } return result; } private async fetchAndSave( logger: Logger, now: Date, setting: FetchSettingModel ): Promise<void> { try { const fetcher = this.fetcherFactory.create(setting.siteId); const monthlyUsages = await fetcher.fetchMonthly(logger, setting); await this.usageRepo.saveElectricityMonthlyUsages(monthlyUsages, now); const dailyUsages = await fetcher.fetchDaily(logger, setting); await this.usageRepo.saveElectricityDailyUsages(dailyUsages, now); await this.fetchStatusRepo.upsertElectricityFetchStatusSuccess( setting.id, new Date() ); } catch (err) { await this.fetchStatusRepo.upsertElectricityFetchStatusFailure( setting.id, new Date() ); throw err; } } } |