Example #1
0
        public void WriteTo(BlittableJsonTextWriter writer)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(UpTime));
            writer.WriteString(UpTime.ToString("c"));
            writer.WriteComma();

            writer.WritePropertyName(nameof(StartUpTime));
            writer.WriteDateTime(StartUpTime, isUtc: true);
            writer.WriteComma();

            writer.WritePropertyName(nameof(LastRequestTime));
            if (LastRequestTime.HasValue)
            {
                writer.WriteDateTime(LastRequestTime.Value, isUtc: true);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(LastAuthorizedNonClusterAdminRequestTime));
            if (LastAuthorizedNonClusterAdminRequestTime.HasValue)
            {
                writer.WriteDateTime(LastAuthorizedNonClusterAdminRequestTime.Value, isUtc: true);
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Example #2
0
        public Task GetNextCronExpressionOccurrence()
        {
            var             expression = GetQueryStringValueAndAssertIfSingleAndNotEmpty("expression");
            CrontabSchedule crontabSchedule;

            try
            {
                // will throw if the cron expression is invalid
                crontabSchedule = CrontabSchedule.Parse(expression);
            }
            catch (Exception e)
            {
                using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName(nameof(NextCronExpressionOccurrence.IsValid));
                        writer.WriteBool(false);
                        writer.WriteComma();
                        writer.WritePropertyName(nameof(NextCronExpressionOccurrence.ErrorMessage));
                        writer.WriteString(e.Message);
                        writer.WriteEndObject();
                        writer.Flush();
                    }

                return(Task.CompletedTask);
            }

            var nextOccurrence = crontabSchedule.GetNextOccurrence(SystemTime.UtcNow.ToLocalTime());

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(nameof(NextCronExpressionOccurrence.IsValid));
                    writer.WriteBool(true);
                    writer.WriteComma();
                    writer.WritePropertyName(nameof(NextCronExpressionOccurrence.Utc));
                    writer.WriteDateTime(nextOccurrence.ToUniversalTime(), true);
                    writer.WriteComma();
                    writer.WritePropertyName(nameof(NextCronExpressionOccurrence.ServerTime));
                    writer.WriteDateTime(nextOccurrence, false);
                    writer.WriteEndObject();
                    writer.Flush();
                }

            return(Task.CompletedTask);
        }
        public static void WriteMetadata(this BlittableJsonTextWriter writer, Document document, BlittableJsonReaderObject metadata)
        {
            writer.WritePropertyName(Constants.Documents.Metadata.Key);
            writer.WriteStartObject();
            bool first = true;

            if (metadata != null)
            {
                var size = metadata.Count;
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < size; i++)
                {
                    if (first == false)
                    {
                        writer.WriteComma();
                    }
                    first = false;
                    metadata.GetPropertyByIndex(i, ref prop);
                    writer.WritePropertyName(prop.Name);
                    writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                }
            }

            if (first == false)
            {
                writer.WriteComma();
            }
            writer.WritePropertyName(Constants.Documents.Metadata.ChangeVector);
            writer.WriteString(document.ChangeVector);

            if (document.Flags != DocumentFlags.None)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Flags);
                writer.WriteString(document.Flags.ToString());
            }
            if (document.Id != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Id);
                writer.WriteString(document.Id);
            }
            if (document.IndexScore != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.IndexScore);
                writer.WriteDouble(document.IndexScore.Value);
            }
            if (document.LastModified != DateTime.MinValue)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.LastModified);
                writer.WriteDateTime(document.LastModified, isUtc: true);
            }
            writer.WriteEndObject();
        }
