Skip to content

Commit 8e5a02a

Browse files
committed
handle date changes
1 parent 93a5f85 commit 8e5a02a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

packages/data-collector/src/Firestore.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class Firestore {
2424
private store: typeof Store
2525
private usageCollection: CollectionReference<DocumentData>
2626
private userCollection: CollectionReference<DocumentData>
27+
private unsubscribeTodaysListener?: () => void
2728

2829
constructor(store: typeof Store) {
2930
this.store = store
@@ -58,7 +59,8 @@ export class Firestore {
5859
callback: (props: { size: number; CO2: number }) => void
5960
) => {
6061
const d = doc(this.usageCollection, `${uid}-${date}`)
61-
onSnapshot(d, (doc) => {
62+
if (this.unsubscribeTodaysListener) this.unsubscribeTodaysListener()
63+
this.unsubscribeTodaysListener = onSnapshot(d, (doc) => {
6264
const data = doc.data() as BaseUsageDoc
6365
const usage = { size: data.size, CO2: data.CO2 }
6466
callback(usage)

packages/data-collector/src/UsageCounter.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class UsageCounter {
99
private usageSinceSubscriptionStarted = 0
1010
private store: typeof Store
1111
private totalCO2 = 0
12+
private currentDate = new Date().setHours(0, 0, 0, 0).valueOf()
1213

1314
constructor(store: typeof Store) {
1415
this.store = store
@@ -47,19 +48,37 @@ export class UsageCounter {
4748
)
4849
}
4950

51+
private checkIfDateHasChanged = () => {
52+
const now = new Date().setHours(0, 0, 0, 0).valueOf()
53+
if (this.currentDate !== now) {
54+
this.currentDate = now
55+
return true
56+
}
57+
return false
58+
}
59+
5060
private handleUsageUpdate = ({
5161
size,
5262
CO2
5363
}: {
5464
size: number
5565
CO2: number
5666
}) => {
67+
if (this.checkIfDateHasChanged()) {
68+
this.usageSinceSubscriptionStarted = 0
69+
this.usageToday = 0
70+
this.usageLast7Days = 0
71+
this.totalUsage = 0
72+
this.totalCO2 = 0
73+
this.listenToChanges()
74+
return
75+
}
5776
if (!this.usageToday) this.usageToday = size
5877
this.usageSinceSubscriptionStarted = size - this.usageToday
5978
this.usageToday += this.usageSinceSubscriptionStarted
6079
this.usageLast7Days += this.usageSinceSubscriptionStarted
6180
this.totalUsage += this.usageSinceSubscriptionStarted
62-
this.totalCO2 += CO2
81+
this.totalCO2 = CO2
6382
this.sendUsageUpdate()
6483
}
6584

packages/data-presenter/src/components/dashboard/UsageDisplay.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ const UsageDisplay: FunctionalComponent = () => {
6666
</div>
6767
<div className="flex justify-center mt-3">
6868
<div className="text-center">
69-
<div className="text-6xl font-medium">{totalCO2.toFixed(2)}</div>
70-
<div className="text-xs font-light">kg CO2</div>
69+
<div className="text-6xl font-medium">{totalCO2.toFixed(4)}</div>
70+
<div className="text-xs font-light">kg CO2 emitted today</div>
7171
</div>
7272
</div>
7373
</div>

0 commit comments

Comments
 (0)