Esempio n. 1
0
func Test1(t *testing.T) {
	t.Skip()
	fmt.Println("Test 1 - Generate x Number")
	numCount := 1000
	workerCount := 100
	idxs := make([]interface{}, numCount)
	want := 0
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i
		want += i
	}

	total := 0
	fmt.Printf("Run Parallel %d job within %d pools \n", numCount, workerCount)
	pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
		for j := range js {
			time.Sleep(1 * time.Microsecond)
			j2 := j.(int)
			total += j2
			r := new(toolkit.Result)
			r.Status = toolkit.Status_OK
			r.Data = j2
			rs <- r
		}
	})

	if total == want && pr.Success == 1000 {
		fmt.Printf("Test OK in %v \n\n", pr.Duration)
	} else {
		fmt.Printf("Test Fail want %d  got %d and has %d records\n", total, want, pr.Success)
	}
}
Esempio n. 2
0
func TestDb2(t *testing.T) {
	fmt.Println("Test 2b - Insert Data (Parallel)")
	numCount := pnumCount
	workerCount := pworkerCount
	idxs := make([]interface{}, numCount)
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i + 2000
	}

	conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
	//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
	e = conn.Connect()
	if e != nil {
		t.Error("Unable to connect to Database | " + e.Error())
		return
	}
	defer conn.Close()
	_, e = conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})
	if e != nil {
		fmt.Println("Unable to delete: " + e.Error())
	}

	fmt.Printf("Insert %d data within %d pools \n", numCount, workerCount)
	pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
		for jb := range js {
			j := jb.(int)
			user := toolkit.M{}
			userid := "User " + strconv.Itoa(j)
			user.Set("_id", userid)
			user.Set("fullname", "User "+strconv.Itoa(j))
			user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
			r := new(toolkit.Result)
			//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
			_, e := conn.Execute("appusers",
				toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
			if e == nil {
				r.Status = toolkit.Status_OK
				r.Data = user
			} else {
				r.Status = toolkit.Status_NOK
				r.Message = "Error ID " + strconv.Itoa(j) + " " + e.Error()
			}
			rs <- r
		}
	})

	if pr.Success == numCount {
		fmt.Printf("Test OK in %v \n\n", pr.Duration)
	} else {
		fmt.Printf("Test Fail has %d records. \nErrors\n%v\nIn %v \n\n", pr.Success, toolkit.JsonString(pr.Errors[0]), pr.Duration)
	}
}
Esempio n. 3
0
func (c *DashboardController) GetPowerVsFuelConsumtion(k *knot.WebContext) interface{} {
	//c.LoadBase(k)
	k.Config.OutputType = knot.OutputJson

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

	e := k.GetPayload(&d)

	r := new(tk.Result)
	r.Run(func(in interface{}) (interface{}, error) {

		var (
			filter []*dbox.Filter
		)

		selectedPeriod := d.Period
		filter = append(filter, dbox.Eq("Year", selectedPeriod))

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

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

		cursor, e := c.DB().Connection.NewQuery().
			Select("Plant as _id").
			From("ValueEquation_Dashboard").
			Where(filter...).
			Group("Plant").
			Aggr(dbox.AggrSum, "UpdatedFuelConsumption", "FuelConsumtion").
			Aggr(dbox.AggrSum, "NetGeneration", "Power").
			Order("_id").
			Cursor(nil)

		defer cursor.Close()

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

		e = c.CheckNotError(e)

		return result, e
	}, nil)

	return ResultInfo(r, e)
}
Esempio n. 4
0
func (c *DashboardController) GetNumberOfWorkOrder(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

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

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

	e = k.GetPayload(&d)

	r.Run(func(in interface{}) (interface{}, error) {

		filter := ""
		sintax := ""

		if len(d.Plant) > 0 {
			filter = d.Plant[0]
		}

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

		if filter == "" {
			sintax = "select dbo.ValueEquation_Dashboard.Year as period, dbo.VEDTop10.WorkOrderType, count(*) as count, sum(dbo.VEDTop10.MaintenanceCost) as cost from dbo.ValueEquation_Dashboard inner join dbo.VEDTop10 on dbo.ValueEquation_Dashboard.Id = dbo.VEDTop10.VEId group by dbo.ValueEquation_Dashboard.Year, dbo.VEDTop10.WorkOrderType order by period asc, cost asc"
		} else {
			sintax = "select dbo.ValueEquation_Dashboard.Year as period, dbo.VEDTop10.WorkOrderType, count(*) as count, sum(dbo.VEDTop10.MaintenanceCost) as cost from dbo.ValueEquation_Dashboard inner join dbo.VEDTop10 on dbo.ValueEquation_Dashboard.Id = dbo.VEDTop10.VEId where dbo.ValueEquation_Dashboard.Plant = '" + filter + "' group by dbo.ValueEquation_Dashboard.Year, dbo.VEDTop10.WorkOrderType order by period asc, cost asc"
		}

		cursor, e := c.DB().Connection.NewQuery().
			Command("freequery", tk.M{}.
				Set("syntax", sintax)).
			Cursor(nil)

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

		e = c.CheckNotError(e)

		return result, e
	}, nil)

	return ResultInfo(r, e)
}
Esempio n. 5
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)
}
Esempio n. 6
0
func checkResult(result *toolkit.Result, t *testing.T) {
	if result.Status != toolkit.Status_OK {
		t.Error(result.Message)
	} else {
		if result.IsEncoded() == false {
			t.Logf("Result: %v", result.Data)
		} else {

			m := struct {
				HelloMessage string
				//TimeNow      time.Time
				Scores []Score
			}{}

			//m := toolkit.M{}
			e := result.GetFromBytes(&m)
			if e != nil {
				t.Errorf("Unable to decode result: %s\n", e.Error())
				return
			}
			t.Logf("Result (decoded): %s", toolkit.JsonString(m))
		}
	}
}
Esempio n. 7
0
func (s *Subscriber) Start(address string) error {
	s.Address = address
	s.Actions = map[string]FnTrigger{}
	s.MessageQues = map[string]*MessageQue{}
	broadcasterUrl := s.BroadcasterAddress + "/nodeadd?node=" + s.Address
	r, e := toolkit.HttpCall(broadcasterUrl, "GET", nil, nil)
	if e != nil {
		return fmt.Errorf("Unable to contact Broadcaster Server. %s", e.Error())
	}
	if sOk := toolkit.HttpContentString(r); sOk != "OK" {
		return fmt.Errorf("Unable to add %s as subsriber to %s. Error: %s", s.Address, s.BroadcasterAddress, sOk)
	}

	//-- inform subscriber if newkey is available to be collected
	s.Route("/newkey", func(k *knot.WebContext) interface{} {
		result := toolkit.NewResult()
		k.Config.OutputType = knot.OutputJson
		msg := new(MessageQue)
		e := k.GetPayload(&msg)
		if e != nil {
			result.Message = e.Error()
			result.Status = toolkit.Status_NOK
		} else {
			msg.Data = nil
			msg.Collected = false
			_, exist := s.MessageQues[msg.Key]
			if !exist {
				s.messageKeys = append(s.messageKeys, msg.Key)
			}
			s.MessageQues[msg.Key] = msg
			//s.messageKeys = append(s.messageKeys, msg.Key)
		}
		return result
	})

	s.Route("/getmsg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		key := k.Query("key")
		result := s.getMsgAsResult(key)
		return result
	})

	//-- replicate message to subscribe
	s.Route("/msg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		var payload Message
		eDecode := k.GetPayload(&payload)
		if eDecode != nil {
			result.Status = toolkit.Status_NOK
			result.Message = fmt.Sprintf("Subscriber Message Decode Error %s", eDecode.Error())
		} else {
			result.Data = payload
		}
		return result
	})

	//-- stop the server
	s.Route("/stop", func(k *knot.WebContext) interface{} {
		defer k.Server.Stop()
		k.Config.OutputType = knot.OutputJson
		result := new(toolkit.Result)
		result.Status = "OK"
		return result
	})

	go func() {
		s.Server.Listen()
	}()
	return nil
}
Esempio n. 8
0
func (this *DataBrowserController) SaveExcel(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	d := DataBrowserInput{}

	_ = k.GetPayload(&d)

	var (
		DisplaySumList []SumList
	)

	r := new(tk.Result)

	r.Run(func(in interface{}) (interface{}, error) {

		params, e := getDataBrowser(d)

		// ret.Set("Datas", datas)

		// get total and summary

		total := 0          //k.Session(d.Hypoid+"Total", nil)
		summary := []tk.M{} //k.Session(d.Hypoid+"Summary", nil)

		// if total == nil || summary == nil || len(fieldsdouble) > 0 {
		summaryStr := " count(*) as Total"

		for _, val := range d.Fieldsdouble {
			strSum := ",(Select CAST((Sum(" + val + ")) as float)) as " + val + "sum"
			strAvg := ",(Select CAST((Avg(" + val + ")) as float)) as " + val + "avg"

			summaryStr += strSum
			summaryStr += strAvg
		}

		params.Set("@Summary", summaryStr)

		script := getSQLScript(SQLScript+"/databrowser_h3_summary.sql", params)
		// tk.Printf("---\n%#v \n----\n", script)
		cursorTotal, e := this.DB().Connection.NewQuery().
			Command("freequery", tk.M{}.Set("syntax", script)).
			Cursor(nil)

		defer cursorTotal.Close()

		resSum := []tk.M{}

		e = cursorTotal.Fetch(&resSum, 0, true)

		if e != nil && e.Error() == "No more data to fetched!" {
			e = nil
		}

		if len(resSum) > 0 {
			tmp := resSum[0]
			total = tmp.GetInt("total")
			tmpSummary := tk.M{}

			for _, val := range d.Fieldsdouble {
				tmpSummary.Set(val+"avg", tmp.GetFloat64(strings.ToLower(val+"avg")))
				tmpSummary.Set(val+"sum", tmp.GetFloat64(strings.ToLower(val+"sum")))
			}

			summary = []tk.M{tmpSummary}
		}

		params.Set("@Offset", 0)
		params.Set("@Limit", total)

		script = getSQLScript(SQLScript+"/databrowser_h3.sql", params)

		// tk.Printf("---\n%#v \n----\n", script)
		cursor, e := this.DB().Connection.NewQuery().
			Command("freequery", tk.M{}.Set("syntax", script)).
			Cursor(nil)

		defer cursor.Close()

		// datas := []SPDataBrowser{}
		datas := make([]tk.M, 0)

		e = cursor.Fetch(&datas, 0, true)

		if e != nil && e.Error() == "No more data to fetched!" {
			e = nil
		}

		DisplayTypeCount := d.DisplayTypeCount
		DisplaySumList = []SumList{}
		for i := 0; i < DisplayTypeCount; i++ {
			sumData := SumList{}
			sumData.field = d.DisplayTypeList[i].Get("field").(string)
			sumData.tipe = d.DisplayTypeList[i].Get("type").(string)
			DisplaySumList = append(DisplaySumList, sumData)
		}

		excelFile, e := this.genExcelFile(d.HeaderList, d.Fields, datas, summary, DisplaySumList)
		return "../" + excelFile, e
	}, nil)

	tk.Printf("%#v \n", r)

	return r
}
Esempio n. 9
0
func (this *DataBrowserController) GetFilter(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	var e error

	r := new(tk.Result)
	d := DataBrowserInput{}
	f := tk.M{}
	ret := tk.M{}

	_ = k.GetForms(&f)
	_ = k.GetPayload(&d)

	// tk.Printf("%#v \n", f)

	r.Run(func(in interface{}) (interface{}, error) {

		activeField := f.GetString("active_field")

		if activeField != "" {
			params, err := getDataBrowser(d)
			// get datas
			params.Set("@GROUP", activeField)

			script := getSQLScript(SQLScript+"/databrowser_h3_filter.sql", params)

			// tk.Printf("---\n%#v \n----\n", script)
			cursor, err := this.DB().Connection.NewQuery().
				Command("freequery", tk.M{}.Set("syntax", script)).
				Cursor(nil)

			defer cursor.Close()

			// datas := []SPDataBrowser{}
			tmpDatas := []tk.M{}
			datas := []tk.M{}

			err = cursor.Fetch(&tmpDatas, 0, true)

			if e != nil && e.Error() == "No more data to fetched!" {
				e = nil
			}

			if len(tmpDatas) > 0 {
				for _, val := range tmpDatas {
					tmp := tk.M{}
					tmp.Set("_id", val.Get(strings.ToLower(activeField)))
					tmp.Set(activeField, val.Get(strings.ToLower(activeField)))

					datas = append(datas, tmp)
				}
			}

			ret.Set("Data", datas)

			e = err
		}

		return ret, e
	}, nil)

	return r
}
Esempio n. 10
0
func (this *DataBrowserController) GetGridDb(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	r := new(tk.Result)
	d := DataBrowserInput{}
	_ = k.GetPayload(&d)
	ret := tk.M{}

	r.Run(func(in interface{}) (interface{}, error) {

		params, e := getDataBrowser(d)

		// get datas

		script := getSQLScript(SQLScript+"/databrowser_h3.sql", params)

		// tk.Printf("---\n%#v \n----\n", script)
		datas := []SPDataBrowser{}
		cursor, e := this.DB().Connection.NewQuery().
			Command("freequery", tk.M{}.Set("syntax", script)).
			Cursor(nil)
		e = cursor.Fetch(&datas, 0, true)

		cursor.Close()

		if e != nil && e.Error() == "No more data to fetched!" {
			e = nil
		}

		ret.Set("Datas", datas)

		// get total and summary

		total := k.Session(d.Hypoid+"Total", nil)
		summary := k.Session(d.Hypoid+"Summary", nil)

		// if total == nil || summary == nil || len(fieldsdouble) > 0 {
		summaryStr := " count(*) as Total"

		for _, val := range d.Fieldsdouble {
			strSum := ",(Select CAST((Sum(" + val + ")) as float)) as " + val + "sum"
			strAvg := ",(Select CAST((Avg(" + val + ")) as float)) as " + val + "avg"

			summaryStr += strSum
			summaryStr += strAvg
		}

		params.Set("@Summary", summaryStr)

		script = getSQLScript(SQLScript+"/databrowser_h3_summary.sql", params)
		// tk.Printf("---\n%#v \n----\n", script)
		resSum := []tk.M{}
		cursorTotal, e := this.DB().Connection.NewQuery().
			Command("freequery", tk.M{}.Set("syntax", script)).
			Cursor(nil)
		e = cursorTotal.Fetch(&resSum, 0, true)
		cursorTotal.Close()

		if e != nil && e.Error() == "No more data to fetched!" {
			e = nil
		}

		if len(resSum) > 0 {
			tmp := resSum[0]
			total = tmp.GetInt("total")
			tmpSummary := tk.M{}

			for _, val := range d.Fieldsdouble {
				tmpSummary.Set(val+"avg", tmp.GetFloat64(strings.ToLower(val+"avg")))
				tmpSummary.Set(val+"sum", tmp.GetFloat64(strings.ToLower(val+"sum")))
			}

			summary = []tk.M{tmpSummary}

			/*k.SetSession(d.Hypoid+"Total", total)
			k.SetSession(d.Hypoid+"Summary", summary)*/

			ret.Set("Total", total)
			ret.Set("Summary", summary)
		}
		/*} else {
			ret.Set("Total", total)
			ret.Set("Summary", summary)
		}*/
		return ret, e
	}, nil)

	return r
}