Ejemplo n.º 1
0
func getNewPointer(m orm.IModel) orm.IModel {
	switch m.TableName() {
	case "MaintenanceCostByHour":
		return new(MaintenanceCostByHour)
	case "MaintenanceCost":
		return new(MaintenanceCost)
	case "PlannedMaintenance":
		return new(PlannedMaintenance)
	case "SummaryData":
		return new(SummaryData)
	case "DataBrowser":
		return new(DataBrowser)
	case "MORCalculationFlatSummary":
		return new(MORCalculationFlatSummary)
	case "PreventiveCorrectiveSummary":
		return new(PreventiveCorrectiveSummary)
	case "RegenMasterPlant":
		return new(RegenMasterPlant)
	case "NotificationFailure":
		return new(NotificationFailure)
	case "WODurationSummary":
		return new(WODurationSummary)
	case "WOListSummary":
		return new(WOListSummary)
	case "SyntheticPM":
		return new(SyntheticPM)
	case "Vibration":
		return new(Vibration)
	default:
		return m
	}

}
Ejemplo n.º 2
0
func (b *BaseController) ConvertMGOToSQLServer(m orm.IModel) error {
	tStart := time.Now()

	tk.Printf("\nConvertMGOToSQLServer: Converting %v \n", m.TableName())
	tk.Println("ConvertMGOToSQLServer: Starting to convert...\n")

	c, e := b.MongoCtx.Connection.NewQuery().From(m.TableName()).Cursor(nil)

	if e != nil {
		return e
	}

	defer c.Close()

	totalData := c.Count()
	processIter := tk.ToInt(tk.ToFloat64(totalData/maxDataEachProcess, 0, tk.RoundingUp), tk.RoundingUp)

	if maxDataEachProcess == 0 {
		processIter = 0
	}

	for iter := 0; iter < processIter+1; iter++ {

		skip := iter * maxDataEachProcess
		take := maxDataEachProcess

		if maxDataEachProcess == 0 {
			take = totalData
		} else if iter == processIter {
			take = totalData - skip
		}

		csr, e := b.MongoCtx.Connection.NewQuery().From(m.TableName()).Skip(skip).Take(take).Cursor(nil)

		if e != nil {
			return e
		}

		result := []tk.M{}
		e = csr.Fetch(&result, 0, false)
		csr.Close()

		if e != nil {
			return e
		}

		dtLen := len(result)

		resPart := make([][]tk.M, worker)

		if dtLen < worker {
			resPart = make([][]tk.M, 1)
			resPart[0] = result
		} else {
			workerTaskCount := tk.ToInt(tk.ToFloat64(dtLen/worker, 0, tk.RoundingAuto), tk.RoundingAuto)
			count := 0

			for i := 0; i < worker; i++ {
				if i == worker-1 {
					resPart[i] = result[count:]
				} else {
					resPart[i] = result[count : count+workerTaskCount]
				}
				count += workerTaskCount
			}
		}

		wg := &sync.WaitGroup{}
		wg.Add(len(resPart))

		for _, val := range resPart {
			// go b.Insert(val, m, wg)
			go b.InsertBulk(val, m, wg, tStart)
		}

		wg.Wait()
	}

	tk.Println("\nConvertMGOToSQLServer: Finish.")
	tk.Printf("Completed Success in %v \n", time.Since(tStart))
	return nil
}