public object Read(QueryParams queryParts)
        {
            //setup the query
            string   query     = queryParts.query;
            DateTime startDate = queryParts.startDate;
            DateTime endDate   = queryParts.endDate;

            var result = new QueryResultPart();

            //run the query
            var connection = (SeqConnection)OpenConnection();

            Task.Run(async() =>
            {
                await Run(connection);
            }).Wait();


            async Task Run(SeqConnection useConnection)
            {
                try
                {
                    result = await connection.Data.QueryAsync(query, startDate, endDate);
                }
                catch (Exception ee)
                {
                    Log.Error(ee, "Error has occured");
                    result = null;
                }
            }

            return(result);
        }
        public int Run()
        {
            int interval = _configuration.Interval;
            var dateFrom = DateTime.Now.AddHours(-interval);
            var dateTo   = DateTime.Now;

            Log.Information("SeqSnapShot started");

            var returnConfig = _configuration.GetConfiguration <List <StatElement> >();
            List <StatElement> statConfiguration = (List <StatElement>)returnConfig;

            List <StatRowItem> rowsToStore = new List <StatRowItem>();

            int successfulWrites = 0;

            Parallel.ForEach(statConfiguration, (stat) =>
            {
                if (!string.IsNullOrWhiteSpace(stat.query))
                {
                    QueryParams parts = new QueryParams()
                    {
                        query     = stat.query,
                        startDate = dateFrom,
                        endDate   = dateTo
                    };

                    QueryResultPart seqQueryResult = (QueryResultPart)_dataSource.Read(parts);

                    if (seqQueryResult != null)
                    {
                        stat.count = seqQueryResult.Rows.Length;

                        rowsToStore.Add(
                            new StatRowItem()
                        {
                            DateFrom  = dateFrom.ToString(),
                            DateTo    = dateTo.ToString(),
                            WriteDate = DateTime.Now.ToString(),
                            StatName  = stat.name,
                            StatCount = stat.count
                        }
                            );
                    }
                }
            });

            _dataStorage.Write(rowsToStore);
            successfulWrites = rowsToStore.Count;

            return(successfulWrites);
        }
