installAnalytics method

Future<AnalyticsData> installAnalytics({
  1. int days = 30,
})

Fetch install-on-request analytics.

GET /api/analytics/install-on-request/homebrew-core/{days}d.json

Implementation

Future<AnalyticsData> installAnalytics({int days = 30}) async {
  final body = await _get(
    Uri.parse(
      '$baseUrl/analytics/install-on-request/homebrew-core/${days}d.json',
    ),
  );
  final json = jsonDecode(body) as Map<String, dynamic>;

  // The API returns items as a list of {formula: count} maps under
  // "items" or "formulae". Normalize to a simple Map<String, int>.
  final items = <String, int>{};
  final rawItems =
      json['items'] as List<dynamic>? ??
      json['formulae'] as List<dynamic>? ??
      [];
  for (final item in rawItems) {
    if (item is Map<String, dynamic>) {
      for (final entry in item.entries) {
        if (entry.value is int) {
          items[entry.key] = entry.value as int;
        } else if (entry.value is String) {
          items[entry.key] = int.tryParse(entry.value as String) ?? 0;
        }
      }
    }
  }

  return AnalyticsData(
    category: json['category'] as String? ?? 'install-on-request',
    totalItems: json['total_items'] as int? ?? items.length,
    startDate: json['start_date'] as String? ?? '',
    endDate: json['end_date'] as String? ?? '',
    totalCount: json['total_count'] as int? ?? 0,
    items: items,
  );
}