blob: 776a765622fb1fa20165e8f066a77578e9a47f2b (
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Core.Helpers;
namespace Tango.CctOptimizer.CLI
{
class Program
{
static void Main(string[] args)
{
DataSource dataSource = new DataSource();
dataSource.Catalog = "Tango_DEV";
dataSource.Address = "twine.database.windows.net";
dataSource.IntegratedSecurity = false;
dataSource.UserName = "Roy";
dataSource.Password = "Aa123456";
RESTART:
Console.Write("Checking CCT Optimizations...");
long totalBytes = 0;
List<Cct> toRemove = new List<Cct>();
List<Cct> allCCT = new List<Cct>();
List<Rml> allRmls = new List<Rml>();
try
{
using (ObservablesContext db = ObservablesContext.CreateDefault(dataSource))
{
allRmls = db.Rmls.Where(x => x.CctGuid != null).ToList();
var cctsGuids = db.Ccts.Select(x => x.Guid).ToList();
int cctCount = cctsGuids.Count;
int cctCounter = 0;
foreach (var guid in cctsGuids)
{
var cct = db.Ccts.SingleOrDefault(x => x.Guid == guid);
allCCT.Add(cct);
cctCounter++;
ClearCurrentConsoleLine();
Console.Write($"Checking CCT Optimizations... {(int)((double)cctCounter / (double)cctCount * 100d)}%");
}
Console.WriteLine();
Console.WriteLine();
//NOT USED
var notUsedCCT = allCCT.Where(x => !allRmls.Exists(y => y.CctGuid == x.Guid)).ToList();
Console.WriteLine($"Not used CCT ({notUsedCCT.Count}):");
Console.WriteLine("---------------------------------------");
foreach (var cct in notUsedCCT)
{
long length = cct.Data != null ? cct.Data.Length : 0;
totalBytes += length;
toRemove.Add(cct);
Console.WriteLine($"{(cct.FileName != null ? cct.FileName : "N/A")} - {FileHelper.GetFriendlyFileSize(length)}");
}
//DUPLICATES
List<Cct> toExamine = allCCT.Where(x => x.FileName != null).Where(x => !notUsedCCT.Contains(x)).ToList();
List<Cct> uniqueCCTs = toExamine.DistinctBy(x => x.FileName).ToList();
List<Cct> duplicates = new List<Cct>();
foreach (var cct in toExamine)
{
if (uniqueCCTs.Exists(x => x != cct && x.FileName == cct.FileName))
{
duplicates.Add(cct);
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine($"Duplicates CCT ({duplicates.Count}):");
Console.WriteLine("---------------------------------------");
foreach (var duplicateGroup in duplicates.GroupBy(x => x.FileName))
{
long length = duplicateGroup.First().Data != null ? duplicateGroup.First().Data.Length * duplicateGroup.Count() : 0;
totalBytes += length;
foreach (var cct in duplicateGroup)
{
toRemove.Add(cct);
}
Console.WriteLine($"{(duplicateGroup.First().FileName != null ? duplicateGroup.First().FileName : "N/A")} - {FileHelper.GetFriendlyFileSize(length)} {(duplicateGroup.Count() > 1 ? $"X{duplicateGroup.Count()}" : String.Empty)}");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine($"Total CCT's found: {allCCT.Count}");
Console.WriteLine($"Optimization will delete {toRemove.Count} CCT records ({FileHelper.GetFriendlyFileSize(totalBytes)})");
Console.WriteLine();
if (toRemove.Count > 0)
{
Console.Write("Press 'Y' to start optimization procedure.");
var key = Console.ReadKey();
if (key.Key != ConsoleKey.Y)
{
Environment.Exit(0);
return;
}
}
else
{
Console.WriteLine("Press enter to exit to optimizer...");
Console.ReadLine();
Environment.Exit(0);
return;
}
//Reassign RMLs from duplicates...
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Reassigning RMLs CCTS...");
Console.WriteLine("---------------------------------------");
Console.WriteLine();
foreach (var rml in allRmls)
{
var duplicateCCT = duplicates.SingleOrDefault(x => x.Guid == rml.CctGuid);
if (duplicateCCT != null)
{
var uniqueCCT = uniqueCCTs.SingleOrDefault(x => x.FileName == duplicateCCT.FileName);
Console.WriteLine($"Reassigning RML '{rml.Name}' to '{uniqueCCT.FileName} (ID: {uniqueCCT.ID})'...");
rml.CctGuid = uniqueCCT.Guid;
db.SaveChanges();
}
}
//Remove unused and duplicate CCTs...
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Removing CCT's...");
Console.WriteLine("---------------------------------------");
Console.WriteLine();
foreach (var cct in toRemove)
{
Console.WriteLine($"Removing '{cct.FileName}' ID: {cct.ID}...");
cct.DeleteCascadeAsync(db).Wait();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Optimization Completed.");
Console.WriteLine();
Console.WriteLine();
goto RESTART;
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error occurred while trying to perform the optimization.");
Console.WriteLine(ex.ToString());
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
public static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
}
}
|