<commit_before>package conversion

import (
	"log"
	"os"
	"strings"
	"sync"
	"time"

	// . "eaciit/wfdemo-git/library/helper"
	. "eaciit/wfdemo-git/library/models"

	"github.com/eaciit/dbox"
	_ "github.com/eaciit/dbox/dbc/mongo"
	"github.com/eaciit/orm"

	tk "github.com/eaciit/toolkit"
)

var (
	separator       = string(os.PathSeparator)
	mutex           = &sync.Mutex{}
	countPerProcess = 1
)

type GroupResult struct {
	Project           string
	Turbine           string
	LatestProcessTime time.Time
	LatestFrom        string
}

type HFDDownConversion struct {
	Ctx *orm.DataContext
}

func NewHFDDownConversion(ctx *orm.DataContext) *HFDDownConversion {
	ev := new(HFDDownConversion)
	ev.Ctx = ctx
	return ev
}

func (ev *HFDDownConversion) Run() {
	var wg sync.WaitGroup
	loops := ev.getLatest()

	for _, loop := range loops {
		// if loop.Turbine == "SSE017" {
		// log.Printf("loop: %v | %v \n", loop.Turbine, loop.LatestProcessTime)
		wg.Add(1)
		go ev.processTurbine(loop, &wg)
		// }
	}

	wg.Wait()
}

