The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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.

StatusController.cs 9.1KB

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