private void ProcessLayerVersionAggregations(string sql, LayerVersion lv)
 {
     foreach (var level in lv.Dataset.Levels)
     {
         ProcessLayerVersion(sql, lv, level);
     }
 }
Пример #2
0
 public BlogServiceStack(Construct parent, string id, IStackProps props) : base(parent, id, props)
 {
     var role  = Role.FromRoleArn(this, "LambdaRole", "arn:aws:iam::312226949769:role/IDTJawsLambdaFullAccessRole");
     var table = new Table(this, "BlogTable", new TableProps
     {
         PartitionKey = new Attribute {
             Name = "Id", Type = AttributeType.STRING
         },
         ReadCapacity  = 3,
         WriteCapacity = 3
     });
     var env = new Dictionary <string, string> {
         { "BlogTable", table.TableName },
         { "DOTNET_SHARED_STORE", "/opt/dotnetcore/store/" }
     };
     var blogLayer = LayerVersion.FromLayerVersionArn(this, "BlogLayer", "arn:aws:lambda:us-east-1:312226949769:layer:BlogLayer:1");
     var getBlogs  = new Function(this, "GetBlogs", new FunctionProps
     {
         Runtime     = Runtime.DOTNET_CORE_2_1,
         Code        = Code.FromAsset("src/GetBlogs/bin/Release/netcoreapp2.1/GetBlogs.zip"),
         Handler     = "GetBlogs::GetBlogs.Function::GetBlogs",
         Description = "Function to get blogs",
         MemorySize  = 256,
         Timeout     = Duration.Seconds(30),
         Role        = role,
         Environment = env,
         Layers      = new [] { blogLayer }
     });
     var addBlog = new Function(this, "AddBlog", new FunctionProps
     {
         Runtime     = Runtime.DOTNET_CORE_2_1,
         Code        = Code.FromAsset("src/AddBlog/bin/Release/netcoreapp2.1/AddBlog.zip"),
         Handler     = "AddBlog::AddBlog.Function::AddBlog",
         Description = "Function to add a blog",
         MemorySize  = 256,
         Timeout     = Duration.Seconds(30),
         Role        = role,
         Environment = env,
         Layers      = new[] { blogLayer }
     });
     var api = new RestApi(this, "BlogsRestApi", new RestApiProps
     {
         RestApiName    = "Blog Service",
         CloudWatchRole = false
     });
     var getMethod  = api.Root.AddMethod("GET", new LambdaIntegration(getBlogs, null), null);
     var postMethod = api.Root.AddMethod("POST", new LambdaIntegration(addBlog, null), null);
 }
        private string calculateAllVersionValueLabelId(LayerVersion lv, bool useAggregateFunction, Cartography level)
        {
            List <string> ret = new List <string>();

            foreach (var v in lv.Variables)
            {
                if (v.Use(useAggregateFunction))
                {
                    string value = LviFunctions.calculateValueField(v);
                    ret.Add(LviFunctions.calculateVersionValueLabelId(v, value, level));
                    ret.Add("'\t'");
                }
            }
            ret.RemoveAt(ret.Count - 1);
            return("CONCAT(" + String.Join(",", ret) + ")");
        }
        private string calculateAllValuesField(LayerVersion lv, bool useAggregateFunction)
        {
            List <string> ret = new List <string>();

            if (lv.Variables.Count == 0)
            {
                throw new Exception("La capa '" + lv.Layer.Caption + "' no tiene indicadores en su revisión '" + lv.Caption + "'.");
            }

            foreach (var v in lv.Variables)
            {
                if (v.Use(useAggregateFunction))
                {
                    ret.Add(LviFunctions.calculateValueField(v));
                    ret.Add("'\t'");
                }
            }
            if (ret.Count > 0)
            {
                ret.RemoveAt(ret.Count - 1);
            }
            return("CONCAT(" + String.Join(",", ret) + ")");
        }
        private void ProcessLayerVersion(string sql, LayerVersion lv, Cartography aggregateLevel = null)
        {
            List <object> fieldList = new List <object>();

            fieldList.Add(lv.Layer.Id.Value);
            fieldList.Add(lv.Id.Value);
            if (aggregateLevel == null)
            {
                fieldList.Add("null");
            }
            else
            {
                fieldList.Add("cai_cartography_id");
            }
            fieldList.Add("cai_id");
            fieldList.Add("cai_urbanity");
            fieldList.Add("id");
            // Valores
            bool   useAggregateFunction = (aggregateLevel != null);
            string allValues            = calculateAllValuesField(lv, useAggregateFunction);

            fieldList.Add(allValues);
            // Etiquetas
            string allVersionValueLabels = calculateAllVersionValueLabelId(lv, useAggregateFunction, aggregateLevel);

            fieldList.Add(allVersionValueLabels);

            // Descripción
            if (useAggregateFunction && lv.DatasetCaptionColumn != null && lv.DatasetCaptionColumn.Aggregation == AggregationEnum.Transpose && useAggregateFunction)
            {
                fieldList.Add("null");
            }
            else
            {
                fieldList.Add(LviFunctions.GetRichColumn(lv.DatasetCaption, lv.DatasetCaptionColumn));
            }
            // Summary
            if (useAggregateFunction && lv.SummaryColumn != null && lv.SummaryColumn.Aggregation == AggregationEnum.Transpose)
            {
                fieldList.Add("0");
            }
            else
            {
                fieldList.Add(LviFunctions.GetRichColumn(lv.Summary, lv.SummaryColumn));
            }

            // featureId
            if (lv.Dataset.DatasetType == DatasetTypeEnum.Shapes ||
                lv.Dataset.DatasetType == DatasetTypeEnum.Locations)
            {
                fieldList.Add(((long)lv.Dataset.Id.Value * 0x100000000) + "+id");
            }
            else
            {
                fieldList.Add("cai_id");
            }

            fieldList.Add(SpecialColumnEnum.AreaM2);
            fieldList.Add(SpecialColumnEnum.People);
            fieldList.Add(SpecialColumnEnum.Household);
            fieldList.Add(SpecialColumnEnum.Children);
            if (lv.Dataset.DatasetType == DatasetTypeEnum.Shapes)
            {
                fieldList.Add("Envelope(geometry)");
                fieldList.Add("id");
                fieldList.Add("area_m2");
                fieldList.Add("centroid");
            }
            else if (lv.Dataset.DatasetType == DatasetTypeEnum.Locations)
            {
                string point = "POINT(" + lv.Dataset.LatitudeColumn.Field + ", " +
                               lv.Dataset.LongitudeColumn.Field + ")";
                fieldList.Add("Envelope(" + point + ")");
                fieldList.Add(null);
                fieldList.Add(null);
                fieldList.Add(point);
            }
            else if (lv.Dataset.DatasetType == DatasetTypeEnum.Data)
            {
                fieldList.Add("Envelope(cai_geometry)");
                fieldList.Add(null);
                fieldList.Add(null);
                fieldList.Add("cai_centroid");
            }
            else
            {
                throw new Exception("Invalid dataset type.");
            }

            int joinShapesId;

            if (lv.Dataset.DatasetType == DatasetTypeEnum.Shapes)
            {
                joinShapesId = lv.Dataset.Id.Value;
            }
            else
            {
                joinShapesId = 0;
            }
            string select = DatasetTable.GetSelectValuesSql(lv.Dataset, fieldList, aggregateLevel);

            select += LviFunctions.AppendGeometryNotNullCondition(lv);

            rowsAffected += medea.context.Data.Session.SqlActions.ExecuteNonQuery(sql + select, true);
        }
