UserActivity.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using Comal.Classes;
  13. using InABox.Clients;
  14. using InABox.Configuration;
  15. using InABox.Core;
  16. using InABox.WPF;
  17. using PRSDesktop.WidgetGroups;
  18. using Syncfusion.UI.Xaml.Charts;
  19. namespace PRSDesktop
  20. {
  21. public class UserActivityDashboardProperties : IUserConfigurationSettings, IDashboardProperties { }
  22. public class UserActivityDashboardElement : DashboardElement<UserActivity, WidgetGroups.System, UserActivityDashboardProperties> { }
  23. /// <summary>
  24. /// Interaction logic for DatabaseActivityDashboard.xaml
  25. /// </summary>
  26. public partial class UserActivity : UserControl, IPanel<ModuleTracking>, IDashboardWidget<WidgetGroups.System, UserActivityDashboardProperties>
  27. {
  28. public enum HistoryView
  29. {
  30. Day,
  31. Week,
  32. Month,
  33. Year
  34. }
  35. private CoreTable _employees;
  36. private int _selected = -1;
  37. private readonly BitmapImage back = PRSDesktop.Resources.back.AsBitmapImage(32, 32);
  38. private HistoryViewModel history;
  39. private readonly BitmapImage next = PRSDesktop.Resources.next.AsBitmapImage(32, 32);
  40. public UserActivity()
  41. {
  42. InitializeComponent();
  43. PrevDay.Content = new Image { Source = back };
  44. NextDay.Content = new Image { Source = next };
  45. }
  46. public bool IsReady { get; set; }
  47. public void CreateToolbarButtons(IPanelHost host)
  48. {
  49. }
  50. public string SectionName => "User Activity";
  51. public UserActivityDashboardProperties Properties { get; set; }
  52. public event LoadSettings<UserActivityDashboardProperties>? LoadSettings;
  53. public event SaveSettings<UserActivityDashboardProperties>? SaveSettings;
  54. public DataModel DataModel(Selection selection)
  55. {
  56. return new UserActivityDataModel(history?.Summary ?? new List<History>());
  57. }
  58. public void Refresh()
  59. {
  60. if (history.To == DateTime.MinValue)
  61. history.To = DateTime.Today;
  62. if (history.View == HistoryView.Day)
  63. CurrentDate.Text = string.Format("{0:ddd, dd MMMM yyyy}", history.To);
  64. else
  65. CurrentDate.Text = string.Format("{0:dd MMM yy} - {1:dd MMM yy}", history.From, history.To);
  66. // Only creating two series active & inactive
  67. var colortable = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public).Where(x => x.PropertyType == typeof(Color))
  68. .ToArray();
  69. Hours.Series.Clear();
  70. var series = new StackingColumnSeries();
  71. series.ItemsSource = history.Summary;
  72. series.XBindingPath = "Name";
  73. series.YBindingPath = "Active";
  74. series.Label = "Active";
  75. series.Interior = new SolidColorBrush(Colors.LightSalmon);
  76. series.SeriesSelectionBrush = new SolidColorBrush(Colors.Salmon);
  77. Hours.Series.Add(series);
  78. series = new StackingColumnSeries();
  79. series.ItemsSource = history.Summary;
  80. series.XBindingPath = "Name";
  81. series.YBindingPath = "Idle";
  82. series.Label = "Idle";
  83. series.Interior = new SolidColorBrush(Colors.LightSteelBlue);
  84. series.SeriesSelectionBrush = new SolidColorBrush(Colors.SteelBlue);
  85. Hours.Series.Add(series);
  86. var axis = Hours.SecondaryAxis as NumericalAxis;
  87. axis.Maximum = history.MaxMalue(5.0F);
  88. }
  89. public Dictionary<string, object[]> Selected()
  90. {
  91. return new Dictionary<string, object[]>();
  92. }
  93. public void Setup()
  94. {
  95. history = new HistoryViewModel();
  96. DataContext = history;
  97. var grplookups = new Dictionary<Guid, string>();
  98. //Dictionary<Guid, String> emplookups = new Dictionary<Guid, string>() { { Guid.Empty, "All Staff" } };
  99. _employees = new Client<Employee>().Query(
  100. LookupFactory.DefineFilter<Employee>(),
  101. new Columns<Employee>(
  102. x => x.ID,
  103. x => x.UserLink.ID,
  104. x => x.Name,
  105. x => x.Group.ID,
  106. x => x.Group.Description
  107. ),
  108. new SortOrder<Employee>(x => x.Name)
  109. );
  110. foreach (var row in _employees.Rows)
  111. {
  112. var grpid = row.Get<Employee, Guid>(x => x.Group.ID);
  113. var grpname = row.Get<Employee, string>(x => x.Group.Description);
  114. if (string.IsNullOrEmpty(grpname))
  115. grpname = "(No Group Assigned)";
  116. if (!grplookups.ContainsKey(grpid))
  117. grplookups[grpid] = grpname;
  118. //emplookups[row.Get<Employee, Guid>(x => x.UserLink.ID)] = row.Get<Employee, String>(x => x.Name);
  119. }
  120. var groups = grplookups.ToList();
  121. groups.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
  122. groups.Insert(0, new KeyValuePair<Guid, string>(CoreUtils.FullGuid, "All Groups"));
  123. GroupView.ItemsSource = groups;
  124. //StaffView.ItemsSource = emplookups;
  125. //LoadCategories(Guid.Empty);
  126. Hours.Legend = new ChartLegend
  127. {
  128. LegendPosition = LegendPosition.Outside,
  129. DockPosition = ChartDock.Right,
  130. FontSize = 11,
  131. Width = 100
  132. //ItemsPanel = Hours.Resources["itemPanelTemplate"] as ItemsPanelTemplate
  133. };
  134. }
  135. public void Shutdown(CancelEventArgs? cancel)
  136. {
  137. }
  138. public event DataModelUpdateEvent? OnUpdateDataModel;
  139. public void Heartbeat(TimeSpan time)
  140. {
  141. // Nothing to do here
  142. }
  143. private bool HasData(List<History> history)
  144. {
  145. foreach (var record in history)
  146. {
  147. var hasdata = record.Active + record.Idle > 0.0F;
  148. if (hasdata)
  149. return true;
  150. }
  151. return false;
  152. }
  153. private void LoadCategories(Guid userid)
  154. {
  155. history.Categories.Clear();
  156. if (userid.Equals(Guid.Empty))
  157. {
  158. foreach (var row in _employees.Rows)
  159. if (GroupView.SelectedValue.Equals(CoreUtils.FullGuid) ||
  160. row.Get<Employee, Guid>(x => x.Group.ID).Equals(GroupView.SelectedValue))
  161. history.Categories[row.Get<Employee, Guid>(x => x.UserLink.ID).ToString()] = row.Get<Employee, string>(x => x.Name);
  162. }
  163. else
  164. {
  165. foreach (var module in Modules.All.OrderBy(x => x))
  166. history.Categories[module] = module;
  167. }
  168. }
  169. private void GroupView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. if (history == null)
  172. return;
  173. if (e.AddedItems.Count > 0)
  174. {
  175. var id = ((KeyValuePair<Guid, string>)e.AddedItems[0]).Key;
  176. var curhistory = history;
  177. history = null;
  178. var emplookups = new Dictionary<Guid, string> { { Guid.Empty, "All Staff" } };
  179. foreach (var row in _employees.Rows)
  180. {
  181. var grpid = row.Get<Employee, Guid>(x => x.Group.ID);
  182. if (id.Equals(CoreUtils.FullGuid) || grpid.Equals(id))
  183. emplookups[row.Get<Employee, Guid>(x => x.UserLink.ID)] = row.Get<Employee, string>(x => x.Name);
  184. }
  185. StaffView.ItemsSource = null;
  186. history = curhistory;
  187. StaffView.ItemsSource = emplookups;
  188. if (!Guid.Empty.Equals(StaffView.SelectedValue))
  189. StaffView.SelectedValue = Guid.Empty;
  190. }
  191. }
  192. private void StaffView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  193. {
  194. if (history == null)
  195. return;
  196. if (e.AddedItems.Count > 0)
  197. {
  198. var id = ((KeyValuePair<Guid, string>)e.AddedItems[0]).Key;
  199. LoadCategories(id);
  200. history.UserID = id;
  201. Refresh();
  202. ShowAll.Width = id != Guid.Empty ? new GridLength(70) : new GridLength(0);
  203. }
  204. }
  205. private void ViewStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
  206. {
  207. if (history == null)
  208. return;
  209. history.View = ViewStyle.SelectedIndex == 0 ? HistoryView.Day :
  210. ViewStyle.SelectedIndex == 1 ? HistoryView.Week :
  211. ViewStyle.SelectedIndex == 2 ? HistoryView.Month : HistoryView.Year;
  212. Refresh();
  213. }
  214. private void Search_KeyUp(object sender, KeyEventArgs e)
  215. {
  216. if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
  217. {
  218. history.Search = Search.Text;
  219. Refresh();
  220. }
  221. }
  222. private void PrevDay_Click(object sender, RoutedEventArgs e)
  223. {
  224. history.Back();
  225. Refresh();
  226. }
  227. private void CurrentDate_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  228. {
  229. history.To = DateTime.Today;
  230. Refresh();
  231. }
  232. private void NextDay_Click(object sender, RoutedEventArgs e)
  233. {
  234. history.Forward();
  235. Refresh();
  236. }
  237. private void Export_Click(object sender, RoutedEventArgs e)
  238. {
  239. }
  240. private void Hours_SelectionChanged(object sender, ChartSelectionChangedEventArgs e)
  241. {
  242. if (e.IsDataPointSelection)
  243. {
  244. if (history.UserID == Guid.Empty && e.SelectedIndex == -1 && _selected > -1)
  245. {
  246. var emps = StaffView.ItemsSource as Dictionary<Guid, string>;
  247. var id = emps.Keys.ElementAt(_selected + 1);
  248. LoadCategories(id);
  249. StaffView.SelectedValue = id;
  250. }
  251. _selected = e.SelectedIndex;
  252. }
  253. }
  254. private void ShowAll_Click(object sender, RoutedEventArgs e)
  255. {
  256. StaffView.SelectedValue = Guid.Empty;
  257. }
  258. // Either:
  259. // - Person + Time Spent on any module
  260. // - Module + Time Spent in Specific Module
  261. public class History
  262. {
  263. public string Key { get; set; }
  264. public string Name { get; set; }
  265. public double Active { get; set; }
  266. public double Idle { get; set; }
  267. }
  268. public class HistoryViewModel
  269. {
  270. private string _search = "";
  271. private DateTime _to = DateTime.MinValue;
  272. private Guid _userid = Guid.Empty;
  273. private HistoryView _view = HistoryView.Day;
  274. public HistoryViewModel()
  275. {
  276. Categories = new Dictionary<string, string>();
  277. Summary = new List<History>();
  278. To = DateTime.Today;
  279. }
  280. public Guid UserID
  281. {
  282. get => _userid;
  283. set
  284. {
  285. _userid = value;
  286. Refresh();
  287. }
  288. }
  289. public HistoryView View
  290. {
  291. get => _view;
  292. set
  293. {
  294. _view = value;
  295. Refresh();
  296. }
  297. }
  298. public DateTime To
  299. {
  300. get => _to.Date;
  301. set
  302. {
  303. _to = value;
  304. Refresh();
  305. }
  306. }
  307. public DateTime From => _view.Equals(HistoryView.Day) ? _to :
  308. _view.Equals(HistoryView.Week) ? _to.AddDays(-6) :
  309. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1).AddDays(1) : _to.AddYears(-1).AddDays(1);
  310. public Dictionary<string, string> Categories { get; }
  311. public List<History> Summary { get; set; }
  312. public string Search
  313. {
  314. get => _search;
  315. set
  316. {
  317. _search = value;
  318. Refresh();
  319. }
  320. }
  321. public void Forward()
  322. {
  323. To = _view.Equals(HistoryView.Day) ? _to.AddDays(1) :
  324. _view.Equals(HistoryView.Week) ? _to.AddDays(7) :
  325. _view.Equals(HistoryView.Month) ? _to.AddMonths(1) : _to.AddYears(1);
  326. }
  327. public void Back()
  328. {
  329. To = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
  330. _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
  331. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
  332. }
  333. private void AddSearchCriteria(Filter<ModuleTracking> filter, string search,
  334. params Expression<Func<ModuleTracking, object>>[] expressions)
  335. {
  336. var comps = search.Trim().Split(' ');
  337. foreach (var comp in comps)
  338. {
  339. Filter<ModuleTracking> result = null;
  340. foreach (var expression in expressions)
  341. result = result == null ? new Filter<ModuleTracking>(expression).Contains(comp) : result.Or(expression).Contains(comp);
  342. filter.Ands.Add(result);
  343. }
  344. }
  345. private void UpdateHistory(CoreRow row)
  346. {
  347. var category = "";
  348. if (_userid.Equals(Guid.Empty))
  349. category = row.Get<ModuleTracking, Guid>(x => x.User.ID).ToString();
  350. else
  351. category = row.Get<ModuleTracking, string>(x => x.Module);
  352. var record = Summary.FirstOrDefault(x => x.Key.Equals(category));
  353. if (record != null)
  354. {
  355. record.Active += row.Get<ModuleTracking, TimeSpan>(x => x.ActiveTime).TotalHours;
  356. record.Idle += row.Get<ModuleTracking, TimeSpan>(x => x.IdleTime).TotalHours;
  357. }
  358. }
  359. public Filter<ModuleTracking> GetFilter()
  360. {
  361. var from = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
  362. _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
  363. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
  364. var criteria = new Filter<ModuleTracking>(x => x.Date).IsGreaterThan(from).And(x => x.Date).IsLessThanOrEqualTo(_to);
  365. if (!_userid.Equals(Guid.Empty))
  366. criteria = criteria.And(x => x.User.ID).IsEqualTo(_userid);
  367. if (!string.IsNullOrWhiteSpace(_search))
  368. criteria = criteria.TextSearch(
  369. Search,
  370. x => x.User.UserID,
  371. x => x.Module
  372. );
  373. return criteria;
  374. }
  375. private void Refresh()
  376. {
  377. var criteria = GetFilter();
  378. var data = new Client<ModuleTracking>().Query(
  379. criteria,
  380. null,
  381. new SortOrder<ModuleTracking>(x => x.User.UserID)
  382. );
  383. Summary.Clear();
  384. foreach (var key in Categories.Keys)
  385. Summary.Add(new History { Key = key, Name = Categories[key] });
  386. foreach (var row in data.Rows) UpdateHistory(row);
  387. }
  388. public double MaxMalue(double min)
  389. {
  390. var result = min;
  391. foreach (var history in Summary)
  392. if (history.Active + history.Idle > result)
  393. result = history.Active + history.Idle;
  394. return Math.Truncate(result + 1.0F);
  395. }
  396. }
  397. }
  398. }