Exemplo n.º 1
0
        public string BuildJsonStructure(PXModel model)
        {
            var jsonStat = new Model.JsonStat();

            var value  = new double?[model.Data.Matrix.Length];
            var status = new Dictionary <int, string>();
            var id     = new List <string>();
            var size   = new List <int>();

            // Role collectors
            var roleTimeList   = new List <string>();
            var roleMetricList = new List <string>();
            var roleGeoList    = new List <string>();

            ExtractValueAndStatus(model.Meta, model.Data.Matrix, out value, out status);

            jsonStat.Value   = value;
            jsonStat.Status  = status.Count == 0 ? null : status;
            jsonStat.Source  = model.Meta.Source;
            jsonStat.Label   = model.Meta.Title;
            jsonStat.Updated = DateTime.Now;

            // Dimension
            jsonStat.Dimension = new Dictionary <string, object>();

            for (var i = 0; i < model.Meta.Variables.Count; i++)
            {
                var variable = model.Meta.Variables[i];

                var dimension = new Model.Dimension();
                dimension.Label = variable.Name;

                // Category
                var category = new Model.Category();
                var unit     = new Dictionary <string, Dictionary <string, object> >();

                for (var j = 0; j < variable.Values.Count; j++)
                {
                    var varvalue = variable.Values[j];

                    category.Label.Add(varvalue.Code, varvalue.Value);
                    category.Index.Add(varvalue.Code, j);

                    if (variable.IsContentVariable)
                    {
                        var unitContent = new Dictionary <string, object>();

                        if (varvalue.ContentInfo != null)
                        {
                            unitContent.Add(BASE, varvalue.ContentInfo.Units);
                        }
                        else
                        {
                            _logger.Warn("Category" + varvalue.Code + " lacks ContentInfo. Unit not set");
                        }

                        var decimals = (varvalue.HasPrecision()) ? varvalue.Precision : model.Meta.ShowDecimals;

                        unitContent.Add(DECIMALS, decimals);
                        unit.Add(varvalue.Code, unitContent);

                        category.Unit = unit;
                    }
                }

                dimension.Category = new Model.Category();
                dimension.Category = category;

                jsonStat.Dimension.Add(variable.Code, dimension);

                size.Add(variable.Values.Count);
                id.Add(variable.Code);

                // Role
                if (variable.IsTime)
                {
                    roleTimeList.Add(variable.Code);
                }

                if (variable.IsContentVariable)
                {
                    roleMetricList.Add(variable.Code);
                }

                if (String.IsNullOrEmpty(variable.Map) == false)
                {
                    roleGeoList.Add(variable.Code);
                }
            }

            // Id
            jsonStat.Id = new string[id.Count];
            jsonStat.Id = id.ToArray();

            // Size
            jsonStat.Size = new int[size.Count];
            jsonStat.Size = size.ToArray();

            // Role
            jsonStat.Role = new Dictionary <string, string[]>();

            if (roleTimeList.Count > 0)
            {
                jsonStat.Role.Add(TIME, roleTimeList.ToArray());
            }
            if (roleMetricList.Count > 0)
            {
                jsonStat.Role.Add(METRIC, roleMetricList.ToArray());
            }
            if (roleGeoList.Count > 0)
            {
                jsonStat.Role.Add(GEO, roleGeoList.ToArray());
            }

            string result = JsonConvert.SerializeObject(jsonStat, Formatting.Indented);

            return(result);
        }
