Exemplo n.º 1
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 reflect.ValueOf(val).Kind() == reflect.Map {
				mVal := val.(map[string]interface{})
				tomVal, _ := toolkit.ToM(mVal)
				switch {
				case tomVal.Has("$eq"):
					fb = dbox.Eq(key, tomVal["$eq"].(string))
				case tomVal.Has("$ne"):
					fb = dbox.Ne(key, tomVal["$ne"].(string))
				case tomVal.Has("$regex"):
					fb = dbox.Contains(key, tomVal["$regex"].(string))
				case tomVal.Has("$gt"):
					fb = dbox.Gt(key, tomVal["$gt"].(string))
				case tomVal.Has("$gte"):
					fb = dbox.Gte(key, tomVal["$gte"].(string))
				case tomVal.Has("$lt"):
					fb = dbox.Lt(key, tomVal["$lt"].(string))
				case tomVal.Has("$lte"):
					fb = dbox.Lte(key, tomVal["$lte"].(string))
				}
			} else {
				fb = dbox.Eq(key, val)
			}
		}
	}

	return fb
}
Exemplo n.º 2
0
func TestSelect(t *testing.T) {
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
	}
	defer c.Close()

	csr, e := c.NewQuery().Select("1", "2", "3", "4", "5").From("HIST").
		// Where(dbox.Contains("2", "183")).
		Where(dbox.Ne("1", "")).
		Cursor(nil)
	if e != nil {
		t.Errorf("Cursor pre error: %s \n", e.Error())
		return
	}
	if csr == nil {
		t.Errorf("Cursor not initialized")
		return
	}
	defer csr.Close()

	fmt.Println("Jumlah selected : ", csr.Count())

	results := make([]map[string]interface{}, 0)
	e = csr.Fetch(&results, 2, false)
	if e != nil {
		t.Errorf("Unable to fetch N1: %s \n", e.Error())
	} else {
		fmt.Printf("Fetch N1 OK. Result: %v \n", results)
	}

	// e = csr.Fetch(&results, 6, false)
	// if e != nil {
	// 	t.Errorf("Unable to fetch N2: %s \n", e.Error())
	// } else {
	// 	fmt.Printf("Fetch N2 OK. Result: %v \n", results)
	// }

	// e = csr.ResetFetch()
	// if e != nil {
	// 	t.Errorf("Unable to reset fetch: %s \n", e.Error())
	// }

	// ds, e = csr.Fetch(nil, 5, false)
	// if e != nil {
	// 	t.Errorf("Unable to fetch N3: %s \n", e.Error())
	// } else {
	// 	fmt.Printf("Fetch N3 OK. Result: %v \n", ds.Data)
	// }
}
Exemplo n.º 3
0
func (c *DashboardController) GetNumberOfTurbines(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	var e error
	r := new(tk.Result)

	d := struct {
		StartDate string
		EndDate   string
		Plant     []string
	}{}

	e = k.GetPayload(&d)

	r.Run(func(in interface{}) (interface{}, error) {
		var filter []*dbox.Filter

		selectedPeriod := time.Now().Year() - 1

		filter = append(filter, dbox.Eq("Year", selectedPeriod))
		filter = append(filter, dbox.Ne("UnitType", ""))

		if len(d.Plant) != 0 {
			filter = append(filter, dbox.Eq("Plant", d.Plant[0]))
		}

		result := make([]tk.M, 0)

		cursor, _ := c.DB().Connection.NewQuery().
			Select("UnitType as _id").
			From("ValueEquation_Dashboard").
			Where(filter...).
			Group("UnitType").
			Aggr(dbox.AggrSum, 1, "count").
			Order("count").
			Cursor(nil)

		defer cursor.Close()
		e = cursor.Fetch(&result, 0, true)

		e = c.CheckNotError(e)
		return result, e
	}, nil)

	return ResultInfo(r, e)
}
Exemplo n.º 4
0
func TestFilter(t *testing.T) {
	fb := dbox.NewFilterBuilder(new(FilterBuilder))
	fb.AddFilter(dbox.Or(
		dbox.Contains("regfield", "1"),
		dbox.Ne("nefield", 1),
		dbox.Eq("group", "administrators")))
	b, e := fb.Build()
	if e != nil {
		t.Errorf("Error %s", e.Error())
	} else {
		fmt.Printf("Result:\n%v\n", toolkit.JsonString(b))
	}

	fb = dbox.NewFilterBuilder(new(FilterBuilder))
	fb.AddFilter(dbox.And(dbox.Or(dbox.Eq("EmployeeId", "101-102-10"), dbox.Eq("EmployeeId", "101-102-3"), dbox.Eq("EmployeeId", "101-102-4")), dbox.Eq("Age", "30")))
	c, e := fb.Build()
	if e != nil {
		t.Errorf("Error %s", e.Error())
	} else {
		fmt.Printf("Result:\n%v\n", toolkit.JsonString(c))
	}
}
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
}
Exemplo n.º 6
0
func (d *GenDataBrowser) generateMaintenanceDataBrowser() (e error) {
	ctx := d.BaseController.Ctx
	c := ctx.Connection
	// dataToSave := []DataBrowser{}
	// dataCount = 0

	tk.Println("Generating Maintenance Data Browser..")

	// Get fuelCost
	csr, e := c.NewQuery().From(new(FuelCost).TableName()).Cursor(nil)
	e = csr.Fetch(&fuelCosts, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	// Get plants
	csr, e = c.NewQuery().From(new(PowerPlantCoordinates).TableName()).Cursor(nil)
	e = csr.Fetch(&plants, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	// Get generalInfo
	csr, e = c.NewQuery().From(new(GeneralInfo).TableName()).Cursor(nil)
	e = csr.Fetch(&generalInfos, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	// Get GeneralInfoDetails
	csr, e = c.NewQuery().From(new(GeneralInfoDetails).TableName()).Cursor(nil)
	e = csr.Fetch(&generalInfoDetails, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	// Get GeneralInfoActualFuelConsumption
	csr, e = c.NewQuery().From(new(GeneralInfoActualFuelConsumption).TableName()).Cursor(nil)
	e = csr.Fetch(&generalInfoActualFuelConsumption, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	// Get NewEquipmentType
	csr, e = c.NewQuery().Select("EquipmentType, NewEquipmentGroup").From(new(NewEquipmentType).TableName()).Where(dbox.Ne("NewEquipmentGroup", "Disregard")).Cursor(nil)
	e = csr.Fetch(&newEquipmentType, 0, false)
	ErrorHandler(e, "generateMaintenanceDataBrowser")
	csr.Close()

	constructEquipmentType(newEquipmentType)

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

	for _, plant := range plants {
		go doGenerateMaintenanceDataBrowser(wg, d, plant)
	}

	wg.Wait()

	return
}
Exemplo n.º 7
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
}
Exemplo n.º 8
0
func (a *DataFlowController) GetDataMonitoring(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	e := r.GetPayload(&payload)
	if e != nil {
		return helper.CreateResult(false, nil, e.Error())
	}

	status := tk.ToString(payload["status"])

	filters := []*dbox.Filter{}
	var filter *dbox.Filter
	filter = new(dbox.Filter)

	if strings.Contains(strings.ToLower(status), "run") {
		filters = append(filters, dbox.Eq("status", "RUN"))
	} else {
		filters = append(filters, dbox.Ne("status", "RUN"))
	}

	take := tk.ToInt(payload["take"], tk.RoundingAuto)
	skip := tk.ToInt(payload["skip"], tk.RoundingAuto)

	start := tk.ToString(payload["startdate"])
	end := tk.ToString(payload["enddate"])

	search := tk.ToString(payload["search"])

	startdate := time.Now()
	enddate := time.Now()

	if start != "" {
		startdate, _ = time.Parse(time.RFC3339, start)
		filters = append(filters, dbox.Gte("startdate", startdate))
		startdate = startdate.AddDate(0, 0, 1)
		startdate = startdate.Add(time.Duration(-1) * time.Second)
		filters = append(filters, dbox.Lte("startdate", startdate))
	}

	if end != "" && !strings.Contains(strings.ToLower(status), "run") {
		enddate, _ = time.Parse(time.RFC3339, end)
		filters = append(filters, dbox.Gte("enddate", enddate))
		enddate = enddate.AddDate(0, 0, 1)
		enddate = enddate.Add(time.Duration(-1) * time.Second)
		filters = append(filters, dbox.Lte("enddate", enddate))
	}

	if search != "" {
		filters = append(filters, dbox.Or(dbox.Contains("flow.name", search), dbox.Contains("flow.description", search)))
	}

	filter = dbox.And(filters...)
	dataDs := []colonycore.DataFlowProcess{}
	cursor, err := colonycore.Finds(new(colonycore.DataFlowProcess), tk.M{}.Set("where", filter).Set("take", take).Set("skip", skip).Set("order", []string{"-startdate"}))
	cursorCount, _ := colonycore.Finds(new(colonycore.DataFlowProcess), tk.M{}.Set("where", filter))
	res := tk.M{}
	if cursor != nil {
		cursor.Fetch(&dataDs, 0, false)
		defer cursor.Close()
		defer cursorCount.Close()
		res.Set("total", cursorCount.Count())
	} else {
		res.Set("total", 0)
	}

	if err != nil && cursor != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	res.Set("data", dataDs)

	return helper.CreateResult(true, res, "success")
}