The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatsController.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Teknik.Areas.Stats.Models;
  8. using Teknik.Areas.Stats.ViewModels;
  9. using Teknik.Attributes;
  10. using Teknik.Configuration;
  11. using Teknik.Controllers;
  12. using Teknik.Data;
  13. using Teknik.Filters;
  14. using Teknik.Logging;
  15. using Teknik.Tracking;
  16. using Teknik.Utilities;
  17. namespace Teknik.Areas.Stats.Controllers
  18. {
  19. [TeknikAuthorize]
  20. [Area("Stats")]
  21. public class StatsController : DefaultController
  22. {
  23. public StatsController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  24. [AllowAnonymous]
  25. public IActionResult Index()
  26. {
  27. ViewBag.Title = "System Statistics - " + _config.Title;
  28. ViewBag.Description = "Current statistics for the services.";
  29. StatsViewModel model = new StatsViewModel();
  30. // Load initial status info
  31. #region Statistics
  32. model.UploadCount = _dbContext.Uploads.Count();
  33. model.UploadSize = _dbContext.Uploads.Sum(u => u.ContentLength);
  34. model.PasteCount = _dbContext.Pastes.Count();
  35. model.UserCount = _dbContext.Users.Count();
  36. model.ShortenedUrlCount = _dbContext.ShortenedUrls.Count();
  37. model.VaultCount = _dbContext.Vaults.Count();
  38. #endregion
  39. // Get Transaction Inforomation
  40. #region Transactions
  41. DateTime curTime = DateTime.Now;
  42. var billSums = _dbContext.Transactions.OfType<Bill>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList();
  43. foreach (var sum in billSums)
  44. {
  45. decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency);
  46. decimal realValue = sum.total * exchangeRate;
  47. model.Transactions.TotalBills += realValue;
  48. model.Transactions.TotalNet += realValue;
  49. if (curTime.Month == sum.month && curTime.Year == sum.year)
  50. {
  51. model.Transactions.CurrentMonthBills += Math.Abs(realValue);
  52. }
  53. }
  54. var oneSums = _dbContext.Transactions.OfType<OneTime>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList();
  55. foreach (var sum in oneSums)
  56. {
  57. decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency);
  58. decimal realValue = sum.total * exchangeRate;
  59. model.Transactions.TotalOneTimes += realValue;
  60. model.Transactions.TotalNet += realValue;
  61. if (curTime.Month == sum.month && curTime.Year == sum.year)
  62. {
  63. model.Transactions.CurrentMonthBills += Math.Abs(realValue);
  64. }
  65. }
  66. var donationSums = _dbContext.Transactions.OfType<Donation>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList();
  67. foreach (var sum in donationSums)
  68. {
  69. decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency);
  70. decimal realValue = sum.total * exchangeRate;
  71. model.Transactions.TotalDonations += realValue;
  72. model.Transactions.TotalNet += realValue;
  73. if (curTime.Month == sum.month && curTime.Year == sum.year)
  74. {
  75. model.Transactions.CurrentMonthIncome += Math.Abs(realValue);
  76. }
  77. }
  78. List<Bill> bills = _dbContext.Transactions.OfType<Bill>().OrderByDescending(b => b.DateSent).ToList();
  79. if (bills != null)
  80. {
  81. foreach (Bill bill in bills)
  82. {
  83. BillViewModel billModel = new BillViewModel();
  84. billModel.Amount = bill.Amount;
  85. billModel.Currency = bill.Currency;
  86. billModel.Reason = bill.Reason;
  87. billModel.DateSent = bill.DateSent;
  88. billModel.Recipient = bill.Recipient;
  89. model.Transactions.Bills.Add(billModel);
  90. }
  91. }
  92. List<OneTime> oneTimes = _dbContext.Transactions.OfType<OneTime>().OrderByDescending(b => b.DateSent).ToList();
  93. if (oneTimes != null)
  94. {
  95. foreach (OneTime oneTime in oneTimes)
  96. {
  97. OneTimeViewModel oneTimeModel = new OneTimeViewModel();
  98. oneTimeModel.Amount = oneTime.Amount;
  99. oneTimeModel.Currency = oneTime.Currency;
  100. oneTimeModel.Reason = oneTime.Reason;
  101. oneTimeModel.DateSent = oneTime.DateSent;
  102. oneTimeModel.Recipient = oneTime.Recipient;
  103. model.Transactions.OneTimes.Add(oneTimeModel);
  104. }
  105. }
  106. List<Donation> donations = _dbContext.Transactions.OfType<Donation>().OrderByDescending(b => b.DateSent).ToList();
  107. if (donations != null)
  108. {
  109. foreach (Donation donation in donations)
  110. {
  111. DonationViewModel donationModel = new DonationViewModel();
  112. donationModel.Amount = donation.Amount;
  113. donationModel.Currency = donation.Currency;
  114. donationModel.Reason = donation.Reason;
  115. donationModel.DateSent = donation.DateSent;
  116. donationModel.Sender = donation.Sender;
  117. model.Transactions.Donations.Add(donationModel);
  118. }
  119. }
  120. #endregion
  121. // Takedown information
  122. #region Takedowns
  123. List<Takedown> takedowns = _dbContext.Takedowns.OrderByDescending(b => b.DateRequested).ToList();
  124. if (takedowns != null)
  125. {
  126. foreach (Takedown takedown in takedowns)
  127. {
  128. TakedownViewModel takedownModel = new TakedownViewModel();
  129. takedownModel.Requester = takedown.Requester;
  130. takedownModel.RequesterContact = takedown.RequesterContact;
  131. takedownModel.Reason = takedown.Reason;
  132. takedownModel.ActionTaken = takedown.ActionTaken;
  133. takedownModel.DateRequested = takedown.DateRequested;
  134. takedownModel.DateActionTaken = takedown.DateActionTaken;
  135. model.Takedowns.Add(takedownModel);
  136. }
  137. }
  138. #endregion
  139. return View(model);
  140. }
  141. [HttpGet]
  142. [AllowAnonymous]
  143. public ActionResult GetVisitorData()
  144. {
  145. // Get the data from the Piwik
  146. if (!string.IsNullOrEmpty(_config.PiwikConfig.API))
  147. {
  148. List<VisitorData> dataList = Reporting.GetVisitSummaryByDays(_config, 31);
  149. List<object> uniqueData = new List<object>();
  150. List<object> totalData = new List<object>();
  151. foreach (VisitorData data in dataList.OrderBy(d => d.Date))
  152. {
  153. object uniqueDay = new { x = Convert.ToInt64((data.Date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds), y = data.UniqueVisitors };
  154. uniqueData.Add(uniqueDay);
  155. object totalDay = new { x = Convert.ToInt64((data.Date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds), y = data.Visits };
  156. totalData.Add(totalDay);
  157. }
  158. return Json(new { result = new { uniqueVisitors = uniqueData.ToArray(), totalVisitors = totalData.ToArray() } });
  159. }
  160. return Json(new { error = new { message = "Tracking not configured" } });
  161. }
  162. }
  163. }