< Summary - ReFlex - Library

Information
Class: ReFlex.Sensor.EmulatorModule.RecordingUtils
Assembly: ReFlex.Sensor.EmulatorModule
File(s): D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\RecordingUtils.cs
Line coverage
56%
Covered lines: 44
Uncovered lines: 34
Coverable lines: 78
Total lines: 129
Line coverage: 56.4%
Branch coverage
40%
Covered branches: 12
Total branches: 30
Branch coverage: 40%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DeleteRecording(...)30%261045.45%
DeleteAllRecordings()0%620%
RetrieveConfigurations()50%2291.66%
GetFrames(...)57.14%141492%
DeleteRecordingDirectory(...)0%620%

File(s)

D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\RecordingUtils.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using Newtonsoft.Json;
 6using ReFlex.Core.Common.Util;
 7
 8namespace ReFlex.Sensor.EmulatorModule
 9{
 10    public static class RecordingUtils
 11    {
 12        public const string SavePath = "wwwroot/recordings/";
 13        public const string ConfigFile = "config.json";
 14
 15        public static bool DeleteRecording(string name)
 2616        {
 2617            if (!Directory.Exists(SavePath))
 218            {
 219                Directory.CreateDirectory(SavePath);
 220                return false;
 21            }
 22
 2423            var recordings = Directory.GetDirectories(SavePath);
 24
 2425            var numDirectoriesDeleted = 0;
 26
 7227            foreach (var dir in recordings)
 028            {
 029                var cfgFile = Path.Combine(dir, ConfigFile);
 30
 031                if (!File.Exists(cfgFile))
 032                    continue;
 33
 034                var cfgObj = File.ReadAllText(cfgFile);
 35
 036                var cfg = JsonConvert.DeserializeObject<StreamParameter>(cfgObj);
 37
 038                if (!Equals(cfg.Name, name))
 039                    continue;
 40
 041                var deleted = DeleteRecordingDirectory(dir);
 042                if (deleted)
 043                    numDirectoriesDeleted += 1;
 044            }
 45
 2446            return numDirectoriesDeleted > 0;
 2647        }
 48
 49        public static int DeleteAllRecordings()
 050        {
 051            if (!Directory.Exists(SavePath))
 052            {
 053                Directory.CreateDirectory(SavePath);
 054                return 0;
 55            }
 56
 057            var recordings = Directory.GetDirectories(SavePath);
 58
 059            return recordings.
 060                // find all directories with config file
 061                Where(dir => File.Exists(Path.Combine(dir, ConfigFile))).
 062                // delete directories and select result of delete Method
 063                Select(DeleteRecordingDirectory).
 064                // count number of successful deletions
 065                Count(deleted => deleted);
 066        }
 67
 68        public static IEnumerable<StreamParameter> RetrieveConfigurations()
 2269        {
 2270            if(!Directory.Exists(SavePath))
 071                return new List<StreamParameter>();
 72
 2273            var recordings = Directory.GetDirectories(SavePath);
 74
 2275            return recordings.
 2276                // Select config file path
 2277                Select(dir => Path.Combine(dir, ConfigFile)).
 2278                // filter all directories that do not contain config file
 2279                Where(File.Exists)
 2280                // load and deserialize config file
 4481                .Select(file => JsonConvert.DeserializeObject<StreamParameter>(File.ReadAllText(file)));
 2282        }
 83
 84        public static string[] GetFrames(string name)
 5285        {
 5286            if(!Directory.Exists(SavePath))
 087                return new string[0];
 88
 5289            var recordings = Directory.GetDirectories(SavePath);
 90
 5291            var settings = recordings.
 5292                // Select tuple containing directory and config file path
 5293                Select(dir => Tuple.Create(dir, Path.Combine(dir, ConfigFile))).
 5294                // filter all directories that do not contain config file
 5295                Where(tpl => File.Exists(tpl.Item2))
 5296                // load and deserialize config file
 5297                .Select(tpl => Tuple.Create(tpl.Item1, JsonConvert.DeserializeObject<StreamParameter>(File.ReadAllText(t
 5298                // filter configs with matching name
 5299                .Where(tpl => Equals(tpl.Item2.Name, name)).
 52100                // return first item (if there are matching elements)
 52101                FirstOrDefault();
 102
 52103            var selectedRecordingsDir = settings?.Item1;
 104
 52105            if (settings?.Item2 == null || selectedRecordingsDir == null || !Directory.Exists(selectedRecordingsDir))
 0106                return new string[0];
 107
 108            // File-system enumeration order is unspecified. The recorder names frames
 109            // by their sequence number; sort numerically, including beyond seven digits.
 110            // Storage format is independent of pixel format. Support both PNG
 111            // and JPEG, including 16-bit streams recorded as lossy JPEG.
 52112            var frames = Directory.GetFiles(selectedRecordingsDir, "*.png");
 52113            if (frames.Length == 0)
 8114                frames = Directory.GetFiles(selectedRecordingsDir, "*.jpg");
 115
 52116            return frames
 22117                .OrderBy(file => ulong.Parse(Path.GetFileNameWithoutExtension(file), System.Globalization.CultureInfo.In
 52118                .ToArray();
 52119        }
 120
 121        private static bool DeleteRecordingDirectory(string directory)
 0122        {
 0123            if (Directory.Exists(directory))
 0124                Directory.Delete(directory, true);
 125
 0126            return !Directory.Exists(directory);
 0127        }
 128    }
 129}