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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Dashboard-specific data types and structures
#![forbid(unsafe_code)]
#![allow(clippy::arithmetic_side_effects)] // Safe for business logic calculations
#![allow(clippy::cast_possible_truncation)] // Controlled truncation for display formatting
#![allow(clippy::cast_lossless)] // Safe casting for USDC formatting
use crate::program_types::{Plan, Subscription};
use anchor_client::solana_sdk::pubkey::Pubkey;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Overview statistics for a merchant dashboard
#[allow(clippy::derive_partial_eq_without_eq)] // Contains f64 methods
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Overview {
/// Total revenue earned (in USDC microlamports)
pub total_revenue: u64,
/// Number of active subscriptions
pub active_subscriptions: u32,
/// Number of inactive subscriptions
pub inactive_subscriptions: u32,
/// Total number of plans
pub total_plans: u32,
/// Revenue this month (in USDC microlamports)
pub monthly_revenue: u64,
/// New subscriptions this month
pub monthly_new_subscriptions: u32,
/// Canceled subscriptions this month
pub monthly_canceled_subscriptions: u32,
/// Average revenue per user (in USDC microlamports)
pub average_revenue_per_user: u64,
/// Merchant authority address
pub merchant_authority: Pubkey,
/// USDC mint being used
pub usdc_mint: Pubkey,
}
impl Overview {
/// Get total revenue formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn total_revenue_formatted(&self) -> f64 {
self.total_revenue as f64 / 1_000_000.0
}
/// Get monthly revenue formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn monthly_revenue_formatted(&self) -> f64 {
self.monthly_revenue as f64 / 1_000_000.0
}
/// Get average revenue per user formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn average_revenue_per_user_formatted(&self) -> f64 {
self.average_revenue_per_user as f64 / 1_000_000.0
}
/// Calculate churn rate as a percentage
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn churn_rate(&self) -> f64 {
let total_subs = self.active_subscriptions + self.inactive_subscriptions;
if total_subs == 0 {
return 0.0;
}
(self.inactive_subscriptions as f64 / total_subs as f64) * 100.0
}
}
/// Analytics data for a specific subscription plan
#[allow(clippy::derive_partial_eq_without_eq)] // Contains f64 fields
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PlanAnalytics {
/// The plan being analyzed
pub plan: Plan,
/// Plan PDA address
pub plan_address: Pubkey,
/// Number of active subscriptions
pub active_count: u32,
/// Number of inactive subscriptions
pub inactive_count: u32,
/// Total revenue generated by this plan (in USDC microlamports)
pub total_revenue: u64,
/// Revenue this month (in USDC microlamports)
pub monthly_revenue: u64,
/// New subscriptions this month
pub monthly_new_subscriptions: u32,
/// Canceled subscriptions this month
pub monthly_canceled_subscriptions: u32,
/// Average subscription duration in days
pub average_duration_days: f64,
/// Conversion rate percentage (if applicable)
pub conversion_rate: Option<f64>,
}
impl PlanAnalytics {
/// Get total revenue formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn total_revenue_formatted(&self) -> f64 {
self.total_revenue as f64 / 1_000_000.0
}
/// Get monthly revenue formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn monthly_revenue_formatted(&self) -> f64 {
self.monthly_revenue as f64 / 1_000_000.0
}
/// Calculate total subscriptions (active + inactive)
#[must_use]
pub const fn total_subscriptions(&self) -> u32 {
self.active_count + self.inactive_count
}
/// Calculate churn rate as a percentage
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn churn_rate(&self) -> f64 {
let total = self.total_subscriptions();
if total == 0 {
return 0.0;
}
(self.inactive_count as f64 / total as f64) * 100.0
}
/// Calculate monthly growth rate as a percentage
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn monthly_growth_rate(&self) -> f64 {
if self.monthly_canceled_subscriptions >= self.monthly_new_subscriptions {
return 0.0;
}
let net_growth = self.monthly_new_subscriptions - self.monthly_canceled_subscriptions;
let base = if self.active_count >= net_growth {
self.active_count - net_growth
} else {
return 100.0; // If we can't calculate a base, assume 100% growth
};
if base == 0 {
return 100.0;
}
(net_growth as f64 / base as f64) * 100.0
}
}
/// Real-time event data for dashboard monitoring
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DashboardEvent {
/// Event type
pub event_type: DashboardEventType,
/// Plan address (if applicable)
pub plan_address: Option<Pubkey>,
/// Subscription address (if applicable)
pub subscription_address: Option<Pubkey>,
/// Subscriber address (if applicable)
pub subscriber: Option<Pubkey>,
/// Amount involved (if applicable, in USDC microlamports)
pub amount: Option<u64>,
/// Transaction signature
pub transaction_signature: Option<String>,
/// Unix timestamp when the event occurred
pub timestamp: i64,
/// Additional event metadata
pub metadata: HashMap<String, String>,
}
/// Types of events that can occur in the subscription system
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DashboardEventType {
/// New subscription started
SubscriptionStarted,
/// Subscription renewed
SubscriptionRenewed,
/// Subscription canceled
SubscriptionCanceled,
/// Payment failed
PaymentFailed,
/// New plan created
PlanCreated,
/// Plan updated
PlanUpdated,
/// Merchant fees withdrawn
FeesWithdrawn,
}
impl DashboardEvent {
/// Get amount formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn amount_formatted(&self) -> Option<f64> {
self.amount.map(|amount| amount as f64 / 1_000_000.0)
}
/// Check if this event affects revenue calculations
#[must_use]
pub const fn affects_revenue(&self) -> bool {
matches!(
self.event_type,
DashboardEventType::SubscriptionStarted | DashboardEventType::SubscriptionRenewed
)
}
/// Check if this event affects subscription counts
#[must_use]
pub const fn affects_subscription_count(&self) -> bool {
matches!(
self.event_type,
DashboardEventType::SubscriptionStarted | DashboardEventType::SubscriptionCanceled
)
}
}
/// Event stream for real-time dashboard updates
#[derive(Clone, Debug)]
pub struct EventStream {
/// Buffer of recent events
pub events: Vec<DashboardEvent>,
/// Maximum number of events to buffer
pub max_buffer_size: usize,
/// Whether the stream is actively monitoring
pub is_active: bool,
}
impl EventStream {
/// Create a new event stream with default buffer size
#[must_use]
pub fn new() -> Self {
Self::with_buffer_size(1000)
}
/// Create a new event stream with custom buffer size
#[must_use]
pub fn with_buffer_size(buffer_size: usize) -> Self {
Self {
events: Vec::with_capacity(buffer_size),
max_buffer_size: buffer_size,
is_active: false,
}
}
/// Add an event to the stream
pub fn add_event(&mut self, event: DashboardEvent) {
self.events.push(event);
// Remove oldest events if buffer is full
if self.events.len() > self.max_buffer_size {
self.events.remove(0);
}
}
/// Get recent events within a time window (in seconds)
#[must_use]
pub fn recent_events(&self, window_secs: i64) -> Vec<&DashboardEvent> {
let now = chrono::Utc::now().timestamp();
let cutoff = now - window_secs;
self.events
.iter()
.filter(|event| event.timestamp >= cutoff)
.collect()
}
/// Get events of a specific type
#[must_use]
pub fn events_of_type(&self, event_type: &DashboardEventType) -> Vec<&DashboardEvent> {
self.events
.iter()
.filter(|event| &event.event_type == event_type)
.collect()
}
/// Clear all events from the buffer
pub fn clear(&mut self) {
self.events.clear();
}
/// Start monitoring events
pub const fn start(&mut self) {
self.is_active = true;
}
/// Stop monitoring events
pub const fn stop(&mut self) {
self.is_active = false;
}
}
impl Default for EventStream {
fn default() -> Self {
Self::new()
}
}
/// Subscription details with enhanced information for dashboard display
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DashboardSubscription {
/// The subscription data from the blockchain
pub subscription: Subscription,
/// Subscription PDA address
pub address: Pubkey,
/// Associated plan information
pub plan: Plan,
/// Plan PDA address
pub plan_address: Pubkey,
/// Human-readable status
pub status: SubscriptionStatus,
/// Days until next renewal (if active)
pub days_until_renewal: Option<i64>,
/// Total amount paid over subscription lifetime
pub total_paid: u64,
}
/// Human-readable subscription status
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SubscriptionStatus {
/// Subscription is active and current
Active,
/// Subscription is active but overdue (within grace period)
Overdue,
/// Subscription is inactive/canceled
Inactive,
/// Subscription is expired (past grace period)
Expired,
}
impl DashboardSubscription {
/// Get total paid amount formatted as USDC (6 decimal places)
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn total_paid_formatted(&self) -> f64 {
self.total_paid as f64 / 1_000_000.0
}
/// Calculate the status based on subscription data
#[must_use]
pub const fn calculate_status(
subscription: &Subscription,
current_timestamp: i64,
) -> SubscriptionStatus {
if !subscription.active {
return SubscriptionStatus::Inactive;
}
if current_timestamp <= subscription.next_renewal_ts {
SubscriptionStatus::Active
} else {
// We would need the plan's grace period to determine if it's overdue or expired
// For now, just mark as overdue if past renewal time
SubscriptionStatus::Overdue
}
}
/// Calculate days until next renewal
#[must_use]
pub const fn calculate_days_until_renewal(
next_renewal_ts: i64,
current_timestamp: i64,
) -> Option<i64> {
if next_renewal_ts <= current_timestamp {
return None; // Past due
}
let seconds_diff = next_renewal_ts - current_timestamp;
Some(seconds_diff / 86400) // Convert to days
}
}