forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.ts
More file actions
250 lines (223 loc) · 6.76 KB
/
Copy pathmetrics.ts
File metadata and controls
250 lines (223 loc) · 6.76 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type { EscrowEvent } from '../soroban/events';
export type CampaignStatus =
| 'Active'
| 'Funding'
| 'Funded'
| 'InProduction'
| 'Harvested'
| 'Disputed'
| 'Resolved'
| 'Settled'
| 'Failed';
export const CAMPAIGN_STATUSES: CampaignStatus[] = [
'Active',
'Funding',
'Funded',
'InProduction',
'Harvested',
'Disputed',
'Resolved',
'Settled',
'Failed',
];
/** Statuses reached only after a campaign's funding target was met. */
const TARGET_REACHED_STATUSES = new Set<CampaignStatus>([
'Funded',
'InProduction',
'Harvested',
'Disputed',
'Resolved',
'Settled',
]);
export interface FundingVolumePoint {
date: string;
amount: number;
}
export interface StatusCount {
status: CampaignStatus;
count: number;
}
export interface SizeBucket {
label: string;
count: number;
}
export interface FundingOutcomeSlice {
label: string;
value: number;
}
export interface AnalyticsMetrics {
totalCampaigns: number;
totalVolume: number;
averageCampaignSize: number;
fundingSuccessRatePercent: number;
fundingVolumeOverTime: FundingVolumePoint[];
campaignsByStatus: StatusCount[];
campaignSizeDistribution: SizeBucket[];
fundingOutcome: FundingOutcomeSlice[];
}
interface CampaignAccumulator {
targetAmount: number | null;
status: CampaignStatus;
}
function toNumber(value: unknown): number {
if (typeof value === 'bigint') return Number(value);
if (typeof value === 'number') return value;
return Number(value ?? 0);
}
function dayKey(unixSeconds: number): string {
return new Date(unixSeconds * 1000).toISOString().slice(0, 10);
}
/**
* Replays ProductionEscrowContract events (in ledger order) into campaign
* accumulators and derived chart-ready metrics. Pure function - no I/O.
*/
export function computeAnalyticsMetrics(
rawEvents: EscrowEvent[],
): AnalyticsMetrics {
const events = [...rawEvents].sort((a, b) => a.ledger - b.ledger);
const campaigns = new Map<string, CampaignAccumulator>();
const volumeByDay = new Map<string, number>();
let totalVolume = 0;
for (const event of events) {
if (!event.campaignId) continue;
switch (event.name) {
case 'CampaignCreated': {
const [, timestamp, targetAmount] = event.values;
campaigns.set(event.campaignId, {
targetAmount: toNumber(targetAmount),
status: 'Active',
});
void timestamp;
break;
}
case 'ContribReceived': {
const [, timestamp, amount] = event.values;
const amountNum = toNumber(amount);
totalVolume += amountNum;
const key = dayKey(toNumber(timestamp));
volumeByDay.set(key, (volumeByDay.get(key) ?? 0) + amountNum);
const campaign = campaigns.get(event.campaignId);
if (campaign && campaign.status === 'Active') {
campaign.status = 'Funding';
}
break;
}
case 'CampaignFunded': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Funded';
break;
}
case 'TrancheReleased': {
const campaign = campaigns.get(event.campaignId);
if (campaign && campaign.status === 'Funded') {
campaign.status = 'InProduction';
}
break;
}
case 'CampaignFailed': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Failed';
break;
}
case 'HarvestReported': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Harvested';
break;
}
case 'DisputeOpened': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Disputed';
break;
}
case 'DisputeResolved': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Resolved';
break;
}
case 'CampaignSettled': {
const campaign = campaigns.get(event.campaignId);
if (campaign) campaign.status = 'Settled';
break;
}
default:
break;
}
}
const totalCampaigns = campaigns.size;
const targetAmounts = [...campaigns.values()]
.map((c) => c.targetAmount)
.filter((amount): amount is number => amount != null);
const averageCampaignSize =
targetAmounts.length > 0
? targetAmounts.reduce((sum, amount) => sum + amount, 0) /
targetAmounts.length
: 0;
const reachedTarget = [...campaigns.values()].filter((c) =>
TARGET_REACHED_STATUSES.has(c.status),
).length;
const failed = [...campaigns.values()].filter(
(c) => c.status === 'Failed',
).length;
const stillRaising = totalCampaigns - reachedTarget - failed;
const fundingSuccessRatePercent =
totalCampaigns > 0 ? (reachedTarget / totalCampaigns) * 100 : 0;
const statusCounts = new Map<CampaignStatus, number>();
for (const status of CAMPAIGN_STATUSES) statusCounts.set(status, 0);
for (const campaign of campaigns.values()) {
statusCounts.set(
campaign.status,
(statusCounts.get(campaign.status) ?? 0) + 1,
);
}
return {
totalCampaigns,
totalVolume,
averageCampaignSize,
fundingSuccessRatePercent,
fundingVolumeOverTime: [...volumeByDay.entries()]
.sort(([a], [b]) => (a < b ? -1 : 1))
.map(([date, amount]) => ({ date, amount })),
campaignsByStatus: CAMPAIGN_STATUSES.map((status) => ({
status,
count: statusCounts.get(status) ?? 0,
})),
campaignSizeDistribution: buildSizeDistribution(targetAmounts),
fundingOutcome: [
{ label: 'Reached target', value: reachedTarget },
{ label: 'Still raising', value: Math.max(stillRaising, 0) },
{ label: 'Failed', value: failed },
],
};
}
/** Buckets campaign target amounts into up to 5 evenly-sized ranges. */
function buildSizeDistribution(amounts: number[]): SizeBucket[] {
if (amounts.length === 0) return [];
const min = Math.min(...amounts);
const max = Math.max(...amounts);
if (min === max) {
return [{ label: formatAmount(min), count: amounts.length }];
}
const bucketCount = Math.min(5, amounts.length);
const bucketSize = (max - min) / bucketCount;
const buckets = Array.from({ length: bucketCount }, () => 0);
for (const amount of amounts) {
const index = Math.min(
bucketCount - 1,
Math.floor((amount - min) / bucketSize),
);
buckets[index] += 1;
}
return buckets.map((count, index) => {
const rangeStart = min + index * bucketSize;
const rangeEnd = index === bucketCount - 1 ? max : rangeStart + bucketSize;
return {
label: `${formatAmount(rangeStart)}–${formatAmount(rangeEnd)}`,
count,
};
});
}
function formatAmount(value: number): string {
return new Intl.NumberFormat('en-US', { notation: 'compact' }).format(
Math.round(value),
);
}