Example #4
0
        public Task RunningQueries()
        {
            var indexes = Database
                          .IndexStore
                          .GetIndexes()
                          .ToList();

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    foreach (var index in indexes)
                    {
                        writer.WritePropertyName(index.Name);
                        writer.WriteStartArray();

                        var isFirstInternal = true;
                        foreach (var query in index.CurrentlyRunningQueries)
                        {
                            if (isFirstInternal == false)
                            {
                                writer.WriteComma();
                            }

                            isFirstInternal = false;

                            writer.WriteStartObject();

                            writer.WritePropertyName((nameof(query.Duration)));
                            writer.WriteString(query.Duration.ToString());
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.QueryId)));
                            writer.WriteInteger(query.QueryId);
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.StartTime)));
                            writer.WriteDateTime(query.StartTime, isUtc: true);
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.QueryInfo)));
                            writer.WriteIndexQuery(context, query.QueryInfo);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }

                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
        public Task GetNextBackupOccurrence()
        {
            var             backupFrequency = GetQueryStringValueAndAssertIfSingleAndNotEmpty("backupFrequency");
            CrontabSchedule crontabSchedule;

            try
            {
                // will throw if the backup frequency is invalid
                crontabSchedule = CrontabSchedule.Parse(backupFrequency);
            }
            catch (Exception e)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                using (var streamWriter = new StreamWriter(ResponseBodyStream()))
                {
                    streamWriter.Write(e.Message);
                    streamWriter.Flush();
                }
                return(Task.CompletedTask);
            }

            var nextOccurrence = crontabSchedule.GetNextOccurrence(SystemTime.UtcNow.ToLocalTime());

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(nameof(NextBackupOccurrence.Utc));
                    writer.WriteDateTime(nextOccurrence.ToUniversalTime(), true);
                    writer.WriteComma();
                    writer.WritePropertyName(nameof(NextBackupOccurrence.ServerTime));
                    writer.WriteDateTime(nextOccurrence, false);
                    writer.WriteEndObject();
                    writer.Flush();
                }

            return(Task.CompletedTask);
        }
Example #6
0
        public Task GetRevisions()
        {
            var etag     = GetLongQueryString("etag", false) ?? 0;
            var pageSize = GetPageSize();

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Revisions");
                        writer.WriteStartArray();

                        var first = true;
                        foreach (var revision in Database.DocumentsStorage.RevisionsStorage.GetRevisionsFrom(context, etag, pageSize))
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first = false;

                            writer.WriteStartObject();

                            writer.WritePropertyName(nameof(Document.Id));
                            writer.WriteString(revision.Id);
                            writer.WriteComma();

                            writer.WritePropertyName(nameof(Document.Etag));
                            writer.WriteInteger(revision.Etag);
                            writer.WriteComma();

                            writer.WritePropertyName(nameof(Document.LastModified));
                            writer.WriteDateTime(revision.LastModified, true);
                            writer.WriteComma();

                            writer.WritePropertyName(nameof(Document.ChangeVector));
                            writer.WriteString(revision.ChangeVector);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                        writer.WriteEndObject();
                    }
            return(Task.CompletedTask);
        }
        public static void WriteDatabaseStatistics(this BlittableJsonTextWriter writer, JsonOperationContext context, DatabaseStatistics statistics)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(statistics.CountOfIndexes));
            writer.WriteInteger(statistics.CountOfIndexes);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.CountOfDocuments));
            writer.WriteInteger(statistics.CountOfDocuments);
            writer.WriteComma();

            if (statistics.CountOfRevisionDocuments > 0)
            {
                writer.WritePropertyName(nameof(statistics.CountOfRevisionDocuments));
                writer.WriteInteger(statistics.CountOfRevisionDocuments);
                writer.WriteComma();
            }

            writer.WritePropertyName(nameof(statistics.CountOfAttachments));
            writer.WriteInteger(statistics.CountOfAttachments);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.CountOfUniqueAttachments));
            writer.WriteInteger(statistics.CountOfUniqueAttachments);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.DatabaseChangeVector));
            writer.WriteString(statistics.DatabaseChangeVector);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.DatabaseId));
            writer.WriteString(statistics.DatabaseId);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Is64Bit));
            writer.WriteBool(statistics.Is64Bit);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Pager));
            writer.WriteString(statistics.Pager);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.LastDocEtag));
            if (statistics.LastDocEtag.HasValue)
            {
                writer.WriteInteger(statistics.LastDocEtag.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName((nameof(statistics.DatabaseChangeVector)));
            writer.WriteString(statistics.DatabaseChangeVector);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.LastIndexingTime));
            if (statistics.LastIndexingTime.HasValue)
            {
                writer.WriteDateTime(statistics.LastIndexingTime.Value, isUtc: true);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Indexes));
            writer.WriteStartArray();
            var isFirstInternal = true;

            foreach (var index in statistics.Indexes)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(index.IsStale));
                writer.WriteBool(index.IsStale);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Name));
                writer.WriteString(index.Name);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Etag));
                writer.WriteInteger(index.Etag);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.LockMode));
                writer.WriteString(index.LockMode.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Priority));
                writer.WriteString(index.Priority.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.State));
                writer.WriteString(index.State.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Type));
                writer.WriteString(index.Type.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.LastIndexingTime));
                if (index.LastIndexingTime.HasValue)
                {
                    writer.WriteDateTime(index.LastIndexingTime.Value, isUtc: true);
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
            }
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
        private static void WriteMetadata(BlittableJsonTextWriter writer, Document document, BlittableJsonReaderObject metadata, Func <LazyStringValue, bool> filterMetadataProperty = null)
        {
            writer.WritePropertyName(Constants.Documents.Metadata.Key);
            writer.WriteStartObject();
            bool first = true;

            if (metadata != null)
            {
                var size = metadata.Count;
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < size; i++)
                {
                    metadata.GetPropertyByIndex(i, ref prop);

                    if (filterMetadataProperty != null && filterMetadataProperty(prop.Name))
                    {
                        continue;
                    }

                    if (first == false)
                    {
                        writer.WriteComma();
                    }
                    first = false;
                    writer.WritePropertyName((string)prop.Name);
                    writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                }
            }

            if (first == false)
            {
                writer.WriteComma();
            }
            writer.WritePropertyName(Constants.Documents.Metadata.ChangeVector);
            writer.WriteString(document.ChangeVector);

            if (document.Flags != DocumentFlags.None)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Flags);
                writer.WriteString(document.Flags.ToString());
            }
            if (document.Id != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Id);
                writer.WriteString(document.Id);
            }
            if (document.IndexScore != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.IndexScore);
                writer.WriteDouble(document.IndexScore.Value);
            }
            if (document.Distance != null)
            {
                writer.WriteComma();
                var result = document.Distance.Value;
                writer.WritePropertyName(Constants.Documents.Metadata.SpatialResult);
                writer.WriteStartObject();
                writer.WritePropertyName(nameof(result.Distance));
                writer.WriteDouble(result.Distance);
                writer.WriteComma();
                writer.WritePropertyName(nameof(result.Latitude));
                writer.WriteDouble(result.Latitude);
                writer.WriteComma();
                writer.WritePropertyName(nameof(result.Longitude));
                writer.WriteDouble(result.Longitude);
                writer.WriteEndObject();
            }
            if (document.LastModified != DateTime.MinValue)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.LastModified);
                writer.WriteDateTime(document.LastModified, isUtc: true);
            }
            writer.WriteEndObject();
        }
