示例#1
0
        public static void Register()
        {
            new Execute(DashboardOperation.Save)
            {
                CanBeNew      = true,
                CanBeModified = true,
                Execute       = (cp, _) => { }
            }.Register();

            new Delete(DashboardOperation.Delete)
            {
                Delete = (cp, _) =>
                {
                    var parts = cp.Parts.Select(a => a.Content).ToList();
                    cp.Delete();
                    Database.DeleteList(parts);
                }
            }.Register();

            new ConstructFrom <DashboardEntity>(DashboardOperation.Clone)
            {
                Construct = (cp, _) => cp.Clone()
            }.Register();

            new Execute(DashboardOperation.RegenerateCachedQueries)
            {
                CanExecute = c => c.CacheQueryConfiguration == null?ValidationMessage._0IsNotSet.NiceToString(ReflectionTools.GetPropertyInfo(() => c.CacheQueryConfiguration)) : null,
                                 AvoidImplicitSave = true,
                                 Execute           = (db, _) =>
                {
                    var cq = db.CacheQueryConfiguration !;

                    var oldCachedQueries = db.CachedQueries().ToList();
                    oldCachedQueries.ForEach(a => a.File.DeleteFileOnCommit());
                    db.CachedQueries().UnsafeDelete();

                    var definitions = DashboardLogic.GetCachedQueryDefinitions(db).ToList();

                    var combined = DashboardLogic.CombineCachedQueryDefinitions(definitions);

                    foreach (var c in combined)
                    {
                        var qr = c.QueryRequest;

                        if (qr.Pagination is Pagination.All)
                        {
                            qr            = qr.Clone();
                            qr.Pagination = new Pagination.Firsts(cq.MaxRows + 1);
                        }

                        var now = Clock.Now;

                        Stopwatch sw = Stopwatch.StartNew();

                        var rt = Connector.CommandTimeoutScope(cq.TimeoutForQueries).Using(_ => QueryLogic.Queries.ExecuteQuery(qr));

                        var queryDuration = sw.ElapsedMilliseconds;

                        if (c.QueryRequest.Pagination is Pagination.All)
                        {
                            if (rt.Rows.Length == cq.MaxRows)
                            {
                                throw new ApplicationException($"The query for {c.UserAssets.CommaAnd(a => a.KeyLong())} has returned more than {cq.MaxRows} rows: " +
                                                               JsonSerializer.Serialize(QueryRequestTS.FromQueryRequest(c.QueryRequest), EntityJsonContext.FullJsonSerializerOptions));
                            }
                            else
                            {
                                rt = new ResultTable(rt.AllColumns(), null, new Pagination.All());
                            }
                        }


                        sw.Restart();

                        var json = new CachedQueryJS
                        {
                            CreationDate = now,
                            QueryRequest = QueryRequestTS.FromQueryRequest(c.QueryRequest),
                            ResultTable  = rt,
                        };

                        var bytes = JsonSerializer.SerializeToUtf8Bytes(json, EntityJsonContext.FullJsonSerializerOptions);

                        var file = new Entities.Files.FilePathEmbedded(CachedQueryFileType.CachedQuery, "CachedQuery.json", bytes).SaveFile();

                        var uploadDuration = sw.ElapsedMilliseconds;

                        new CachedQueryEntity
                        {
                            CreationDate   = now,
                            UserAssets     = c.UserAssets.ToMList(),
                            NumColumns     = qr.Columns.Count + (qr.GroupResults ? 0 : 1),
                            NumRows        = rt.Rows.Length,
                            QueryDuration  = queryDuration,
                            UploadDuration = uploadDuration,
                            File           = file,
                            Dashboard      = db.ToLite(),
                        }.Save();
                    }
                }
            }.SetMinimumTypeAllowed(TypeAllowedBasic.Read).Register();