Skip to content

Instantly share code, notes, and snippets.

@jsnape
Created January 15, 2024 15:49
Show Gist options
  • Save jsnape/29b53688cf6520feb5c511ce6d210d0b to your computer and use it in GitHub Desktop.
Save jsnape/29b53688cf6520feb5c511ce6d210d0b to your computer and use it in GitHub Desktop.
#region Copyright (c) Microsoft Limited, all rights reserved.
// Copyright (c) Microsoft Limited, all rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AnalysisServices;
namespace Microsoft.PSfD.Samples
{
class Program
{
private static string serverName = "(local)";
private static string databaseName;
private static string cubeName;
private static string measureGroupName;
private static string subCube;
private static bool wait;
private static Server server;
private static Database database;
private static Cube cube;
private static MeasureGroup mg;
static void Main(string[] args)
{
ConsoleColor oldColor = Console.ForegroundColor;
try
{
if (ParseArgs(args) == false)
return;
server = new Server();
server.Connect("Datasource=" + serverName + ";");
if (!server.Connected)
throw new InvalidOperationException(string.Format("Server '{0}' does not exist.", serverName));
foreach (Database database in server.Databases)
{
if (database.Name == Program.databaseName)
{
Program.database = database;
break;
}
}
if (Program.database == null)
throw new InvalidOperationException(string.Format("Database '{0}' does not exist.", Program.databaseName));
foreach (Cube cube in Program.database.Cubes)
{
if (cube.Name == Program.cubeName)
{
Program.cube = cube;
break;
}
}
if (Program.cube == null)
throw new InvalidOperationException(string.Format("Cube '{0}' does not exist.", Program.cubeName));
foreach (MeasureGroup mg in Program.cube.MeasureGroups)
{
if (mg.Name == Program.measureGroupName)
{
Program.mg = mg;
break;
}
}
if (Program.mg == null)
throw new InvalidOperationException(string.Format("Measure Group '{0}' does not exist.", Program.measureGroupName));
if (subCube == null || subCube.Length == 0)
{
PrintAllDimensions();
}
else
{
PrintUsedDimensions();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
}
if (Program.wait)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\nPress any key to close.");
Console.ReadKey(true);
}
Console.ForegroundColor = oldColor;
}
private static void PrintUsedDimensions()
{
bool newDimension = true;
int dimensionIndex = 0;
int attributeIndex = 0;
CubeDimension dimension = null;
CubeAttribute attribute = null;
for (int i = 0; i < subCube.Length; i++)
{
char next = subCube[i];
switch (next)
{
case ',':
attributeIndex = -1;
dimensionIndex++;
newDimension = true;
break;
case '0':
break;
case '1':
if (newDimension)
{
if (dimensionIndex >= mg.Dimensions.Count)
break;
dimension = mg.Dimensions[dimensionIndex].CubeDimension;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("+DIM:\t{0} ({1})", dimension.Name, dimension.Attributes.Count);
newDimension = false;
}
if (attributeIndex >= dimension.Dimension.Attributes.Count)
break;
attribute = dimension.Attributes[attributeIndex];
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("+ATTR:\t\t{0}", attribute.Attribute.Name);
break;
default:
attributeIndex--;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Unknown character '{0}' in input string.", next);
break;
}
attributeIndex++;
}
}
private static void PrintAllDimensions()
{
for (int j = 0; j < mg.Dimensions.Count; ++j)
{
CubeDimension dimension = mg.Dimensions[j].CubeDimension;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("DIM:\t{0} ({1})", dimension.Name, dimension.Attributes.Count);
for (int k = 0; k < dimension.Attributes.Count; ++k)
{
CubeAttribute attribute = dimension.Attributes[k];
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("ATTR:\t\t{0}", attribute.Attribute.Name);
}
}
}
private static void PrintMeasureGroupDimensions(MeasureGroup mg)
{
for (int j = 0; j < mg.Dimensions.Count; ++j)
{
CubeDimension dim = mg.Dimensions[j].CubeDimension;
Console.WriteLine("DIM:\t{0} ({1})", dim.Name, dim.Attributes.Count);
for (int k = 0; k < dim.Attributes.Count; ++k)
{
CubeAttribute attr = dim.Attributes[k];
Console.WriteLine("ATTR:\t\t{0}", attr.Attribute.Name);
}
}
}
static bool ParseArgs(string[] args)
{
if (args.Length == 0)
{
ShowHelp();
return false;
}
foreach (string arg in args)
{
if (arg.StartsWith("/s:"))
{
serverName = arg.Substring(3);
}
else if (arg.StartsWith("/d:"))
{
databaseName = arg.Substring(3);
}
else if (arg.StartsWith("/c:"))
{
cubeName = arg.Substring(3);
}
else if (arg.StartsWith("/m:"))
{
measureGroupName = arg.Substring(3);
}
else if (arg.StartsWith("/w"))
{
wait = true;
}
else if (arg.StartsWith("/h"))
{
ShowHelp();
return false;
}
else
{
// Assume this arg is the sub cube string.
subCube = arg;
}
}
return true;
}
private static void ShowHelp()
{
Console.Write(@"
Displays a list of dimensions and attributes used by a measure group.
Usage:
ListAttributes [/s:<server>] /d:<database> /c:<cube>
/m:<measure group> [/w] [subcube string]
/s Specifies the Analysis Server to connect to.
Defaults to (local).
/d Specifies the database to use.
/c Specifies the database cube.
/m Specifies the measure group to print dimensions for.
/w Causes the program to wait before exiting.
[subcube string] If supplied, causes only the dimensions and attributes
in the string to be printed. Otherwise all dimensions
and attributes are shown. The string is of the format:
000100000,000010,000,00001000,00100,00001000 where
each group denotes a dimension and each character
denotes an attribute in that dimension group. This
string is the same as output by the 'Query Subcube'
event class in SQL Server Profiler.
");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment