Files
linqpad/q4.linq
T

303 lines
10 KiB
C#

<Query Kind="Program">
<Connection>
<ID>77b65bea-e766-4e3c-a1ed-c4c2534b6019</ID>
<Persist>true</Persist>
<Server>D0001-SQL-01.sdt.local</Server>
<Database>jazz dev master</Database>
<ShowServer>true</ShowServer>
</Connection>
<Reference>&lt;ProgramFilesX64&gt;\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
<NuGetReference>Amazon.Lambda.S3Events</NuGetReference>
<NuGetReference>AWSSDK.Core</NuGetReference>
<NuGetReference>AWSSDK.S3</NuGetReference>
<Namespace>Amazon</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy.ActionIdentifiers</Namespace>
<Namespace>Amazon.Internal</Namespace>
<Namespace>Amazon.Lambda.S3Events</Namespace>
<Namespace>Amazon.MissingTypes</Namespace>
<Namespace>Amazon.Runtime</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement.Internal</Namespace>
<Namespace>Amazon.Runtime.EventStreams</Namespace>
<Namespace>Amazon.Runtime.EventStreams.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal.Auth</Namespace>
<Namespace>Amazon.Runtime.Internal.Settings</Namespace>
<Namespace>Amazon.Runtime.Internal.Transform</Namespace>
<Namespace>Amazon.Runtime.Internal.Util</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces.Internal</Namespace>
<Namespace>Amazon.S3</Namespace>
<Namespace>Amazon.S3.Encryption</Namespace>
<Namespace>Amazon.S3.Encryption.Internal</Namespace>
<Namespace>Amazon.S3.Internal</Namespace>
<Namespace>Amazon.S3.IO</Namespace>
<Namespace>Amazon.S3.Model</Namespace>
<Namespace>Amazon.S3.Model.Internal.MarshallTransformations</Namespace>
<Namespace>Amazon.S3.Transfer</Namespace>
<Namespace>Amazon.S3.Util</Namespace>
<Namespace>Amazon.Util</Namespace>
<Namespace>Amazon.Util.Internal</Namespace>
<Namespace>Amazon.Util.Internal.PlatformServices</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>System</Namespace>
<Namespace>System.ComponentModel.Composition</Namespace>
<Namespace>System.ComponentModel.Composition.Hosting</Namespace>
<Namespace>System.ComponentModel.Composition.Primitives</Namespace>
<Namespace>System.ComponentModel.Composition.ReflectionModel</Namespace>
<Namespace>System.Diagnostics</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
<Namespace>System.IO.Compression</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>System.Numerics</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1.Utilities</Namespace>
<Namespace>ThirdParty.BouncyCastle.Math</Namespace>
<Namespace>ThirdParty.BouncyCastle.OpenSsl</Namespace>
<Namespace>ThirdParty.BouncyCastle.Utilities.IO.Pem</Namespace>
<Namespace>ThirdParty.Ionic.Zlib</Namespace>
<Namespace>ThirdParty.Json.LitJson</Namespace>
<Namespace>ThirdParty.MD5</Namespace>
</Query>
private string accessKeyID = "AKIAIZZXT7LVCPR5COFA";
private string secretKey = "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";
void Main()
{
var process = 3;
var version = (from s in DBSettings where s.Name == "GENERAL_APPVERSION" select s.Setting).SingleOrDefault();
switch (process)
{
case 1:
var reports = (from rr in REReports
join f in Folders on rr.FolderGUID equals f.FolderGuid
join up in UserProfiles on rr.AuthorUserGUID equals up.UserGUID into ups
from up in ups.DefaultIfEmpty()
where rr.IsPublished == true
orderby f.Name, rr.Name
select new reportBase(rr.ReportGUID, f.Name, rr.Name, rr.Description, up == null ? rr.AuthorFullName : up.UserName, rr.DateCreated, version))
.ToList();
PushReportsJson(reports);
break;
case 2:
PullReportsJson();
break;
case 3:
JsonConvert.SerializeObject(from rr in REReports
join f in Folders on rr.FolderGUID equals f.FolderGuid
join up in UserProfiles on rr.AuthorUserGUID equals up.UserGUID into ups
from up in ups.DefaultIfEmpty()
where rr.IsPublished == true
orderby f.Name, rr.Name
select new reportBase(rr.ReportGUID, f.Name, rr.Name, rr.Description, up == null ? rr.AuthorFullName : up.UserName, rr.DateCreated, version), Newtonsoft.Json.Formatting.Indented)
.Dump();
break;
case 4:
var test = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(from rr in REReports
join f in Folders on rr.FolderGUID equals f.FolderGuid
join up in UserProfiles on rr.AuthorUserGUID equals up.UserGUID into ups
from up in ups.DefaultIfEmpty()
where rr.IsPublished == true
orderby f.Name, rr.Name
select new reportBase(rr.ReportGUID, f.Name, rr.Name, rr.Description, up == null ? rr.AuthorFullName : up.UserName, rr.DateCreated, version), Newtonsoft.Json.Formatting.Indented));
var modules = (from t in test select (string)t["Module"]).Distinct().ToList();
modules.Dump();
// var result = ((JArray)test["Module"]).Distinct().ToList();
// result.Dump();
test.Dump();
break;
default:
var migrations = (from rr in REReports
join f in Folders on rr.FolderGUID equals f.FolderGuid
join up in UserProfiles on rr.AuthorUserGUID equals up.UserGUID into ups
from up in ups.DefaultIfEmpty()
where rr.IsPublished == true
orderby f.Name, rr.Name
select new report(rr.ReportGUID, f.Name, rr.Name, rr.Description, up == null ? rr.AuthorFullName : up.UserName, rr.DateCreated, version))
.ToList();
migrations.ForEach(r =>
{
var migration = new StringBuilder();
((SqlConnection)Connection).InfoMessage += (sender, args) => migration.AppendLine(args.Message);
procMigrateGenerateAdHocMigration(r.ReportGUID);
r.Migration = migration.ToString();
});
UploadReports(migrations);
DumpReports();
break;
}
}
void PullReportsJson()
{
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
var getRequest = new GetObjectRequest
{
BucketName = "stratasphere",
Key = "Reports.json"
};
var response = client.GetObject(getRequest);
var reader = new StreamReader(response.ResponseStream);
//reader.ReadToEnd().Dump();
//reader = new StreamReader(response.ResponseStream);
var reports = JsonConvert.DeserializeObject<List<report>>(reader.ReadToEnd());
reports.Select(r => new reportBase(r)).Dump();
}
}
void PushReportsJson(List<reportBase> reports)
{
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
var putRequest = new PutObjectRequest
{
BucketName = "stratasphere",
Key = "Reports.json",
ContentType = "text/plain",
ContentBody = JsonConvert.SerializeObject(reports, Newtonsoft.Json.Formatting.None)
};
client.PutObject(putRequest);
}
}
void DeleteReports(List<report> reportQuery)
{
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
reportQuery.Select(q => q.Category).Distinct().ToList()
.ForEach(r =>
{
var request = new DeleteObjectRequest
{
BucketName = "stratasphere",
Key = r,
};
try
{
client.DeleteObject(request);
}
catch (Exception e)
{
e.Dump();
}
});
}
}
void UploadReports(List<report> reportQuery)
{
$"reports: {reportQuery.Count()}".Dump();
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
var response = client.ListBuckets();
response.Buckets.ForEach(b => $"{b.BucketName} {b.CreationDate}".Dump());
}
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
reportQuery.ToList().ForEach(r =>
{
var putRequest = new PutObjectRequest
{
BucketName = "stratasphere",
Key = $"{r.Category}/{r.Module}/{r.ReportGUID}",
ContentType = "text/plain",
ContentBody = JsonConvert.SerializeObject(r, Newtonsoft.Json.Formatting.Indented)
};
client.PutObject(putRequest);
});
}
}
void DumpReports()
{
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
{
var request = new Amazon.S3.Model.ListObjectsRequest
{
BucketName = "stratasphere"
};
do
{
var response = client.ListObjects(request);
response.Dump();
response.S3Objects.Dump();
response.S3Objects.Where(obj => obj.Key == "Reports.json").Dump();
if (response.IsTruncated)
{
request.Marker = response.NextMarker;
}
else
{
request = null;
}
} while (request != null);
}
}
class reportBase
{
public Guid ReportGUID { get; set; }
public string Category { get; set; }
public string Module { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string UserName { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime UploadDate { get; set; }
public string DbVersion { get; set; }
public reportBase(Guid reportGuid, string module, string name, string description, string userName, DateTime createdDate, string dbVersion)
{
ReportGUID = reportGuid;
Module = module;
Name = name;
Description = description;
Category = "Report";
UserName = userName;
CreatedDate = createdDate;
UploadDate = DateTime.Now;
DbVersion = dbVersion;
}
public reportBase(report r)
{
ReportGUID = r.ReportGUID;
Module = r.Module;
Name = r.Name;
Description = r.Description;
Category = r.Category;
UserName = r.UserName;
CreatedDate = r.CreatedDate;
UploadDate = r.UploadDate;
DbVersion = r.DbVersion;
}
}
class report : reportBase
{
public string Migration { get; set; }
public report(Guid reportGuid, string module, string name, string description, string userName, DateTime createdDate, string dbVersion)
: base(reportGuid, module, name, description, userName, createdDate, dbVersion)
{
Migration = string.Empty;
}
};