func (ev *HFDDownConversion) processTurbine(loop GroupResult, wg *sync.WaitGroup) {
	// mutex.Lock()

	now := time.Now()
	log.Printf("Starting process %v | %v | %v \n", loop.Project, loop.Turbine, loop.LatestProcessTime.String())

	pipes := []tk.M{}

	match := tk.M{
		"projectname": loop.Project,
		"turbine":     loop.Turbine,
		// "eventtype":    "alarmchanged",
		// "brakeprogram": tk.M{"$gt": 0},
	}

	if loop.LatestFrom == "Raw" {
		match.Set("timestamp", tk.M{"$gte": loop.LatestProcessTime})
	} else {
		match.Set("timestamp", tk.M{"$gt": loop.LatestProcessTime})
	}

	pipes = append(pipes, tk.M{"$match": match})
	pipes = append(pipes, tk.M{"$sort": tk.M{"timestamp": 1}})

	csr, err := ev.Ctx.Connection.NewQuery().From(new(EventRawHFD).TableName()).Command("pipe", pipes).Cursor(nil)
	defer csr.Close()

	eventRaws := []EventRawHFD{}

	if err != nil {
		tk.Println("Error: " + err.Error())
	} else {
		err = csr.Fetch(&eventRaws, 0, false)
		if err != nil {
			tk.Println("Error: " + err.Error())
		} else {

			loopData := eventRaws

			AlarmCode := ".AlarmCode"
			ErrorState := ".ErrorState"
			TurbineState := ".TurbineState"

		mainLoop:
			for {
				startIdx := -1
				endIdx := -1

				var start, end EventRawHFD
				lastAlarmCode := -1
				lastAlarmDesc := ""
				// lastBrakeProgram := 0

			reloop:
				for idx, data := range loopData {
					if idx > 0 {
						lastAlarmCode = loopData[idx-1].AlarmId
						lastAlarmDesc = loopData[idx-1].AlarmDescription
						// lastBrakeProgram = loopData[idx-1].BrakeProgram
					}

					if data.DateInfo.MonthId != 0 {
						/*else if lastBrakeProgram != 0 && lastBrakeProgram != 999 && lastAlarmCode != 0 && strings.Contains(data.EventType, AlarmCode) && startIdx == -1 {
							startIdx = idx
							start = data
							ev.InsertToMonitoringEvent(start, "down")
						} */

						if data.BrakeProgram != 0 && data.BrakeProgram != 999 && data.AlarmId != 0 && strings.Contains(data.EventType, AlarmCode) && startIdx == -1 {
							startIdx = idx
							start = data
							ev.InsertToMonitoringEvent(start, "down")
						} else if ((strings.Contains(data.EventType, ErrorState) && data.AlarmId == 0) || (strings.Contains(data.EventType, AlarmCode) && data.AlarmId == 0) || (strings.Contains(data.EventType, TurbineState) && (data.AlarmId >= 0 && data.AlarmId <= 11) || data.AlarmId == 0)) && startIdx != -1 {
							end = data
						} else if data.AlarmId == 999 && startIdx != -1 {
							if (strings.Contains(data.EventType, ErrorState) && lastAlarmCode == 0) || (strings.Contains(data.EventType, AlarmCode) && lastAlarmCode == 0) || (strings.Contains(data.EventType, TurbineState) && (lastAlarmCode >= 0 && lastAlarmCode <= 11) || lastAlarmCode == 0) {
								end = data
							}
						}

						if end.TimeStamp.Year() != 1 && startIdx != -1 {
							tmp := EventRawHFD{}
							tmp = end

							if tmp.AlarmId == 999 {
								tmp.AlarmId = lastAlarmCode
								tmp.AlarmDescription = lastAlarmDesc<commit_msg>fixing event down calculation, put && condition for event type and turbine state<commit_after>package conversion

import (
	"log"
	"os"
	"strings"
	"sync"
	"time"

	// . "eaciit/wfdemo-git/library/helper"
	. "eaciit/wfdemo-git/library/models"

	"github.com/eaciit/dbox"
	_ "github.com/eaciit/dbox/dbc/mongo"
	"github.com/eaciit/orm"

	tk "github.com/eaciit/toolkit"
)

var (
	separator       = string(os.PathSeparator)
	mutex           = &sync.Mutex{}
	countPerProcess = 1
)

type GroupResult struct {
	Project           string
	Turbine           string
	LatestProcessTime time.Time
	LatestFrom        string
}

type HFDDownConversion struct {
	Ctx *orm.DataContext
}

func NewHFDDownConversion(ctx *orm.DataContext) *HFDDownConversion {
	ev := new(HFDDownConversion)
	ev.Ctx = ctx
	return ev
}

func (ev *HFDDownConversion) Run() {
	var wg sync.WaitGroup
	loops := ev.getLatest()

	for _, loop := range loops {
		// if loop.Turbine == "SSE017" {
		// log.Printf("loop: %v | %v \n", loop.Turbine, loop.LatestProcessTime)
		wg.Add(1)
		go ev.processTurbine(loop, &wg)
		// }
	}

	wg.Wait()
}

func (ev *HFDDownConversion) processTurbine(loop GroupResult, wg *sync.WaitGroup) {
	// mutex.Lock()

	now := time.Now()
	log.Printf("Starting process %v | %v | %v \n", loop.Project, loop.Turbine, loop.LatestProcessTime.String())

	pipes := []tk.M{}

	match := tk.M{
		"projectname": loop.Project,
		"turbine":     loop.Turbine,
		// "eventtype":    "alarmchanged",
		// "brakeprogram": tk.M{"$gt": 0},
	}

	if loop.LatestFrom == "Raw" {
		match.Set("timestamp", tk.M{"$gte": loop.LatestProcessTime})
	} else {
		match.Set("timestamp", tk.M{"$gt": loop.LatestProcessTime})
	}

	pipes = append(pipes, tk.M{"$match": match})
	pipes = append(pipes, tk.M{"$sort": tk.M{"timestamp": 1}})

	csr, err := ev.Ctx.Connection.NewQuery().From(new(EventRawHFD).TableName()).Command("pipe", pipes).Cursor(nil)
	defer csr.Close()

	eventRaws := []EventRawHFD{}

	if err != nil {
		tk.Println("Error: " + err.Error())
	} else {
		err = csr.Fetch(&eventRaws, 0, false)
		if err != nil {
			tk.Println("Error: " + err.Error())
		} else {

			loopData := eventRaws

			AlarmCode := ".AlarmCode"
			ErrorState := ".ErrorState"
			TurbineState := ".TurbineState"

		mainLoop:
			for {
				startIdx := -1
				endIdx := -1

				var start, end EventRawHFD
				lastAlarmCode := -1
				lastAlarmDesc := ""
				// lastBrakeProgram := 0

			reloop:
				for idx, data := range loopData {
					if idx > 0 {
						lastAlarmCode = loopData[idx-1].AlarmId
						lastAlarmDesc = loopData[idx-1].AlarmDescription
						// lastBrakeProgram = loopData[idx-1].BrakeProgram
					}

					if data.DateInfo.MonthId != 0 {
						/*else if lastBrakeProgram != 0 && lastBrakeProgram != 999 && lastAlarmCode != 0 && strings.Contains(data.EventType, AlarmCode) && startIdx == -1 {
							startIdx = idx
							start = data
							ev.InsertToMonitoringEvent(start, "down")
						} */

						if data.BrakeProgram != 0 && data.BrakeProgram != 999 && data.AlarmId != 0 && strings.Contains(data.EventType, AlarmCode) && startIdx == -1 {
							startIdx = idx
							start = data
							ev.InsertToMonitoringEvent(start, "down")
						} else if ((strings.Contains(data.EventType, ErrorState) && data.AlarmId == 0) || ((strings.Contains(data.EventType, AlarmCode) && data.AlarmId == 0) && (strings.Contains(data.EventType, TurbineState) && (data.AlarmId >= 0 && data.AlarmId <= 11)))) && startIdx != -1 {
							end = data
						}
						/* else if ((strings.Contains(data.EventType, ErrorState) && data.AlarmId == 0) || (strings.Contains(data.EventType, AlarmCode) && data.AlarmId == 0) || (strings.Contains(data.EventType, TurbineState) && (data.AlarmId >= 0 && data.AlarmId <= 11) || data.AlarmId == 0)) && startIdx != -1 {
							end = data
						}
						else if data.AlarmId == 999 && startIdx != -1 {
							if (strings.Contains(data.EventType, ErrorState) && lastAlarmCode == 0) || (strings.Contains(data.EventType, AlarmCode) && lastAlarmCode == 0) || (strings.Contains(data.EventType, TurbineState) && (lastAlarmCode >= 0 && lastAlarmCode <= 11) || lastAlarmCode == 0) {
								end = data
							}
						}*/

						if end.TimeStamp.Year() != 1 && startIdx != -1 {
							tmp := EventRawHFD{}
							tmp = end

							if tmp.AlarmId == 999 {
								tmp.AlarmId = lastAlarmCode
								tmp.AlarmDescription = lastAlarmDesc
{'fraction_non_alphanumeric': 0.16198109821749013, 'fraction_numerical': 0.01292020576623998, 'mean_word_length': 3.076060458313018, 'pattern_counts': {'":': 20, '<': 8, '<?xml version=': 0, '>': 10, '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': '7388427', 'n_tokens_mistral': 3587, 'n_tokens_neox': 3029, 'n_words': 608}