Beispiel #1
0
        private void WriteCommandName(string name, ICommandOptions command)
        {
            var param = command.Argument;

            if (param != null)
                Writer.WriteLine("{0} <{1}> ({2})", name, param.Name, param.Member.Type.GetRealClassName());
            else
                Writer.WriteLine("{0}", name);
        }
        protected override Task InvalidateCacheAsync(IReadOnlyCollection<ModifiedDocument<User>> documents, ICommandOptions options = null) {
            if (!IsCacheEnabled)
                return Task.CompletedTask;

            var users = documents.UnionOriginalAndModified();
            var keysToRemove = users.Select(u => String.Concat("Email:", u.EmailAddress.ToLowerInvariant().Trim())).Distinct().ToList();
            return Task.WhenAll(
                Cache.RemoveAllAsync(keysToRemove),
                InvalidateCachedQueriesAsync(users, options),
                base.InvalidateCacheAsync(documents, options)
            );
        }
 public static string GetCacheKey(this ICommandOptions options, string defaultCacheKey = null)
 {
     return(options.SafeGetOption <string>(SetCacheOptionsExtensions.CacheKeyKey, defaultCacheKey ?? options.GetDefaultCacheKey()));
 }
 public static bool ShouldReadCache(this ICommandOptions options)
 {
     return(options.SafeGetOption(SetCacheOptionsExtensions.ReadCacheEnabledKey, options.ShouldUseCache()));
 }
 public static TimeSpan GetExpiresIn(this ICommandOptions options)
 {
     return(options.SafeGetOption(SetCacheOptionsExtensions.CacheExpiresInKey, RepositoryConstants.DEFAULT_CACHE_EXPIRATION_TIMESPAN));
 }
Beispiel #6
0
 public CommandOptions(ICommandOptions options)
 {
     _options       = options;
     NoVersionCheck = options?.NoVersionCheck ?? false;
     Verbose        = options?.Verbose ?? false;
 }
        protected override Task InvalidateCachedQueriesAsync(IReadOnlyCollection <WebHook> documents, ICommandOptions options = null)
        {
            var keysToRemove = documents.Select(d => $"paged:Organization:{d.OrganizationId}:Project:{d.ProjectId}").Distinct();
            var tasks        = new List <Task>();

            foreach (string key in keysToRemove)
            {
                tasks.Add(Cache.RemoveByPrefixAsync(key));
            }

            tasks.Add(base.InvalidateCachedQueriesAsync(documents, options));
            return(Task.WhenAll(tasks));
        }
