Skip to content
43 changes: 41 additions & 2 deletions api/restHandler/AppStoreDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
appstore2 "github.com/devtron-labs/devtron/internal/sql/repository/appstore"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/appstore"
Expand Down Expand Up @@ -233,9 +234,10 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri
writeJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
v := r.URL.Query()
token := r.Header.Get("token")
var envs []int
envsQueryParam := r.URL.Query().Get("envs")
envsQueryParam := v.Get("envs")
if envsQueryParam != "" {
envsStr := strings.Split(envsQueryParam, ",")
for _, t := range envsStr {
Expand All @@ -248,8 +250,45 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri
envs = append(envs, env)
}
}
deprecated := false
deprecatedStr := v.Get("includeDeprecated")
if len(deprecatedStr) > 0 {
deprecated, err = strconv.ParseBool(deprecatedStr)
if err != nil {
deprecated = false
}
}

var chartRepoIds []int
chartRepoIdsStr := v.Get("chartRepoId")
if len(chartRepoIdsStr) > 0 {
chartRepoIdStrArr := strings.Split(chartRepoIdsStr, ",")
for _, chartRepoIdStr := range chartRepoIdStrArr {
chartRepoId, err := strconv.Atoi(chartRepoIdStr)
if err == nil {
chartRepoIds = append(chartRepoIds, chartRepoId)
}
}
}
appStoreName := v.Get("appStoreName")
appName := v.Get("appName")
offset := 0
offsetStr := v.Get("offset")
if len(offsetStr) > 0 {
offset, _ = strconv.Atoi(offsetStr)
}
size := 0
sizeStr := v.Get("size")
if len(sizeStr) > 0 {
size, _ = strconv.Atoi(sizeStr)
}
filter := &appstore2.AppStoreFilter{IncludeDeprecated: deprecated, ChartRepoId: chartRepoIds, AppStoreName: appStoreName, EnvIds: envs, AppName: appName}
if size > 0 {
filter.Size = size
filter.Offset = offset
}
handler.Logger.Infow("request payload, GetAllInstalledApp", "envsQueryParam", envsQueryParam)
res, err := handler.installedAppService.GetAll(envs)
res, err := handler.installedAppService.GetAll(filter)
if err != nil {
handler.Logger.Errorw("service err, GetAllInstalledApp", "err", err, "envsQueryParam", envsQueryParam)
writeJsonResp(w, err, nil, http.StatusInternalServerError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ type AppStoreWithVersion struct {
type AppStoreFilter struct {
ChartRepoId []int `json:"chartRepoId"`
AppStoreName string `json:"appStoreName"`
AppName string `json:"appName"`
IncludeDeprecated bool `json:"includeDeprecated"`
Offset int `json:"offset"`
Size int `json:"size"`
EnvIds []int `json:"envIds"`
}

type ChartRepoSearch struct {
Expand Down
45 changes: 35 additions & 10 deletions internal/sql/repository/appstore/AppStoreDeploymentRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type InstalledAppRepository interface {
GetInstalledApp(id int) (*InstalledApps, error)
GetInstalledAppVersion(id int) (*InstalledAppVersions, error)
GetInstalledAppVersionAny(id int) (*InstalledAppVersions, error)
GetAllInstalledApps(envIds []int) ([]InstalledAppsWithChartDetails, error)
GetAllInstalledApps(filter *AppStoreFilter) ([]InstalledAppsWithChartDetails, error)
GetAllIntalledAppsByAppStoreId(appStoreId int) ([]InstalledAppAndEnvDetails, error)
GetInstalledAppVersionByInstalledAppIdAndEnvId(installedAppId int, envId int) (*InstalledAppVersions, error)
GetInstalledAppVersionByAppStoreId(appStoreId int) ([]*InstalledAppVersions, error)
Expand Down Expand Up @@ -217,17 +217,42 @@ func (impl InstalledAppRepositoryImpl) GetInstalledAppVersionAny(id int) (*Insta
return model, err
}

func (impl InstalledAppRepositoryImpl) GetAllInstalledApps(envIds []int) ([]InstalledAppsWithChartDetails, error) {
func (impl InstalledAppRepositoryImpl) GetAllInstalledApps(filter *AppStoreFilter) ([]InstalledAppsWithChartDetails, error) {
var installedAppsWithChartDetails []InstalledAppsWithChartDetails
var queryTemp string
if len(envIds) > 0 {
queryTemp = "select iav.updated_on, iav.id as installed_app_version_id, ch.name as chart_repo_name, env.environment_name, env.id as environment_id, a.app_name, asav.icon, asav.name as app_store_application_name, asav.id as app_store_application_version_id, ia.id " +
", asav.deprecated from installed_app_versions iav inner join installed_apps ia on iav.installed_app_id = ia.id inner join app a on a.id = ia.app_id inner join environment env on ia.environment_id = env.id inner join app_store_application_version asav on iav.app_store_application_version_id = asav.id inner join app_store aps on aps.id = asav.app_store_id inner join chart_repo ch on ch.id = aps.chart_repo_id where ia.active=true and env.id in (" + sqlIntSeq(envIds) + ") and iav.active=true"
} else {
queryTemp = "select iav.updated_on, iav.id as installed_app_version_id, ch.name as chart_repo_name, env.environment_name, env.id as environment_id, a.app_name, asav.icon, asav.name as app_store_application_name, asav.id as app_store_application_version_id, ia.id " +
", asav.deprecated from installed_app_versions iav inner join installed_apps ia on iav.installed_app_id = ia.id inner join app a on a.id = ia.app_id inner join environment env on ia.environment_id = env.id inner join app_store_application_version asav on iav.app_store_application_version_id = asav.id inner join app_store aps on aps.id = asav.app_store_id inner join chart_repo ch on ch.id = aps.chart_repo_id where ia.active=true and iav.active=true"
var query string
query = "select iav.updated_on, iav.id as installed_app_version_id, ch.name as chart_repo_name,"
query = query + " env.environment_name, env.id as environment_id, a.app_name, asav.icon, asav.name as app_store_application_name,"
query = query + " asav.id as app_store_application_version_id, ia.id , asav.deprecated"
query = query + " from installed_app_versions iav"
query = query + " inner join installed_apps ia on iav.installed_app_id = ia.id"
query = query + " inner join app a on a.id = ia.app_id"
query = query + " inner join environment env on ia.environment_id = env.id"
query = query + " inner join app_store_application_version asav on iav.app_store_application_version_id = asav.id"
query = query + " inner join app_store aps on aps.id = asav.app_store_id"
query = query + " inner join chart_repo ch on ch.id = aps.chart_repo_id"
query = query + " where ia.active = true and iav.active = true"
if !filter.IncludeDeprecated {
query = query + " AND asav.deprecated = FALSE"
}
if len(filter.AppStoreName) > 0 {
query = query + " AND aps.name LIKE '%" + filter.AppStoreName + "%'"
}
if len(filter.AppName) > 0 {
query = query + " AND a.app_name LIKE '%" + filter.AppName + "%'"
}
if len(filter.ChartRepoId) > 0 {
query = query + " AND ch.id IN (" + sqlIntSeq(filter.ChartRepoId) + ")"
}
if len(filter.EnvIds) > 0 {
query = query + " AND env.id IN (" + sqlIntSeq(filter.EnvIds) + ")"
}
query = query + " ORDER BY aps.name ASC"
if filter.Size > 0 {
query = query + " OFFSET " + strconv.Itoa(filter.Offset) + " LIMIT " + strconv.Itoa(filter.Size) + ""
}
_, err := impl.dbConnection.Query(&installedAppsWithChartDetails, queryTemp)
query = query + ";"
var err error
_, err = impl.dbConnection.Query(&installedAppsWithChartDetails, query)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/appstore/AppStoreDeploymentService.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type InstalledAppService interface {
UpdateInstalledApp(ctx context.Context, installAppVersionRequest *InstallAppVersionDTO) (*InstallAppVersionDTO, error)
GetInstalledApp(id int) (*InstallAppVersionDTO, error)
GetInstalledAppVersion(id int) (*InstallAppVersionDTO, error)
GetAll(environments []int) ([]InstalledAppsResponse, error)
GetAll(filter *appstore.AppStoreFilter) ([]InstalledAppsResponse, error)
GetAllInstalledAppsByAppStoreId(w http.ResponseWriter, r *http.Request, token string, appStoreId int) ([]InstalledAppsResponse, error)
DeleteInstalledApp(ctx context.Context, installAppVersionRequest *InstallAppVersionDTO) (*InstallAppVersionDTO, error)

Expand Down Expand Up @@ -460,8 +460,8 @@ func (impl InstalledAppServiceImpl) GetInstalledAppVersion(id int) (*InstallAppV
return installAppVersion, err
}

func (impl InstalledAppServiceImpl) GetAll(environments []int) ([]InstalledAppsResponse, error) {
installedApps, err := impl.installedAppRepository.GetAllInstalledApps(environments)
func (impl InstalledAppServiceImpl) GetAll(filter *appstore.AppStoreFilter) ([]InstalledAppsResponse, error) {
installedApps, err := impl.installedAppRepository.GetAllInstalledApps(filter)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Error(err)
return nil, err
Expand Down