mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Move library elements models to library elements service (#62507)
* Chore: Move library elements models to library elements service * Fix stat import * Fix faulty error assignment
This commit is contained in:
@@ -11,8 +11,8 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/kinds/librarypanel"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/libraryelements/model"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
@@ -29,7 +29,7 @@ SELECT DISTINCT
|
||||
, u1.email AS created_by_email
|
||||
, u2.login AS updated_by_name
|
||||
, u2.email AS updated_by_email
|
||||
, (SELECT COUNT(connection_id) FROM ` + models.LibraryElementConnectionTableName + ` WHERE element_id = le.id AND kind=1) AS connected_dashboards`
|
||||
, (SELECT COUNT(connection_id) FROM ` + model.LibraryElementConnectionTableName + ` WHERE element_id = le.id AND kind=1) AS connected_dashboards`
|
||||
)
|
||||
|
||||
// redundant SELECT to trick mysql's optimizer
|
||||
@@ -52,26 +52,26 @@ LEFT JOIN ` + user + ` AS u2 ON le.updated_by = u2.id
|
||||
return userJoin
|
||||
}
|
||||
|
||||
func syncFieldsWithModel(libraryElement *LibraryElement) error {
|
||||
var model map[string]interface{}
|
||||
if err := json.Unmarshal(libraryElement.Model, &model); err != nil {
|
||||
func syncFieldsWithModel(libraryElement *model.LibraryElement) error {
|
||||
var modelLibraryElement map[string]interface{}
|
||||
if err := json.Unmarshal(libraryElement.Model, &modelLibraryElement); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if models.LibraryElementKind(libraryElement.Kind) == models.VariableElement {
|
||||
model["name"] = libraryElement.Name
|
||||
if model.LibraryElementKind(libraryElement.Kind) == model.VariableElement {
|
||||
modelLibraryElement["name"] = libraryElement.Name
|
||||
}
|
||||
if model["type"] != nil {
|
||||
libraryElement.Type = model["type"].(string)
|
||||
if modelLibraryElement["type"] != nil {
|
||||
libraryElement.Type = modelLibraryElement["type"].(string)
|
||||
} else {
|
||||
model["type"] = libraryElement.Type
|
||||
modelLibraryElement["type"] = libraryElement.Type
|
||||
}
|
||||
if model["description"] != nil {
|
||||
libraryElement.Description = model["description"].(string)
|
||||
if modelLibraryElement["description"] != nil {
|
||||
libraryElement.Description = modelLibraryElement["description"].(string)
|
||||
} else {
|
||||
model["description"] = libraryElement.Description
|
||||
modelLibraryElement["description"] = libraryElement.Description
|
||||
}
|
||||
syncedModel, err := json.Marshal(&model)
|
||||
syncedModel, err := json.Marshal(&modelLibraryElement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -81,8 +81,8 @@ func syncFieldsWithModel(libraryElement *LibraryElement) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLibraryElement(dialect migrator.Dialect, session *db.Session, uid string, orgID int64) (LibraryElementWithMeta, error) {
|
||||
elements := make([]LibraryElementWithMeta, 0)
|
||||
func getLibraryElement(dialect migrator.Dialect, session *db.Session, uid string, orgID int64) (model.LibraryElementWithMeta, error) {
|
||||
elements := make([]model.LibraryElementWithMeta, 0)
|
||||
sql := selectLibraryElementDTOWithMeta +
|
||||
", coalesce(dashboard.title, 'General') AS folder_name" +
|
||||
", coalesce(dashboard.uid, '') AS folder_uid" +
|
||||
@@ -92,34 +92,34 @@ func getLibraryElement(dialect migrator.Dialect, session *db.Session, uid string
|
||||
sess := session.SQL(sql, uid, orgID)
|
||||
err := sess.Find(&elements)
|
||||
if err != nil {
|
||||
return LibraryElementWithMeta{}, err
|
||||
return model.LibraryElementWithMeta{}, err
|
||||
}
|
||||
if len(elements) == 0 {
|
||||
return LibraryElementWithMeta{}, ErrLibraryElementNotFound
|
||||
return model.LibraryElementWithMeta{}, model.ErrLibraryElementNotFound
|
||||
}
|
||||
if len(elements) > 1 {
|
||||
return LibraryElementWithMeta{}, fmt.Errorf("found %d elements, while expecting at most one", len(elements))
|
||||
return model.LibraryElementWithMeta{}, fmt.Errorf("found %d elements, while expecting at most one", len(elements))
|
||||
}
|
||||
|
||||
return elements[0], nil
|
||||
}
|
||||
|
||||
// createLibraryElement adds a library element.
|
||||
func (l *LibraryElementService) createLibraryElement(c context.Context, signedInUser *user.SignedInUser, cmd CreateLibraryElementCommand) (LibraryElementDTO, error) {
|
||||
func (l *LibraryElementService) createLibraryElement(c context.Context, signedInUser *user.SignedInUser, cmd model.CreateLibraryElementCommand) (model.LibraryElementDTO, error) {
|
||||
if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
|
||||
return LibraryElementDTO{}, err
|
||||
return model.LibraryElementDTO{}, err
|
||||
}
|
||||
createUID := cmd.UID
|
||||
if len(createUID) == 0 {
|
||||
createUID = util.GenerateShortUID()
|
||||
} else {
|
||||
if !util.IsValidShortUID(createUID) {
|
||||
return LibraryElementDTO{}, errLibraryElementInvalidUID
|
||||
return model.LibraryElementDTO{}, model.ErrLibraryElementInvalidUID
|
||||
} else if util.IsShortUIDTooLong(createUID) {
|
||||
return LibraryElementDTO{}, errLibraryElementUIDTooLong
|
||||
return model.LibraryElementDTO{}, model.ErrLibraryElementUIDTooLong
|
||||
}
|
||||
}
|
||||
element := LibraryElement{
|
||||
element := model.LibraryElement{
|
||||
OrgID: signedInUser.OrgID,
|
||||
FolderID: cmd.FolderID,
|
||||
UID: createUID,
|
||||
@@ -136,7 +136,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
|
||||
}
|
||||
|
||||
if err := syncFieldsWithModel(&element); err != nil {
|
||||
return LibraryElementDTO{}, err
|
||||
return model.LibraryElementDTO{}, err
|
||||
}
|
||||
|
||||
err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
|
||||
@@ -145,14 +145,14 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
|
||||
}
|
||||
if _, err := session.Insert(&element); err != nil {
|
||||
if l.SQLStore.GetDialect().IsUniqueConstraintViolation(err) {
|
||||
return errLibraryElementAlreadyExists
|
||||
return model.ErrLibraryElementAlreadyExists
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
dto := LibraryElementDTO{
|
||||
dto := model.LibraryElementDTO{
|
||||
ID: element.ID,
|
||||
OrgID: element.OrgID,
|
||||
FolderID: element.FolderID,
|
||||
@@ -163,7 +163,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
|
||||
Description: element.Description,
|
||||
Model: element.Model,
|
||||
Version: element.Version,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
Meta: model.LibraryElementDTOMeta{
|
||||
ConnectedDashboards: 0,
|
||||
Created: element.Created,
|
||||
Updated: element.Updated,
|
||||
@@ -207,7 +207,7 @@ func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedIn
|
||||
if err := session.SQL(sql, element.ID).Find(&connectionIDs); err != nil {
|
||||
return err
|
||||
} else if len(connectionIDs) > 0 {
|
||||
return errLibraryElementHasConnections
|
||||
return model.ErrLibraryElementHasConnections
|
||||
}
|
||||
|
||||
result, err := session.Exec("DELETE FROM library_element WHERE id=?", element.ID)
|
||||
@@ -217,7 +217,7 @@ func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedIn
|
||||
if rowsAffected, err := result.RowsAffected(); err != nil {
|
||||
return err
|
||||
} else if rowsAffected != 1 {
|
||||
return ErrLibraryElementNotFound
|
||||
return model.ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
elementID = element.ID
|
||||
@@ -227,8 +227,8 @@ func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedIn
|
||||
}
|
||||
|
||||
// getLibraryElements gets a Library Element where param == value
|
||||
func getLibraryElements(c context.Context, store db.DB, cfg *setting.Cfg, signedInUser *user.SignedInUser, params []Pair) ([]LibraryElementDTO, error) {
|
||||
libraryElements := make([]LibraryElementWithMeta, 0)
|
||||
func getLibraryElements(c context.Context, store db.DB, cfg *setting.Cfg, signedInUser *user.SignedInUser, params []Pair) ([]model.LibraryElementDTO, error) {
|
||||
libraryElements := make([]model.LibraryElementWithMeta, 0)
|
||||
err := store.WithDbSession(c, func(session *db.Session) error {
|
||||
builder := db.NewSqlBuilder(cfg, store.GetDialect())
|
||||
builder.Write(selectLibraryElementDTOWithMeta)
|
||||
@@ -251,18 +251,18 @@ func getLibraryElements(c context.Context, store db.DB, cfg *setting.Cfg, signed
|
||||
return err
|
||||
}
|
||||
if len(libraryElements) == 0 {
|
||||
return ErrLibraryElementNotFound
|
||||
return model.ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return []LibraryElementDTO{}, err
|
||||
return []model.LibraryElementDTO{}, err
|
||||
}
|
||||
|
||||
leDtos := make([]LibraryElementDTO, len(libraryElements))
|
||||
leDtos := make([]model.LibraryElementDTO, len(libraryElements))
|
||||
for i, libraryElement := range libraryElements {
|
||||
leDtos[i] = LibraryElementDTO{
|
||||
leDtos[i] = model.LibraryElementDTO{
|
||||
ID: libraryElement.ID,
|
||||
OrgID: libraryElement.OrgID,
|
||||
FolderID: libraryElement.FolderID,
|
||||
@@ -274,7 +274,7 @@ func getLibraryElements(c context.Context, store db.DB, cfg *setting.Cfg, signed
|
||||
Description: libraryElement.Description,
|
||||
Model: libraryElement.Model,
|
||||
Version: libraryElement.Version,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
Meta: model.LibraryElementDTOMeta{
|
||||
FolderName: libraryElement.FolderName,
|
||||
FolderUID: libraryElement.FolderUID,
|
||||
ConnectedDashboards: libraryElement.ConnectedDashboards,
|
||||
@@ -298,40 +298,40 @@ func getLibraryElements(c context.Context, store db.DB, cfg *setting.Cfg, signed
|
||||
}
|
||||
|
||||
// getLibraryElementByUid gets a Library Element by uid.
|
||||
func (l *LibraryElementService) getLibraryElementByUid(c context.Context, signedInUser *user.SignedInUser, UID string) (LibraryElementDTO, error) {
|
||||
func (l *LibraryElementService) getLibraryElementByUid(c context.Context, signedInUser *user.SignedInUser, UID string) (model.LibraryElementDTO, error) {
|
||||
libraryElements, err := getLibraryElements(c, l.SQLStore, l.Cfg, signedInUser, []Pair{{key: "org_id", value: signedInUser.OrgID}, {key: "uid", value: UID}})
|
||||
if err != nil {
|
||||
return LibraryElementDTO{}, err
|
||||
return model.LibraryElementDTO{}, err
|
||||
}
|
||||
if len(libraryElements) > 1 {
|
||||
return LibraryElementDTO{}, fmt.Errorf("found %d elements, while expecting at most one", len(libraryElements))
|
||||
return model.LibraryElementDTO{}, fmt.Errorf("found %d elements, while expecting at most one", len(libraryElements))
|
||||
}
|
||||
|
||||
return libraryElements[0], nil
|
||||
}
|
||||
|
||||
// getLibraryElementByName gets a Library Element by name.
|
||||
func (l *LibraryElementService) getLibraryElementsByName(c context.Context, signedInUser *user.SignedInUser, name string) ([]LibraryElementDTO, error) {
|
||||
func (l *LibraryElementService) getLibraryElementsByName(c context.Context, signedInUser *user.SignedInUser, name string) ([]model.LibraryElementDTO, error) {
|
||||
return getLibraryElements(c, l.SQLStore, l.Cfg, signedInUser, []Pair{{"org_id", signedInUser.OrgID}, {"name", name}})
|
||||
}
|
||||
|
||||
// getAllLibraryElements gets all Library Elements.
|
||||
func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedInUser *user.SignedInUser, query searchLibraryElementsQuery) (LibraryElementSearchResult, error) {
|
||||
elements := make([]LibraryElementWithMeta, 0)
|
||||
result := LibraryElementSearchResult{}
|
||||
if query.perPage <= 0 {
|
||||
query.perPage = 100
|
||||
func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedInUser *user.SignedInUser, query model.SearchLibraryElementsQuery) (model.LibraryElementSearchResult, error) {
|
||||
elements := make([]model.LibraryElementWithMeta, 0)
|
||||
result := model.LibraryElementSearchResult{}
|
||||
if query.PerPage <= 0 {
|
||||
query.PerPage = 100
|
||||
}
|
||||
if query.page <= 0 {
|
||||
query.page = 1
|
||||
if query.Page <= 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
var typeFilter []string
|
||||
if len(strings.TrimSpace(query.typeFilter)) > 0 {
|
||||
typeFilter = strings.Split(query.typeFilter, ",")
|
||||
if len(strings.TrimSpace(query.TypeFilter)) > 0 {
|
||||
typeFilter = strings.Split(query.TypeFilter, ",")
|
||||
}
|
||||
folderFilter := parseFolderFilter(query)
|
||||
if folderFilter.parseError != nil {
|
||||
return LibraryElementSearchResult{}, folderFilter.parseError
|
||||
return model.LibraryElementSearchResult{}, folderFilter.parseError
|
||||
}
|
||||
err := l.SQLStore.WithDbSession(c, func(session *db.Session) error {
|
||||
builder := db.NewSqlBuilder(l.Cfg, l.SQLStore.GetDialect())
|
||||
@@ -363,7 +363,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
if signedInUser.OrgRole != org.RoleAdmin {
|
||||
builder.WriteDashboardPermissionFilter(signedInUser, dashboards.PERMISSION_VIEW)
|
||||
}
|
||||
if query.sortDirection == search.SortAlphaDesc.Name {
|
||||
if query.SortDirection == search.SortAlphaDesc.Name {
|
||||
builder.Write(" ORDER BY 1 DESC")
|
||||
} else {
|
||||
builder.Write(" ORDER BY 1 ASC")
|
||||
@@ -373,9 +373,9 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
return err
|
||||
}
|
||||
|
||||
retDTOs := make([]LibraryElementDTO, 0)
|
||||
retDTOs := make([]model.LibraryElementDTO, 0)
|
||||
for _, element := range elements {
|
||||
retDTOs = append(retDTOs, LibraryElementDTO{
|
||||
retDTOs = append(retDTOs, model.LibraryElementDTO{
|
||||
ID: element.ID,
|
||||
OrgID: element.OrgID,
|
||||
FolderID: element.FolderID,
|
||||
@@ -387,7 +387,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
Description: element.Description,
|
||||
Model: element.Model,
|
||||
Version: element.Version,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
Meta: model.LibraryElementDTOMeta{
|
||||
FolderName: element.FolderName,
|
||||
FolderUID: element.FolderUID,
|
||||
ConnectedDashboards: element.ConnectedDashboards,
|
||||
@@ -407,7 +407,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
})
|
||||
}
|
||||
|
||||
var libraryElements []LibraryElement
|
||||
var libraryElements []model.LibraryElement
|
||||
countBuilder := db.SQLBuilder{}
|
||||
countBuilder.Write("SELECT * FROM library_element AS le")
|
||||
countBuilder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id")
|
||||
@@ -423,11 +423,11 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
return err
|
||||
}
|
||||
|
||||
result = LibraryElementSearchResult{
|
||||
result = model.LibraryElementSearchResult{
|
||||
TotalCount: int64(len(libraryElements)),
|
||||
Elements: retDTOs,
|
||||
Page: query.page,
|
||||
PerPage: query.perPage,
|
||||
Page: query.Page,
|
||||
PerPage: query.PerPage,
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -436,7 +436,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, elementToPatch *LibraryElement, fromFolderID int64, toFolderID int64, user *user.SignedInUser) error {
|
||||
func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, elementToPatch *model.LibraryElement, fromFolderID int64, toFolderID int64, user *user.SignedInUser) error {
|
||||
// FolderID was not provided in the PATCH request
|
||||
if toFolderID == -1 {
|
||||
toFolderID = fromFolderID
|
||||
@@ -460,10 +460,10 @@ func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, eleme
|
||||
}
|
||||
|
||||
// patchLibraryElement updates a Library Element.
|
||||
func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInUser *user.SignedInUser, cmd PatchLibraryElementCommand, uid string) (LibraryElementDTO, error) {
|
||||
var dto LibraryElementDTO
|
||||
func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInUser *user.SignedInUser, cmd model.PatchLibraryElementCommand, uid string) (model.LibraryElementDTO, error) {
|
||||
var dto model.LibraryElementDTO
|
||||
if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
|
||||
return LibraryElementDTO{}, err
|
||||
return model.LibraryElementDTO{}, err
|
||||
}
|
||||
err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
|
||||
elementInDB, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID)
|
||||
@@ -471,25 +471,25 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
|
||||
return err
|
||||
}
|
||||
if elementInDB.Version != cmd.Version {
|
||||
return errLibraryElementVersionMismatch
|
||||
return model.ErrLibraryElementVersionMismatch
|
||||
}
|
||||
updateUID := cmd.UID
|
||||
if len(updateUID) == 0 {
|
||||
updateUID = uid
|
||||
} else if updateUID != uid {
|
||||
if !util.IsValidShortUID(updateUID) {
|
||||
return errLibraryElementInvalidUID
|
||||
return model.ErrLibraryElementInvalidUID
|
||||
} else if util.IsShortUIDTooLong(updateUID) {
|
||||
return errLibraryElementUIDTooLong
|
||||
return model.ErrLibraryElementUIDTooLong
|
||||
}
|
||||
|
||||
_, err := getLibraryElement(l.SQLStore.GetDialect(), session, updateUID, signedInUser.OrgID)
|
||||
if !errors.Is(err, ErrLibraryElementNotFound) {
|
||||
return errLibraryElementAlreadyExists
|
||||
if !errors.Is(err, model.ErrLibraryElementNotFound) {
|
||||
return model.ErrLibraryElementAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
var libraryElement = LibraryElement{
|
||||
var libraryElement = model.LibraryElement{
|
||||
ID: elementInDB.ID,
|
||||
OrgID: signedInUser.OrgID,
|
||||
FolderID: cmd.FolderID,
|
||||
@@ -520,14 +520,14 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
|
||||
}
|
||||
if rowsAffected, err := session.ID(elementInDB.ID).Update(&libraryElement); err != nil {
|
||||
if l.SQLStore.GetDialect().IsUniqueConstraintViolation(err) {
|
||||
return errLibraryElementAlreadyExists
|
||||
return model.ErrLibraryElementAlreadyExists
|
||||
}
|
||||
return err
|
||||
} else if rowsAffected != 1 {
|
||||
return ErrLibraryElementNotFound
|
||||
return model.ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
dto = LibraryElementDTO{
|
||||
dto = model.LibraryElementDTO{
|
||||
ID: libraryElement.ID,
|
||||
OrgID: libraryElement.OrgID,
|
||||
FolderID: libraryElement.FolderID,
|
||||
@@ -538,7 +538,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
|
||||
Description: libraryElement.Description,
|
||||
Model: libraryElement.Model,
|
||||
Version: libraryElement.Version,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
Meta: model.LibraryElementDTOMeta{
|
||||
ConnectedDashboards: elementInDB.ConnectedDashboards,
|
||||
Created: libraryElement.Created,
|
||||
Updated: libraryElement.Updated,
|
||||
@@ -561,17 +561,17 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
|
||||
}
|
||||
|
||||
// getConnections gets all connections for a Library Element.
|
||||
func (l *LibraryElementService) getConnections(c context.Context, signedInUser *user.SignedInUser, uid string) ([]LibraryElementConnectionDTO, error) {
|
||||
connections := make([]LibraryElementConnectionDTO, 0)
|
||||
func (l *LibraryElementService) getConnections(c context.Context, signedInUser *user.SignedInUser, uid string) ([]model.LibraryElementConnectionDTO, error) {
|
||||
connections := make([]model.LibraryElementConnectionDTO, 0)
|
||||
err := l.SQLStore.WithDbSession(c, func(session *db.Session) error {
|
||||
element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var libraryElementConnections []libraryElementConnectionWithMeta
|
||||
var libraryElementConnections []model.LibraryElementConnectionWithMeta
|
||||
builder := db.NewSqlBuilder(l.Cfg, l.SQLStore.GetDialect())
|
||||
builder.Write("SELECT lec.*, u1.login AS created_by_name, u1.email AS created_by_email, dashboard.uid AS connection_uid")
|
||||
builder.Write(" FROM " + models.LibraryElementConnectionTableName + " AS lec")
|
||||
builder.Write(" FROM " + model.LibraryElementConnectionTableName + " AS lec")
|
||||
builder.Write(" LEFT JOIN " + l.SQLStore.GetDialect().Quote("user") + " AS u1 ON lec.created_by = u1.id")
|
||||
builder.Write(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.id")
|
||||
builder.Write(` WHERE lec.element_id=?`, element.ID)
|
||||
@@ -583,7 +583,7 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser *
|
||||
}
|
||||
|
||||
for _, connection := range libraryElementConnections {
|
||||
connections = append(connections, LibraryElementConnectionDTO{
|
||||
connections = append(connections, model.LibraryElementConnectionDTO{
|
||||
ID: connection.ID,
|
||||
Kind: connection.Kind,
|
||||
ElementID: connection.ElementID,
|
||||
@@ -605,16 +605,16 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser *
|
||||
}
|
||||
|
||||
// getElementsForDashboardID gets all elements for a specific dashboard
|
||||
func (l *LibraryElementService) getElementsForDashboardID(c context.Context, dashboardID int64) (map[string]LibraryElementDTO, error) {
|
||||
libraryElementMap := make(map[string]LibraryElementDTO)
|
||||
func (l *LibraryElementService) getElementsForDashboardID(c context.Context, dashboardID int64) (map[string]model.LibraryElementDTO, error) {
|
||||
libraryElementMap := make(map[string]model.LibraryElementDTO)
|
||||
err := l.SQLStore.WithDbSession(c, func(session *db.Session) error {
|
||||
var libraryElements []LibraryElementWithMeta
|
||||
var libraryElements []model.LibraryElementWithMeta
|
||||
sql := selectLibraryElementDTOWithMeta +
|
||||
", coalesce(dashboard.title, 'General') AS folder_name" +
|
||||
", coalesce(dashboard.uid, '') AS folder_uid" +
|
||||
getFromLibraryElementDTOWithMeta(l.SQLStore.GetDialect()) +
|
||||
" LEFT JOIN dashboard AS dashboard ON dashboard.id = le.folder_id" +
|
||||
" INNER JOIN " + models.LibraryElementConnectionTableName + " AS lce ON lce.element_id = le.id AND lce.kind=1 AND lce.connection_id=?"
|
||||
" INNER JOIN " + model.LibraryElementConnectionTableName + " AS lce ON lce.element_id = le.id AND lce.kind=1 AND lce.connection_id=?"
|
||||
sess := session.SQL(sql, dashboardID)
|
||||
err := sess.Find(&libraryElements)
|
||||
if err != nil {
|
||||
@@ -622,7 +622,7 @@ func (l *LibraryElementService) getElementsForDashboardID(c context.Context, das
|
||||
}
|
||||
|
||||
for _, element := range libraryElements {
|
||||
libraryElementMap[element.UID] = LibraryElementDTO{
|
||||
libraryElementMap[element.UID] = model.LibraryElementDTO{
|
||||
ID: element.ID,
|
||||
OrgID: element.OrgID,
|
||||
FolderID: element.FolderID,
|
||||
@@ -633,7 +633,7 @@ func (l *LibraryElementService) getElementsForDashboardID(c context.Context, das
|
||||
Description: element.Description,
|
||||
Model: element.Model,
|
||||
Version: element.Version,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
Meta: model.LibraryElementDTOMeta{
|
||||
FolderName: element.FolderName,
|
||||
FolderUID: element.FolderUID,
|
||||
ConnectedDashboards: element.ConnectedDashboards,
|
||||
@@ -662,7 +662,7 @@ func (l *LibraryElementService) getElementsForDashboardID(c context.Context, das
|
||||
// connectElementsToDashboardID adds connections for all elements Library Elements in a Dashboard.
|
||||
func (l *LibraryElementService) connectElementsToDashboardID(c context.Context, signedInUser *user.SignedInUser, elementUIDs []string, dashboardID int64) error {
|
||||
err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
|
||||
_, err := session.Exec("DELETE FROM "+models.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
|
||||
_, err := session.Exec("DELETE FROM "+model.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -675,7 +675,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c context.Context,
|
||||
return err
|
||||
}
|
||||
|
||||
connection := libraryElementConnection{
|
||||
connection := model.LibraryElementConnection{
|
||||
ElementID: element.ID,
|
||||
Kind: 1,
|
||||
ConnectionID: dashboardID,
|
||||
@@ -698,7 +698,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c context.Context,
|
||||
// disconnectElementsFromDashboardID deletes connections for all Library Elements in a Dashboard.
|
||||
func (l *LibraryElementService) disconnectElementsFromDashboardID(c context.Context, dashboardID int64) error {
|
||||
return l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
|
||||
_, err := session.Exec("DELETE FROM "+models.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
|
||||
_, err := session.Exec("DELETE FROM "+model.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -734,14 +734,14 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
|
||||
ConnectionID int64 `xorm:"connection_id"`
|
||||
}
|
||||
sql := "SELECT lec.connection_id FROM library_element AS le"
|
||||
sql += " INNER JOIN " + models.LibraryElementConnectionTableName + " AS lec on le.id = lec.element_id"
|
||||
sql += " INNER JOIN " + model.LibraryElementConnectionTableName + " AS lec on le.id = lec.element_id"
|
||||
sql += " WHERE le.folder_id=? AND le.org_id=?"
|
||||
err = session.SQL(sql, folderID, signedInUser.OrgID).Find(&connectionIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(connectionIDs) > 0 {
|
||||
return ErrFolderHasConnectedLibraryElements
|
||||
return model.ErrFolderHasConnectedLibraryElements
|
||||
}
|
||||
|
||||
var elementIDs []struct {
|
||||
@@ -752,7 +752,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
|
||||
return err
|
||||
}
|
||||
for _, elementID := range elementIDs {
|
||||
_, err := session.Exec("DELETE FROM "+models.LibraryElementConnectionTableName+" WHERE element_id=?", elementID.ID)
|
||||
_, err := session.Exec("DELETE FROM "+model.LibraryElementConnectionTableName+" WHERE element_id=?", elementID.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user