<commit_before>package controller

import (
	"fmt"
	mongomodels "koding/db/models"
	"koding/db/mongodb/modelhelper"
	"koding/helpers"
	"socialapi/models"
	"strings"

	"github.com/VerbalExpressions/GoVerbalExpressions"
	"github.com/jinzhu/gorm"
	"labix.org/v2/mgo/bson"
)

func (mwc *Controller) migrateAllTags() error {
	s := modelhelper.Selector{
		"socialApiChannelId": modelhelper.Selector{"$exists": false},
	}
	errCount := 0
	successCount := 0

	handleError := func(t *mongomodels.Tag, err error) {
		mwc.log.Error("an error occured for tag %s: %s", t.Id.Hex(), err)
		errCount++
	}

	migrateTag := func(tag interface{}) error {
		oldTag := tag.(*mongomodels.Tag)
		channelId, err := createTagChannel(oldTag)
		if err != nil {
			handleError(oldTag, err)
			return nil
		}
		if err := mwc.createTagFollowers(oldTag, channelId); err != nil {
			handleError(oldTag, err)
			return nil
		}

		if err := completeTagMigration(oldTag, channelId); err != nil {
			handleError(oldTag, err)
			return nil
		}
		successCount++

		return nil
	}

	iterOptions := helpers.NewIterOptions()
	iterOptions.CollectionName = "jTags"
	iterOptions.F = migrateTag
	iterOptions.Filter = s
	iterOptions.Result = &mongomodels.Tag{}
	iterOptions.Limit = 10000000
	iterOptions.Skip = 0

	helpers.Iter(modelhelper.Mongo, iterOptions)

	mwc.log.Notice("Tag migration completed for %d tags with %d errors", successCount, errCount)

	return nil
}

func createTagChannel(t *mongomodels.Tag) (int64, error) {
	creatorId, err := fetchTagCreatorId(t)
	if err != nil {
		return 0, err
	}

	c := models.NewChannel()

	channelId, err := c.FetchChannelIdByNameAndGroupName(t.Slug, t.Group)
	if err == nil {
		return channelId, nil
	}
	if err != gorm.RecordNotFound {
		return 0, err
	}

	c.CreatorId = creatorId
	c.Name = t.Slug
	c.GroupName = t.Group // create group if needed
	c.Purpose = "Channel for " + c.Name + " topic"
	c.TypeConstant = models.Channel_TYPE_TOPIC
	c.PrivacyConstant = models.Channel_PRIVACY_PRIVATE
	c.CreatedAt = t.Meta.CreatedAt<commit_msg> Merge branch 'master.activity.migration' into 'master.activity'<commit_after>package controller

import (
	"fmt"
	mongomodels "koding/db/models"
	"koding/db/mongodb/modelhelper"
	"koding/helpers"
	"socialapi/models"
	"strings"

	"github.com/VerbalExpressions/GoVerbalExpressions"
	"github.com/jinzhu/gorm"
	"labix.org/v2/mgo/bson"
)

func (mwc *Controller) migrateAllTags() error {
	s := modelhelper.Selector{
		"socialApiChannelId": modelhelper.Selector{"$exists": false},
	}
	errCount := 0
	successCount := 0

	handleError := func(t *mongomodels.Tag, err error) {
		mwc.log.Error("an error occured for tag %s: %s", t.Id.Hex(), err)
		errCount++
	}

	migrateTag := func(tag interface{}) error {
		oldTag := tag.(*mongomodels.Tag)
		channelId, err := createTagChannel(oldTag)
		if err != nil {
			handleError(oldTag, err)
			return nil
		}
		if err := mwc.createTagFollowers(oldTag, channelId); err != nil {
			handleError(oldTag, err)
			return nil
		}

		if err := completeTagMigration(oldTag, channelId); err != nil {
			handleError(oldTag, err)
			return nil
		}
		successCount++

		return nil
	}

	iterOptions := helpers.NewIterOptions()
	iterOptions.CollectionName = "jTags"
	iterOptions.F = migrateTag
	iterOptions.Filter = s
	iterOptions.Result = &mongomodels.Tag{}
	iterOptions.Limit = 10000000
	iterOptions.Skip = 0

	helpers.Iter(modelhelper.Mongo, iterOptions)

	mwc.log.Notice("Tag migration completed for %d tags with %d errors", successCount, errCount)

	return nil
}

func createTagChannel(t *mongomodels.Tag) (int64, error) {
	creatorId, err := fetchTagCreatorId(t)
	if err != nil {
		return 0, err
	}

	c := models.NewChannel()

	channelId, err := c.FetchChannelIdByNameAndGroupName(t.Slug, t.Group)
	if err == nil {
		return channelId, nil
	}
	if err != gorm.RecordNotFound {
		return 0, err
	}

	c.CreatorId = creatorId
	c.Name = t.Slug
	c.GroupName = t.Group // create group if needed
	c.Purpose = "Channel for " + c.Name + " topic"
	c.TypeConstant = models.Channel_TYPE_TOPIC
	c.PrivacyConstant = models.Channel_PRIVACY_PUBLIC
	c.CreatedAt = t.Meta.CreatedAt
{'fraction_non_alphanumeric': 0.12767509727626458, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 4.212927756653992, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1460506', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1484, 'n_words': 330}