128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Text.Json;
|
|
|
|
namespace YourApp.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class VisitorController : ControllerBase
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private static readonly string API_URL = "http://10.50.50.61:5678/webhook/analitycs-web?site=lingkunganhidup.jakarta.go.id";
|
|
|
|
public VisitorController(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
[HttpGet("daily-visitor")]
|
|
public async Task<IActionResult> GetDailyVisitor()
|
|
{
|
|
try
|
|
{
|
|
var data = await GetVisitorDataFromApiAsync();
|
|
return Ok(new { dailyVisitorCount = data.visitors.harian + 7390 });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { message = "Error fetching daily visitor data", details = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("monthly-visitor")]
|
|
public async Task<IActionResult> GetMonthlyVisitor()
|
|
{
|
|
try
|
|
{
|
|
var data = await GetVisitorDataFromApiAsync();
|
|
return Ok(new { monthlyVisitorCount = data.visitors.bulanan + 10488 });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { message = "Error fetching monthly visitor data", details = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("visitor-stats")]
|
|
public async Task<IActionResult> GetVisitorStats()
|
|
{
|
|
try
|
|
{
|
|
var data = await GetVisitorDataFromApiAsync();
|
|
|
|
var stats = new
|
|
{
|
|
dailyVisitorCount = data.visitors.harian + 7390,
|
|
monthlyVisitorCount = data.visitors.bulanan + 10488,
|
|
lastUpdated = DateTime.UtcNow
|
|
};
|
|
|
|
return Ok(stats);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new
|
|
{
|
|
message = "Error fetching visitor statistics",
|
|
details = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
private async Task<ApiResponse> GetVisitorDataFromApiAsync()
|
|
{
|
|
try
|
|
{
|
|
var response = await _httpClient.GetAsync(API_URL);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
var data = JsonSerializer.Deserialize<ApiResponse>(jsonString, new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
});
|
|
|
|
return data;
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
throw new Exception($"Failed to fetch data from API: {ex.Message}", ex);
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
throw new Exception($"Failed to parse API response: {ex.Message}", ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ApiResponse
|
|
{
|
|
public PageviewsData pageviews { get; set; }
|
|
public VisitorsData visits { get; set; }
|
|
public visitorsData visitors { get; set; }
|
|
}
|
|
|
|
public class PageviewsData
|
|
{
|
|
public int harian { get; set; }
|
|
public int bulanan { get; set; }
|
|
public int all { get; set; }
|
|
}
|
|
|
|
public class VisitorsData
|
|
{
|
|
public int harian { get; set; }
|
|
public int bulanan { get; set; }
|
|
public int all { get; set; }
|
|
}
|
|
|
|
public class visitorsData
|
|
{
|
|
public int harian { get; set; }
|
|
public int bulanan { get; set; }
|
|
public int all { get; set; }
|
|
}
|
|
} |