コード例 #1
0
ファイル: scans.go プロジェクト: iwarsong/bearded
func (m *ScanManager) Update(obj *scan.Scan) error {
	now := time.Now().UTC()
	obj.Dates.Updated = &now
	switch st := obj.Status; true {
	case st == scan.StatusQueued:
		if obj.Queued == nil {
			obj.Queued = &now
		}
	case st == scan.StatusFinished || st == scan.StatusFailed:
		if obj.Finished == nil {
			obj.Finished = &now
		}
	case st == scan.StatusWorking:
		if obj.Started == nil {
			obj.Started = &now
		}
	}
	return m.col.UpdateId(obj.Id, obj)
}
コード例 #2
0
ファイル: scan_service.go プロジェクト: iwarsong/bearded
func (s *ScanService) reports(_ *restful.Request, resp *restful.Response, sc *scan.Scan) {

	mgr := s.Manager()
	defer mgr.Close()

	results := []*report.Report{}

	results, count, err := mgr.Reports.FilterBySessions(sc.GetAllSessions())
	if err != nil {
		logrus.Error(stackerr.Wrap(err))
		resp.WriteServiceError(http.StatusInternalServerError, services.DbErr)
		return
	}

	reportList := report.ReportList{
		Meta:    pagination.Meta{Count: count},
		Results: results,
	}

	resp.WriteEntity(reportList)
}
コード例 #3
0
ファイル: scans.go プロジェクト: iwarsong/bearded
func (m *ScanManager) Create(raw *scan.Scan) (*scan.Scan, error) {
	// TODO (m0sth8): add validation
	raw.Id = bson.NewObjectId()
	raw.Dates.Created = TimeP(time.Now().UTC())
	raw.Dates.Updated = raw.Dates.Created
	for _, sess := range raw.Sessions {
		sess.Scan = raw.Id
	}
	if err := m.col.Insert(raw); err != nil {
		return nil, err
	}
	return raw, nil
}
コード例 #4
0
ファイル: scans.go プロジェクト: iwarsong/bearded
func (m *ScanManager) UpdateSession(sc *scan.Scan, obj *scan.Session) error {
	var index *int
	isRootSession := !obj.HasParent()
	if isRootSession {
		// find session indx for updating, but only for root session
		for i, s := range sc.Sessions {
			if s.Id == obj.Id {
				index = &i
				break
			}
		}
	}
	if isRootSession && index == nil {
		return mgo.ErrNotFound
	}
	now := time.Now().UTC()
	switch st := obj.Status; true {
	case st == scan.StatusQueued:
		if obj.Queued == nil {
			obj.Queued = &now
		}
	case st == scan.StatusFinished || st == scan.StatusFailed:
		if obj.Finished == nil {
			obj.Finished = &now
		}
	case st == scan.StatusWorking:
		if obj.Started == nil {
			obj.Started = &now
		}
	}

	scanModified := false

	// only root sessions have influence on scan
	if isRootSession {
		// if session is queued then scan should also be queued
		if obj.Status == scan.StatusQueued && (sc.Status == scan.StatusCreated || sc.Status == scan.StatusWorking) {
			sc.Status = scan.StatusQueued
			scanModified = true
		}

		// if session is working then scan should also be working
		if obj.Status == scan.StatusWorking && (sc.Status != scan.StatusWorking) {
			sc.Status = scan.StatusWorking
			scanModified = true
		}

		// if session failed then scan should be failed too
		if obj.Status == scan.StatusFailed {
			sc.Status = scan.StatusFailed
			scanModified = true
		}
		// if session was the last one
		if obj.Status == scan.StatusFinished && (*index+1) == len(sc.Sessions) {
			sc.Status = scan.StatusFinished
			scanModified = true
		}
	}

	// if scan modified then update the whole scan object
	if scanModified || !isRootSession {
		return m.Update(sc)
	}

	// TODO (m0sth8): update non root session with set
	key := fmt.Sprintf("sessions.%d", *index)
	update := bson.M{"$set": bson.M{key: obj, "updated": now}}
	m.col.UpdateId(sc.Id, update)
	return m.Update(sc)
}