Beispiel #3
0
        public void Should_Fail_Nicely_When_Using_Invalid_SEQ_Server()
        {
            DataProviderSettings settings = new DataProviderSettings();

            settings.Location = "http://imchelle/";
            settings.APIKey   = "MbZ6wyBSuTVaYNfErgkO";

            seqConnection = new SeqDataConnection(settings);

            QueryParams parts = new QueryParams()
            {
                query     = "SELECT * FROM STREAM",
                startDate = new DateTime(2018, 9, 13, 16, 0, 0),
                endDate   = new DateTime(2018, 9, 14, 17, 0, 0)
            };

            QueryResultPart expected = null;
            var             actual   = (QueryResultPart)seqConnection.Read(parts);

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Requests a block of parts.
        /// </summary>
        /// <param name="resultId"></param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        /// <param name="totalRowCount"></param>
        /// <remarks>
        /// If the block can be completely retrieved from the cache then it is
        /// done so.  Otherwise, it forwards the request for the rows onto the
        /// connection object.
        /// </remarks>
        /// <returns></returns>
        public QueryResultPart GetResultPart(int resultId, int rowIndex, int rowCount, int colCount, int totalRowCount)
        {
            lock (this) {
                // What was requested....
                int origRowIndex = rowIndex;
                int origRowCount = rowCount;

                var cachedRows = new List <CachedRow>();

                // The top row that isn't found in the cache.
                bool foundNotcached = false;
                // Look for the top row in the block that hasn't been cached
                for (int r = 0; r < rowCount && !foundNotcached; ++r)
                {
                    int daRow = rowIndex + r;
                    // Is the row in the cache?
                    var rowRef = new RowRef(resultId, daRow);
                    // Not in cache so mark this as top row not in cache...
                    object rowObj;
                    if (!rowCache.TryGet(rowRef, out rowObj))
                    {
                        rowIndex = daRow;
                        if (rowIndex + rowCount > totalRowCount)
                        {
                            rowCount = totalRowCount - rowIndex;
                        }
                        foundNotcached = true;
                    }
                    else
                    {
                        var row = (CachedRow)rowObj;
                        cachedRows.Add(row);
                    }
                }

                var notCachedRows = new List <CachedRow>();
                if (foundNotcached)
                {
                    // Now work up from the bottom and find row that isn't in cache....
                    foundNotcached = false;
                    // Look for the bottom row in the block that hasn't been cached
                    for (int r = rowCount - 1; r >= 0 && !foundNotcached; --r)
                    {
                        int daRow = rowIndex + r;
                        // Is the row in the cache?
                        var rowRef = new RowRef(resultId, daRow);
                        // Not in cache so mark this as top row not in cache...
                        object rowObj;
                        if (!rowCache.TryGet(rowRef, out rowObj))
                        {
                            if (rowIndex == origRowIndex)
                            {
                                rowIndex = rowIndex - (rowCount - (r + 1));
                                if (rowIndex < 0)
                                {
                                    rowCount = rowCount + rowIndex;
                                    rowIndex = 0;
                                }
                            }
                            else
                            {
                                rowCount = r + 1;
                            }
                            foundNotcached = true;
                        }
                        else
                        {
                            var row = (CachedRow)rowObj;
                            notCachedRows.Insert(0, row);
                        }
                    }
                }

                // Some of it not in the cache...
                if (foundNotcached)
                {
                    // Request a part of a result from the server (blocks)
                    QueryResultPart block = connection.RequestResultPart(resultId, rowIndex, rowCount);

                    for (int r = 0; r < rowCount; ++r)
                    {
                        var rowData   = new ISqlObject[block.ColumnCount];
                        var dataSizes = new int[block.ColumnCount];

                        int theRow  = (rowIndex + r);
                        int colSize = 0;
                        var row     = block.GetRow(r);
                        for (int c = 0; c < colCount; ++c)
                        {
                            var ob = row.Values[c];
                            rowData[c]   = ob;
                            dataSizes[c] = row.ValueSizes[c];
                            colSize     += row.ValueSizes[c];
                        }

                        var cachedRow = new CachedRow {
                            ResultId = resultId,
                            Row      = theRow,
                            RowData  = rowData,
                            Sizes    = dataSizes
                        };

                        // Don't cache if it's over a certain size,
                        if (colSize <= 3200)
                        {
                            rowCache.Set(new RowRef(resultId, theRow), cachedRow);
                        }

                        cachedRows.Add(cachedRow);
                    }
                }

                // At this point, the cached rows should be completely in the cache so
                // retrieve it from the cache.
                var resultPart = new QueryResultPart(colCount);
                int low        = origRowIndex;
                int high       = origRowIndex + origRowCount;

                foreach (CachedRow row in cachedRows)
                {
                    if (row.ResultId != resultId)
                    {
                        continue;
                    }

                    // Put into the result block
                    if (row.Row >= low && row.Row < high)
                    {
                        var rowArray = new ISqlObject[colCount];
                        var rowSizes = new int[colCount];
                        for (int c = 0; c < colCount; ++c)
                        {
                            rowArray[c] = row.RowData[c];
                            rowSizes[c] = row.Sizes[c];
                        }

                        resultPart.AddRow(new QueryResultRow(rowArray, rowSizes));
                    }
                }

                foreach (CachedRow row in notCachedRows)
                {
                    if (row.ResultId != resultId)
                    {
                        continue;
                    }

                    // Put into the result block
                    if (row.Row >= low && row.Row < high)
                    {
                        var rowArray = new ISqlObject[colCount];
                        var sizes    = new int[colCount];
                        for (int c = 0; c < colCount; ++c)
                        {
                            rowArray[c] = row.RowData[c];
                            sizes[c]    = row.Sizes[c];
                        }

                        resultPart.AddRow(new QueryResultRow(rowArray, sizes));
                    }
                }

                // And return the result (phew!)
                return(resultPart);
            }
        }