Пример #1
0
func (p Paginator) PaginateJobBuilds(jobName string, startingJobBuildID int, resultsGreaterThanStartingID bool) ([]db.Build, pagination.PaginationData, error) {
	var paginationData pagination.PaginationData

	maxID, _ := p.PaginatorDB.GetJobBuildsMaxID(jobName)

	if startingJobBuildID == 0 && !resultsGreaterThanStartingID {
		startingJobBuildID = maxID
	}

	builds, moreResultsInGivenDirection, err := p.PaginatorDB.GetJobBuildsCursor(jobName, startingJobBuildID, resultsGreaterThanStartingID, 100)
	if err != nil {
		return []db.Build{}, pagination.PaginationData{}, err
	}

	if len(builds) > 0 {
		paginationData = pagination.NewPaginationData(
			resultsGreaterThanStartingID,
			moreResultsInGivenDirection,
			maxID,
			builds[0].ID,
			builds[len(builds)-1].ID,
		)
	} else {
		paginationData = pagination.PaginationData{}
	}

	return builds, paginationData, nil
}
Пример #2
0
func FetchTemplateData(resourceDB ResourcesDB, authenticated bool, resourceName string, startingID int, resultsGreaterThanStartingID bool) (TemplateData, error) {
	config, _, found, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	dbResource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	maxID, err := resourceDB.GetResourceHistoryMaxID(dbResource.ID)
	if err != nil {
		return TemplateData{}, err
	}

	if startingID == 0 && !resultsGreaterThanStartingID {
		startingID = maxID
	}

	history, moreResultsInGivenDirection, err := resourceDB.GetResourceHistoryCursor(configResource.Name, startingID, resultsGreaterThanStartingID, 100)
	if err != nil {
		return TemplateData{}, err
	}

	var paginationData pagination.PaginationData

	if len(history) > 0 {
		paginationData = pagination.NewPaginationData(
			resultsGreaterThanStartingID,
			moreResultsInGivenDirection,
			maxID,
			history[0].VersionedResource.ID,
			history[len(history)-1].VersionedResource.ID,
		)
	} else {
		paginationData = pagination.PaginationData{}
	}

	resource := present.Resource(configResource, config.Groups, dbResource, authenticated)

	templateData := TemplateData{
		Resource:       resource,
		History:        history,
		PaginationData: paginationData,
		PipelineName:   resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}