/// <summary>
        /// Creates a new Heartbeat entity for the specified Agent id
        /// </summary>
        /// <param name="request"></param>
        /// <param name="agentId"></param>
        /// <returns>Newly created Heartbeat</returns>
        public AgentHeartbeat PerformAgentHeartbeat(HeartbeatViewModel request, string agentId)
        {
            Agent agent = _agentRepo.GetOne(new Guid(agentId));

            if (agent == null)
            {
                throw new EntityDoesNotExistException("The Agent ID provided does not match any existing Agents");
            }

            if (agent.IsConnected == false)
            {
                throw new EntityOperationException("Agent is not connected. Please connect the agent and try again");
            }

            if (request.IsHealthy == false)
            {
                _webhookPublisher.PublishAsync("Agents.UnhealthyReported", agent.Id.ToString(), agent.Name).ConfigureAwait(false);
            }

            AgentHeartbeat agentHeartbeat = request.Map(request);

            //Add HeartBeat Values
            agentHeartbeat.AgentId        = new Guid(agentId);
            agentHeartbeat.CreatedBy      = _caller?.Identity.Name;
            agentHeartbeat.CreatedOn      = DateTime.UtcNow;
            agentHeartbeat.LastReportedOn = request.LastReportedOn ?? DateTime.UtcNow;
            _agentHeartbeatRepo.Add(agentHeartbeat);

            return(agentHeartbeat);
        }
Beispiel #2
0
        public async Task <IActionResult> AddHeartbeat([FromBody] AgentHeartbeat request, string agentId)
        {
            try
            {
                if (request == null)
                {
                    ModelState.AddModelError("Save", "No data passed");
                    return(BadRequest(ModelState));
                }

                Guid entityId = Guid.NewGuid();
                if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty))
                {
                    request.Id = entityId;
                }

                Agent agent = agentRepo.GetOne(new Guid(agentId));
                if (agent == null)
                {
                    return(NotFound("The Agent ID provided does not match any existing Agents"));
                }

                if (agent.IsConnected == false)
                {
                    ModelState.AddModelError("HeartBeat", "Agent is not connected");
                    return(BadRequest(ModelState));
                }

                if (request.IsHealthy == false)
                {
                    await webhookPublisher.PublishAsync("Agents.UnhealthyReported", agent.Id.ToString(), agent.Name).ConfigureAwait(false);
                }

                //Add HeartBeat Values
                request.AgentId        = new Guid(agentId);
                request.CreatedBy      = applicationUser?.UserName;
                request.CreatedOn      = DateTime.UtcNow;
                request.LastReportedOn = request.LastReportedOn ?? DateTime.UtcNow;
                agentHeartbeatRepo.Add(request);
                var resultRoute = "GetAgentHeartbeat";

                return(CreatedAtRoute(resultRoute, new { id = request.Id.Value.ToString("b") }, request));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Heartbeat", ex.Message);
                return(ex.GetActionResult());
            }
        }