Beispiel #8
0
        public ExecuteResultEnum Build(ICommandOptions options)
        {
            if (!_configFile.Exists())
            {
                _reportService.Error($"Config file not found: {Configuration.ConfigurationFile}");   // why do we use Configuration here?
                _reportService.Output($"\tPlease make sure you are in the right working directory");
                return(ExecuteResultEnum.Error);
            }

            RunAutoUpdate(options);

            if (RunConfigVersionCheck(options) == ExecuteResultEnum.Aborted)
            {
                return(ExecuteResultEnum.Aborted);
            }

            _reportService.PrintTitle("Build DataContext from spocr.json");

            var config           = _configFile.Config;
            var project          = config?.Project;
            var schemas          = config?.Schema;
            var connectionString = project?.DataBase?.ConnectionString;

            var hasSchemas          = schemas?.Any() ?? false;
            var hasConnectionString = string.IsNullOrWhiteSpace(connectionString);

            if (!hasConnectionString)
            {
                _dbContext.SetConnectionString(connectionString);
            }

            if (!hasSchemas)
            {
                _reportService.Error($"Schema is empty: {Configuration.ConfigurationFile}"); // why do we use Configuration here?
                _reportService.Output($"\tPlease run pull to get the DB-Schema.");
                return(ExecuteResultEnum.Error);
            }

            var stopwatch = new Stopwatch();
            var elapsed   = new Dictionary <string, long>();

            var codeBaseAlreadyExists = project.Role.Kind == ERoleKind.Extension;

            if (!codeBaseAlreadyExists)
            {
                stopwatch.Start();
                _reportService.PrintSubTitle("Generating CodeBase");
                _output.GenerateCodeBase(project.Output, options.DryRun);
                elapsed.Add("CodeBase", stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating TableTypes");
            _engine.GenerateDataContextTableTypes(options.DryRun);
            elapsed.Add("TableTypes", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Inputs");
            _engine.GenerateDataContextInputs(options.DryRun);
            elapsed.Add("Inputs", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Outputs");
            _engine.GenerateDataContextOutputs(options.DryRun);
            elapsed.Add("Outputs", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Models");
            _engine.GenerateDataContextModels(options.DryRun);
            elapsed.Add("Models", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating StoredProcedures");
            _engine.GenerateDataContextStoredProcedures(options.DryRun);
            elapsed.Add("StoredProcedures", stopwatch.ElapsedMilliseconds);

            var summary = elapsed.Select(_ => $"{_.Key} generated in {_.Value} ms.");

            _reportService.PrintSummary(summary, $"SpocR v{_spocr.Version.ToVersionString()}");
            _reportService.PrintTotal($"Total elapsed time: {elapsed.Sum(_ => _.Value)} ms.");

            if (options.DryRun)
            {
                _reportService.PrintDryRunMessage();
            }

            return(ExecuteResultEnum.Succeeded);
        }
Beispiel #9
0
        public static async Task ConfigureSearchAsync <T>(this IElasticQueryBuilder builder, IRepositoryQuery query, ICommandOptions options, SearchDescriptor <T> search) where T : class, new()
        {
            if (search == null)
            {
                throw new ArgumentNullException(nameof(search));
            }

            var q = await builder.BuildQueryAsync(query, options, search).AnyContext();

            search.Query(d => q);
        }
Beispiel #10
0
        public static async Task <QueryContainer> BuildQueryAsync <T>(this IElasticQueryBuilder builder, IRepositoryQuery query, ICommandOptions options, SearchDescriptor <T> search) where T : class, new()
        {
            var ctx = new QueryBuilderContext <T>(query, options, search);
            await builder.BuildAsync(ctx).AnyContext();

            return(new BoolQuery {
                Must = new[] { ctx.Query },
                Filter = new[] { ctx.Filter ?? new MatchAllQuery() }
            });
        }
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Command"/> class.
 /// </summary>
 /// <param name="command"> The command must haves.</param>
 /// <param name="commandOptions"> The command options.</param>
 public Command(ICommand command, ICommandOptions commandOptions)
 {
     this.MainCommand = command.MainCommand;
     this.Handler     = command.Handler;
     this.Set(commandOptions);
 }
 public DocumentsChangeEventArgs(ChangeType changeType, IReadOnlyCollection <ModifiedDocument <T> > documents, IRepository <T> repository, ICommandOptions options)
 {
     ChangeType = changeType;
     Documents  = documents ?? EmptyReadOnly <ModifiedDocument <T> > .Collection;
     Repository = repository;
     Options    = options;
 }
 public ModifiedDocumentsEventArgs(IReadOnlyCollection <ModifiedDocument <T> > documents, IRepository <T> repository, ICommandOptions options)
 {
     Documents  = documents ?? EmptyReadOnly <ModifiedDocument <T> > .Collection;
     Repository = repository;
     Options    = options;
 }
 protected string[] GetIndexesByQuery(IRepositoryQuery query, ICommandOptions options = null)
 {
     return(HasMultipleIndexes ? TimeSeriesType.GetIndexesByQuery(query) : new[] { ElasticIndex.Name });
 }
        protected async Task <SearchDescriptor <T> > ConfigureSearchDescriptorAsync(SearchDescriptor <T> search, IRepositoryQuery query, ICommandOptions options)
        {
            if (search == null)
            {
                search = new SearchDescriptor <T>();
            }

            query = ConfigureQuery(query);
            search.Type(ElasticType.Name);
            var indices = GetIndexesByQuery(query);

            if (indices?.Length > 0)
            {
                search.Index(String.Join(",", indices));
            }
            if (HasVersion)
            {
                search.Version(HasVersion);
            }

            search.IgnoreUnavailable();

            await ElasticType.QueryBuilder.ConfigureSearchAsync(query, options, search).AnyContext();

            return(search);
        }
        public async Task <FindResults <TResult> > FindAsAsync <TResult>(IRepositoryQuery query, ICommandOptions options = null) where TResult : class, new()
        {
            if (query == null)
            {
                query = new RepositoryQuery();
            }

            bool useSnapshotPaging = options.ShouldUseSnapshotPaging();
            // don't use caching with snapshot paging.
            bool allowCaching = IsCacheEnabled && useSnapshotPaging == false;

            options = ConfigureOptions(options);
            await OnBeforeQueryAsync(query, options, typeof(TResult)).AnyContext();

            Func <FindResults <TResult>, Task <FindResults <TResult> > > getNextPageFunc = async r => {
                var previousResults = r;
                if (previousResults == null)
                {
                    throw new ArgumentException(nameof(r));
                }

                if (!String.IsNullOrEmpty(previousResults.GetScrollId()))
                {
                    var scrollResponse = await _client.ScrollAsync <TResult>(options.GetSnapshotLifetime(), previousResults.GetScrollId()).AnyContext();

                    _logger.Trace(() => scrollResponse.GetRequest());

                    var results = scrollResponse.ToFindResults();
                    results.Page    = previousResults.Page + 1;
                    results.HasMore = scrollResponse.Hits.Count() >= options.GetLimit();
                    return(results);
                }

                if (options == null)
                {
                    return(new FindResults <TResult>());
                }

                options?.PageNumber(!options.HasPageNumber() ? 2 : options.GetPage() + 1);

                return(await FindAsAsync <TResult>(query, options).AnyContext());
            };

            string cacheSuffix = options?.HasPageLimit() == true?String.Concat(options.GetPage().ToString(), ":", options.GetLimit().ToString()) : null;

            FindResults <TResult> result;

            if (allowCaching)
            {
                result = await GetCachedQueryResultAsync <FindResults <TResult> >(options, cacheSuffix : cacheSuffix).AnyContext();

                if (result != null)
                {
                    ((IGetNextPage <TResult>)result).GetNextPageFunc = async r => await getNextPageFunc(r).AnyContext();

                    return(result);
                }
            }

            ISearchResponse <TResult> response = null;

            if (useSnapshotPaging == false || !options.HasSnapshotScrollId())
            {
                var searchDescriptor = await CreateSearchDescriptorAsync(query, options).AnyContext();

                if (useSnapshotPaging)
                {
                    searchDescriptor.Scroll(options.GetSnapshotLifetime());
                }

                response = await _client.SearchAsync <TResult>(searchDescriptor).AnyContext();
            }
            else
            {
                response = await _client.ScrollAsync <TResult>(options.GetSnapshotLifetime(), options.GetSnapshotScrollId()).AnyContext();
            }

            _logger.Trace(() => response.GetRequest());
            if (!response.IsValid)
            {
                if (response.ApiCall.HttpStatusCode.GetValueOrDefault() == 404)
                {
                    return(new FindResults <TResult>());
                }

                string message = response.GetErrorMessage();
                _logger.Error().Exception(response.OriginalException).Message(message).Property("request", response.GetRequest()).Write();
                throw new ApplicationException(message, response.OriginalException);
            }

            if (useSnapshotPaging)
            {
                result = response.ToFindResults();
                // TODO: Is there a better way to figure out if you are done scrolling?
                result.HasMore = response.Hits.Count() >= options.GetLimit();
                ((IGetNextPage <TResult>)result).GetNextPageFunc = getNextPageFunc;
            }
            else if (options.HasPageLimit() == true)
            {
                result         = response.ToFindResults(options.GetLimit());
                result.HasMore = response.Hits.Count() > options.GetLimit();
                ((IGetNextPage <TResult>)result).GetNextPageFunc = getNextPageFunc;
            }
            else
            {
                result = response.ToFindResults();
            }

            result.Page = options.GetPage();

            if (!allowCaching)
            {
                return(result);
            }

            var nextPageFunc = ((IGetNextPage <TResult>)result).GetNextPageFunc;

            ((IGetNextPage <TResult>)result).GetNextPageFunc = null;
            await SetCachedQueryResultAsync(options, result, cacheSuffix : cacheSuffix).AnyContext();

            ((IGetNextPage <TResult>)result).GetNextPageFunc = nextPageFunc;

            return(result);
        }
Beispiel #17
0
        public ExecuteResultEnum Pull(ICommandOptions options)
        {
            RunAutoUpdate(options);

            if (!_configFile.Exists())
            {
                _reportService.Error($"File not found: {Configuration.ConfigurationFile}");
                _reportService.Output($"\tPlease make sure you are in the right working directory");
                return(ExecuteResultEnum.Error);
            }

            var userConfigFileName = Configuration.UserConfigurationFile.Replace("{userId}", _globalConfigFile.Config?.UserId);
            var userConfigFile     = new FileManager <ConfigurationModel>(_spocr, userConfigFileName);

            if (userConfigFile.Exists())
            {
                // TODO
                // userConfigFile.OnVersionMismatch = (spocrVersion, configVersion) => {
                //     _reportService.Warn($"Your installed SpocR Version {spocrVersion} does not match with spocr.json Version {configVersion}");
                // };

                var userConfig = userConfigFile.Read();
                _configFile.OverwriteWithConfig = userConfig;
            }

            if (RunConfigVersionCheck(options) == ExecuteResultEnum.Aborted)
            {
                return(ExecuteResultEnum.Aborted);
            }

            if (!string.IsNullOrWhiteSpace(_configFile.Config?.Project?.DataBase?.ConnectionString))
            {
                _dbContext.SetConnectionString(_configFile.Config.Project.DataBase.ConnectionString);
            }

            if (string.IsNullOrWhiteSpace(_configFile.Config.Project.DataBase.ConnectionString))
            {
                _reportService.Error($"ConnectionString is empty: {Configuration.ConfigurationFile}");
                _reportService.Output($"\tPlease run {Configuration.Name} set --cs <ConnectionString>");
                return(ExecuteResultEnum.Error);
            }

            _reportService.PrintTitle("Pulling DB-Schema from Database");

            var config        = _configFile.Config;
            var configSchemas = config?.Schema ?? new List <SchemaModel>();

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            _schemaManager.ListAsync(config).ContinueWith(t =>
            {
                var result = t.Result;
                var overwriteWithCurrentConfig = configSchemas.Any();
                if (overwriteWithCurrentConfig)
                {
                    foreach (var schema in result ?? Enumerable.Empty <SchemaModel>())
                    {
                        var currentSchema = configSchemas.SingleOrDefault(i => i.Name == schema.Name);
                        schema.Status     = currentSchema != null ? currentSchema.Status : _configFile.Config.Project.DefaultSchemaStatus;
                    }
                }
                configSchemas = result;
            }).Wait();

            if (configSchemas == null)
            {
                return(ExecuteResultEnum.Error);
            }

            var pullSchemas   = configSchemas.Where(x => x.Status == SchemaStatusEnum.Build);
            var ignoreSchemas = configSchemas.Where(x => x.Status == SchemaStatusEnum.Ignore);

            var pulledStoredProcedures            = pullSchemas.SelectMany(x => x.StoredProcedures ?? new List <StoredProcedureModel>()).ToList();
            var pulledSchemasWithStoredProcedures = pullSchemas
                                                    .Select(x => new
            {
                Schema           = x,
                StoredProcedures = x.StoredProcedures?.ToList()
            }).ToList();

            pulledSchemasWithStoredProcedures.ForEach(schema =>
            {
                schema.StoredProcedures?.ForEach((sp => _reportService.Verbose($"PULL: [{schema.Schema.Name}].[{sp.Name}]")));
            });
            _reportService.Output("");

            if (ignoreSchemas.Any())
            {
                _reportService.Error($"Ignored {ignoreSchemas.Count()} Schemas [{string.Join(", ", ignoreSchemas.Select(x => x.Name))}]");
                _reportService.Output("");
            }

            _reportService.Note($"Pulled {pulledStoredProcedures.Count()} StoredProcedures from {pullSchemas.Count()} Schemas [{string.Join(", ", pullSchemas.Select(x => x.Name))}] in {stopwatch.ElapsedMilliseconds} ms.");
            _reportService.Output("");

            if (options.DryRun)
            {
                _reportService.PrintDryRunMessage();
            }
            else
            {
                config.Schema = configSchemas;
                _configFile.Save(config);
            }

            return(ExecuteResultEnum.Succeeded);
        }
Beispiel #18
0
 public static bool ShouldNotify(this ICommandOptions options)
 {
     return(options.SafeGetOption(SetNotificationOptionsExtensions.NotificationsKey, true));
 }
        protected override Task InvalidateCacheAsync(IReadOnlyCollection <ModifiedDocument <WebHook> > documents, ICommandOptions options = null)
        {
            if (!IsCacheEnabled)
            {
                return(Task.CompletedTask);
            }

            var keys = documents.Select(d => d.Value).Union(documents.Select(d => d.Original).Where(d => d != null)).Select(h => String.Concat("Organization:", h.OrganizationId, ":Project:", h.ProjectId)).Distinct();

            return(Task.WhenAll(Cache.RemoveAllAsync(keys), base.InvalidateCacheAsync(documents, options)));
        }
        protected override Task InvalidateCacheAsync(IReadOnlyCollection <ModifiedDocument <T> > documents, ICommandOptions options = null)
        {
            if (!IsCacheEnabled)
            {
                return(Task.CompletedTask);
            }

            return(Task.WhenAll(InvalidateCachedQueriesAsync(documents.Select(d => d.Value).ToList(), options), base.InvalidateCacheAsync(documents, options)));
        }
Beispiel #21
0
        protected override Task SendQueryNotificationsAsync(ChangeType changeType, IRepositoryQuery query, ICommandOptions options)
        {
            if (!NotificationsEnabled || !options.ShouldNotify())
            {
                return(Task.CompletedTask);
            }

            var delay         = TimeSpan.FromSeconds(1.5);
            var organizations = query.GetOrganizations();
            var projects      = query.GetProjects();
            var stacks        = query.GetStacks();
            var ids           = query.GetIds();
            var tasks         = new List <Task>();

            string organizationId = organizations.Count == 1 ? organizations.Single() : null;

            if (ids.Count > 0)
            {
                string projectId = projects.Count == 1 ? projects.Single() : null;
                string stackId   = stacks.Count == 1 ? stacks.Single() : null;

                foreach (string id in ids)
                {
                    tasks.Add(PublishMessageAsync(CreateEntityChanged(changeType, organizationId, projectId, stackId, id), delay));
                }

                return(Task.WhenAll(tasks));
            }

            if (stacks.Count > 0)
            {
                string projectId = projects.Count == 1 ? projects.Single() : null;
                foreach (string stackId in stacks)
                {
                    tasks.Add(PublishMessageAsync(CreateEntityChanged(changeType, organizationId, projectId, stackId), delay));
                }

                return(Task.WhenAll(tasks));
            }

            if (projects.Count > 0)
            {
                foreach (string projectId in projects)
                {
                    tasks.Add(PublishMessageAsync(CreateEntityChanged(changeType, organizationId, projectId), delay));
                }

                return(Task.WhenAll(tasks));
            }

            if (organizations.Count > 0)
            {
                foreach (string organization in organizations)
                {
                    tasks.Add(PublishMessageAsync(CreateEntityChanged(changeType, organization), delay));
                }

                return(Task.WhenAll(tasks));
            }

            return(PublishMessageAsync(new EntityChanged {
                ChangeType = changeType,
                Type = EntityTypeName
            }, delay));
        }
Beispiel #22
0
 public static bool HasPageNumber(this ICommandOptions options)
 {
     return(options.SafeHasOption(SetPagingOptionsExtensions.PageNumberKey));
 }
 public static string GetDefaultCacheKey(this ICommandOptions options)
 {
     return(options.SafeGetOption <string>(SetCacheOptionsExtensions.CacheKeyKey, null));
 }
Beispiel #24
0
 public static int GetPage(this ICommandOptions options)
 {
     return(options.SafeGetOption(SetPagingOptionsExtensions.PageNumberKey, 1));
 }
 public static bool ShouldUseCache(this ICommandOptions options, bool defaultValue = false)
 {
     return(options.SafeGetOption(SetCacheOptionsExtensions.CacheEnabledKey, defaultValue));
 }
Beispiel #26
0
 public static bool ShouldUseSkip(this ICommandOptions options)
 {
     return(options.HasPageLimit() && options.GetPage() > 1);
 }
 public static bool HasCacheKey(this ICommandOptions options)
 {
     return(options.Values.Contains(SetCacheOptionsExtensions.CacheKeyKey) || options.Values.Contains(SetCacheOptionsExtensions.DefaultCacheKeyKey));
 }
 public WrappedCommandOptions(ICommandOptions innerOptions)
 {
     InnerOptions = innerOptions;
 }
        protected override async Task InvalidateCacheAsync(IReadOnlyCollection <ModifiedDocument <Stack> > documents, ICommandOptions options = null)
        {
            if (!IsCacheEnabled)
            {
                return;
            }

            var keys = documents.UnionOriginalAndModified().Select(GetStackSignatureCacheKey).Distinct();
            await Cache.RemoveAllAsync(keys).AnyContext();

            await base.InvalidateCacheAsync(documents, options).AnyContext();
        }
 protected virtual Task InvalidateCachedQueriesAsync(IReadOnlyCollection <User> documents, ICommandOptions options = null)
 {
     return(Task.CompletedTask);
 }
        protected override async Task AddDocumentsToCacheAsync(ICollection <FindHit <User> > findHits, ICommandOptions options)
        {
            await base.AddDocumentsToCacheAsync(findHits, options).AnyContext();

            var cacheEntries = new Dictionary <string, FindHit <User> >();

            foreach (var hit in findHits.Where(d => !String.IsNullOrEmpty(d.Document?.EmailAddress)))
            {
                cacheEntries.Add(EmailCacheKey(hit.Document.EmailAddress), hit);
            }

            if (cacheEntries.Count > 0)
            {
                await AddDocumentsToCacheWithKeyAsync(cacheEntries, options.GetExpiresIn()).AnyContext();
            }
        }
Beispiel #32
0
 public CommandRegistry(string command, ICommandOptions parser)
 {
     this.command = command;
     this.parser = parser;
 }