package api import ( "context" "next-terminal/server/common/nt" "next-terminal/server/repository" "time" "github.com/labstack/echo/v4" ) type DashboardApi struct{} func (api DashboardApi) GetTimeCounterEndpoint(c echo.Context) error { var ( totalUser int64 onlineUser int64 countOnlineSession int64 totalAsset int64 activeAsset int64 failLoginCount int64 totalWebsite int64 ) totalUser, _ = repository.UserRepository.Count(context.TODO()) onlineUser, _ = repository.UserRepository.CountOnlineUser(context.TODO()) countOnlineSession, _ = repository.SessionRepository.CountOnlineSession(context.TODO()) totalAsset, _ = repository.AssetRepository.Count(context.TODO()) activeAsset, _ = repository.AssetRepository.CountByActive(context.TODO(), true) failLoginCount, _ = repository.LoginLogRepository.CountByState(context.TODO(), "0") gatewayList, _ := repository.GatewayRepository.FindAll(context.TODO()) gatewayActiveCount := int64(len(gatewayList)) totalWebsite, _ = repository.WebsiteRepository.Count(context.TODO()) counter := map[string]interface{}{ "loginFailedTimes": failLoginCount, "userOnlineCount": onlineUser, "sessionOnlineCount": countOnlineSession, "sessionTotalCount": 0, "userTotalCount": totalUser, "assetActiveCount": activeAsset, "assetTotalCount": totalAsset, "websiteActiveCount": 0, "websiteTotalCount": totalWebsite, "gatewayActiveCount": gatewayActiveCount, "gatewayTotalCount": gatewayActiveCount, } return Success(c, counter) } func (api DashboardApi) GetDateCounterV2Endpoint(c echo.Context) error { d := c.QueryParam("d") var days = 7 if d == "month" { days = 30 } now := time.Now() lastDate := now.AddDate(0, 0, -days) loginLogCounters, err := repository.LoginLogRepository.CountWithGroupByLoginTime(context.TODO(), lastDate) if err != nil { return err } userCounters, err := repository.LoginLogRepository.CountWithGroupByLoginTimeAndUsername(context.TODO(), lastDate) if err != nil { return err } sessionCounters, err := repository.SessionRepository.CountWithGroupByLoginTime(context.TODO(), lastDate) if err != nil { return err } var counters []map[string]interface{} for i := 0; i < days; i++ { day := lastDate.AddDate(0, 0, i).Format("2006-01-02") item := map[string]interface{}{ "date": day, "login": int64(0), "user": int64(0), "asset": int64(0), } for _, counter := range loginLogCounters { if counter.Date == day { item["login"] = counter.Value break } } for _, counter := range userCounters { if counter.Date == day { item["user"] = counter.Value break } } for _, counter := range sessionCounters { if counter.Date == day { item["asset"] = counter.Value break } } counters = append(counters, item) } return Success(c, counters) } func (api DashboardApi) GetAssetTypesEndpoint(c echo.Context) error { var ( ssh int64 rdp int64 vnc int64 telnet int64 kubernetes int64 http int64 ) ssh, _ = repository.AssetRepository.CountByProtocol(context.TODO(), nt.SSH) rdp, _ = repository.AssetRepository.CountByProtocol(context.TODO(), nt.RDP) vnc, _ = repository.AssetRepository.CountByProtocol(context.TODO(), nt.VNC) telnet, _ = repository.AssetRepository.CountByProtocol(context.TODO(), nt.Telnet) kubernetes, _ = repository.AssetRepository.CountByProtocol(context.TODO(), nt.K8s) types := []map[string]interface{}{ {"type": "SSH", "value": ssh}, {"type": "RDP", "value": rdp}, {"type": "VNC", "value": vnc}, {"type": "TELNET", "value": telnet}, {"type": "KUBERNETES", "value": kubernetes}, {"type": "HTTP", "value": http}, } return Success(c, types) }