Ejemplo n.º 1
0
func (s *ScenarioSimulation) GetData(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson
	status := ""
	msg := ""
	res := []*ValueEquation{}

	payloads := struct {
		StartPeriod         string
		EndPeriod           string
		SelectedPlant       []string
		SelectedUnit        []string
		SelectedSimulation  string
		SelectedDescription string
	}{}

	e := k.GetPayload(&payloads)
	if e != nil {
		s.WriteLog(e)
	}

	start, _ := time.Parse(time.RFC3339, payloads.StartPeriod)
	end, _ := time.Parse(time.RFC3339, payloads.EndPeriod)

	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", start))
	query = append(query, dbox.Lte("Dates", end))

	if payloads.SelectedPlant != nil && len(payloads.SelectedPlant) > 0 {
		query = append(query, dbox.In("Plant", payloads.SelectedPlant))
	}

	if payloads.SelectedUnit != nil && len(payloads.SelectedUnit) > 0 {
		query = append(query, dbox.In("Unit", payloads.SelectedUnit))
	}
	csr, err := s.DB().Connection.NewQuery().From("ValueEquation").Select("Plant", "AvgNetGeneration", "VOMR", "TotalOutageDuration", "TotalDuration", "OperatingCost", "Revenue", "TotalLabourCost", "TotalMaterialCost", "TotalServicesCost", "TotalFuelCost").Where(query...).Cursor(nil)
	if err != nil {
		msg = err.Error()
		status = "NOK"
	} else {
		status = "OK"
	}
	err = csr.Fetch(&res, 0, true)
	defer csr.Close()

	result := tk.M{}
	result.Set("Status", status)
	result.Set("Message", msg)
	result.Set("Data", res)

	return result
}
Ejemplo n.º 2
0
func (m *ValueEquationComparison) GetData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	m.SetPayLoad(k)
	type DataValue struct {
		Revenue         float64 `json:'Revenue'`
		MaintenanceCost float64 `json:'MaintenanceCost'`
		OperatingCost   float64 `json:'OperatingCost'`
		NetGeneration   float64 `json:'NetGeneration'`
	}
	result, Result := tk.M{}, []DataValue{}
	c := ctx.Connection
	ve := ValueEquation{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	if m.SelectedPlant != nil && len(m.SelectedPlant) > 0 {
		query = append(query, dbox.In("Plant", m.SelectedPlant))
	}
	if m.SelectedUnit != nil && len(m.SelectedUnit) > 0 {
		query = append(query, dbox.In("Unit", m.SelectedUnit))
	}
	if m.SelectedUnitGroup != "ALL" {
		query = append(query, dbox.In("UnitGroup", m.SelectedUnitGroup))
	}

	csr, e := c.NewQuery().
		Where(query...).
		Aggr(dbox.AggrSum, "Revenue", "Revenue").
		Aggr(dbox.AggrSum, "MaintenanceCost", "MaintenanceCost").
		Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
		Aggr(dbox.AggrSum, "NetGeneration", "NetGeneration").
		From(ve.TableName()).Cursor(nil)
	e = csr.Fetch(&Result, 0, false)
	if e != nil {
		return nil, e
	}
	csr.Close()
	result.Set("Index", m.Index)
	result.Set("DataValue", Result)
	return result, nil
}
Ejemplo n.º 3
0
func (u *UserModel) LoginDo(ctx *orm.DataContext, username string, password string) (*UserModel, bool) {
	PasswordHash := GetMD5Hash(password)
	found := false
	query := []*dbox.Filter{}
	query = append(query, dbox.In("UserName", username))
	query = append(query, dbox.In("PasswordHash", PasswordHash))
	csr, e := ctx.Connection.NewQuery().From(u.TableName()).Where(query...).Cursor(nil)
	if e != nil {
		return nil, found
	}
	result := []*UserModel{}
	csr.Fetch(&result, 0, false)
	csr.Close()
	if len(result) > 0 {
		found = true
		for _, i := range result {
			u = i
		}
	}
	return u, found

}
Ejemplo n.º 4
0
func (m *ValueEquationComparison) GetUnitList(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	m.SetPayLoad(k)
	mup := MasterUnitPlant{}
	result, UnitData := tk.M{}, []MasterUnitPlant{}
	c := ctx.Connection
	query := []*dbox.Filter{}
	if m.SelectedPlant != nil && len(m.SelectedPlant) > 0 {
		query = append(query, dbox.In("Plant", m.SelectedPlant))
	}

	csr, e := c.NewQuery().
		Where(query...).Select("Unit").
		From(mup.TableName()).Group("Unit").Cursor(nil)
	e = csr.Fetch(&UnitData, 0, false)
	if e != nil {
		return nil, e
	}
	csr.Close()
	result.Set("Index", m.Index)
	result.Set("UnitData", UnitData)
	return result, e
}
Ejemplo n.º 5
0
func (d *DataSourceController) ConnectToDataSourceDB(payload toolkit.M) (int, []toolkit.M, *colonycore.DataBrowser, error) {
	var hasLookup bool
	toolkit.Println("payload : ", payload)
	if payload.Has("haslookup") {
		hasLookup = payload.Get("haslookup").(bool)
	}
	_id := toolkit.ToString(payload.Get("browserid", ""))
	sort := payload.Get("sort")
	search := payload.Get("search")
	_ = search
	take := toolkit.ToInt(payload.Get("take", ""), toolkit.RoundingAuto)
	skip := toolkit.ToInt(payload.Get("skip", ""), toolkit.RoundingAuto)

	TblName := toolkit.M{}
	payload.Unset("browserid")
	//sorter = ""
	if sort != nil {
		tmsort, _ := toolkit.ToM(sort.([]interface{})[0])
		fmt.Printf("====== sort %#v\n", tmsort["dir"])
		if tmsort["dir"] == "asc" {
			sorter = tmsort["field"].(string)
		} else if tmsort["dir"] == "desc" {
			sorter = "-" + tmsort["field"].(string)
		} else if tmsort["dir"] == nil {
			sorter = " "
		}
	} else {
		sorter = " "
	}

	dataDS := new(colonycore.DataBrowser)
	err := colonycore.Get(dataDS, _id)
	if err != nil {
		return 0, nil, nil, err
	}

	dataConn := new(colonycore.Connection)
	err = colonycore.Get(dataConn, dataDS.ConnectionID)
	if err != nil {
		return 0, nil, nil, err
	}

	if err := d.checkIfDriverIsSupported(dataConn.Driver); err != nil {
		return 0, nil, nil, err
	}

	connection, err := helper.ConnectUsingDataConn(dataConn).Connect()
	if err != nil {
		return 0, nil, nil, err
	}

	if dataDS.QueryType == "" {
		TblName.Set("from", dataDS.TableNames)
		payload.Set("from", dataDS.TableNames)
	} else if dataDS.QueryType == "Dbox" {
		getTableName := toolkit.M{}
		toolkit.UnjsonFromString(dataDS.QueryText, &getTableName)
		payload.Set("from", getTableName.Get("from").(string))

		if qSelect := getTableName.Get("select", "").(string); qSelect != "" {
			payload.Set("select", getTableName.Get("select").(string))
		}
	} else if dataDS.QueryType == "SQL" {
		var QueryString string
		if dataConn.Driver == "mysql" || dataConn.Driver == "hive" {
			QueryString = " LIMIT " + toolkit.ToString(take) + " OFFSET " + toolkit.ToString(skip)
		} else if dataConn.Driver == "mssql" {
			QueryString = " OFFSET " + toolkit.ToString(skip) + " ROWS FETCH NEXT " +
				toolkit.ToString(take) + " ROWS ONLY "

		} else if dataConn.Driver == "postgres" {
			QueryString = " LIMIT " + toolkit.ToString(take) +
				" OFFSET " + toolkit.ToString(skip)
		}
		stringQuery := toolkit.Sprintf("%s %s", dataDS.QueryText, QueryString)
		payload.Set("freetext", stringQuery)
		// toolkit.Println(stringQuery)
	}

	qcount, _ := d.parseQuery(connection.NewQuery(), TblName)
	query, _ := d.parseQuery(connection.NewQuery() /*.Skip(skip).Take(take) .Order(sorter)*/, payload)

	var selectfield string
	for _, metadata := range dataDS.MetaData {
		tField := metadata.Field
		if payload.Has(tField) {
			selectfield = toolkit.ToString(tField)
			if toolkit.IsSlice(payload[tField]) {
				query = query.Where(dbox.In(tField, payload[tField].([]interface{})...))
				qcount = qcount.Where(dbox.In(tField, payload[tField].([]interface{})...))
			} else if !toolkit.IsNilOrEmpty(payload[tField]) {
				var hasPattern bool
				for _, val := range querypattern {
					if strings.Contains(toolkit.ToString(payload[tField]), val) {
						hasPattern = true
					}
				}
				if hasPattern {
					query = query.Where(dbox.ParseFilter(toolkit.ToString(tField), toolkit.ToString(payload[tField]),
						toolkit.ToString(metadata.DataType), ""))
					qcount = qcount.Where(dbox.ParseFilter(toolkit.ToString(tField), toolkit.ToString(payload[tField]),
						toolkit.ToString(metadata.DataType), ""))
				} else {
					switch toolkit.ToString(metadata.DataType) {
					case "int":
						query = query.Where(dbox.Eq(tField, toolkit.ToInt(payload[tField], toolkit.RoundingAuto)))
						qcount = qcount.Where(dbox.Eq(tField, toolkit.ToInt(payload[tField], toolkit.RoundingAuto)))
					case "float32":
						query = query.Where(dbox.Eq(tField, toolkit.ToFloat32(payload[tField], 2, toolkit.RoundingAuto)))
						qcount = qcount.Where(dbox.Eq(tField, toolkit.ToFloat32(payload[tField], 2, toolkit.RoundingAuto)))
					case "float64":
						query = query.Where(dbox.Eq(tField, toolkit.ToFloat64(payload[tField], 2, toolkit.RoundingAuto)))
						qcount = qcount.Where(dbox.Eq(tField, toolkit.ToFloat64(payload[tField], 2, toolkit.RoundingAuto)))
					default:
						query = query.Where(dbox.Contains(tField, toolkit.ToString(payload[tField])))
						qcount = qcount.Where(dbox.Contains(tField, toolkit.ToString(payload[tField])))
					}
				}
			}
		}
	}

	if hasLookup && selectfield != "" {
		if toolkit.HasMember(ds_flatfile, dataConn.Driver) {
			query = query.Select(selectfield)
			qcount = qcount.Select(selectfield)
		} else {
			query = query.Select(selectfield).Group(selectfield)
			qcount = qcount.Select(selectfield).Group(selectfield)
		}

	}

	ccount, err := qcount.Cursor(nil)
	if err != nil {
		return 0, nil, nil, err
	}
	defer ccount.Close()

	dcount := ccount.Count()

	cursor, err := query.Cursor(nil)
	if err != nil {
		return 0, nil, nil, err
	}
	defer cursor.Close()

	data := []toolkit.M{}
	cursor.Fetch(&data, 0, false)

	if err != nil {
		return 0, nil, nil, err
	}

	if hasLookup && selectfield != "" && !toolkit.HasMember(ds_rdbms, dataConn.Driver) &&
		!toolkit.HasMember(ds_flatfile, dataConn.Driver) {
		dataMongo := []toolkit.M{}
		for _, val := range data {
			mVal, _ := toolkit.ToM(val.Get("_id"))
			dataMongo = append(dataMongo, mVal)
		}
		data = dataMongo
	} else if hasLookup && selectfield != "" && toolkit.HasMember(ds_flatfile, dataConn.Driver) {
		/*distinct value for flat file*/
		dataFlat := []toolkit.M{}
		var existingVal = []string{""}
		for _, val := range data {
			valString := toolkit.ToString(val.Get(selectfield))
			if !toolkit.HasMember(existingVal, valString) {
				dataFlat = append(dataFlat, val)
				existingVal = append(existingVal, valString)
			}
		}
		data = dataFlat
	}

	return dcount, data, dataDS, nil
}
Ejemplo n.º 6
0
func filterCondition(cond toolkit.M) *dbox.Filter {
	fb := new(dbox.Filter)

	for key, val := range cond {
		if key == "$and" || key == "$or" {
			afb := []*dbox.Filter{}
			for _, sVal := range val.([]interface{}) {
				rVal := sVal.(map[string]interface{})
				mVal := toolkit.M{}
				for rKey, mapVal := range rVal {
					mVal.Set(rKey, mapVal)
				}

				afb = append(afb, filterCondition(mVal))
			}

			if key == "$and" {
				fb = dbox.And(afb...)
			} else {
				fb = dbox.Or(afb...)
			}

		} else {
			if toolkit.TypeName(val) == "map[string]interface {}" {
				mVal := val.(map[string]interface{})
				tomVal, _ := toolkit.ToM(mVal)
				switch {
				case tomVal.Has("$eq"):
					fb = dbox.Eq(key, tomVal["$eq"])
				case tomVal.Has("$ne"):
					fb = dbox.Ne(key, tomVal["$ne"])
				case tomVal.Has("$regex"):
					fb = dbox.Contains(key, toolkit.ToString(tomVal["$regex"]))
				case tomVal.Has("$gt"):
					fb = dbox.Gt(key, tomVal["$gt"])
				case tomVal.Has("$gte"):
					fb = dbox.Gte(key, tomVal["$gte"])
				case tomVal.Has("$lt"):
					fb = dbox.Lt(key, tomVal["$lt"])
				case tomVal.Has("$lte"):
					fb = dbox.Lte(key, tomVal["$lte"])
				case tomVal.Has("$in"):
					tval := make([]interface{}, 0, 0)
					if toolkit.TypeName(tomVal["$in"]) == "[]interface {}" {
						for _, tin := range tomVal["$in"].([]interface{}) {
							tval = append(tval, tin)
						}
					} else {
						tval = append(tval, tomVal["$in"])
					}

					fb = dbox.In(key, tval...)
				case tomVal.Has("$nin"):
					tval := make([]interface{}, 0, 0)
					if toolkit.TypeName(tomVal["$nin"]) == "[]interface {}" {
						for _, tin := range tomVal["$nin"].([]interface{}) {
							tval = append(tval, tin)
						}
					} else {
						tval = append(tval, tomVal["$nin"])
					}

					fb = dbox.Nin(key, tval...)
				}
			} else {
				fb = dbox.Eq(key, val)
			}
		}
	}

	return fb
}
Ejemplo n.º 7
0
func (q *Query) Exec(in toolkit.M) error {
	setting, e := q.prepare(in)
	commandType := setting["commandtype"].(string)
	//toolkit.Printf("Command type: %s\n", commandType)
	if e != nil {
		return err.Error(packageName, modQuery, "Exec: "+commandType, e.Error())
	}

	if setting.GetString("commandtype") == dbox.QueryPartSelect {
		return err.Error(packageName, modQuery, "Exec: "+commandType, "Exec is not working with select command, please use .Cursor instead")
	}

	q.Lock()
	defer q.Unlock()

	var dataM toolkit.M
	var dataMs []toolkit.M

	hasData := in.Has("data")
	dataIsSlice := false
	data := in.Get("data")
	if toolkit.IsSlice(data) {
		dataIsSlice = true
		e = toolkit.Unjson(toolkit.Jsonify(data), dataMs)
		if e != nil {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data encoding error: "+e.Error())
		}
	} else {
		dataM, e = toolkit.ToM(data)
		dataMs = append(dataMs, dataM)
		if e != nil {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data encoding error: "+e.Error())
		}
	}

	hasWhere := setting.Has("where")
	where := setting.Get("where", []*dbox.Filter{}).([]*dbox.Filter)
	if hasWhere && len(where) == 0 {
		inWhere := in.Get("where")
		if inWhere == nil {
			hasWhere = false
			where = nil
		} else {
			if !toolkit.IsSlice(inWhere) {
				where = append(where, inWhere.(*dbox.Filter))
			} else {
				where = inWhere.([]*dbox.Filter)
			}
		}
	}

	if hasData && hasWhere == false && toolkit.HasMember([]interface{}{dbox.QueryPartInsert, dbox.QueryPartDelete,
		dbox.QueryPartUpdate, dbox.QueryPartSave}, commandType) {
		hasWhere = true
		//toolkit.Println("check where")
		if toolkit.IsSlice(data) {
			ids := []interface{}{}
			idField := ""
			if idField == "" {
				return err.Error(packageName, modQuery, "Exec: "+commandType, "Data send is a slice, but its element has no ID")
			}
			dataCount := toolkit.SliceLen(data)
			for i := 0; i < dataCount; i++ {
				dataI := toolkit.SliceItem(data, i)
				if i == 0 {
					idField = toolkit.IdField(dataI)
				}
				ids = append(ids, toolkit.Id(dataI))
			}
			where = []*dbox.Filter{dbox.In(idField, ids)}
		} else {
			idfield := "_id"
			id := toolkit.Id(data)
			if !toolkit.IsNilOrEmpty(id) {
				where = []*dbox.Filter{dbox.Eq(idfield, id)}
			} else {
				where = nil
				hasWhere = false
			}
		}
	}
	/*
		toolkit.Printf("CommandType: %s HasData: %v HasWhere: %v Where: %s\n",
			commandType, hasData, hasWhere, toolkit.JsonString(where))
	*/
	e = q.openFile(commandType)
	//toolkit.Printf(commandType+" Open File, found record: %d\nData:%s\n", len(q.data), toolkit.JsonString(q.data))
	if e != nil {
		return err.Error(packageName, modQuery, "Exec: "+commandType, e.Error())
	}

	var indexes []interface{}
	if hasWhere && commandType != dbox.QueryPartInsert {
		whereIndex := dbox.Find(q.data, where)
		indexes = toolkit.ToInterfaceArray(&whereIndex)
		//toolkit.Printf("Where Index: %s Index:%s\n", toolkit.JsonString(whereIndex), toolkit.JsonString(indexes))
	}
	if commandType == dbox.QueryPartInsert {
		if !hasData {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data is empty")
		}
		if !dataIsSlice {
			dataMs = []toolkit.M{dataM}
		}

		//-- validate
		for _, datam := range dataMs {
			idField, idValue := toolkit.IdInfo(datam)
			toolkit.Serde(dbox.Find(q.data, []*dbox.Filter{dbox.Eq(idField, idValue)}), &indexes, "")
			if len(indexes) > 0 {
				return err.Error(packageName, modQuery, "Exec: "+commandType, toolkit.Sprintf("Data %v already exist", idValue))
			}
		}

		//-- insert the data
		q.data = append(q.data, dataMs...)
	} else if commandType == dbox.QueryPartUpdate {

		//-- valida
		if !hasData {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data is empty")
		}

		var dataUpdate toolkit.M
		var updateDataIndex int

		// if it is a slice then we need to update each data passed on its slice
		isDataSlice := toolkit.IsSlice(data)
		if isDataSlice == false {
			isDataSlice = false
			e = toolkit.Serde(data, &dataUpdate, "")
			if e != nil {
				return err.Error(packageName, modQuery, "Exec: "+commandType, "Serde data fail"+e.Error())
			}
		}

		var idField string
		//toolkit.Printf("Indexes: %s\n", toolkit.JsonString(indexes))

		for i, v := range q.data {
			// update only data that match given inde
			if toolkit.HasMember(indexes, i) || !hasWhere {
				if idField == "" {
					idField = toolkit.IdField(v)
					if idField == "" {
						return err.Error(packageName, modQuery, "Exec: "+commandType, "No ID")
					}
				}

				// If dataslice is sent, iterate f
				if isDataSlice {
					e = toolkit.Serde(toolkit.SliceItem(data, updateDataIndex), &dataUpdate, "")
					if e != nil {
						return err.Error(packageName, modQuery, "Exec: "+commandType, "Serde data fail "+e.Error())
					}
					updateDataIndex++
				}
				dataOrigin := q.data[i]
				toolkit.CopyM(&dataUpdate, &dataOrigin, false, []string{"_id"})
				toolkit.Serde(dataOrigin, &v, "")
				q.data[i] = v
			}
		}
	} else if commandType == dbox.QueryPartDelete {
		if hasWhere && len(where) > 0 {
			indexes := dbox.Find(q.data, where)
			if len(indexes) > 0 {
				newdata := []toolkit.M{}
				for index, v := range q.data {
					partOfIndex := toolkit.HasMember(indexes, index)
					if partOfIndex == false {
						newdata = append(newdata, v)
					}
					//toolkit.Println("i:", indexes, ", index:", index, ", p.ofIndex: ", partOfIndex, ", data: ", toolkit.JsonString(newdata))
				}
				q.data = newdata
			}
		} else {
			q.data = []toolkit.M{}
		}
		//toolkit.Printf("Data now: %s\n", toolkit.JsonString(q.data))
	} else if commandType == dbox.QueryPartSave {
		if !hasData {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data is empty")
		}

		var dataMs []toolkit.M
		var dataM toolkit.M
		if !toolkit.IsSlice(data) {
			e = toolkit.Serde(&data, &dataM, "json")
			if e != nil {
				return err.Error(packageName, modQuery, "Exec: "+commandType+" Serde data fail", e.Error())
			}
			dataMs = append(dataMs, dataM)
		} else {
			e = toolkit.Serde(&data, &dataMs, "json")
			if e != nil {
				return err.Error(packageName, modQuery, "Exec: "+commandType+" Serde data fail", e.Error())
			}
		}
		//toolkit.Printf("Saving: %s\n", toolkit.JsonString(dataMs))

		for _, v := range dataMs {
			idField, idValue := toolkit.IdInfo(v)
			indexes := dbox.Find(q.data, []*dbox.Filter{dbox.Eq(idField, idValue)})
			if len(indexes) == 0 {
				q.data = append(q.data, v)
			} else {
				dataOrigin := q.data[indexes[0]]
				//toolkit.Printf("Copy data %s to %s\n", toolkit.JsonString(v), toolkit.JsonString(dataOrigin))
				toolkit.CopyM(&v, &dataOrigin, false, []string{idField})
				q.data[indexes[0]] = dataOrigin
			}
		}
	}
	e = q.writeFile()
	if e != nil {
		return err.Error(packageName, modQuery, "Exec: "+commandType+" Write fail", e.Error())
	}
	return nil
}
Ejemplo n.º 8
0
func (c *HypothesisController) Initiate(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	type ReturnValue struct {
		PlantList        []MasterPlant
		PlantListH2      []MasterPlant
		EQTypeList       []MasterEquipmentType
		MROElementList   []MasterMROElement
		OrderTypeList    []MasterOrderType
		ActivityTypeList []MasterActivityType
		FailureCode      []MasterFailureCode
		UnitList         []tk.M
	}

	var (
		Result ReturnValue
		e      error
	)

	d := struct {
		selectedPlant []string
	}{}
	e = k.GetPayload(&d)
	// tk.Printf("d: %#v \n\n", d)
	curr, _ := c.DB().Find(&MasterPlant{}, nil)

	defer curr.Close()

	e = curr.Fetch(&Result.PlantList, 0, false)

	// tk.Printf("Result.PlantList: %#v \n\n", Result.PlantList)

	filter := tk.M{}

	filter.Set("where", dbox.Nin("Plant", "Qurayyah CC", "PP9"))

	curr = nil
	curr, e = c.DB().Find(&MasterPlant{}, filter)
	e = curr.Fetch(&Result.PlantListH2, 0, false)

	// tk.Printf("Result.PlantListH2: %#v \n\n", Result.PlantListH2)

	curr = nil
	curr, e = c.DB().Find(&MasterEquipmentType{}, nil)
	e = curr.Fetch(&Result.EQTypeList, 0, false)

	// tk.Printf("Result.EQTypeList: %#v \n\n", Result.EQTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterMROElement{}, nil)
	e = curr.Fetch(&Result.MROElementList, 0, false)

	// tk.Printf("Result.MROElementList: %#v \n\n", Result.MROElementList)

	curr = nil
	curr, e = c.DB().Find(&MasterOrderType{}, nil)
	e = curr.Fetch(&Result.OrderTypeList, 0, false)

	// tk.Printf("Result.OrderTypeList: %#v \n\n", Result.OrderTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterActivityType{}, nil)
	e = curr.Fetch(&Result.ActivityTypeList, 0, false)

	// tk.Printf("Result.ActivityTypeList: %#v \n\n", Result.ActivityTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterFailureCode{}, nil)
	e = curr.Fetch(&Result.FailureCode, 0, false)

	// tk.Printf("Result.FailureCode: %#v \n\n", Result.FailureCode)

	var filter1 []*dbox.Filter

	if len(d.selectedPlant) != 0 {
		filter1 = append(filter1, dbox.In("Plant", d.selectedPlant))
	} else {
		filter1 = append(filter1, dbox.Eq("1", "1"))
	}

	/*pipes = append(pipes, tk.M{"$group": tk.M{
		"_id": "$unit",
	}})*/

	curr = nil

	curr, e = c.DB().Connection.NewQuery().
		Select("Unit").
		From(new(MasterUnitPlant).TableName()).
		Where(filter1...).
		Group("Unit").
		Cursor(nil)

	e = curr.Fetch(&Result.UnitList, 0, true)

	// tk.Printf("Result.UnitList: %#v \n\n", Result.UnitList)

	return ResultInfo(Result, e)
}
Ejemplo n.º 9
0
func (m *HistoricalValueEquation) GetAssetWorkData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	type DataValue struct {
		ID                 string
		Plant              string  `json:'Plant'`
		Unit               string  `json:'Unit'`
		ValueEquationCost  float64 `json:'ValueEquationCost'`
		MaxPowerGeneration float64 `json:'MaxPowerGeneration'`
		PotentialRevenue   float64 `json:'PotentialRevenue'`
		NetGeneration      float64 `json:'NetGeneration'`
		Revenue            float64 `json:'Revenue'`
		ForcedOutages      float64 `json:'ForcedOutages'`
		ForcedOutagesLoss  float64 `json:'ForcedOutagesLoss'`
		UnforcedOutages    float64 `json:'UnforcedOutages'`

		UnforcedOutagesLoss float64 `json:'UnforcedOutagesLoss'`
		TotalLabourCost     float64 `json:'TotalLabourCost'`
		TotalMaterialCost   float64 `json:'TotalMaterialCost'`
		TotalServicesCost   float64 `json:'TotalServicesCost'`
		MaintenanceCost     float64 `json:'MaintenanceCost'`
		OperatingCost       float64 `json:'OperatingCost'`
	}

	m.SetPayLoad(k)
	var e error = nil
	result := []*DataValue{}
	c := ctx.Connection
	ve := ValueEquation{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	csr, e := c.NewQuery().
		Where(query...).Select(groupBy).
		Aggr(dbox.AggrSum, "ValueEquationCost", "ValueEquationCost").
		Aggr(dbox.AggrSum, "MaxPowerGeneration", "MaxPowerGeneration").
		Aggr(dbox.AggrSum, "PotentialRevenue", "PotentialRevenue").
		Aggr(dbox.AggrSum, "NetGeneration", "NetGeneration").
		Aggr(dbox.AggrSum, "Revenue", "Revenue").
		Aggr(dbox.AggrSum, "ForcedOutages", "ForcedOutages").
		Aggr(dbox.AggrSum, "ForcedOutagesLoss", "ForcedOutagesLoss").
		Aggr(dbox.AggrSum, "UnforcedOutages", "UnforcedOutages").
		Aggr(dbox.AggrSum, "UnforcedOutagesLoss", "UnforcedOutagesLoss").
		Aggr(dbox.AggrSum, "TotalLabourCost", "TotalLabourCost").
		Aggr(dbox.AggrSum, "TotalMaterialCost", "TotalMaterialCost").
		Aggr(dbox.AggrSum, "TotalServicesCost", "TotalServicesCost").
		Aggr(dbox.AggrSum, "MaintenanceCost", "MaintenanceCost").
		Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
		From(ve.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&result, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range result {
		if i.Unit != "" {
			i.ID = i.Unit
		} else {
			i.ID = i.Plant
		}
	}
	return result, e
}
Ejemplo n.º 10
0
func (m *HistoricalValueEquation) GetPerformanceData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	type DataValue struct {
		ID            string
		Plant         string  `json:'Plant'`
		Unit          string  `json:'Unit'`
		NetGeneration float64 `json:'NetGeneration'`
		PrctWAF       float64 `json:'PrctWAF'`
	}
	m.SetPayLoad(k)
	var e error = nil
	result := []*DataValue{}
	c := ctx.Connection
	ve := ValueEquation{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	csr, e := c.NewQuery().
		Where(query...).Select(groupBy).
		Aggr(dbox.AggrSum, "NetGeneration", "NetGeneration").
		Aggr(dbox.AggrAvr, "PrctWAF", "PrctWAF").
		From(ve.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&result, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range result {
		if i.Unit != "" {
			i.ID = i.Unit
		} else {
			i.ID = i.Plant
		}
	}
	return result, e
}
Ejemplo n.º 11
0
func (m *HistoricalValueEquation) GetDataQuality(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	type DataValue struct {
		ID                       string
		Plant                    string  `json:'Plant'`
		Unit                     string  `json:'Unit'`
		Count                    float64 `json:'Count'`
		CapacityPayment_Data     float64 `json:'CapacityPayment_Data'`
		EnergyPayment_Data       float64 `json:'EnergyPayment_Data'`
		StartupPayment_Data      float64 `json:'StartupPayment_Data'`
		Penalty_Data             float64 `json:'Penalty_Data'`
		Incentive_Data           float64 `json:'Incentive_Data'`
		MaintenanceCost_Data     float64 `json:'MaintenanceCost_Data'`
		MaintenanceDuration_Data float64 `json:'MaintenanceDuration_Data'`
		PrimaryFuel1st_Data      float64 `json:'PrimaryFuel1st_Data'`
		PrimaryFuel2nd_Data      float64 `json:'PrimaryFuel2nd_Data'`
		BackupFuel_Data          float64 `json:'BackupFuel_Data'`
		FuelTransport_Data       float64 `json:'FuelTransport_Data'`
	}

	m.SetPayLoad(k)
	var e error = nil
	result := []*DataValue{}
	c := ctx.Connection
	vedq := ValueEquationDataQuality{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	if m.Scope == "Unit" || (m.Scope == "Plant" && m.Selected != nil && len(m.Selected) == 1) {
		query := []*dbox.Filter{}
		query = append(query, dbox.Gte("Dates", m.StartPeriod))
		query = append(query, dbox.Lte("Dates", m.EndPeriod))
		if m.Scope == "Unit" {
			query = append(query, dbox.Eq("Plant", m.SelectedPlant))
			if m.Selected != nil && len(m.Selected) > 0 {
				query = append(query, dbox.In("Unit", m.Selected))
			}
		} else {
			query = append(query, dbox.Eq("Plant", m.Selected[0]))
		}
		temp := []*ValueEquationDataQuality{}
		csr, e := ctx.Find(new(ValueEquationDataQuality), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			e = csr.Fetch(&temp, 0, false)
		}
		csr.Close()
		if e != nil {
			return nil, e
		} else {
			m.GetValueEquationDocument(ctx, temp)
			return temp, e
		}
	} else {
		csr, e := c.NewQuery().
			Where(query...).Select(groupBy).
			Aggr(dbox.AggrSum, "1", "Count").
			Aggr(dbox.AggrSum, "CapacityPayment_Data", "CapacityPayment_Data").
			Aggr(dbox.AggrSum, "EnergyPayment_Data", "EnergyPayment_Data").
			Aggr(dbox.AggrSum, "StartupPayment_Data", "StartupPayment_Data").
			Aggr(dbox.AggrSum, "Penalty_Data", "Penalty_Data").
			Aggr(dbox.AggrSum, "Incentive_Data", "Incentive_Data").
			Aggr(dbox.AggrSum, "MaintenanceCost_Data", "MaintenanceCost_Data").
			Aggr(dbox.AggrSum, "MaintenanceDuration_Data", "MaintenanceDuration_Data").
			Aggr(dbox.AggrSum, "PrimaryFuel1st_Data", "PrimaryFuel1st_Data").
			Aggr(dbox.AggrSum, "PrimaryFuel2nd_Data", "PrimaryFuel2nd_Data").
			Aggr(dbox.AggrSum, "BackupFuel_Data", "BackupFuel_Data").
			Aggr(dbox.AggrSum, "FuelTransport_Data", "FuelTransport_Data").
			From(vedq.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
		if csr != nil {
			e = csr.Fetch(&result, 0, false)
		}
		csr.Close()
		if e != nil {
			return nil, e
		}
		for _, i := range result {
			if i.Unit != "" {
				i.ID = i.Unit
			} else {
				i.ID = i.Plant
			}
		}
	}
	return result, e
}
Ejemplo n.º 12
0
func (m *HistoricalValueEquation) GetRevenueData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	type DataValue struct {
		ID              string
		Plant           string  `json:'Plant'`
		Unit            string  `json:'Unit'`
		CapacityPayment float64 `json:'CapacityPayment'`
		EnergyPayment   float64 `json:'EnergyPayment'`
		StartupPayment  float64 `json:'StartupPayment'`
		PenaltyAmount   float64 `json:'PenaltyAmount'`
		Incentive       float64 `json:'Incentive'`
		Revenue         float64 `json:'Revenue'`
	}

	m.SetPayLoad(k)
	result, DataChartRevenue, DataChartRevenueEx := tk.M{}, []*DataValue{}, []*DataValue{}
	c := ctx.Connection
	ve := ValueEquation{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	csr, e := c.NewQuery().
		Where(query...).
		Aggr(dbox.AggrSum, "CapacityPayment", "CapacityPayment").
		Aggr(dbox.AggrSum, "EnergyPayment", "EnergyPayment").
		Aggr(dbox.AggrSum, "StartupPayment", "StartupPayment").
		Aggr(dbox.AggrSum, "PenaltyAmount", "PenaltyAmount").
		Aggr(dbox.AggrSum, "Incentive", "Incentive").
		Aggr(dbox.AggrSum, "Revenue", "Revenue").
		From(ve.TableName()).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&DataChartRevenue, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	csr, e = c.NewQuery().
		Where(query...).Select(groupBy).
		Aggr(dbox.AggrSum, "CapacityPayment", "CapacityPayment").
		Aggr(dbox.AggrSum, "EnergyPayment", "EnergyPayment").
		Aggr(dbox.AggrSum, "StartupPayment", "StartupPayment").
		Aggr(dbox.AggrSum, "PenaltyAmount", "PenaltyAmount").
		Aggr(dbox.AggrSum, "Incentive", "Incentive").
		Aggr(dbox.AggrSum, "Revenue", "Revenue").
		From(ve.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&DataChartRevenueEx, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range DataChartRevenueEx {
		if i.Unit != "" {
			i.ID = i.Unit
		} else {
			i.ID = i.Plant
		}
	}
	return result.Set("DataChartRevenue", DataChartRevenue).Set("DataChartRevenueEx", DataChartRevenueEx), e
}
Ejemplo n.º 13
0
func (m *HistoricalValueEquation) GetOperatingData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	type DataValue struct {
		ID                string
		Plant             string  `json:'Plant'`
		Unit              string  `json:'Unit'`
		OperatingCost     float64 `json:'OperatingCost'`
		FuelTransportCost float64 `json:'FuelTransportCost'`
		Capacity          float64 `json:'Capacity'`
		NetGeneration     float64 `json:'NetGeneration'`
	}

	type DataTableType struct {
		IsPrimaryFuel         bool    `json:'IsPrimaryFuel'`
		FuelType              string  `json:'FuelType'`
		ConvertedFuelConsumed float64 `json:'ConvertedFuelConsumed'`
		FuelCostPerUnit       float64 `json:'FuelCostPerUnit'`
		FuelCost              float64 `json:'FuelCost'`
	}

	m.SetPayLoad(k)
	result, DataChart, DataTable, DataTotal, DataDetail := tk.M{}, []tk.M{}, []*DataTableType{}, []*DataValue{}, []*DataValue{}
	c := ctx.Connection
	ve := ValueEquation{}
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	// Getting "DataTable" - skip, need $unwind [ $Fuel ]
	tempValueEquation := []ValueEquation{}
	csr, e := c.NewQuery().
		Where(query...).Select("Id").
		From(ve.TableName()).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&tempValueEquation, 0, false)
	}
	csr.Close()
	VEId := []interface{}{}
	for _, i := range tempValueEquation {
		VEId = append(VEId, i.Id)
	}

	csr, e = c.NewQuery().
		Where(query...).
		Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
		Aggr(dbox.AggrSum, "FuelTransportCost", "FuelTransportCost").
		From(ve.TableName()).Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&DataTotal, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}

	if m.Scope != "Unit" {
		csr, e = c.NewQuery().
			Where(query...).Select(groupBy).
			Aggr(dbox.AggrSum, "Capacity", "Capacity").
			Aggr(dbox.AggrSum, "NetGeneration", "NetGeneration").
			Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
			From(ve.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
		if csr != nil {
			e = csr.Fetch(&DataDetail, 0, false)
		}
		csr.Close()
		if e != nil {
			return nil, e
		}
		for _, i := range DataDetail {
			if i.Unit != "" {
				i.ID = i.Unit
			} else {
				i.ID = i.Plant
			}
		}
	}

	query = append(query[0:0], dbox.In("VEId", VEId...))
	csr, e = c.NewQuery().
		Where(query...).
		Select("IsPrimaryFuel", "FuelType").
		Aggr(dbox.AggrSum, "ConvertedFuelConsumed", "ConvertedFuelConsumed").
		Aggr(dbox.AggrSum, "FuelCostPerUnit", "FuelCostPerUnit").
		Aggr(dbox.AggrSum, "FuelCost", "FuelCost").
		From(new(ValueEquationFuel).TableName()).Group("IsPrimaryFuel", "FuelType").Order("IsPrimaryFuel", "FuelType").Cursor(nil)
	if csr != nil {
		e = csr.Fetch(&DataTable, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}

	result.Set("DataChart", DataChart).Set("DataTable", DataTable).Set("DataTotal", DataTotal).Set("DataDetail", DataDetail)
	return result, e
}
Ejemplo n.º 14
0
func (m *HistoricalValueEquation) GetMaintenanceData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	m.SetPayLoad(k)
	type DataValue struct {
		ID            string
		DataSource    string  `json:'DataSource'`
		WorkOrderType string  `json:'WorkOrderType'`
		Plant         string  `json:'Plant'`
		Unit          string  `json:'Unit'`
		LaborCost     float64 `json:'LaborCost'`
		MaterialCost  float64 `json:'MaterialCost'`
		ServiceCost   float64 `json:'ServiceCost'`
	}

	result, DataMainEx, DataOrder, DataChart, DataTable, Temp, IDList := tk.M{}, []*DataValue{}, []*DataValue{}, []tk.M{}, []DataValue{}, []tk.M{}, []interface{}{}
	c := ctx.Connection
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))
	groupBy := "Plant"
	switch m.Scope {
	case "Kingdom":
		break
	case "Plant":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
		groupBy = "Unit"
		break
	case "Phase":
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Phase", m.Selected))
		}
		break
	case "Unit":
		query = append(query, dbox.Eq("Plant", m.SelectedPlant))
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Unit", m.Selected))
		}
		groupBy = "Unit"
		break
	default:
		break
	}

	// Get DataMainEx
	csr, e := c.NewQuery().
		Where(query...).
		Select(groupBy).
		Aggr(dbox.AggrSum, "TotalLabourCost", "LaborCost").
		Aggr(dbox.AggrSum, "TotalMaterialCost", "MaterialCost").
		Aggr(dbox.AggrSum, "TotalServicesCost", "ServiceCost").
		From(new(ValueEquation).TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
	if e != nil {
		return nil, e
	}
	if csr != nil {
		e = csr.Fetch(&DataMainEx, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range DataMainEx {
		if i.Unit != "" {
			i.ID = i.Unit
		} else {
			i.ID = i.Plant
		}
	}

	csr, e = c.NewQuery().
		Where(query...).Select("Id").
		From(new(ValueEquation).TableName()).Cursor(nil)
	if e != nil {
		return nil, e
	}
	if csr != nil {
		e = csr.Fetch(&Temp, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range Temp {
		IDList = append(IDList, i.GetInt("id"))
	}

	query = []*dbox.Filter{}
	query = append(query, dbox.In("VEId", IDList...))

	// Get DataTable
	csr, e = c.NewQuery().
		Where(query...).
		Select("DataSource", "WorkOrderType").
		Aggr(dbox.AggrSum, "LaborCost", "LaborCost").
		Aggr(dbox.AggrSum, "MaterialCost", "MaterialCost").
		Aggr(dbox.AggrSum, "ServiceCost", "ServiceCost").
		From(new(ValueEquationDetails).TableName()).Group("DataSource", "WorkOrderType").
		Order("DataSource").
		Cursor(nil)
	if e != nil {
		return nil, e
	}
	if csr != nil {
		e = csr.Fetch(&DataTable, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}

	// Get DataOrder - For Visualisation
	csr, e = c.NewQuery().
		Where(query...).
		Select("WorkOrderType").
		From(new(ValueEquationTop10).TableName()).Group("WorkOrderType").Cursor(nil)
	if e != nil {
		return nil, e
	}
	if csr != nil {
		e = csr.Fetch(&DataOrder, 0, false)
	}
	csr.Close()
	if e != nil {
		return nil, e
	}
	for _, i := range DataOrder {
		if i.Unit != "" {
			i.ID = i.Unit
		} else {
			i.ID = i.Plant
		}
	}

	// Data Chart - set for empty, if you want to add it. its available on the previous version while its still using beego.
	result.Set("DataMainEx", DataMainEx).Set("DataOrder", DataOrder).Set("DataChart", DataChart).Set("DataTable", DataTable)
	return result, nil
}
Ejemplo n.º 15
0
func (m *HistoricalValueEquation) GetSummaryData(ctx *orm.DataContext, k *knot.WebContext) (interface{}, error) {
	m.SetPayLoad(k)
	type DataValue struct {
		ID              string
		Plant           string  `json:'Plant'`
		Unit            string  `json:'Unit'`
		Revenue         float64 `json:'Revenue'`
		MaintenanceCost float64 `json:'MaintenanceCost'`
		OperatingCost   float64 `json:'OperatingCost'`
	}
	result, DataChart, DataDetail := tk.M{}, []DataValue{}, []*DataValue{}
	c := ctx.Connection
	ve := new(ValueEquation)
	query := []*dbox.Filter{}
	query = append(query, dbox.Gte("Dates", m.StartPeriod))
	query = append(query, dbox.Lte("Dates", m.EndPeriod))

	csr, e := c.NewQuery().
		Where(query...).
		Select("Plant").
		Aggr(dbox.AggrSum, "Revenue", "Revenue").
		Aggr(dbox.AggrSum, "MaintenanceCost", "MaintenanceCost").
		Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
		From(ve.TableName()).Group("Plant").Order("Plant").Cursor(nil)
	if e != nil {
		return nil, e
	}

	if csr != nil {
		e = csr.Fetch(&DataChart, 0, false)
	}
	if e != nil {
		return nil, e
	}
	csr.Close()
	result.Set("DataChart", DataChart)

	groupBy := "Plant"
	if m.Scope == "Plant" {
		groupBy = "Unit"
		if m.Selected != nil && len(m.Selected) > 0 {
			query = append(query, dbox.In("Plant", m.Selected))
		}
	}

	if m.Scope != "Unit" {
		csr, e := c.NewQuery().
			Where(query...).Select(groupBy).
			Aggr(dbox.AggrSum, "Revenue", "Revenue").
			Aggr(dbox.AggrSum, "MaintenanceCost", "MaintenanceCost").
			Aggr(dbox.AggrSum, "OperatingCost", "OperatingCost").
			From(ve.TableName()).Group(groupBy).Order(groupBy).Cursor(nil)
		if csr != nil {
			e = csr.Fetch(&DataDetail, 0, false)
		}
		csr.Close()
		if e != nil {
			return nil, e
		}
		for _, i := range DataDetail {
			if i.Unit != "" {
				i.ID = i.Unit
			} else {
				i.ID = i.Plant
			}
		}
	}

	result.Set("DataDetail", DataDetail)
	return result, nil
}
Ejemplo n.º 16
0
func (d *DataSourceController) filterParse(where toolkit.M) *dbox.Filter {
	field := where.Get("field", "").(string)
	value := fmt.Sprintf("%v", where["value"])

	if key := where.Get("key", "").(string); key == "Eq" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Eq(field, valueInt)
		} else {
			return dbox.Eq(field, value)
		}
	} else if key == "Ne" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Ne(field, valueInt)
		} else {
			return dbox.Ne(field, value)
		}
	} else if key == "Lt" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Lt(field, valueInt)
		} else {
			return dbox.Lt(field, value)
		}
	} else if key == "Lte" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Lte(field, valueInt)
		} else {
			return dbox.Lte(field, value)
		}
	} else if key == "Gt" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Gt(field, valueInt)
		} else {
			return dbox.Gt(field, value)
		}
	} else if key == "Gte" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Gte(field, valueInt)
		} else {
			return dbox.Gte(field, value)
		}
	} else if key == "In" {
		valueArray := []interface{}{}
		for _, e := range strings.Split(value, ",") {
			valueArray = append(valueArray, strings.Trim(e, ""))
		}
		return dbox.In(field, valueArray...)
	} else if key == "Nin" {
		valueArray := []interface{}{}
		for _, e := range strings.Split(value, ",") {
			valueArray = append(valueArray, strings.Trim(e, ""))
		}
		return dbox.Nin(field, valueArray...)
	} else if key == "Contains" {
		return dbox.Contains(field, value)
	} else if key == "Or" {
		subs := where.Get("value", []interface{}{}).([]interface{})
		filtersToMerge := []*dbox.Filter{}
		for _, eachSub := range subs {
			eachWhere, _ := toolkit.ToM(eachSub)
			filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere))
		}
		return dbox.Or(filtersToMerge...)
	} else if key == "And" {
		subs := where.Get("value", []interface{}{}).([]interface{})
		filtersToMerge := []*dbox.Filter{}
		for _, eachSub := range subs {
			eachWhere, _ := toolkit.ToM(eachSub)
			filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere))
		}
		return dbox.And(filtersToMerge...)
	}

	return nil
}
Ejemplo n.º 17
0
func (q *Query) Exec(in toolkit.M) error {
	setting, e := q.prepare(in)
	commandType := setting["commandtype"].(string)
	if e != nil {
		return err.Error(packageName, modQuery, "Exec: "+commandType, e.Error())
	}

	if setting.GetString("commandtype") == dbox.QueryPartSelect {
		return err.Error(packageName, modQuery, "Exec: "+commandType, "Exec is not working with select command, please use .Cursor instead")
	}

	q.Lock()
	defer q.Unlock()

	var dataM toolkit.M
	var dataMs []toolkit.M

	hasData := in.Has("data")
	dataIsSlice := false
	data := in.Get("data")
	if toolkit.IsSlice(data) {
		dataIsSlice = true
		e = toolkit.Unjson(toolkit.Jsonify(data), dataMs)
		if e != nil {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data encoding error: "+e.Error())
		}
	} else {
		dataM, e = toolkit.ToM(data)
		dataMs = append(dataMs, dataM)
		if e != nil {
			return err.Error(packageName, modQuery, "Exec: "+commandType, "Data encoding error: "+e.Error())
		}
	}

	hasWhere := in.Has("where")
	where := in.Get("where", []*dbox.Filter{}).([]*dbox.Filter)

	if hasData && hasWhere == false && toolkit.HasMember([]interface{}{dbox.QueryPartInsert, dbox.QueryPartUpdate, dbox.QueryPartSave}, commandType) {
		hasWhere = true
		if toolkit.IsSlice(data) {
			ids := []interface{}{}
			idField := ""
			if idField == "" {
				return err.Error(packageName, modQuery, "Exec:"+commandType, "Data send is a slice, but its element has no ID")
			}
			dataCount := toolkit.SliceLen(data)
			for i := 0; i < dataCount; i++ {
				dataI := toolkit.SliceItem(data, i)
				if i == 0 {
					idField = toolkit.IdField(dataI)
				}
				ids = append(ids, toolkit.Id(dataI))
			}
			where = []*dbox.Filter{dbox.In(idField, ids)}
		} else {
			id := toolkit.Id(data)
			if toolkit.IsNilOrEmpty(id) {
				where = []*dbox.Filter{dbox.Eq(toolkit.IdField(id), id)}
			} else {
				where = nil
				hasWhere = false
			}
		}
	}

	q.openFile()
	if commandType == dbox.QueryPartInsert {
		if !hasData {
			return err.Error(packageName, modQuery, "Exec:"+commandType, "Data is empty")
		}
		if dataIsSlice {
			q.data = append(q.data, dataMs...)
		} else {
			q.data = append(q.data, dataM)
		}
	} else if commandType == dbox.QueryPartUpdate {
		if !hasData {
			return err.Error(packageName, modQuery, "Exec:"+commandType, "Data is empty")
		}

		var indexes []interface{}
		if hasWhere {
			toolkit.Serde(dbox.Find(q.data, where), &indexes, "")
		}

		var dataUpdate toolkit.M
		var updateDataIndex int

		isDataSlice := toolkit.IsSlice(data)
		if isDataSlice == false {
			isDataSlice = false
			e = toolkit.Serde(data, &dataUpdate, "")
			if e != nil {
				return err.Error(packageName, modQuery, "Exec:"+commandType, "Unable to serialize data. "+e.Error())
			}
		}
		var idField string
		for i, v := range q.data {
			if toolkit.HasMember(indexes, i) || len(indexes) == 0 {
				if idField == "" {
					idField = toolkit.IdField(v)
					if idField == "" {
						return err.Error(packageName, modQuery, "Exec:"+commandType, "No ID")
					}
				}

				var dataOrigin toolkit.M
				e = toolkit.Serde(v, &dataOrigin, "")
				if e != nil {
					return err.Error(packageName, modQuery, "Exec:"+commandType, "Unable to serialize data origin. "+e.Error())
				}
				if isDataSlice {
					e = toolkit.Serde(toolkit.SliceItem(data, updateDataIndex), &dataUpdate, "")
					if e != nil {
						return err.Error(packageName, modQuery, "Exec:"+commandType, "Unable to serialize data. "+e.Error())
					}
					updateDataIndex++
				}
				for fieldName, fieldValue := range dataUpdate {
					if fieldName != idField {
						if dataOrigin.Has(fieldName) {
							dataOrigin.Set(fieldName, fieldValue)
						}

					}
				}
				toolkit.Serde(dataOrigin, &v, "")
				q.data[i] = v
			}
		}
	} else if commandType == dbox.QueryPartDelete {
		if hasWhere {
			var indexes []interface{}
			toolkit.Serde(dbox.Find(q.data, where), &indexes, "")
			if len(indexes) > 0 {
				newdata := []toolkit.M{}
				for index, v := range q.data {
					if toolkit.HasMember(indexes, index) == false {
						newdata = append(newdata, v)
					}
				}
				q.data = newdata
			}
		} else {
			q.data = []toolkit.M{}
		}
	} else if commandType == dbox.QueryPartSave {
		if !hasData {
			return err.Error(packageName, modQuery, "Exec:"+commandType, "Data is empty")
		}
	}
	q.writeFile()
	return nil
}