Example #1
0
        private static void removeOldestBackup(string[] backupFiles, Dictionary<String, String> parameters)
        {
            DateTime[] timeStamps = new DateTime[backupFiles.Length];
            for (int i = 0; i < backupFiles.Length; i++)
            {
                string[] timestamp = backupFiles[i].Split('-');
                DateTime toAdd = new DateTime(Int32.Parse(timestamp[1]), Int32.Parse(timestamp[2]), Int32.Parse(timestamp[3]),
                    Int32.Parse(timestamp[4]), Int32.Parse(timestamp[5]), Int32.Parse(timestamp[6]));
                timeStamps[i] = toAdd;
            }

            DateTime leastRecentBackup = timeStamps.OrderBy(x => x).FirstOrDefault();
            string filePath = parameters["backupPath"] + parameters["dbName"] + "-" + leastRecentBackup.ToString("yyyy-MM-dd-HH-mm-ss") + "-.bak";
            System.IO.File.Delete(filePath);
        }
Example #2
0
        public static bool Export(DateTime[] timestamps, Series[] series, TimeSpanOption aggregationPeriod, string filename, BackgroundWorker worker = null, GroupingType groupingType = GroupingType.Centered)
        {
            var progress = 0;
            var firstTimeStamp = timestamps.Min().RoundDown(aggregationPeriod.TimeSpan);
            var finalTimeStamp = timestamps.Max().RoundUp(aggregationPeriod.TimeSpan);
            var totalTimeSpan = finalTimeStamp - firstTimeStamp;
            var sortedTimestamps = timestamps.OrderBy(x => x).ToArray();

            var halfOfTimeSpan = aggregationPeriod.TimeSpan.Subtract(new TimeSpan(aggregationPeriod.TimeSpan.Ticks / 2));

            var aggregatedTimestamps = new List<DateTime>();
            var currentTimestampIndex = 0;
            for (var timestamp = firstTimeStamp; timestamp <= finalTimeStamp; timestamp = timestamp.Add(aggregationPeriod.TimeSpan))
            {
                aggregatedTimestamps.Add(timestamp);
                var inclusiveStart = DateTime.MinValue;
                var exclusiveEnd = DateTime.MaxValue;

                switch (groupingType)
                {
                    case GroupingType.Centered:
                        inclusiveStart = timestamp - halfOfTimeSpan;
                        exclusiveEnd = timestamp + halfOfTimeSpan;
                        break;
                    case GroupingType.Forward:
                        inclusiveStart = timestamp;
                        exclusiveEnd = timestamp.Add(aggregationPeriod.TimeSpan);
                        break;
                }

                var timestampsList = new List<DateTime>();

                while (currentTimestampIndex < sortedTimestamps.Length && sortedTimestamps[currentTimestampIndex] < inclusiveStart)
                    currentTimestampIndex++;

                while (currentTimestampIndex < sortedTimestamps.Length && sortedTimestamps[currentTimestampIndex] < exclusiveEnd)
                {
                    timestampsList.Add(sortedTimestamps[currentTimestampIndex]);
                    currentTimestampIndex++;
                }

                var includedTimestamps = timestampsList.ToArray();

                foreach (var x in series)
                {
                    x.AggregatedValues[timestamp] =
                        x.AggregationModel.AggregationMethod.Aggregate((from includedTimestamp in includedTimestamps where x.Values.ContainsKey(includedTimestamp) select new KeyValuePair<DateTime, float>(includedTimestamp, x.Values[includedTimestamp])).OrderBy(y => y.Key).ToArray(), inclusiveStart, exclusiveEnd, timestamp);
                }

                if (worker == null || !worker.WorkerReportsProgress) continue;

                var currentTimeSpan = timestamp - firstTimeStamp;
                var currentProgress = (int)(currentTimeSpan.TotalSeconds / totalTimeSpan.TotalSeconds * 100);

                if (currentProgress <= progress) continue;

                worker.ReportProgress(currentProgress);
                progress = currentProgress;
            }
            try
            {
                using (var writer = File.CreateText(filename))
                {
                    writer.Write("YYYY/MM/DD hh:mm");
                    foreach (var x in series)
                    {
                        writer.Write("," + x.Name);
                    }

                    foreach (var aggregatedTimestamp in aggregatedTimestamps)
                    {
                        writer.Write(writer.NewLine);
                        writer.Write(aggregatedTimestamp.ToString("yyyy/MM/dd HH:mm"));
                        foreach (var x in series)
                        {
                            writer.Write(",");
                            if (x.AggregatedValues.ContainsKey(aggregatedTimestamp) && !float.IsNaN(x.AggregatedValues[aggregatedTimestamp]))
                            {
                                writer.Write(x.AggregatedValues[aggregatedTimestamp]);
                            }
                        }
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }