aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.Portal/Utils/StatisticsUtils.cs
blob: f8bfcdd6d792a12e600da909b74827a01cd1c3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Tango.BL.Entities;
using Tango.Portal.Enumerations;
using Tango.Portal.Models;

namespace Tango.Portal.Utils
{
    public class StatisticsUtils
    {
        private static readonly Lazy<StatisticsUtils> _default = new Lazy<StatisticsUtils>(() => new StatisticsUtils());
        public static StatisticsUtils Default => _default.Value;

        private Statistics _stats;
        private DateTime _fetchDate { get; set; }

        public Statistics GetStats()
        {
            if (_stats == null || DateTime.Now > _fetchDate.AddDays(1))
            {
                _fetchDate = DateTime.Now;
                _stats = new Statistics();

                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        foreach (var env in Enum.GetValues(typeof(PortalEnvironment)).Cast<PortalEnvironment>())
                        {
                            using (var db = DbUtils.CreateContext(env))
                            {
                                var jobRuns = db.JobRuns.Select(x => new { x.ActualStartPosition, x.ActualEndPosition, x.EndPosition, x.Status })
                                    .ToList()
                                    .Select(x => new JobRun()
                                    {
                                        ActualStartPosition = x.ActualStartPosition,
                                        ActualEndPosition = x.ActualEndPosition,
                                        EndPosition = x.EndPosition,
                                        Status = x.Status
                                    })
                                    .ToList();

                                int[] arr = (new List<BL.Enumerations.TangoUpdateStatuses>()
                        {
                             BL.Enumerations.TangoUpdateStatuses.DatabaseCompleted,
                              BL.Enumerations.TangoUpdateStatuses.OfflineFirmwareUpgradeCompleted,
                               BL.Enumerations.TangoUpdateStatuses.OfflineUpdateCompleted,
                                BL.Enumerations.TangoUpdateStatuses.SetupCompleted,
                                 BL.Enumerations.TangoUpdateStatuses.UpdateCompleted
                        }).Cast<int>().ToArray();

                                var upgrades = db.TangoUpdates.Where(x => arr.Contains(x.Status)).Count();

                                _stats.TotalDyedThread += (int)jobRuns.Sum(x => x.Distance) / 1000;
                                _stats.JobsCompleted += jobRuns.Count;
                                _stats.SoftwareUpgrades += upgrades;
                            }
                        }
                    }
                    catch { }
                });
            }

            return _stats;
        }

        private StatisticsUtils()
        {
            _stats = new Statistics();
        }
    }
}