<commit_before>package scheduler

import (
	"time"

	"github.com/cozy/cozy-stack/pkg/consts"
	"github.com/cozy/cozy-stack/pkg/couchdb"
	"github.com/cozy/cozy-stack/pkg/logger"
)

// importFromMemStorage fetch triggers from the old storage and add them
// into the new scheduler
func (sched *RedisScheduler) importFromMemStorage() error {

	oldStorage := &globalDBStorage{}

	ts, err := oldStorage.GetAll()
	if err != nil {
		return nil
	}

	for _, t := range ts {
		domain := t.Domain

		_, err := sched.Get(domain, t.ID())
		switch err {
		case nil:
			// trigger already copied
			errd := couchdb.DeleteDoc(couchdb.GlobalTriggersDB, t)
			if errd != nil {
				logger.WithNamespace("event-trigger").Errorln("error deleting old trigger", err)
			}
			continue

		case ErrNotFoundTrigger:
			// trigger does not exists in new storage, copy it
			trigger, errn := NewTrigger(t)
			if errn != nil {
				return errn
			}

			db := couchdb.SimpleDatabasePrefix(t.Domain)
			if err = couchdb.CreateNamedDoc(db, t); err != nil {
				return err
			}

			if err = sched.addToRedis(trigger, time.Now()); err != nil {
				return err
			}

			if err = oldStorage.Delete(trigger); err != nil {
				return err
			}

		default:
			return err
		}
	}

	return nil
}

// ImportFromDB fetch the triggers from couchdb an reimport them into redis
func (sched *RedisScheduler) ImportFromDB(domain string) error {

	db := couchdb.SimpleDatabasePrefix(domain)
	var docs []*TriggerInfos
	req := &couchdb.AllDocsRequest{Limit: 100}
	if err := couchdb.GetAllDocs(db, consts.Triggers, req, &docs); err != nil {
		return err
	}

	for _, d := range docs {
		t, err := NewAtTrigger(d)
		if err != nil {
			logger.WithDomain(domain).Errorln("Bad trigger in couchdb: ", t, err)
		} else {
			sched.addToRedis(t, time.Now())
		}
	}

	return nil
}
<commit_msg>prevent bug with old triggers<commit_after>package scheduler

import (
	"time"

	"github.com/cozy/cozy-stack/pkg/consts"
	"github.com/cozy/cozy-stack/pkg/couchdb"
	"github.com/cozy/cozy-stack/pkg/logger"
)

// importFromMemStorage fetch triggers from the old storage and add them
// into the new scheduler
func (sched *RedisScheduler) importFromMemStorage() error {

	oldStorage := &globalDBStorage{}

	ts, err := oldStorage.GetAll()
	if err != nil {
		return nil
	}

	for _, t := range ts {
		domain := t.Domain

		_, err := sched.Get(domain, t.ID())
		switch err {
		case nil:
			// trigger already copied
			errd := couchdb.DeleteDoc(couchdb.GlobalTriggersDB, t)
			if errd != nil {
				logger.WithNamespace("event-trigger").Errorln("error deleting old trigger", err)
			}
			continue

		case ErrNotFoundTrigger:
			// trigger does not exists in new storage, copy it
			trigger, errn := NewTrigger(t)
			if errn != nil {
				return errn
			}

			db := couchdb.SimpleDatabasePrefix(t.Domain)
			revback := t.Rev()
			t.TRev = ""
			if err = couchdb.CreateNamedDoc(db, t); err != nil {
				return err
			}
			t.SetRev(revback)

			if err = sched.addToRedis(trigger, time.Now()); err != nil {
				return err
			}

			if err = oldStorage.Delete(trigger); err != nil {
				return err
			}

		default:
			return err
		}
	}

	return nil
}

// ImportFromDB fetch the triggers from couchdb an reimport them into redis
func (sched *RedisScheduler) ImportFromDB(domain string) error {

	db := couchdb.SimpleDatabasePrefix(domain)
	var docs []*TriggerInfos
	req := &couchdb.AllDocsRequest{Limit: 100}
	if err := couchdb.GetAllDocs(db, consts.Triggers, req, &docs); err != nil {
		return err
	}

	for _, d := range docs {
		t, err := NewAtTrigger(d)
		if err != nil {
			logger.WithDomain(domain).Errorln("Bad trigger in couchdb: ", t, err)
		} else {
			sched.addToRedis(t, time.Now())
		}
	}

	return nil
}

{'fraction_non_alphanumeric': 0.12567132116004295, 'fraction_numerical': 0.001611170784103115, 'mean_word_length': 3.2914746543778803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1427167', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1281, 'n_words': 346}