Ejemplo n.º 1
0
        private MonitoredEndpoint CleanDeserializedMonitoredEndpoint(MonitoredEndpoint deserializedMonitoredEndpoint)
        {
            var cleanMonitoredEndpoint = new MonitoredEndpoint();

            cleanMonitoredEndpoint.Name = deserializedMonitoredEndpoint.Name;
            cleanMonitoredEndpoint.Url  = deserializedMonitoredEndpoint.Url;
            cleanMonitoredEndpoint.MonitoredInterval = deserializedMonitoredEndpoint.MonitoredInterval;
            return(cleanMonitoredEndpoint);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <MonitoredEndpoint> > PostMonitoredEndpoint([FromBody] MonitoredEndpoint monitoredEndpoint)
        {
            monitoredEndpoint = CleanDeserializedMonitoredEndpoint(monitoredEndpoint);

            monitoredEndpoint.Owner = _owner.Data;

            _context.MonitoredEndpoint.Add(monitoredEndpoint);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetMonitoredEndpoint", new { id = monitoredEndpoint.Id }, monitoredEndpoint));
        }
        private bool TimeTest(MonitoredEndpoint endpoint, DateTime timeStamp)
        {
            var result = true;

            if (endpoint.DateOfLastCheck != default)
            {
                var targetTime = endpoint.DateOfLastCheck.AddSeconds(endpoint.MonitoredInterval);
                if (targetTime > timeStamp)
                {
                    result = false;
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutMonitoredEndpoint(int id, [FromBody] MonitoredEndpoint monitoredEndpoint)
        {
            monitoredEndpoint = CleanDeserializedMonitoredEndpoint(monitoredEndpoint);

            var endpointToUpdate = await _context.MonitoredEndpoint.FindAsync(id);

            if (endpointToUpdate == null)
            {
                return(NotFound("ENTITY NOT FOUND IN DB, USE PUT ONLY FOR UPDATES WITH EXISTING ID"));
            }

            if (monitoredEndpoint.Owner.Id != _owner.Data.Id)
            {
                return(Unauthorized(_UNAUTHORIZED_MSG));
            }

            endpointToUpdate.Name = monitoredEndpoint.Name;
            endpointToUpdate.Url  = monitoredEndpoint.Url;

            _context.Entry(endpointToUpdate).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                var endpointExistsTest = await MonitoredEndpointExists(id);

                if (endpointExistsTest)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        private async Task <bool> SaveMonitoringResultAsync(MonitoredEndpoint endpoint, MonitoringResult monitoringResult)
        {
            var result = false;

            try
            {
                using (var scope = _scopeFactory.CreateScope())
                {
                    var dbContext = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
                    dbContext.MonitoredEndpoint.Update(endpoint);
                    dbContext.MonitoringResult.Add(monitoringResult);
                    await dbContext.SaveChangesAsync();

                    result = true;
                }
            }
            catch (Exception ex)
            {
                Log.Logger.ForContext(typeof(MonitoringWorker)).Error(ex, String.Format("ERROR SAVING CHANGES TO DB FOR ENDPOINT ID {0}", endpoint.Id));
            }
            return(result);
        }
        public static List <MonitoredEndpoint> GenerateEndpoint(int count, User owner, int startId = 1)
        {
            var result = new List <MonitoredEndpoint>();

            count += startId;

            for (int i = startId; i < count; i++)
            {
                MonitoredEndpoint endpoint = new MonitoredEndpoint()
                {
                    Id             = i,
                    Name           = string.Format("EndopintNumber{0}", i),
                    Url            = string.Format("www.number{0}.com", i),
                    UserForeignKey = owner.Id,
                    Owner          = owner
                };

                result.Add(endpoint);
            }

            return(result);
        }
        public static List <MonitoringResult> GenerateMonitoringResult(int count, MonitoredEndpoint endpoint, User owner, int startId = 1)
        {
            var result = new List <MonitoringResult>();

            count += startId;

            for (int i = startId; i < count; i++)
            {
                MonitoringResult monitoringResult = new MonitoringResult()
                {
                    Id                          = i,
                    DateOfCheck                 = DateTime.Now,
                    ReturnedHttpStatusCode      = 100,
                    ReturnedPayload             = String.Format("Payload for id {0}", i),
                    MonitoredEndpointForeignKey = endpoint.Id,
                    MonitoredEndpoint           = endpoint
                };

                result.Add(monitoringResult);
            }

            return(result);
        }
Ejemplo n.º 8
0
        private async Task <List <MonitoringResult> > GetTenLastResultsForEndpointAsync(MonitoredEndpoint endpoint)
        {
            var result = await _context.MonitoringResult.Where(x => x.MonitoredEndpointForeignKey == endpoint.Id).OrderByDescending(x => x.DateOfCheck).Take(10).ToListAsync();

            return(result);
        }