)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » HeartbeatController (Ia.Api.Wa.Controllers)

Public general use code classes and xml files that we've compiled and used over the years:

Heartbeat API Controller class.

    1: using Microsoft.AspNetCore.Mvc;
    2: using System.Net;
    3:  
    4: namespace Ia.Api.Wa.Controllers
    5: {
    6:     ////////////////////////////////////////////////////////////////////////////
    7:  
    8:     /// <summary publish="true">
    9:     /// Heartbeat API Controller class.
   10:     /// </summary>
   11:     /// 
   12:     /// <remarks> 
   13:     /// Copyright © 2019-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   14:     ///
   15:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   16:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   17:     ///
   18:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   19:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   20:     /// 
   21:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   22:     /// 
   23:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   24:     /// </remarks> 
   25:     [ApiController]
   26:     [Route("api/heartbeats")]
   27:     public class HeartbeatController : ControllerBase
   28:     {
   29:         private static readonly object objectLock = new object();
   30:         private static List<Ia.Cl.Models.Heartbeat> heartbeats = new List<Ia.Cl.Models.Heartbeat>();
   31:  
   32:         // See https://docs.opsgenie.com/docs/heartbeat-api for referece
   33:  
   34:         ////////////////////////////////////////////////////////////////////////////
   35:  
   36:         /// <summary>
   37:         /// 
   38:         /// </summary>
   39:         [HttpPost]
   40:         [Route("{name}")]
   41:         public HttpStatusCode PostHeartbeat(string name)
   42:         {
   43:             HttpStatusCode httpStatusCode;
   44:             Ia.Cl.Models.Heartbeat heartbeat;
   45:  
   46:             FilterHeartbeatPast24HourList();
   47:  
   48:             if (!string.IsNullOrEmpty(name))
   49:             {
   50:                 heartbeat = new Ia.Cl.Models.Heartbeat() { Name = name, Created = DateTime.UtcNow.AddHours(3) };
   51:  
   52:                 heartbeats.Add(heartbeat);
   53:  
   54:                 httpStatusCode = HttpStatusCode.OK;
   55:             }
   56:             else
   57:             {
   58:                 httpStatusCode = HttpStatusCode.BadRequest;
   59:             }
   60:  
   61:             return httpStatusCode;
   62:         }
   63:  
   64:         ////////////////////////////////////////////////////////////////////////////
   65:  
   66:         /// <summary>
   67:         /// 
   68:         /// </summary>
   69:         [HttpGet]
   70:         [Route("{name}")]
   71:         public List<Ia.Cl.Models.Heartbeat> GetHeartbeatsForName(string name)
   72:         {
   73:             var list = new List<Ia.Cl.Models.Heartbeat>();
   74:  
   75:             FilterHeartbeatPast24HourList();
   76:  
   77:             if (!string.IsNullOrEmpty(name))
   78:             {
   79:                 list = (from h in heartbeats where h.Name == name select h).ToList();
   80:             }
   81:             else
   82:             {
   83:             }
   84:  
   85:             return list;
   86:         }
   87:  
   88:         ////////////////////////////////////////////////////////////////////////////
   89:  
   90:         /// <summary>
   91:         /// 
   92:         /// </summary>
   93:         [HttpGet]
   94:         [Route("")]
   95:         public List<Ia.Cl.Models.Heartbeat> GetHeartbeats()
   96:         {
   97:             FilterHeartbeatPast24HourList();
   98:  
   99:             return heartbeats;
  100:         }
  101:  
  102:         ////////////////////////////////////////////////////////////////////////////
  103:  
  104:         /// <summary>
  105:         /// 
  106:         /// </summary>
  107:         private void FilterHeartbeatPast24HourList()
  108:         {
  109:             try
  110:             {
  111:                 lock (objectLock)
  112:                 {
  113:                     var dateTime = DateTime.UtcNow.AddDays(-1).AddHours(3); // keep inside lock
  114:  
  115:                     /*
  116:     {"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":" at Ia.Api.Wa.Controller.HeartbeatController.<>c__DisplayClass6_0.b__0(Heartbeat h) in C:\\Users\\Jasem\\Documents\\Visual Studio 2022\\Projects\\Integrated Applications\\api.ia.com.kw\\controller\\heartbeat.cs:line 121\r\n at System.Linq.Enumerable.WhereListIterator`1.MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Ia.Api.Wa.Controller.HeartbeatController.FilterHeartbeatPast24HourList() in C:\\Users\\Jasem\\Documents\\Visual Studio 2022\\Projects\\Integrated Applications\\api.ia.com.kw\\controller\\heartbeat.cs:line 121\r\n at Ia.Api.Wa.Controller.HeartbeatController.GetHeartbeats() in C:\\Users\\Jasem\\Documents\\Visual Studio 2022\\Projects\\Integrated Applications\\api.ia.com.kw\\controller\\heartbeat.cs:line 103\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"}                 */
  117:  
  118:                     if (heartbeats != null)
  119:                     {
  120:                         lock (objectLock) // double look to see if I could avoid the above error
  121:                         {
  122:                             heartbeats = (from h in heartbeats
  123:                                           where h.Created != null && h.Created >= dateTime
  124:                                           select h).ToList();
  125:                         }
  126:                     }
  127:                     else heartbeats = new List<Ia.Cl.Models.Heartbeat>();
  128:                 }
  129:             }
  130:             catch (Exception ex)
  131:             {
  132:                 heartbeats = new List<Ia.Cl.Models.Heartbeat>();
  133:             }
  134:         }
  135:  
  136:         ////////////////////////////////////////////////////////////////////////////
  137:         ////////////////////////////////////////////////////////////////////////////
  138:     }
  139:  
  140:     ////////////////////////////////////////////////////////////////////////////
  141:     ////////////////////////////////////////////////////////////////////////////
  142: }