-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Month Budget & Realtime with Better Signalr Status and Health check (#17
- Loading branch information
Showing
98 changed files
with
9,967 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,4 +128,6 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
.azurite | ||
.azurite | ||
|
||
local.settings.json |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"FUNCTIONS_WORKER_RUNTIME": "node", | ||
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing", | ||
"AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
"AzureSignalRConnectionString": "Endpoint=https://<your-service>.service.signalr.net;AccessKey" | ||
}, | ||
"ConnectionStrings": {}, | ||
"watchDirectories": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { AzureTableEntityBase } from '../libs/azure-table'; | ||
|
||
/** | ||
* Monthly Budget Cache Entity | ||
* | ||
* This entity is used to cache monthly budget data from Google Sheet | ||
* | ||
* Partition Key: MonthKey | ||
* Row Key: id | ||
*/ | ||
|
||
export interface MonthlyBudgetCacheEntity extends AzureTableEntityBase { | ||
/** | ||
* Rowkey is same value of `id` | ||
*/ | ||
id: string; | ||
title: string; | ||
hide: boolean; | ||
type: string; | ||
categoryGroup: string; | ||
categoryGroupID: string; | ||
selectable: boolean; | ||
baseOrder: number; | ||
order: number; | ||
/** | ||
* for partition key, we use MonthKey | ||
* | ||
* pattern is 'YYYY-MM' | ||
* e.g. '2021-01' | ||
*/ | ||
filterMonth: Date; | ||
assigned: number; | ||
activity: number; | ||
cumulativeAssigned: number; | ||
cumulativeActivity: number; | ||
available: number; | ||
} | ||
|
||
/** | ||
* Monthly Budget Summary Cache Entity | ||
* | ||
* This entity is used to cache monthly budget summary data from Google Sheet | ||
* | ||
* Partition Key: year from filterMonth (e.g. '2021') | ||
* Row Key: MonthKey from filterMonth (e.g. '2021-01') | ||
*/ | ||
|
||
export interface MonthlyBudgetSummaryCacheEntity extends AzureTableEntityBase { | ||
latestUpdate: Date; | ||
startBudgetDate: Date; | ||
filterMonth: Date; | ||
startDate: Date; | ||
endDate: Date; | ||
readyToAssign: number; | ||
totalIncome: number; | ||
totalAssigned: number; | ||
totalActivity: number; | ||
totalAvailable: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { InvocationContext } from '@azure/functions'; | ||
import { | ||
monthlyBudgetSummaryTableCache, | ||
monthlyBudgetTableCache, | ||
sheetClient, | ||
transactionTableCache, | ||
} from '../bootstrap'; | ||
import { MonthlyBudgetCacheService, MonthlyBudgetSummaryCacheService } from '../services/monthly-budget-cache.service'; | ||
import { TransactionCacheService } from '../services/transaction-cache.service'; | ||
|
||
export async function startCacheUpdate(context: InvocationContext, partial = false) { | ||
const workers: Promise<void>[] = []; | ||
|
||
if (!partial) { | ||
workers.push( | ||
new TransactionCacheService(context, sheetClient.transaction, transactionTableCache).updateWhenExpired() | ||
); | ||
workers.push( | ||
new TransactionCacheService(context, sheetClient.transaction, transactionTableCache).deleteNonExistentRows() | ||
); | ||
} | ||
|
||
workers.push( | ||
new MonthlyBudgetSummaryCacheService( | ||
context, | ||
sheetClient.monthlyBudgetSummary, | ||
monthlyBudgetSummaryTableCache | ||
).forceUpdate() | ||
); | ||
|
||
workers.push( | ||
new MonthlyBudgetCacheService(context, sheetClient.monthlyBudget, monthlyBudgetTableCache).forceUpdate() | ||
); | ||
|
||
const result = await Promise.allSettled(workers); | ||
let isError = false; | ||
const errors: string[] = []; | ||
for (const res of result) { | ||
if (res.status === 'rejected') { | ||
isError = true; | ||
errors.push(res.reason); | ||
} | ||
} | ||
if (isError) { | ||
throw new Error(errors.join('\n')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { z } from 'zod'; | ||
import { func } from '../nammatham'; | ||
import { startCacheUpdate } from './cache-helper'; | ||
import { output } from '@azure/functions'; | ||
import { generateRealtimeMessage } from '../libs/signalr'; | ||
|
||
const longQueueSchema = z.object({ | ||
type: z.enum(['update_monthly_budget']), | ||
}); | ||
|
||
const signalrOutput = output.generic({ | ||
type: 'signalR', | ||
hubName: 'serverless', | ||
connectionStringSetting: 'AzureSignalRConnectionString', | ||
}); | ||
|
||
export default func | ||
.storageQueue('handleLongQueue', { | ||
connection: 'AzureWebJobsStorage', | ||
queueName: 'budgetlongqueue', | ||
extraOutputs: [signalrOutput], | ||
}) | ||
.handler(async c => { | ||
const context = c.context; | ||
context.log('Storage queue function processed work item:', c.trigger); | ||
const triggerMetadata = c.context.triggerMetadata; | ||
context.log('Queue metadata (dequeueCount):', triggerMetadata?.dequeueCount); | ||
const data = longQueueSchema.parse(c.trigger); | ||
context.log('data:', data); | ||
|
||
if (data.type === 'update_monthly_budget') { | ||
await startCacheUpdate(c.context, true); | ||
context.extraOutputs.set(signalrOutput, [generateRealtimeMessage('monthlyBudgetUpdated')]); | ||
} else { | ||
throw new Error('Invalid type'); | ||
} | ||
}); |
Oops, something went wrong.