Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add data source sync method #10

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ const docTemplate = `{
}
}
},
"/datasources/sync/{name}": {
"post": {
"description": "Synchronize data source schemas",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"data_source"
],
"summary": "Sync Data Source",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/models.HTTPError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.HTTPError"
}
}
}
}
},
"/datasources/{name}": {
"get": {
"description": "Retrieve data source configuration with the specified name",
Expand Down Expand Up @@ -191,7 +226,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
"$ref": "#/definitions/models.DataSource"
}
},
"400": {
Expand Down Expand Up @@ -233,7 +268,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
"$ref": "#/definitions/models.DataSource"
}
},
"400": {
Expand Down
39 changes: 37 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@
}
}
},
"/datasources/sync/{name}": {
"post": {
"description": "Synchronize data source schemas",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"data_source"
],
"summary": "Sync Data Source",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/models.HTTPError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.HTTPError"
}
}
}
}
},
"/datasources/{name}": {
"get": {
"description": "Retrieve data source configuration with the specified name",
Expand Down Expand Up @@ -182,7 +217,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
"$ref": "#/definitions/models.DataSource"
}
},
"400": {
Expand Down Expand Up @@ -224,7 +259,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.HTTPSuccess"
"$ref": "#/definitions/models.DataSource"
}
},
"400": {
Expand Down
27 changes: 25 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/models.HTTPSuccess'
$ref: '#/definitions/models.DataSource'
"400":
description: Bad Request
schema:
Expand Down Expand Up @@ -234,7 +234,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/models.HTTPSuccess'
$ref: '#/definitions/models.DataSource'
"400":
description: Bad Request
schema:
Expand Down Expand Up @@ -271,6 +271,29 @@ paths:
summary: List Data Sources
tags:
- data_source
/datasources/sync/{name}:
post:
consumes:
- application/json
description: Synchronize data source schemas
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.HTTPSuccess'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/models.HTTPError'
"404":
description: Not Found
schema:
$ref: '#/definitions/models.HTTPError'
summary: Sync Data Source
tags:
- data_source
/prompts/generate:
post:
consumes:
Expand Down
58 changes: 58 additions & 0 deletions internal/controllers/ds.controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,63 @@ func (self *DataSourceController) CreateDataSource(c *gin.Context) {

// @BasePath /

// PingExample godoc
// @Summary Sync Data Source
// @Schemes
// @Description Synchronize data source schemas
// @Tags data_source
// @Accept json
// @Produce json
// @Success 200 {object} models.HTTPSuccess
// @Failure 401 {object} models.HTTPError
// @Failure 404 {object} models.HTTPError
// @Router /datasources/sync/{name} [post]
func (self *DataSourceController) SyncDataSource(c *gin.Context) {
name := c.Param("name")

_ownerId, _ := c.Get("owner_id")
ownerId, ok := _ownerId.(string)
if !ok {
c.JSON(http.StatusInternalServerError, models.HTTPError{
Error: "internal_server_error",
Description: "Failed to read user identity",
})
}

_sub, _ := c.Get("sub")
sub, ok := _sub.(string)
if !ok {
c.JSON(http.StatusInternalServerError, models.HTTPError{
Error: "internal_server_error",
Description: "Failed to read user identity",
})
}

dataSource, err := self.DataSourceService.GetByName(name, ownerId, false)
if err != nil {
logger.Error.Println(fmt.Printf("[%s][%s] Couldn't find data: %v\n", ownerId, sub, err))
c.JSON(http.StatusNotFound, models.HTTPError{
Error: "not_found",
Description: "No data source found for your organization",
})
return
}

err = self.DataSourceService.Sync(dataSource.ID, dataSource.Type)
if err != nil {
logger.Error.Println(fmt.Printf("[%s][%s] Couldn't find data: %v\n", ownerId, sub, err))
c.JSON(http.StatusNotFound, models.HTTPError{
Error: "not_found",
Description: "Error while syncing data source",
})
return
}

c.JSON(http.StatusOK, models.HTTPSuccess{Message: "Data Source synced successfully"})
}

// @BasePath /

// PingExample godoc
// @Summary Modify Data Source
// @Schemes
Expand Down Expand Up @@ -322,6 +379,7 @@ func (self *DataSourceController) RegisterDataSourceRoutes(rg *gin.RouterGroup)
dataSourceRoute := rg.Group("datasources")
dataSourceRoute.GET("/:name", self.GetDataSourceByName)
dataSourceRoute.GET("/all", self.GetAllDataSource)
dataSourceRoute.POST("/sync/:name", self.SyncDataSource)
dataSourceRoute.POST("", self.CreateDataSource)
dataSourceRoute.PUT("/:name", self.UpdateDataSourceByName)
dataSourceRoute.DELETE("/:name", self.DeleteDataSourceByName)
Expand Down
Loading