数据准确性与UI控件映射检查

在现代的应用程序中,数据的准确性和UI控件的映射正确性对于提供良好的用户体验至关重要。本文将探讨如何确保数据准确性,并通过映射检查来避免不必要的服务器调用,从而提高应用程序的性能。

在开发一个包含大量图表、网格、地图等UI控件的应用程序时,使用了MDX查询来生成这些控件。众所周知,数据的准确性对于仪表板应用程序至关重要,因为只有准确的数据才能保证图表、网格、地图等UI控件的正确显示。因此,必须检查数据的正确性,并尽量避免不必要的服务器调用以获取数据。

在做什么

正在执行以下步骤:

  • 通过AJAX调用传递需要检查的信息
  • 在控制器函数中创建一个ADOMD连接对象,并将信息传递给模型类
  • 在模型类中构建并运行带有所需参数的查询
  • 最后一步将提供信息,说明给定的度量组是否与维度组映射

使用代码

现在让开始编码。

以下是AJAX调用实现。

var cubeName = ''; var measureGroupName = ''; var dimensionGroupName = ''; var serverName = 'My server name'; var databaseName = 'My database name'; var cubeName = 'My cube name'; measureGroupName = sessionStorage.getItem("measureGroupName"); dimensionGroupName = sessionStorage.getItem("dimensionGroupName"); var checkMeasuresMapped = { cubeName: cubeName, measureGroupName: measureGroupName, dimensionGroupName: dimensionGroupName, serverName: serverName, databaseName: databaseName }; $.ajax({ async: true, url: '../CheckMeasureGroupMapping/', type: 'POST', dataType: 'json', data: JSON.stringify(checkMeasuresMapped), contentType: "application/json; charset=utf-8", success: function(data) { if (data.indexOf("Error") > -1) { $('#btnCreate').css('enabled', 'false'); $('#Preview').html('

Error : ' + data.replace('Error', '') + '

'); } else if (data == "Mapped") { // Condition satisfied, write your codes here ajaxAsync = true; } else { $('#Preview').html('

Warning : The given measure is not mapped with the dimension. Please check.

'); } }, error: function(xhrequest, ErrorText, thrownError) { console.log(ErrorText + "," + thrownError); $('#Preview').html('

Error : ' + ErrorText + '

'); } });

请注意,已经传递了所有需要的信息。将列出它们。

  • I. 立方体名称
  • II. 度量组名称
  • III. 维度组名称
  • IV. 服务器名称
  • V. 数据库名称

现在已经完成了AJAX实现,接下来将进入下一部分。请参阅以下代码。

/// <summary> /// This method is used to find whether the given measure is mapped to the dimension /// </summary> /// <param name="cubeName"></param> /// <param name="measureGroupName"></param> /// <param name="dimensionGroupName"></param> /// </summary> [HandleError] [AcceptVerbs(HttpVerbs.Post)] public ActionResult CheckMeasureGroupMapping(string cubeName, string measureGroupName, string dimensionGroupName, string serverName, string databaseName) { try { DataTable dt = new DataTable(); dt = adomdConn.CheckMeasureGroupMapping(cubeName, measureGroupName, dimensionGroupName, serverName, databaseName); if (dt.Rows.Count < 1) { return Json("The given measure is not mapped with the dimension. Please check.", JsonRequestBehavior.AllowGet); } else { return Json("Mapped", JsonRequestBehavior.AllowGet); } } catch (AdomdErrorResponseException ex) { string errorText = "Query Error" + System.Web.HttpUtility.HtmlEncode(ex.Message); return Json(errorText, JsonRequestBehavior.AllowGet); } }

在这里,CheckMeasureGroupMapping是ActionResult,将信息传递给模型类,如果条件满足,上述函数将返回数据表中的行。通过该数据表的行数,可以得出结论。

现在在最后一部分,因为在模型类中得到了所需的参数,是时候构建查询并将其运行到ADOMD服务器了。以下是执行此操作的函数。

public DataTable CheckMeasureGroupMapping(string cubeName, string measureGroupName, string dimensionGroupName, string serverName, string databaseName) { try { string query = string.Empty; string queryBefore = string.Empty; queryBefore = "SELECT [MEASUREGROUP_NAME] AS [MEASUREGROUP],[MEASUREGROUP_CARDINALITY],[DIMENSION_UNIQUE_NAME] AS [DIM],[DIMENSION_GRANULARITY] AS [DIM_KEY],[DIMENSION_CARDINALITY],[DIMENSION_IS_VISIBLE] AS [IS_VISIBLE],[DIMENSION_IS_FACT_DIMENSION] AS [IS_FACT_DIM] FROM $system.MDSCHEMA_MEASUREGROUP_DIMENSIONS WHERE [CUBE_NAME] = {cubeName} AND [MEASUREGROUP_NAME] ={measureGroupName} AND [DIMENSION_UNIQUE_NAME]={dimensionGroupName}"; query = queryBefore.Replace("{cubeName}", "'" + cubeName + "'").Replace("{measureGroupName}", "'" + measureGroupName + "'").Replace("{dimensionGroupName}", "'" + dimensionGroupName + "'"); StringBuilder sbConnectionString = new StringBuilder(); sbConnectionString.Append("Provider=MSOLAP;data source=" + serverName + ";initial catalog=" + databaseName + ";Integrated Security=SSPI;Persist Security Info=False;"); using (AdomdConnection conn = new AdomdConnection(sbConnectionString.ToString())) { conn.Open(); using (AdomdCommand cmd = new AdomdCommand(query, conn)) { DataTable dt = new DataTable(); AdomdDataAdapter da = new AdomdDataAdapter(cmd); da.Fill(dt); return dt; } } } catch (AdomdErrorResponseException ex) { throw; } }

正如大家所看到的,以下是用于查找给定的度量组是否与维度组映射的查询。

SELECT [MEASUREGROUP_NAME] AS [MEASUREGROUP],[MEASUREGROUP_CARDINALITY],[DIMENSION_UNIQUE_NAME] AS [DIM],[DIMENSION_GRANULARITY] AS [DIM_KEY],[DIMENSION_CARDINALITY],[DIMENSION_IS_VISIBLE] AS [IS_VISIBLE],[DIMENSION_IS_FACT_DIMENSION] AS [IS_FACT_DIM] FROM $system.MDSCHEMA_MEASUREGROUP_DIMENSIONS WHERE [CUBE_NAME] = {cubeName} AND [MEASUREGROUP_NAME] ={measureGroupName} AND [DIMENSION_UNIQUE_NAME]={dimensionGroupName}

使用参数运行此查询,并将结果返回给控制器。

结束

最后,将检查在AJAX调用中显示的条件。

if (data.indexOf("Error") > -1) { $('#btnCreate').css('enabled', 'false'); $('#Preview').html('

Error : ' + data.replace('Error', '') + '

'); } else if (data == "Mapped") { // Condition satisfied, write your codes here ajaxAsync = true; } else { $('#Preview').html('

Warning : The given measure is not mapped with the dimension. Please check.

'); }

已经完成了这个需求。

感兴趣的点

ADOMD, ADOMD Measure, ADOMD Dimension, ADOMD Measure Groups, ADOMD Dimension Group, Mapping measure group and dimension group.

希望喜欢这篇文章。请分享宝贵的反馈。这对来说意义重大。

沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485