UserActivity.xaml.cs 16 KB

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