Пример #6
0
        internal AppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            // 面倒くさいのでスタックは分けない。
            var vpc = new Vpc(this, "Vpc");
            var efs = new EFS.FileSystem(this, "Efs", new EFS.FileSystemProps()
            {
                Vpc = vpc,
            });
            var efsUser = new PosixUser()
            {
                Gid = "1001",
                Uid = "1001",
            };
            var efsCreateAcl = new Acl()
            {
                OwnerGid    = "1001",
                OwnerUid    = "1001",
                Permissions = "755",
            };
            var efsAccessPoint = new EFS.AccessPoint(this, "EfsAccessPoint", new EFS.AccessPointProps()
            {
                FileSystem = efs,
                // 他の設定そのままで "/" では書き込み権限が得られていなかった。
                // CDK上ではなく、NFSマウント後にルートユーザーで権限を操作すればよい。
                // (ルートディレクトリは既定でルートユーザーが所有している状態)
                // See. https://docs.aws.amazon.com/ja_jp/efs/latest/ug/using-fs.html
                //      https://docs.aws.amazon.com/ja_jp/efs/latest/ug/accessing-fs-nfs-permissions-per-user-subdirs.html
                Path = "/",
                // ファイルIOに用いるユーザーとディレクトリ作成時権限の設定は必須である様子。
                // CDKが既定のユーザーを構成してくれるようなことはない。
                // -> ↑嘘。必要がなければ構成しなくても問題ない。所詮はNFSなので、権限が他のユーザーに解放されているディレクトリは操作できる。はず。
                PosixUser = efsUser,
                CreateAcl = efsCreateAcl,
            });

            // Assets
            // https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-assets-readme.html
            // vs
            // https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-deployment-readme.html
            // 静的にS3にファイルを残し、スタックのデプロイ後にDataSyncでEFSに転送するのでDeployment。
            var assetBucket = new Bucket(this, "AssetBucket", new BucketProps()
            {
            });

            new BucketDeployment(this, "AssetBucketDeployment", new BucketDeploymentProps()
            {
                Sources           = new ISource[] { Source.Asset("assets") },
                DestinationBucket = assetBucket,
            });

            // https://github.com/shelfio/chrome-aws-lambda-layer
            var chromeLayer = new LayerVersion(this, "ChromeLayer", new LayerVersionProps()
            {
                Code = AssetCode.FromAsset("chrome_aws_lambda.zip"),
                CompatibleRuntimes = new Runtime[] { Runtime.NODEJS_12_X }
            });

            var renderImageBucket = new Bucket(this, "RenderImageBucket", new BucketProps()
            {
            });

            var renderHtmlToS3Function = new Function(this, "RenderHtmlToS3Function", new FunctionProps()
            {
                Vpc         = vpc,
                Runtime     = Runtime.NODEJS_12_X,
                MemorySize  = 1024,
                Timeout     = Duration.Seconds(10),
                Code        = Code.FromAsset("handlers"),
                Handler     = "render-html-to-s3.handler",
                Environment = new Dictionary <string, string>()
                {
                    ["BucketName"]   = renderImageBucket.BucketName,
                    ["EfsMountPath"] = "/mnt/efs",
                },
                Layers     = new ILayerVersion[] { chromeLayer },
                Filesystem = Lambda.FileSystem.FromEfsAccessPoint(efsAccessPoint, "/mnt/efs"),
            });

            // VPCやEFSに関してはCDK上の関連から
            // セキュリティグループや既定のロールへのインラインポリシーが構成される。
            // S3バケットはCDK上の関連はないため明に権限を付与する。
            renderImageBucket.GrantReadWrite(renderHtmlToS3Function);

            // 踏み台
            var bastion = new BastionHostLinux(this, "Bastion", new BastionHostLinuxProps()
            {
                InstanceType = InstanceType.Of(InstanceClass.BURSTABLE3, InstanceSize.NANO),
                Vpc          = vpc,
            });

            assetBucket.GrantRead(bastion);
            // https://docs.aws.amazon.com/cdk/api/latest/docs/aws-efs-readme.html
            efs.Connections.AllowDefaultPortFrom(bastion);
            bastion.Instance.UserData.AddCommands(
                "yum check-update -y",                          // Ubuntu: apt-get -y update
                "yum upgrade -y",                               // Ubuntu: apt-get -y upgrade
                "yum install -y amazon-efs-utils",              // Ubuntu: apt-get -y install amazon-efs-utils
                "yum install -y nfs-utils",                     // Ubuntu: apt-get -y install nfs-common
                "file_system_id_1=" + efs.FileSystemId,
                "efs_mount_point_1=/mnt/efs/fs1",
                "mkdir -p \"${efs_mount_point_1}\"",
                "test -f \"/sbin/mount.efs\" && echo \"${file_system_id_1}:/ ${efs_mount_point_1} efs defaults,_netdev\" >> /etc/fstab || " +
                "echo \"${file_system_id_1}.efs." + Stack.Of(this).Region + ".amazonaws.com:/ ${efs_mount_point_1} nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport,_netdev 0 0\" >> /etc/fstab",
                "mount -a -t efs,nfs4 defaults",
                "chmod go+rw /mnt/efs/fs1"
                );

            new CfnOutput(this, "BastionInstanceId", new CfnOutputProps()
            {
                ExportName = "BastionInstanceId",
                Value      = bastion.InstanceId,
            });
            new CfnOutput(this, "AssetBucketName", new CfnOutputProps()
            {
                ExportName = "AssetBucketName",
                Value      = assetBucket.BucketName,
            });
        }