@@ -19,6 +19,7 @@ package environment
1919import (
2020 "encoding/json"
2121 "fmt"
22+ "github.com/devtron-labs/devtron/client/grafana"
2223 bean3 "github.com/devtron-labs/devtron/pkg/attributes/bean"
2324 "github.com/devtron-labs/devtron/pkg/cluster"
2425 bean4 "github.com/devtron-labs/devtron/pkg/cluster/bean"
@@ -45,6 +46,7 @@ import (
4546
4647type EnvironmentService interface {
4748 FindOne (environment string ) (* bean2.EnvironmentBean , error )
49+ GetDataSourceName (* bean2.EnvironmentBean ) (dataSourceData * bean2.DataSourceMetaData , err error )
4850 Create (mappings * bean2.EnvironmentBean , userId int32 ) (* bean2.EnvironmentBean , error )
4951 Update (mappings * bean2.EnvironmentBean , userId int32 ) (* bean2.EnvironmentBean , error )
5052 GetAllActive () ([]bean2.EnvironmentBean , error )
@@ -80,14 +82,16 @@ type EnvironmentServiceImpl struct {
8082 userAuthService user.UserAuthService
8183 attributesRepository repository2.AttributesRepository
8284 clusterReadService read.ClusterReadService
85+ grafanaClient grafana.GrafanaClient
8386}
8487
8588func NewEnvironmentServiceImpl (environmentRepository repository.EnvironmentRepository ,
8689 clusterService cluster.ClusterService , logger * zap.SugaredLogger ,
8790 K8sUtil * util2.K8sServiceImpl , k8sInformerFactory informer.K8sInformerFactory ,
8891 // propertiesConfigService pipeline.PropertiesConfigService,
8992 userAuthService user.UserAuthService , attributesRepository repository2.AttributesRepository ,
90- clusterReadService read.ClusterReadService ) * EnvironmentServiceImpl {
93+ clusterReadService read.ClusterReadService ,
94+ grafanaClient grafana.GrafanaClient ) * EnvironmentServiceImpl {
9195 return & EnvironmentServiceImpl {
9296 environmentRepository : environmentRepository ,
9397 logger : logger ,
@@ -98,6 +102,25 @@ func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepos
98102 userAuthService : userAuthService ,
99103 attributesRepository : attributesRepository ,
100104 clusterReadService : clusterReadService ,
105+ grafanaClient : grafanaClient ,
106+ }
107+ }
108+
109+ func (impl EnvironmentServiceImpl ) GetDataSourceName (bean * bean2.EnvironmentBean ) (* bean2.DataSourceMetaData , error ) {
110+ datasource := & bean2.DataSourceMetaData {}
111+ if bean .DataSourceId == 0 || bean .PrometheusEndpoint == "" {
112+ impl .logger .Debugw ("grafana data source not configured for given" , "dataSourceId" , bean .DataSourceId )
113+ return datasource , nil
114+ } else {
115+ impl .logger .Debugw ("environment datasource" , "datasource" , bean .DataSourceId )
116+ data , err := impl .grafanaClient .GetDatasource (bean .DataSourceId )
117+ if err != nil {
118+ impl .logger .Errorw ("error in fetching datasource" , "err" , err )
119+ return datasource , err
120+ }
121+ datasource .Name = data .Name
122+ datasource .Id = bean .DataSourceId
123+ return datasource , nil
101124 }
102125}
103126
@@ -183,6 +206,7 @@ func (impl EnvironmentServiceImpl) FindOne(environment string) (*bean2.Environme
183206 Default : model .Default ,
184207 EnvironmentIdentifier : model .EnvironmentIdentifier ,
185208 Description : model .Description ,
209+ DataSourceId : model .GrafanaDatasourceId ,
186210 }
187211 return bean , nil
188212}
0 commit comments