UserActivity.xaml.cs 16 KB

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