UserActivity.xaml.cs 16 KB

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