// ContainerHost slugifies the container's name and append docker func (expose Expose) ContainerHost(name string) string { if composeRe.MatchString(name) { parts := composeRe.FindStringSubmatch(name) name = slug.Make(parts[2]) + parts[3] + "." + slug.Make(parts[1]) } else { name = slug.Make(name) } return fmt.Sprintf("%s.docker", name) }
func (app *AppPlugin) initApp() { app.initFrontendPlugin() // check if we have child panels for _, panel := range Panels { if strings.HasPrefix(panel.PluginDir, app.PluginDir) { panel.setPathsBasedOnApp(app) app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ Name: panel.Name, Id: panel.Id, Type: panel.Type, }) } } // check if we have child datasources for _, ds := range DataSources { if strings.HasPrefix(ds.PluginDir, app.PluginDir) { ds.setPathsBasedOnApp(app) app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ Name: ds.Name, Id: ds.Id, Type: ds.Type, }) } } // slugify pages for _, page := range app.Pages { if page.Slug == "" { page.Slug = slug.Make(page.Name) } } }
func AddSystem(cmd *m.AddSystemsCommand) error { return inTransaction(func(sess *xorm.Session) error { // check if service exists var err error for _, systemName := range cmd.SystemsName { if res, err := sess.Query("SELECT 1 from systems WHERE systems_name=? and org_id=?", systemName, cmd.OrgId); err != nil { return err } else if len(res) == 1 { return m.ErrSystemAlreadyAdded } entity := m.Systems{ OrgId: cmd.OrgId, SystemsName: systemName, Slug: slug.Make(systemName), } _, err = sess.Insert(&entity) apiEntity := m.AddApiKeyCommand{} apiEntity.OrgId = cmd.OrgId apiEntity.Name = strconv.FormatInt(entity.Id, 16) apiEntity.Role = m.ROLE_ADMIN newKeyInfo := apikeygen.New(apiEntity.OrgId, apiEntity.Name) apiEntity.Key = newKeyInfo.ClientSecret err = AddApiKey(&apiEntity) } return err }) }
func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error { if err := decoder.Decode(&app); err != nil { return err } if app.Css != nil { app.Css.Dark = evalRelativePluginUrlPath(app.Css.Dark, app.Id) app.Css.Light = evalRelativePluginUrlPath(app.Css.Light, app.Id) } app.PluginDir = pluginDir app.initFrontendPlugin() // check if we have child panels for _, panel := range Panels { if strings.HasPrefix(panel.PluginDir, app.PluginDir) { panel.IncludedInAppId = app.Id app.Includes = append(app.Includes, &AppIncludeInfo{ Name: panel.Name, Id: panel.Id, Type: panel.Type, }) } } for _, page := range app.Pages { if page.Slug == "" { page.Slug = slug.Make(page.Name) } } Apps[app.Id] = app return nil }
func (g StaticBlogGenerator) generatePosts() error { for _, post := range g.posts { err := g.generatePage(g.postTemplate, slug.Make(post.Title)+".html", post) if err != nil { return err } } return nil }
func (n *Ns1) MonitorsMetrics(client *Client, mts []plugin.MetricType) ([]plugin.MetricType, error) { metrics := make([]plugin.MetricType, 0) conf := mts[0].Config().Table() jobId, ok := conf["jobId"] if !ok || jobId.(ctypes.ConfigValueStr).Value == "" { LogError("jobId missing from config.") return metrics, nil } jobName, ok := conf["jobName"] if !ok || jobName.(ctypes.ConfigValueStr).Value == "" { LogError("jobName missing from config.") return metrics, nil } jSlug := slug.Make(jobName.(ctypes.ConfigValueStr).Value) j, err := client.MonitoringJobById(jobId.(ctypes.ConfigValueStr).Value) if err != nil { LogError("failed to query for job.", err) return nil, err } for region, status := range j.Status { data, ok := statusMap[status.Status] if !ok { return nil, fmt.Errorf("Unknown monitor status") } metrics = append(metrics, plugin.MetricType{ Data_: data, Namespace_: core.NewNamespace("raintank", "apps", "ns1", "monitoring", jSlug, region, "state"), Timestamp_: time.Now(), Version_: mts[0].Version(), }) } jobMetrics, err := client.MonitoringMetics(j.Id) if err != nil { return nil, err } for _, jm := range jobMetrics { for stat, m := range jm.Metrics { metrics = append(metrics, plugin.MetricType{ Data_: m.Avg, Namespace_: core.NewNamespace("raintank", "apps", "ns1", "monitoring", jSlug, jm.Region, stat), Timestamp_: time.Now(), Version_: mts[0].Version(), }) } } return metrics, nil }
func (g StaticBlogGenerator) generateTags() error { for tag, posts := range g.postsByTag { err := g.generatePage(g.tagTemplate, filepath.Join("tags", slug.Make(tag)+".html"), struct { Name string Posts []domain.Post }{tag, posts}) if err != nil { return err } } return nil }
func (r FSBlogRepository) writePost(id string, post domain.Post) error { path := filepath.Join(r.dir, id, postsDir) if post.Draft { path = filepath.Join(path, draftDir) } path = filepath.Join(path, slug.Make(post.Title)+".md") var data string if len(post.Tags) == 0 { data = fmt.Sprintf("# %s\n\n*%s*\n\n%s\n", post.Title, post.Written.Format("Jan 2, 2006"), post.Content) } else { data = fmt.Sprintf("# %s\n\n*%s*\n\n*%s*\n\n%s\n", post.Title, post.Written.Format("Jan 2, 2006"), strings.Join(post.Tags, ", "), post.Content) } return ioutil.WriteFile(path, []byte(data), 0600) }
func (app *AppPlugin) initApp() { app.initFrontendPlugin() // check if we have child panels for _, panel := range Panels { if strings.HasPrefix(panel.PluginDir, app.PluginDir) { panel.setPathsBasedOnApp(app) app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ Name: panel.Name, Id: panel.Id, Type: panel.Type, }) } } // check if we have child datasources for _, ds := range DataSources { if strings.HasPrefix(ds.PluginDir, app.PluginDir) { ds.setPathsBasedOnApp(app) app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ Name: ds.Name, Id: ds.Id, Type: ds.Type, }) } } app.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + app.Id + "/edit" // slugify pages for _, include := range app.Includes { if include.Slug == "" { include.Slug = slug.Make(include.Name) } if include.Type == "page" && include.DefaultNav { app.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + app.Id + "/page/" + include.Slug } if include.Type == "dashboard" && include.DefaultNav { app.DefaultNavUrl = setting.AppSubUrl + "/dashboard/db/" + include.Slug } } }
func (a *Article) Create() error { if a == nil { return apierror.NewServerError("article not instanced") } if a.Slug == "" { a.Slug = slug.Make(a.Title) } if bson.IsObjectIdHex(a.Slug) { return apierror.NewBadRequest("slug cannot be a ObjectId") } a.CreatedAt = time.Now() // To prevent duplicates on the slug, we'll retry the insert() up to 10 times originalSlug := a.Slug var err error for i := 0; i < 10; i++ { a.ID = bson.NewObjectId() err = Query().Insert(a) if err != nil { // In case of duplicate we'll add "-X" at the end of the slug, where X is // a number a.Slug = fmt.Sprintf("%s-%d", originalSlug, i) if mgo.IsDup(err) == false { return apierror.NewServerError(err.Error()) } } else { // everything went well return nil } } // after 10 try we just return an error return apierror.NewConflict(err.Error()) }
func (n *Ns1) ZoneMetrics(client *Client, mts []plugin.MetricType) ([]plugin.MetricType, error) { metrics := make([]plugin.MetricType, 0) conf := mts[0].Config().Table() zone, ok := conf["zone"] if !ok || zone.(ctypes.ConfigValueStr).Value == "" { LogError("zone missing from config.") return metrics, nil } zSlug := slug.Make(zone.(ctypes.ConfigValueStr).Value) qps, err := client.Qps(zone.(ctypes.ConfigValueStr).Value) if err != nil { return nil, err } metrics = append(metrics, plugin.MetricType{ Data_: qps.Qps, Namespace_: core.NewNamespace("raintank", "apps", "ns1", "zones", zSlug, "qps"), Timestamp_: time.Now(), Version_: mts[0].Version(), }) return metrics, nil }
func gitStats(accessToken string, mts []plugin.MetricType) ([]plugin.MetricType, error) { ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: accessToken}, ) tc := oauth2.NewClient(oauth2.NoContext, ts) client := github.NewClient(tc) collectionTime := time.Now() repos := make(map[string]map[string]map[string]int) users := make(map[string]map[string]int) userRepos := make(map[string]struct{}) authUser := "" useRepo := "" conf := mts[0].Config().Table() confUser, ok := conf["user"] if ok && confUser.(ctypes.ConfigValueStr).Value != "" { authUser = confUser.(ctypes.ConfigValueStr).Value } confRepo, ok := conf["repo"] if ok && confRepo.(ctypes.ConfigValueStr).Value != "" { useRepo = confRepo.(ctypes.ConfigValueStr).Value } metrics := make([]plugin.MetricType, 0) for _, m := range mts { ns := m.Namespace().Strings() fmt.Printf("getting %s\n", m.Namespace().String()) switch ns[3] { case "repo": user := ns[4] repo := ns[5] stat := ns[6] if user == "*" { //need to get user if authUser == "" { gitUser, _, err := client.Users.Get("") if err != nil { LogError("failed to get authenticated user.", err) return nil, err } stats, err := userStats(gitUser, client) if err != nil { LogError("failed to get stats from user object.", err) return nil, err } users[*gitUser.Login] = stats authUser = *gitUser.Login } user = authUser } if repo == "*" && useRepo == "" { // get list of all repos owned by the user. if _, ok := userRepos[user]; !ok { opt := &github.RepositoryListOptions{Type: "owner"} repoList, _, err := client.Repositories.List(user, opt) if err != nil { LogError("failed to get repos owned by user.", err) return nil, err } userRepos[user] = struct{}{} if _, ok := repos[user]; !ok { repos[user] = make(map[string]map[string]int) } for _, r := range repoList { stats, err := repoStats(r) if err != nil { LogError("failed to get stats from repo object.", err) return nil, err } repoSlug := slug.Make(*r.Name) repos[user][repoSlug] = stats } } for repo, stats := range repos[user] { mt := plugin.MetricType{ Data_: stats[stat], Namespace_: core.NewNamespace("raintank", "apps", "gitstats", "repo", user, repo, stat), Timestamp_: collectionTime, Version_: m.Version(), } metrics = append(metrics, mt) } } else { if repo == "*" { repo = useRepo } repoSlug := slug.Make(repo) if _, ok := repos[user]; !ok { repos[user] = make(map[string]map[string]int) } if _, ok := repos[user][repoSlug]; !ok { r, _, err := client.Repositories.Get(user, repo) if err != nil { LogError("failed to user repos.", err) return nil, err } stats, err := repoStats(r) if err != nil { LogError("failed to get stats from repo object.", err) return nil, err } repos[user][repoSlug] = stats } mt := plugin.MetricType{ Data_: repos[user][repo][stat], Namespace_: core.NewNamespace("raintank", "apps", "gitstats", "repo", user, repoSlug, stat), Timestamp_: collectionTime, Version_: m.Version(), } metrics = append(metrics, mt) } case "user": user := ns[4] stat := ns[5] if user == "*" { //need to get user if authUser == "" { gitUser, _, err := client.Users.Get("") if err != nil { LogError("failed to get authenticated user.", err) return nil, err } authUser = *gitUser.Login stats, err := userStats(gitUser, client) if err != nil { LogError("failed to get stats from user object", err) return nil, err } users[authUser] = stats } else { if _, ok := users[authUser]; !ok { gitUser, _, err := client.Users.Get(authUser) if err != nil { LogError("failed to get authenticated user.", err) return nil, err } stats, err := userStats(gitUser, client) if err != nil { LogError("failed to get stats from user object", err) return nil, err } users[authUser] = stats } } user = authUser } else { if _, ok := users[user]; !ok { fmt.Printf("getting stats for user %s\n", user) u, _, err := client.Users.Get(user) if err != nil { LogError("failed to lookup user.", err) return nil, err } stats, err := userStats(u, client) if err != nil { LogError("failed to get stats from user object.", err) return nil, err } users[user] = stats } } mt := plugin.MetricType{ Data_: users[user][stat], Namespace_: core.NewNamespace("raintank", "apps", "gitstats", "user", user, stat), Timestamp_: collectionTime, Version_: m.Version(), } metrics = append(metrics, mt) } } return metrics, nil }
// UpdateSlug updates the slug func (dash *Dashboard) UpdateSlug() { title := strings.ToLower(dash.Data.Get("title").MustString()) dash.Slug = slug.Make(title) }
func TestDashboardDataAccess(t *testing.T) { Convey("Testing DB", t, func() { InitTestDB(t) Convey("Given saved dashboard", func() { savedDash := insertTestDashboard("test dash 23", 1, "prod", "webapp") insertTestDashboard("test dash 45", 1, "prod") insertTestDashboard("test dash 67", 1, "prod", "webapp") Convey("Should return dashboard model", func() { So(savedDash.Title, ShouldEqual, "test dash 23") So(savedDash.Slug, ShouldEqual, "test-dash-23") So(savedDash.Id, ShouldNotEqual, 0) }) Convey("Should be able to get dashboard", func() { query := m.GetDashboardQuery{ Slug: "test-dash-23", OrgId: 1, } err := GetDashboard(&query) So(err, ShouldBeNil) So(query.Result.Title, ShouldEqual, "test dash 23") So(query.Result.Slug, ShouldEqual, "test-dash-23") }) Convey("Should be able to delete dashboard", func() { insertTestDashboard("delete me", 1, "delete this") dashboardSlug := slug.Make("delete me") err := DeleteDashboard(&m.DeleteDashboardCommand{ Slug: dashboardSlug, OrgId: 1, }) So(err, ShouldBeNil) }) Convey("Should return error if no dashboard is updated", func() { cmd := m.SaveDashboardCommand{ OrgId: 1, Overwrite: true, Dashboard: simplejson.NewFromAny(map[string]interface{}{ "id": float64(123412321), "title": "Expect error", "tags": []interface{}{}, }), } err := SaveDashboard(&cmd) So(err, ShouldNotBeNil) }) Convey("Should not be able to overwrite dashboard in another org", func() { query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1} GetDashboard(&query) cmd := m.SaveDashboardCommand{ OrgId: 2, Overwrite: true, Dashboard: simplejson.NewFromAny(map[string]interface{}{ "id": float64(query.Result.Id), "title": "Expect error", "tags": []interface{}{}, }), } err := SaveDashboard(&cmd) So(err, ShouldNotBeNil) }) Convey("Should be able to search for dashboard", func() { query := search.FindPersistedDashboardsQuery{ Title: "test dash 23", OrgId: 1, } err := SearchDashboards(&query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 1) hit := query.Result[0] So(len(hit.Tags), ShouldEqual, 2) }) Convey("Should be able to search for dashboard by dashboard ids", func() { Convey("should be able to find two dashboards by id", func() { query := search.FindPersistedDashboardsQuery{ DashboardIds: []int{1, 2}, OrgId: 1, } err := SearchDashboards(&query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 2) hit := query.Result[0] So(len(hit.Tags), ShouldEqual, 2) hit2 := query.Result[1] So(len(hit2.Tags), ShouldEqual, 1) }) Convey("DashboardIds that does not exists should not cause errors", func() { query := search.FindPersistedDashboardsQuery{ DashboardIds: []int{1000}, OrgId: 1, } err := SearchDashboards(&query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 0) }) }) Convey("Should not be able to save dashboard with same name", func() { cmd := m.SaveDashboardCommand{ OrgId: 1, Dashboard: simplejson.NewFromAny(map[string]interface{}{ "id": nil, "title": "test dash 23", "tags": []interface{}{}, }), } err := SaveDashboard(&cmd) So(err, ShouldNotBeNil) }) Convey("Should be able to get dashboard tags", func() { query := m.GetDashboardTagsQuery{OrgId: 1} err := GetDashboardTags(&query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 2) }) Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() { starredDash := insertTestDashboard("starred dash", 1) StarDashboard(&m.StarDashboardCommand{ DashboardId: starredDash.Id, UserId: 10, }) StarDashboard(&m.StarDashboardCommand{ DashboardId: savedDash.Id, UserId: 1, }) Convey("Should be able to search for starred dashboards", func() { query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true} err := SearchDashboards(&query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 1) So(query.Result[0].Title, ShouldEqual, "starred dash") }) }) }) }) }
// UpdateSlug updates the slug func (dash *Dashboard) UpdateSlug() { title := strings.ToLower(dash.Data["title"].(string)) dash.Slug = slug.Make(title) }
func (n *News) autoGenSlug() { n.Slug = slug.Make(n.Title) }
func slugify(str string) string { return slug.Make(str) }