aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.JobRunsGenerator/Program.cs
blob: 6c3c62606a1beb58d57a888af36db73937d4bd1d (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
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;

namespace Tango.JobRunsGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Job Runs Generator Started...");

            DateTime now = DateTime.UtcNow;
            DateTime yearago = DateTime.UtcNow.AddYears(-1);
            Random rnd = new Random();
            List<String> messages = new List<string>()
            {
                "Timeout failure",
                "Thread Break",
                "Application Exception",
                "Over Temperature",
                "Communication Error"
            };

            int count = 0;

            using (ObservablesContext db = ObservablesContext.CreateDefault("localhost\\SQLEXPRESS"))
            {
                var machines = db.Machines.ToList();

                foreach (var machine in machines)
                {
                    new MachineBuilder(db).Set(machine).WithJobs().Build();

                    foreach (var job in machine.Jobs)
                    {
                        using (ObservablesContext db2 = ObservablesContext.CreateDefault("localhost\\SQLEXPRESS"))
                        {
                            for (DateTime date = yearago; date < now; date = date.AddDays(1))
                            {
                                Console.WriteLine($"Adding job runs for machine '{machine.SerialNumber}' job '{job.Name}' date {date.ToShortDateString()}...");

                                for (int i = 0; i < rnd.Next(0, 9); i++)
                                {
                                    int status = rnd.Next(0, 3);
                                    String message = messages[rnd.Next(0, messages.Count)];

                                    db2.JobRuns.Add(new JobRun()
                                    {
                                        StartDate = date,
                                        EndDate = date.AddMinutes(rnd.Next(10, 61)),
                                        JobGuid = job.Guid,
                                        EndPosition = job.Length / rnd.Next(1, 11),
                                        Status = status,
                                        FailedMessage = status == 2 ? message : null,
                                    });

                                    count++;
                                }
                            }

                            db2.SaveChanges();
                        }
                    }
                }

                Console.WriteLine($"The generator is about to insert {count} job runs to the database are you sure? [Y/N]:");
                var key = Console.ReadKey().Key;

                if (key == ConsoleKey.Y)
                {
                    Console.WriteLine("Saving changes to database...");
                    Console.WriteLine("Done!");
                    Console.ReadLine();
                }
                else
                {
                    return;
                }
            }
        }
    }
}