Exemplo n.º 2
0
        public string BuildJsonStructure(PXModel model)
        {
            var jsonStat = new Model.JsonStat();

            var value  = new double?[model.Data.Matrix.Length];
            var status = new Dictionary <int, string>();
            var id     = new List <string>();
            var size   = new List <int>();

            // Role collectors
            var roleTimeList   = new List <string>();
            var roleMetricList = new List <string>();
            var roleGeoList    = new List <string>();

            ExtractValueAndStatus(model, out value, out status);

            jsonStat.Value  = value;
            jsonStat.Status = status.Count == 0 ? null : status;
            jsonStat.Source = model.Meta.Source;
            jsonStat.Label  = model.Meta.Title;


            if (model.Meta.ContentVariable != null && model.Meta.ContentVariable.Values.Count > 0)
            {
                var lastUpdatedContentsVariable = model.Meta.ContentVariable.Values.OrderByDescending(x => x.ContentInfo.LastUpdated).FirstOrDefault();
                jsonStat.Updated = lastUpdatedContentsVariable.ContentInfo.LastUpdated.PxDateStringToDateTime().ToString();
            }
            else
            {
                jsonStat.Updated = model.Meta.CreationDate.PxDateStringToDateTime().ToString();
            }

            // Dimension
            jsonStat.Dimension = new Dictionary <string, object>();

            for (var i = 0; i < model.Meta.Variables.Count; i++)
            {
                var variable  = model.Meta.Variables[i];
                var link      = new Dictionary <string, object>();
                var dimension = new Model.Dimension();
                dimension.Label = variable.Name;

                // Category
                var category = new Model.Category();
                var unit     = new Dictionary <string, Dictionary <string, object> >();

                for (var j = 0; j < variable.Values.Count; j++)
                {
                    var varvalue = variable.Values[j];

                    category.Label.Add(varvalue.Code, varvalue.Value);
                    category.Index.Add(varvalue.Code, j);

                    if (variable.IsContentVariable)
                    {
                        var unitContent = new Dictionary <string, object>();

                        if (varvalue.ContentInfo != null)
                        {
                            unitContent.Add(BASE, varvalue.ContentInfo.Units);
                        }
                        else
                        {
                            _logger.Warn("Category" + varvalue.Code + " lacks ContentInfo. Unit not set");
                        }

                        var decimals = (varvalue.HasPrecision()) ? varvalue.Precision : model.Meta.ShowDecimals;

                        unitContent.Add(DECIMALS, decimals);
                        unit.Add(varvalue.Code, unitContent);

                        category.Unit = unit;
                    }
                }

                dimension.Category = new Model.Category();
                dimension.Category = category;
                var extensions = GetAllSerializedMetaIdsForVariable(variable);
                if (extensions.Count > 0)
                {
                    link.Add(DESCRIBEDBY, new List <object> {
                        extensions
                    });
                    dimension.Link = link;
                }
                jsonStat.Dimension.Add(variable.Code, dimension);

                size.Add(variable.Values.Count);
                id.Add(variable.Code);

                // Role
                if (variable.IsTime)
                {
                    roleTimeList.Add(variable.Code);
                }

                if (variable.IsContentVariable)
                {
                    roleMetricList.Add(variable.Code);
                }
                if (variable.VariableType != null)
                {
                    if (variable.VariableType.ToUpper() == "G" || (variable.Map != null))
                    {
                        roleGeoList.Add(variable.Code);
                    }
                }
            }

            // Id
            jsonStat.Id = new string[id.Count];
            jsonStat.Id = id.ToArray();

            // Size
            jsonStat.Size = new int[size.Count];
            jsonStat.Size = size.ToArray();

            // Role
            jsonStat.Role = new Dictionary <string, string[]>();

            //Extension, PX
            if (model.Meta.InfoFile != null || model.Meta.TableID != null || model.Meta.Decimals != -1)
            {
                jsonStat.Extension = new Dictionary <string, object>();
                var px = new Model.Px();

                px.infofile = model.Meta.InfoFile;
                px.tableid  = model.Meta.TableID;
                //If not Showdecimal has value use Decimal
                var decimals = model.Meta.ShowDecimals < 0 ? model.Meta.Decimals : model.Meta.ShowDecimals;
                px.decimals = decimals;

                jsonStat.Extension.Add(PX, px);
            }


            if (roleTimeList.Count > 0)
            {
                jsonStat.Role.Add(TIME, roleTimeList.ToArray());
            }
            if (roleMetricList.Count > 0)
            {
                jsonStat.Role.Add(METRIC, roleMetricList.ToArray());
            }
            if (roleGeoList.Count > 0)
            {
                jsonStat.Role.Add(GEO, roleGeoList.ToArray());
            }



            // override converter to stop adding ".0" after interger values.
            string result = JsonConvert.SerializeObject(jsonStat, new DecimalJsonConverter());

            return(result);
        }