| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/torkelo/grafana-pro/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/torkelo/grafana-pro/pkg/middleware" | 
					
						
							|  |  |  | 	m "github.com/torkelo/grafana-pro/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-01-05 14:59:18 +08:00
										 |  |  | 	"github.com/torkelo/grafana-pro/pkg/setting" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getFrontendSettings(c *middleware.Context) (map[string]interface{}, error) { | 
					
						
							|  |  |  | 	accountDataSources := make([]*m.DataSource, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-16 21:32:18 +08:00
										 |  |  | 	if c.IsSignedIn { | 
					
						
							|  |  |  | 		query := m.GetDataSourcesQuery{AccountId: c.UsingAccountId} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		err := bus.Dispatch(&query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		accountDataSources = query.Result | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	datasources := make(map[string]interface{}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 19:12:29 +08:00
										 |  |  | 	for _, ds := range accountDataSources { | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		url := ds.Url | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ds.Access == m.DS_ACCESS_PROXY { | 
					
						
							| 
									
										
										
										
											2015-01-05 14:59:18 +08:00
										 |  |  | 			url = setting.AppSubUrl + "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var dsMap = map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2015-01-09 23:36:23 +08:00
										 |  |  | 			"type":    ds.Type, | 
					
						
							|  |  |  | 			"url":     url, | 
					
						
							|  |  |  | 			"default": ds.IsDefault, | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ds.Type == m.DS_INFLUXDB { | 
					
						
							|  |  |  | 			if ds.Access == m.DS_ACCESS_DIRECT { | 
					
						
							|  |  |  | 				dsMap["username"] = ds.User | 
					
						
							|  |  |  | 				dsMap["password"] = ds.Password | 
					
						
							|  |  |  | 				dsMap["url"] = url + "/db/" + ds.Database | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 17:39:26 +08:00
										 |  |  | 		if ds.Type == m.DS_ES { | 
					
						
							|  |  |  | 			dsMap["index"] = ds.Database | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		datasources[ds.Name] = dsMap | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add grafana backend data source
 | 
					
						
							|  |  |  | 	datasources["grafana"] = map[string]interface{}{ | 
					
						
							|  |  |  | 		"type":      "grafana", | 
					
						
							|  |  |  | 		"grafanaDB": true, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonObj := map[string]interface{}{ | 
					
						
							|  |  |  | 		"datasources": datasources, | 
					
						
							| 
									
										
										
										
											2015-01-05 14:59:18 +08:00
										 |  |  | 		"appSubUrl":   setting.AppSubUrl, | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 		"buildInfo": map[string]interface{}{ | 
					
						
							|  |  |  | 			"version":    setting.BuildVersion, | 
					
						
							|  |  |  | 			"commit":     setting.BuildCommit, | 
					
						
							|  |  |  | 			"buildstamp": setting.BuildStamp, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return jsonObj, nil | 
					
						
							|  |  |  | } |