Example #9
0
        public Task Stats()
        {
            var documentId = GetStringQueryString("docId");

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                {
                    var document = Database.DocumentsStorage.Get(context, documentId, DocumentFields.Data);
                    if (document == null)
                    {
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        return(Task.CompletedTask);
                    }

                    var timeSeriesNames = new List <string>();

                    if (document.TryGetMetadata(out var metadata))
                    {
                        if (metadata.TryGet(Constants.Documents.Metadata.TimeSeries, out BlittableJsonReaderArray timeSeries) && timeSeries != null)
                        {
                            foreach (object name in timeSeries)
                            {
                                if (name == null)
                                {
                                    continue;
                                }

                                if (name is string || name is LazyStringValue || name is LazyCompressedStringValue)
                                {
                                    timeSeriesNames.Add(name.ToString());
                                }
                            }
                        }
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.DocumentId));
                        writer.WriteString(documentId);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.TimeSeries));

                        writer.WriteStartArray();

                        var first = true;
                        foreach (var tsName in timeSeriesNames)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first = false;

                            var stats = Database.DocumentsStorage.TimeSeriesStorage.Stats.GetStats(context, documentId, tsName);

                            writer.WriteStartObject();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.Name));
                            writer.WriteString(tsName);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.NumberOfEntries));
                            writer.WriteInteger(stats.Count);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.StartDate));
                            writer.WriteDateTime(stats.Start, isUtc: true);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.EndDate));
                            writer.WriteDateTime(stats.End, isUtc: true);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                }

            return(Task.CompletedTask);
        }
Example #10
0
        public Task Stats()
        {
            var documentId = GetStringQueryString("docId");

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                {
                    var document = Database.DocumentsStorage.Get(context, documentId, DocumentFields.Data);
                    if (document == null)
                    {
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        return(Task.CompletedTask);
                    }

                    var timeSeriesNames = GetTimesSeriesNames(document);
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.DocumentId));
                        writer.WriteString(documentId);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.TimeSeries));

                        writer.WriteStartArray();

                        var first = true;
                        foreach (var tsName in timeSeriesNames)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first = false;

                            var stats = Database.DocumentsStorage.TimeSeriesStorage.Stats.GetStats(context, documentId, tsName);

                            writer.WriteStartObject();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.Name));
                            writer.WriteString(tsName);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.NumberOfEntries));
                            writer.WriteInteger(stats.Count);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.StartDate));
                            writer.WriteDateTime(stats.Start, isUtc: true);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.EndDate));
                            writer.WriteDateTime(stats.End, isUtc: true);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                }

            return(Task.CompletedTask);
        }