-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add endpoints for repo details
- Loading branch information
Showing
6 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
"github.com/diggerhq/digger/backend/middleware" | ||
"github.com/diggerhq/digger/backend/models" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func GetDashboardStatusApi(c *gin.Context) { | ||
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) | ||
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) | ||
|
||
var org models.Organisation | ||
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) | ||
} else { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) | ||
} | ||
return | ||
} | ||
|
||
response := make(map[string]interface{}) | ||
|
||
c.JSON(http.StatusOK, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
"github.com/diggerhq/digger/backend/middleware" | ||
"github.com/diggerhq/digger/backend/models" | ||
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func GetJobsForRepoApi(c *gin.Context) { | ||
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) | ||
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) | ||
repoId := c.Param("repo_id") | ||
|
||
if repoId == "" { | ||
log.Printf("missing parameter: repo_full_name") | ||
c.String(http.StatusBadRequest, "missing parameter: repo_full_name") | ||
return | ||
} | ||
|
||
var org models.Organisation | ||
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
log.Printf("could not find organisation: %v err: %v", organisationId, err) | ||
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) | ||
} else { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) | ||
} | ||
return | ||
} | ||
|
||
repo, err := models.DB.GetRepoById(org.ID, repoId) | ||
if err != nil { | ||
log.Printf("could not fetch repo details %v", err) | ||
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching jobs from database") | ||
return | ||
} | ||
|
||
jobsRes, err := models.DB.GetJobsByRepoName(org.ID, repo.RepoFullName) | ||
if err != nil { | ||
log.Printf("could not fetch job details") | ||
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching jobs from database") | ||
return | ||
} | ||
|
||
// update the values of "status" accordingly | ||
for i, j := range jobsRes { | ||
statusInt, err := strconv.Atoi(j.Status) | ||
if err != nil { | ||
log.Printf("could not convert status to string: job id: %v status: %v", j.ID, j.Status) | ||
continue | ||
} | ||
statusI := orchestrator_scheduler.DiggerJobStatus(statusInt) | ||
jobsRes[i].Status = statusI.ToString() | ||
} | ||
|
||
response := make(map[string]interface{}) | ||
response["repo"] = repo | ||
response["jobs"] = jobsRes | ||
|
||
c.JSON(http.StatusOK, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type JobQueryResult struct { | ||
ID uint `gorm:"column:id"` | ||
CreatedAt time.Time `gorm:"column:created_at"` | ||
UpdatedAt time.Time `gorm:"column:updated_at"` | ||
DeletedAt *time.Time `gorm:"column:deleted_at"` | ||
DiggerJobID string `gorm:"column:digger_job_id"` | ||
Status string `gorm:"column:status"` | ||
WorkflowRunURL string `gorm:"column:workflow_run_url"` | ||
WorkflowFile string `gorm:"column:workflow_file"` | ||
TerraformOutput string `gorm:"column:terraform_output"` | ||
PRNumber int `gorm:"column:pr_number"` | ||
RepoFullName string `gorm:"column:repo_full_name"` | ||
BranchName string `gorm:"column:branch_name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/diggerhq/digger/libs/scheduler" | ||
"time" | ||
) | ||
|
||
func (db *Database) GetRepoCount(orgID uint) (int64, error) { | ||
var count int64 | ||
|
||
thirtyDaysAgo := time.Now().AddDate(0, 0, -30) | ||
|
||
result := db.GormDB.Model(&Repo{}). | ||
Where("organisation_id = ? AND created_at >= ?", orgID, thirtyDaysAgo). | ||
Count(&count) | ||
|
||
if result.Error != nil { | ||
return 0, result.Error | ||
} | ||
|
||
return count, nil | ||
} | ||
|
||
func (db *Database) GetJobsCountThisMonth(orgID uint) (int64, error) { | ||
var count int64 | ||
|
||
thirtyDaysAgo := time.Now().AddDate(0, 0, -30) | ||
|
||
result := db.GormDB.Model(DiggerJob{}). | ||
Joins("JOIN digger_batches ON digger_jobs.batch_id = digger_batches.id"). | ||
Joins("JOIN github_app_installation_links ON digger_batches.github_installation_id = github_app_installation_links.github_installation_id"). | ||
Joins("JOIN organisations ON github_app_installation_links.organisation_id = organisations.id"). | ||
Where("digger_jobs.created_at >= ? AND organisations.id = ?", thirtyDaysAgo, orgID). | ||
Count(&count) | ||
|
||
if result.Error != nil { | ||
return 0, result.Error | ||
} | ||
|
||
return count, nil | ||
} | ||
|
||
func (db *Database) GetSuccessfulJobsCountThisMonth(orgID uint) (int64, error) { | ||
var count int64 | ||
|
||
thirtyDaysAgo := time.Now().AddDate(0, 0, -30) | ||
|
||
result := db.GormDB.Model(DiggerJob{}). | ||
Joins("JOIN digger_batches ON digger_jobs.batch_id = digger_batches.id"). | ||
Joins("JOIN github_app_installation_links ON digger_batches.github_installation_id = github_app_installation_links.github_installation_id"). | ||
Joins("JOIN organisations ON github_app_installation_links.organisation_id = organisations.id"). | ||
Where("digger_jobs.created_at >= ? AND organisations.id = ?", thirtyDaysAgo, orgID). | ||
Where("digger_jobs.status = ?", scheduler.DiggerJobSucceeded). | ||
Count(&count) | ||
|
||
if result.Error != nil { | ||
return 0, result.Error | ||
} | ||
|
||
return count, nil | ||
} |