< Summary

Information
Class: WebApi.DAO.SensorDataDAO
Assembly: IndeklimaWebApi
File(s): /home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/DAO/SensorDataDAO.cs
Line coverage
40%
Covered lines: 25
Uncovered lines: 36
Coverable lines: 61
Total lines: 101
Line coverage: 40.9%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetSensorDataAsync()100%1.08157.14%
GetSensorDataAsync()100%210%
GetSensorDataAsync()100%210%
AddSensorDataAsync()75%4.02489.47%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/DAO/SensorDataDAO.cs

#LineLine coverage
 1using MongoDB.Driver;
 2using WebApi.Models;
 3
 4
 5namespace WebApi.DAO
 6{
 7    public class SensorDataDAO : ISensorDataDAO
 8    {
 9        private readonly IMongoCollection<SensorData> _sensorDataMongoCollection;
 10
 11
 312        public SensorDataDAO(MongoDbContext context)
 313        {
 314            _sensorDataMongoCollection = context.Database.GetCollection<SensorData>("SensorData");
 315        }
 16
 17        public async Task<List<SensorData>> GetSensorDataAsync(int hallId)
 318        {
 19            try
 320            {
 321                return await _sensorDataMongoCollection.Find(s => s.HallId == hallId).ToListAsync();
 22            }
 023            catch (Exception ex)
 024            {
 025                throw new NullReferenceException(ex.Message);
 26            }
 327        }
 28        public async Task<List<SensorData>> GetSensorDataAsync(int hallId, int limit)
 029        {
 30            try
 031            {
 032                return await _sensorDataMongoCollection.Find(s => s.HallId == hallId).Limit(limit).ToListAsync();
 33            }
 034            catch (Exception ex)
 035            {
 036                throw new NullReferenceException(ex.Message);
 37            }
 038        }
 39
 40        public async Task<List<SensorData>> GetSensorDataAsync(int hallId, DateTime startDate, DateTime endDate)
 041        {
 42            try
 043            {
 044                var filter = Builders<SensorData>.Filter.And(
 045                    Builders<SensorData>.Filter.Eq(s => s.HallId, hallId),
 046                    Builders<SensorData>.Filter.Or(
 047                        Builders<SensorData>.Filter.And(
 048                            Builders<SensorData>.Filter.Gte(s => s.TemperatureTimestamp, startDate),
 049                            Builders<SensorData>.Filter.Lte(s => s.TemperatureTimestamp, endDate)
 050                        ),
 051                        Builders<SensorData>.Filter.And(
 052                            Builders<SensorData>.Filter.Gte(s => s.HumidityTimestamp, startDate),
 053                            Builders<SensorData>.Filter.Lte(s => s.HumidityTimestamp, endDate)
 054                        ),
 055                        Builders<SensorData>.Filter.And(
 056                            Builders<SensorData>.Filter.Gte(s => s.CO2Timestamp, startDate),
 057                            Builders<SensorData>.Filter.Lte(s => s.CO2Timestamp, endDate)
 058                        )
 059                    )
 060                );
 61
 062                return await _sensorDataMongoCollection.Find(filter).ToListAsync();            }
 063            catch (Exception ex)
 064            {
 065                throw new NullReferenceException(ex.Message);
 66            }
 067        }
 68
 69        public async Task AddSensorDataAsync(SensorData sensorData)
 570        {
 71            try
 572            {
 573                if (sensorData == null)
 074                {
 075                    throw new ArgumentNullException(nameof(sensorData));
 76                }
 77
 78                // Check for duplicate data
 579                var duplicateData = await _sensorDataMongoCollection.Find(s => s.HallId == sensorData.HallId && (
 580                    s.TemperatureTimestamp == sensorData.TemperatureTimestamp ||
 581                    s.HumidityTimestamp == sensorData.HumidityTimestamp ||
 582                    s.CO2Timestamp == sensorData.CO2Timestamp)
 583                ).FirstOrDefaultAsync();
 584                if (duplicateData != null)
 185                {
 186                    throw new Exception("Duplicate data");
 87                }
 88
 89                //Add data to the collection
 490                await _sensorDataMongoCollection.InsertOneAsync(sensorData);
 91                //Return 0 if successful
 492                return;
 93            }
 194            catch (Exception ex)
 195            {
 196                throw new Exception(ex.Message);
 97            }
 498        }
 99
 100    }
 101}