Files
linqpad/StarterSet API/Starter Set API Definitions Search.linq
T

318 lines
12 KiB
C#

<Query Kind="Program">
<Connection>
<ID>463c2997-8a6a-4688-9575-6549e5215f7b</ID>
<Persist>true</Persist>
<Server>D0011-SQL-01.sdt.local</Server>
<Database>jazz tst Riverside Health System 20190930.1</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>
<NuGetReference>RestSharp</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.Diagnostics</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
<Namespace>System.IO.Compression</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>
<Namespace>RestSharp</Namespace>
</Query>
private string BaseUrl = "https://api-dev.sdt.local/StrataSphere.Web.API.Service/api/v1/";
private IList<StrataSphereMappedDefinition> _mappedDefinitions;
void Main()
{
RestClient client = new RestClient();
var definitions = GetList<DefinitionDto>(client, "Definitions");
var standardDefinitionMappings = from def in StandardDefinitionMappings
select new
{
def.MappingId,
def.StandardDefinitionGUID,
def.StandardDefinitionType,
MappingMemberValue = JsonConvert.DeserializeObject(def.MappingMemberValue)
};
var guidDefinitionDictionary = standardDefinitionMappings.ToDictionary(u => u.StandardDefinitionGUID);
var foundDefinitions = definitions.Where(p => guidDefinitionDictionary.ContainsKey(p.Id));
_mappedDefinitions = (from d in foundDefinitions
join m in StandardDefinitionMappings on d.Id equals m.StandardDefinitionGUID
select new StrataSphereMappedDefinition(d, m.MappingMemberValue)).ToList();
var pathAlias = "HIERALIAS";
var sqlTableAlias = "viewDimMetri";
var attributeID = new[] { "Cardiomyopathy" }.ToList();
StrataSphereMappedValue find;
attributeID.ForEach(id =>
{
find = MapToStrataSphere("[Case Type Family MSDRG]", "[Case Type Family MSDRG].[Case Type Family]",
new List<string> { $"{id}" });
find.Dump($"StrataSphereMappedValue: {id}");
});
}
// Define other methods and classes here
public StrataSphereMappedValue MapToStrataSphere(string dimensionId, string attributeId, IList<string> valuesToMap)
{
var mdx = new Regex(@"^(\[.+\]\.)+\[.+\]$");
var clientValues = valuesToMap.Select(valueToMap => mdx.IsMatch(valueToMap)
? AnalysisServicesUtils.GetMeasureIDFromMDXName(valueToMap)
: valueToMap).ToList();
return FindMappedValue(dimensionId, attributeId, clientValues);
}
public static class AnalysisServicesUtils
{
public static string GetMeasureIDFromMDXName(string aMemberMDXName)
{
return aMemberMDXName;
}
}
protected StrataSphereMappedValue FindMappedValue(string dimensionId, string attributeId, List<string> valuesToMap)
{
var definitions = _mappedDefinitions
.Where(d => d.DimensionId.Equals(dimensionId, StringComparison.CurrentCultureIgnoreCase)
&& d.AttributeId.Equals(attributeId, StringComparison.CurrentCultureIgnoreCase)).ToList();
var mappings = definitions.Where(p => p.MappedValues != null && p.MappedValues.mappings.Any()).SelectMany(p => p.MappedValues.mappings);
mappings.Dump();
var mappedDefinition = definitions
.SingleOrDefault(p => p.MappedValues != null
&& (!p.MappedValues.mappings.Any() ||
p.MappedValues.mappings.SelectMany(m => m.attributeValues
.Select(a => a.ToString()))
.OrderBy(v => v).SequenceEqual(valuesToMap.OrderBy(v => v), StringComparer.CurrentCultureIgnoreCase))) ??
definitions
.SingleOrDefault(p => p.MappedValues != null
&& (!p.MappedValues.mappings.Any() ||
p.MappedValues.mappings
.Select(m => m.name)
.OrderBy(v => v).SequenceEqual(valuesToMap.OrderBy(v => v), StringComparer.CurrentCultureIgnoreCase))) ??
definitions
.SingleOrDefault(p => p.MappedValues != null
&& (!p.MappedValues.mappings.Any() ||
p.MappedValues.mappings
.Select(m => m.text)
.OrderBy(v => v).SequenceEqual(valuesToMap.OrderBy(v => v), StringComparer.CurrentCultureIgnoreCase))) ??
definitions
.SingleOrDefault(p => p.MappedValues != null
&& (!p.MappedValues.mappings.Any() ||
p.MappedValues.mappings
.Select(m => m.id)
.OrderBy(v => v).SequenceEqual(valuesToMap.OrderBy(v => v), StringComparer.CurrentCultureIgnoreCase))) ??
definitions
.SingleOrDefault(p => p.MappedValues != null
&& (!p.MappedValues.mappings.Any() ||
p.MappedValues.mappings
.SelectMany(m => m.hierarchyPaths)
.OrderBy(v => v).SequenceEqual(valuesToMap.OrderBy(v => v), StringComparer.CurrentCultureIgnoreCase))) ??
definitions.FirstOrDefault();
return mappedDefinition != null
? new StrataSphereMappedValue(valuesToMap, mappedDefinition.StrataSphereDefinitionGuid)
: new StrataSphereMappedValue(valuesToMap);
}
public struct StrataSphereMappedValue
{
public IList<string> ClientValues { get; set; }
public Guid? StrataSphereDefinitionGuid { get; set; }
public StrataSphereMappedValue(IList<string> clientValues)
{
ClientValues = clientValues;
StrataSphereDefinitionGuid = null;
}
public StrataSphereMappedValue(IList<string> clientValues, Guid definitionGuid)
{
ClientValues = clientValues;
StrataSphereDefinitionGuid = definitionGuid;
}
public bool IsMapped => StrataSphereDefinitionGuid.HasValue;
}
public class StrataSphereMappedDefinition
{
public StrataSphereMappedDefinition(DefinitionDto definitionDto, string mappedValues)
{
StrataSphereDefinitionGuid = definitionDto.Id;
HierarchyGuid = definitionDto.HierarchyGuid;
DimensionGuid = definitionDto.DimensionGuid;
AttributeGuid = definitionDto.AttributeGuid;
HierarchyId = definitionDto.HierarchyId;
DimensionId = definitionDto.DimensionId;
AttributeId = definitionDto.AttributeId;
MappedValues = JsonConvert.DeserializeObject<StrataSphereMappingValues>(mappedValues);
}
public Guid StrataSphereDefinitionGuid { get; }
public Guid HierarchyGuid { get; }
public Guid DimensionGuid { get; }
public Guid AttributeGuid { get; }
public string HierarchyId { get; }
public string DimensionId { get; }
public string AttributeId { get; }
public StrataSphereMappingValues MappedValues { get; }
}
public class StrataSphereMappingValues
{
public string HierarchyGUID { get; set; }
public string DimensionGUID { get; set; }
public List<string> values { get; set; }
public List<MappingValue> mappings { get; set; }
public List<string> hierarchyPaths { get; set; }
public StrataSphereMappingValues()
{
mappings = new List<MappingValue>();
}
}
public class MappingValue
{
public string id { get; set; }
public string displayHierarchyName { get; set; }
public string text { get; set; }
public string name { get; set; }
public List<object> attributeValues { get; set; }
public List<string> hierarchyPaths { get; set; }
public MappingValue()
{
attributeValues = new List<object>();
hierarchyPaths = new List<string>();
}
}
public class ApiBaseDto
{
/// <summary>
/// API Item Id Guid in StrataSphere
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public Guid Id { get; set; }
/// <summary>
/// When the API item was uploaded to StrataSphere
/// </summary>
public DateTime DateCreated { get; set; }
/// <summary>
/// When the API item was last updated in StrataSphere
/// </summary>
public DateTime DateLastModified { get; set; }
}
public class DefinitionDto : ApiBaseDto
{
/// <summary>
/// The name of the definition
/// </summary>
/// <example>TOP 10 DRGs BY SERVICE LINE</example>
public string Name { get; set; }
/// <summary>
/// A short description of the definition's content
/// </summary>
/// <example>Top 10 DRGs by Service Line filtered per service line rollup</example>
public string Description { get; set; } = "";
/// <summary>
/// The Guid of the Definition's hierarchy in a GM database
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public Guid HierarchyGuid { get; set; }
/// <summary>
/// The Guid of the Definition's dimension in a GM database
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public Guid DimensionGuid { get; set; }
/// <summary>
/// The Guid of the Definition's attribute in a GM database
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public Guid AttributeGuid { get; set; }
/// <summary>
/// The string of the Definition's hierarchy friendly name
/// </summary>
/// <example>PatientTypeRollupID</example>
public string HierarchyId { get; set; }
/// <summary>
/// The string of the Definition's dimension Id
/// </summary>
/// <example>[Patient Encounter]</example>
public string DimensionId { get; set; }
/// <summary>
/// The string of the Definition's attribute Id
/// </summary>
/// <example>[Patient Encounter].[Service Line Strata Decision]</example>
public string AttributeId { get; set; }
}
IEnumerable<T> GetList<T>(RestClient client, string something)
{
return Get<List<T>>(client, something);
}
T Get<T>(RestClient client, string something)
{
client.BaseUrl = new Uri(BaseUrl + something);
RestRequest request = new RestRequest();
request.Method = Method.GET;
return JsonConvert.DeserializeObject<T>(client.Execute(request).Content);
}