示例#1
0
func (a *GroupController) GetLdapdataAddress(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	autoFilters := []*dbox.Filter{}

	var query *dbox.Filter

	if len(autoFilters) > 0 {
		query = dbox.And(autoFilters...)
	}

	cursor, err := colonycore.Find(new(colonycore.Ldap), query)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := []colonycore.Ldap{}
	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}
示例#2
0
func (d *DataContext) Find(m IModel, parms tk.M) (dbox.ICursor, error) {
	////_ = "breakpoint"
	q := d.Connection.NewQuery().From(m.TableName())
	if qe := parms.Get(ConfigSelect); qe != nil {
		fields := qe.(string)
		selectFields := strings.Split(fields, ",")
		q = q.Select(selectFields...)
	}
	if qe := parms.Get(ConfigWhere, nil); qe != nil {
		//q = q.Where(qe.(*dbox.Filter))
		filters := qe.([]*dbox.Filter)
		if len(filters) > 0 {
			q = q.Where(dbox.And(filters...))
		}
	}
	if qe := parms.Get(ConfigOrder, nil); qe != nil {
		q = q.Order(qe.([]string)...)
	}
	if qe := parms.Get(ConfigSkip, nil); qe != nil {
		q = q.Skip(qe.(int))
	}
	if qe := parms.Get(ConfigLimit, nil); qe != nil {
		q = q.Take(qe.(int))
	}
	//fmt.Printf("Debug Q: %s\n", tk.JsonString(q))
	return q.Cursor(nil)
	//return c
}
示例#3
0
func TestDelete(t *testing.T) {
	t.Skip()
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
		return
	}
	defer c.Close()

	e = c.NewQuery().From("students").Where(dbox.And(dbox.Eq("name", "dwayne johnson"), dbox.Eq("age", 32))).Delete().Exec(nil)
	// e = c.NewQuery().From("students").Where(dbox.Eq("name", "dwayne johnson")).Delete().Exec(nil)
	if e != nil {
		t.Errorf("Unable to delete table %s\n", e.Error())
		return
	}

	/* ===============================CLEAR ALL TABLE DATA==============================*/

	// e = c.NewQuery().SetConfig("multiexec", true).
	// 	From("coba").Delete().Exec(nil)
	// if e != nil {
	// 	t.Errorf("Unable to clear table %s\n", e.Error())
	// 	return
	// }
}
示例#4
0
func TestSelectFilter(t *testing.T) {
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
		return
	}
	defer c.Close()

	csr, e := c.NewQuery().
		Select("id", "name").
		Where(dbox.And(dbox.Eq("id", "1"), (dbox.Eq("name", "a")))).
		From("tes").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()

	//rets := []toolkit.M{}

	ds, e := csr.Fetch(nil, 0, false)
	if e != nil {
		t.Errorf("Unable to fetch: %s \n", e.Error())
	} else {
		fmt.Printf("Fetch N OK. Result: %v \n",
			ds.Data)
	}
}
示例#5
0
func (w *WebGrabberController) ConnectToSedotanServer() (*SshSetting, *colonycore.Server, error) {
	filter := dbox.And(dbox.Eq("os", "linux"), dbox.Eq("serverType", "node"))
	cursor, err := colonycore.Find(new(colonycore.Server), filter)
	if err != nil {
		return nil, nil, err
	}

	data := []colonycore.Server{}
	err = cursor.Fetch(&data, 0, true)
	if err != nil {
		return nil, nil, err
	}

	if len(data) == 0 {
		return nil, nil, errors.New("No sedotan server found")
	}

	server := data[0]

	var client SshSetting
	client.SSHHost = server.Host
	client.SSHAuthType = SSHAuthType_Password
	client.SSHUser = server.SSHUser
	client.SSHPassword = server.SSHPass

	return &client, &server, nil
}
func (d *DataSourceController) FindConnection(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	//~ payload := map[string]string{"inputText":"tes","inputDrop":""}
	payload := map[string]interface{}{}

	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	text := payload["inputText"].(string)
	pilih := payload["inputDrop"].(string)

	textLow := strings.ToLower(text)

	// == try useing Contains for support autocomplite
	var query *dbox.Filter
	if text != "" {
		query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("Database", text), dbox.Contains("Database", textLow), dbox.Contains("Driver", text), dbox.Contains("Driver", textLow), dbox.Contains("Host", text), dbox.Contains("Host", textLow), dbox.Contains("UserName", text), dbox.Contains("UserName", textLow), dbox.Contains("Password", text), dbox.Contains("Password", textLow))
	}

	if pilih != "" {
		query = dbox.And(query, dbox.Eq("Driver", pilih))
	}

	data := []colonycore.Connection{}
	cursor, err := colonycore.Find(new(colonycore.Connection), query)
	cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}
示例#7
0
func (d *WebGrabberController) FindWebGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	//~ payload := map[string]string{"inputText": "GRAB_TEST", "inputRequest": "", "inputType": ""}
	payload := map[string]interface{}{}

	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	text := payload["inputText"].(string)
	req := payload["inputRequest"].(string)
	tipe := payload["inputType"].(string)

	textLow := strings.ToLower(text)

	// == bug, cant find if autocomplite, just full text can be get result
	var query *dbox.Filter
	if text != "" {
		valueInt, errv := strconv.Atoi(text)
		if errv == nil {
			// == try useing Eq for support integer
			query = dbox.Or(dbox.Eq("GrabInterval", valueInt), dbox.Eq("TimeoutInterval", valueInt))
		} else {
			// == try useing Contains for support autocomplite
			query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("Calltype", text), dbox.Contains("Calltype", textLow), dbox.Contains("SourceType", text), dbox.Contains("SourceType", textLow), dbox.Contains("IntervalType", text), dbox.Contains("IntervalType", textLow))
		}
	}

	if req != "" {
		query = dbox.And(query, dbox.Eq("Calltype", req))
	}

	if tipe != "" {
		query = dbox.And(query, dbox.Eq("SourceType", tipe))
	}

	data := []colonycore.WebGrabber{}
	cursor, err := colonycore.Find(new(colonycore.WebGrabber), query)
	cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, payload, "")
}
示例#8
0
func (s *ServerController) GetServers(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := struct {
		Search     string `json:"search"`
		ServerOS   string `json:"serverOS"`
		ServerType string `json:"serverType"`
		SSHType    string `json:"sshType"`
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	filters := []*dbox.Filter{}
	if payload.Search != "" {
		filters = append(filters, dbox.Or(
			dbox.Contains("_id", payload.Search),
			dbox.Contains("os", payload.Search),
			dbox.Contains("host", payload.Search),
			dbox.Contains("serverType", payload.Search),
			dbox.Contains("sshtype", payload.Search),
		))
	}
	if payload.ServerOS != "" {
		filters = append(filters, dbox.Eq("os", payload.ServerOS))
	}
	if payload.ServerType != "" {
		filters = append(filters, dbox.Eq("serverType", payload.ServerType))
	}
	if payload.SSHType != "" {
		filters = append(filters, dbox.Eq("sshtype", payload.SSHType))
	}

	var query *dbox.Filter
	if len(filters) > 0 {
		query = dbox.And(filters...)
	}

	cursor, err := colonycore.Find(new(colonycore.Server), query)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := []colonycore.Server{}
	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}
示例#9
0
func (w *WebGrabberController) GetScrapperData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

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

	var query *dbox.Filter
	query = dbox.Or(dbox.Contains("_id", search))

	if sourcetype == "" {
		//default sourcetype == "SourceType_HttpHtml"
		query = dbox.And(query, dbox.Eq("sourcetype", "SourceType_HttpHtml"))
	} else {
		query = dbox.And(query, dbox.Eq("sourcetype", sourcetype))
	}

	if requesttype != "" {
		query = dbox.And(query, dbox.Eq("grabconf.calltype", requesttype))
	}

	cursor, err := colonycore.Find(new(colonycore.WebGrabber), query)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := []colonycore.WebGrabber{}
	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()
	return helper.CreateResult(true, data, "")
}
示例#10
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
}
示例#11
0
func TestSelect(t *testing.T) {
	// t.Skip()
	skipIfConnectionIsNil(t)

	cursor, e := ctx.NewQuery().
		Select("id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("amount", 150), dbox.Eq("nama", "buku"))).
		// Where(dbox.Contains("nama", "tem", "pe")).
		// Order("nama").
		// Skip(2).
		// Take(1).
		Cursor(nil)
	// Where(dbox.And(dbox.Gt("price", "@price"), dbox.Eq("status", "@status"))).
	// Cursor(toolkit.M{}.Set("@price", 100000).Set("@status", "available"))
	// Where(dbox.And(dbox.Or(dbox.Eq("nama", "@name1"), dbox.Eq("nama", "@name2"),
	// dbox.Eq("nama", "@name3")), dbox.Lt("quantity", "@quantity"))).
	// Cursor(toolkit.M{}.Set("@name1", "buku").Set("@name2", "tas").
	// Set("@name3", "dompet").Set("@quantity", 4))
	if e != nil {
		t.Fatalf("Cursor error: " + e.Error())
	}
	defer cursor.Close()

	var results []toolkit.M
	e = cursor.Fetch(&results, 0, false)

	operation = "Test Select Filter"
	sintaks = `
		ctx.NewQuery().
		Select("id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("amount", 150000), 
			dbox.Eq("nama", "buku"))).
		Cursor(nil)`

	if e != nil {
		t.Errorf("Unable to fetch: %s \n", e.Error())
	} else {
		toolkit.Println("======================")
		toolkit.Println(operation)
		toolkit.Println("======================")
		toolkit.Println(sintaks)
		toolkit.Println("Fetch OK. Result:")
		for _, val := range results {
			toolkit.Printf("%v \n",
				toolkit.JsonString(val))
		}
	}
}
示例#12
0
func TestCRUD(t *testing.T) {
	//t.Skip()
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
		return
	}
	defer c.Close()

	e = c.NewQuery().From("tes").Where(dbox.And(dbox.Eq("id", "1133331"), dbox.Eq("name", "testing"))).Delete().Exec(nil)
	if e != nil {
		t.Errorf("Unablet to delete table %s\n", e.Error())
		return
	}
	defer c.Close()

	e = c.NewQuery().From("tes").Delete().Exec(nil)
	if e != nil {
		t.Errorf("Unablet to clear table %s\n", e.Error())
		return
	}

	defer c.Close()

	// q := c.NewQuery().SetConfig("multiexec", true).From("tes").Save()
	type user struct {
		// Id     int
		Name string
		// Date   time.Time
	}

	// // 	//go func(q dbox.IQuery, i int) {
	data := user{}
	// data.Id =111
	data.Name = "testingupdate2222"
	// data.Date = time.Now()

	// e = q.Exec(toolkit.M{
	// 	"data": data,
	// })
	// if e != nil {
	// 	t.Errorf("Unable to save: %s \n", e.Error())
	// }
	e = c.NewQuery().From("tes").Where(dbox.Eq("id", "111")).Update().Exec(toolkit.M{"data": data})
	if e != nil {
		t.Errorf("Unable to update: %s \n", e.Error())
	}

}
示例#13
0
// 	if e != nil {
// 		t.Errorf("Unable to fetch: %s \n", e.Error())
// 	} else {
// 		toolkit.Println("======================")
// 		toolkit.Println(operation)
// 		toolkit.Println("======================")
// 		toolkit.Println(sintaks)
// 		toolkit.Println("Fetch OK. Result:")
// 		for _, val := range results {
// 			toolkit.Printf("%v \n",
// 				toolkit.JsonString(val))
// 		}
// 	}
// }
func TestSelectFilter(t *testing.T) {
	// t.Skip()
	skipIfConnectionIsNil(t)

	cursor, e := ctx.NewQuery().
		Select("_id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("amount", 150), dbox.Eq("nama", "buku"))).
		Cursor(nil)

	operation = "TestSelectFilter"
	sintaks = `
		ctx.NewQuery().
		Select("_id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("amount", 150000), 
			dbox.Eq("nama", "buku"))).
		Cursor(nil)`

	if e != nil {
		t.Fatalf("Cursor error: " + e.Error())
	}
	defer cursor.Close()

	if cursor.Count() == 0 {
		t.Fatalf("No record found")
	}

	var results []toolkit.M
	e = cursor.Fetch(&results, 0, false)

	if e != nil {
		t.Errorf("Unable to fetch: %s \n", e.Error())
	} else {
		toolkit.Println("======================")
		toolkit.Println(operation)
		toolkit.Println("======================")
		toolkit.Println(sintaks)
		toolkit.Println("Fetch OK. Result:")
		for _, val := range results {
			toolkit.Printf("%v \n",
				toolkit.JsonString(val))
		}
	}
}
示例#14
0
func (w *WebGrabberController) DaemonStat(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	if runtime.GOOS == "windows" {
		sedotandExist := GetSedotandWindows()
		if sedotandExist == false {
			return helper.CreateResult(true, false, "")
		}

		return helper.CreateResult(true, true, "")

	} else {
		filters := dbox.And(dbox.Eq("serverType", "node"), dbox.Eq("os", "linux"))
		cursor, err := colonycore.Find(new(colonycore.Server), filters)
		if err != nil {
			return helper.CreateResult(false, false, err.Error())
		}

		all := []colonycore.Server{}
		err = cursor.Fetch(&all, 0, true)
		if err != nil {
			return helper.CreateResult(false, false, err.Error())
		}

		if len(all) == 0 {
			return helper.CreateResult(false, false, "No server registered")
		}

		serverC := &ServerController{}
		var howManyOn = 0
		for _, server := range all {
			isOn, _ := serverC.ToggleSedotanService("stat", server.ID)
			if isOn {
				howManyOn = howManyOn + 1
			}
		}

		if howManyOn > 0 {
			return helper.CreateResult(true, true, "")
		}

		return helper.CreateResult(false, false, "")
	}

}
示例#15
0
func (d *DataSourceController) GetConnections(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

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

	search := ""
	if payload["search"] != nil {
		search = payload["search"].(string)
	}

	driver := ""
	if payload["driver"] != nil {
		search = payload["driver"].(string)
	}

	// search := payload["search"]
	// driver := payload["driver"]

	var query *dbox.Filter
	query = dbox.Or(dbox.Contains("_id", search), dbox.Contains("Driver", search), dbox.Contains("Host", search), dbox.Contains("Database", search), dbox.Contains("UserName", search))

	if driver != "" {
		query = dbox.And(query, dbox.Eq("Driver", driver))
	}

	data := []colonycore.Connection{}
	cursor, err := colonycore.Find(new(colonycore.Connection), query)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}
示例#16
0
func TestSelectParameter(t *testing.T) {
	t.Skip()
	skipIfConnectionIsNil(t)

	cursor, e := ctx.NewQuery().
		Select("id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("price", "@price"), dbox.Eq("status", "@status"))).
		Cursor(toolkit.M{}.Set("@price", 100).Set("@status", "available"))
	if e != nil {
		t.Fatalf("Cursor error: " + e.Error())
	}
	defer cursor.Close()

	var results []toolkit.M
	e = cursor.Fetch(&results, 0, false)
	operation = "Test Select Parameter"
	sintaks = `
		ctx.NewQuery().
		Select("id", "nama", "amount").
		From(tableName).
		Where(dbox.And(dbox.Gt("price", "@price"),
			dbox.Eq("status", "@status"))).
		Cursor(toolkit.M{}.Set("@price", 100).
			Set("@status", "available"))`

	if e != nil {
		t.Errorf("Unable to fetch: %s \n", e.Error())
	} else {
		toolkit.Println("======================")
		toolkit.Println(operation)
		toolkit.Println("======================")
		toolkit.Println(sintaks)
		toolkit.Println("Fetch OK. Result:")
		for _, val := range results {
			toolkit.Printf("%v \n",
				toolkit.JsonString(val))
		}
	}
}
示例#17
0
文件: csv_test.go 项目: haibudi/dbox
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))
	}
}
示例#18
0
func TestFind(t *testing.T) {
	ms := []toolkit.M{}
	for i := 1; i <= 10; i++ {
		m := toolkit.M{}
		m.Set("_id", i)
		m.Set("random", toolkit.RandInt(100))
		ms = append(ms, m)
	}
	toolkit.Printf("Original Value\n%s\n", toolkit.JsonString(ms))

	indexes := dbox.Find(ms, []*dbox.Filter{
		//dbox.Or(dbox.Lt("random", 20), dbox.And(dbox.Gte("random", 60), dbox.Lte("random", 70)))})
		dbox.And(dbox.Gte("random", 30), dbox.Lte("random", 80))})

	records := []toolkit.M{}
	for _, v := range indexes {
		records = append(records, ms[v])
	}
	for _, r := range records {
		toolkit.Printf("Record: %s \n", toolkit.JsonString(r))
	}
	toolkit.Printf("Find %d records of %d records\n", len(indexes), len(ms))
}
示例#19
0
func TestDelete(t *testing.T) {
	t.Skip()
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
		return
	}
	defer c.Close()

	e = c.NewQuery().From(tableProducts).Where(dbox.And(dbox.Eq("id", "1"), dbox.Eq("productname", "Hendro"))).Delete().Exec(nil)
	if e != nil {
		t.Errorf("Unable to delete data %s\n", e.Error())
		return
	}

	/*data := products{}
	data.Id = 2

	e = c.NewQuery().From(tableProducts).Delete().Exec(toolkit.M{"data": data})
	if e != nil {
		t.Errorf("Unable to delete data %s\n", e.Error())
		return
	}*/
}
示例#20
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")
}
示例#21
0
func doGenerateMaintenanceDataBrowser(wg *sync.WaitGroup, d *GenDataBrowser, plant PowerPlantCoordinates) {
	var e error
	ctx := d.BaseController.Ctx
	c := ctx.Connection

	saveDatas := []orm.IModel{}
	plantCodeStr := plant.PlantCode
	dataCount := 0

	tmpPlantCondition := conditions.Get(plantCodeStr)
	if tmpPlantCondition != nil {
		plantCondition := tmpPlantCondition.(tk.M)

		length := plantCondition.GetInt("length")
		dets := plantCondition.Get("det").([]tk.M)
		dets = append(dets, tk.M{})

		turbinesCodes := []string{}
		genIDTempTable := tk.GenerateRandomString("", 20)

		detsLen := len(dets) - 1

		tk.Printf("detsLen: %v \n", detsLen)

		for i, det := range dets {
			freeQuery := false
			tk.Println(i)
			tk.Printf("dets: %#v \n", det)
			assets := []FunctionalLocation{}
			systemAssets := []FunctionalLocation{}
			desc := det.GetString("desc")

			query := []*dbox.Filter{}
			query = append(query, dbox.Contains("FunctionalLocationCode", plantCodeStr))
			query = append(query, dbox.Eq("PIPI", plantCodeStr))

			queryStr := "select * from FunctionalLocation where FunctionalLocationCode like('%" + plantCodeStr + "%') and PIPI = '" + plantCodeStr + "' "

			if i == 0 {
				query = append(query, dbox.Contains("Description", desc))
				if plantCodeStr == "2220" {
					query = append(query, dbox.Lte("LEN(FunctionalLocationCode)", length))
				} else {
					query = append(query, dbox.Eq("LEN(FunctionalLocationCode)", length))
				}
			} else if i == 1 && detsLen == 2 {
				query = append(query, dbox.Contains("Description", desc))
				if plantCodeStr == "2220" {
					query = append(query, dbox.Lte("LEN(FunctionalLocationCode)", length))
				} else {
					query = append(query, dbox.Eq("LEN(FunctionalLocationCode)", length))
				}
			} else {
				tk.Printf("turbinesCodes: %v \n", len(turbinesCodes))
				if len(turbinesCodes) > 1 {
					maxLength := 1000
					maxLoop := tk.ToInt(tk.ToFloat64(len(turbinesCodes)/maxLength, 0, tk.RoundingUp), tk.RoundingUp)

					for i := 0; i <= maxLoop; i++ {
						datas := []string{}

						if i != maxLoop {
							datas = turbinesCodes[i*maxLength : (i*maxLength)+maxLength]

						} else {
							datas = turbinesCodes[i*maxLength:]
						}

						tmpNotIn := []orm.IModel{}
						for _, val := range datas {
							tmpData := new(GenDataBrowserNotInTmp)
							tmpData.ID = genIDTempTable
							tmpData.FLCode = val
							tmpNotIn = append(tmpNotIn, tmpData)
						}
						// tk.Printf("tmpNotIn: %v \n", len(tmpNotIn))
						e = ctx.InsertBulk(tmpNotIn)
						ErrorHandler(e, "generateMaintenanceDataBrowser")
					}

					queryStr = queryStr + " and FunctionalLocationCode not in(select flcode from GenDataBrowserNotInTmp where ID = '" + genIDTempTable + "')"
					freeQuery = true
				}
			}

			if freeQuery {
				csrDet, e := c.NewQuery().Command("freequery", tk.M{}.Set("syntax", queryStr)).Cursor(nil)
				ErrorHandler(e, "generateMaintenanceDataBrowser")

				e = csrDet.Fetch(&assets, 0, false)
				ErrorHandler(e, "generateMaintenanceDataBrowser")
				csrDet.Close()
			} else {
				csrDet, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(dbox.And(query...)).Cursor(nil)
				ErrorHandler(e, "generateMaintenanceDataBrowser")

				e = csrDet.Fetch(&assets, 0, false)
				ErrorHandler(e, "generateMaintenanceDataBrowser")
				csrDet.Close()
			}

			tk.Printf("-- assets: %v \n", len(assets))

			if len(assets) > 0 {
				relatedAssets := []FunctionalLocation{}

				for _, asset := range assets {

					if plantCodeStr == "2110" {
						if len(asset.FunctionalLocationCode) <= 13 {
							query = []*dbox.Filter{}
							query = append(query,
								dbox.And(
									dbox.Eq("PG", "MP1"),
									dbox.Eq("PIPI", plantCodeStr),
									dbox.Contains("FunctionalLocationCode", asset.FunctionalLocationCode),
									dbox.And(
										dbox.Lte("LEN(FunctionalLocationCode)", 13),
										dbox.Gte("LEN(FunctionalLocationCode)", 12),
									),
								),
							)

							csrDet, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Cursor(nil)
							ErrorHandler(e, "generateMaintenanceDataBrowser")
							e = csrDet.Fetch(&systemAssets, 0, false)
							ErrorHandler(e, "generateMaintenanceDataBrowser")
							csrDet.Close()
						}

						if systemAssets == nil || len(systemAssets) == 0 {
							query = []*dbox.Filter{}

							if i != 2 {
								query = append(query, dbox.Contains("FunctionalLocationCode", asset.FunctionalLocationCode))
							} else {
								query = append(query, dbox.Eq("FunctionalLocationCode", asset.FunctionalLocationCode))
							}

							csrDet, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Cursor(nil)
							ErrorHandler(e, "generateMaintenanceDataBrowser")
							e = csrDet.Fetch(&relatedAssets, 0, false)
							ErrorHandler(e, "generateMaintenanceDataBrowser")
							csrDet.Close()

							for _, relasset := range relatedAssets {
								isTurbineSystem := false
								if relasset.FunctionalLocationCode == asset.FunctionalLocationCode && i != 2 {
									isTurbineSystem = true
								}

								newEquipment := d.getNewEquipmentType(relasset.ObjectType, isTurbineSystem)

								if newEquipment != "" {

									if i != 2 {
										turbinesCodes = append(turbinesCodes, relasset.FunctionalLocationCode)
									}

									for _, year := range years {
										data := DataBrowser{}
										data.PeriodYear = year
										data.FunctionalLocation = relasset.FunctionalLocationCode
										data.FLDescription = relasset.Description
										data.IsTurbine = false

										// tk.Printf("%v | %v | ", relasset.FunctionalLocationCode, i)

										if relasset.FunctionalLocationCode == asset.FunctionalLocationCode && i != 2 {
											data.IsTurbine = true
											// tk.Println(" isTurbine: TRUE")
										} else {
											// tk.Println(" isTurbine: FALSE")
											data.TurbineParent = asset.FunctionalLocationCode
										}

										data.AssetType = "Other"

										if i == 0 {
											data.AssetType = "Steam"
										} else if i == 1 {
											data.AssetType = "Gas"
										}

										data.EquipmentType = newEquipment
										data.EquipmentTypeDescription = newEquipment
										data.Plant = plant
										data.PlantCode = plant.PlantCode

										if data.IsTurbine {
											info := GeneralInfo{}
											substr := ""
											substrValInt := 0
											if data.AssetType == "Steam" {
												substrValInt = 1
												substr = "ST"
											} else if data.AssetType == "Gas" {
												substrValInt = 2
											}

											if substrValInt != 0 {
												tmpInfo := crowd.From(&generalInfos).Where(func(x interface{}) interface{} {
													y := x.(GeneralInfo)
													substr = substr + data.FunctionalLocation[len(data.FunctionalLocation)-substrValInt:]
													return strings.Contains(strings.ToLower(strings.Trim(y.Plant, " ")), strings.ToLower(plant.PlantName)) && y.Unit == substr
												}).Exec().Result.Data().([]GeneralInfo)
												if len(tmpInfo) > 0 {
													info = tmpInfo[0]
												}

												if info.Id != "" {
													data.TInfShortName = info.Unit
													data.TInfManufacturer = info.Manufacturer
													data.TInfModel = info.Model
													data.TInfUnitType = info.UnitType
													data.TInfInstalledCapacity = info.InstalledCapacity
													data.TInfOperationalCapacity = info.OperationalCapacity
													data.TInfPrimaryFuel = info.PrimaryFuel1
													data.TInfPrimaryFuel2 = info.PrimaryFuel2Startup
													data.TInfBackupFuel = info.BackupFuel
													data.TInfHeatRate = info.HeatRate
													data.TInfEfficiency = info.Efficiency

													commDate, e := time.Parse("01/02/2006", "01/01"+tk.ToString(info.CommissioningDate))
													ErrorHandler(e, "generateMaintenanceDataBrowser")
													data.TInfCommisioningDate = commDate

													if info.RetirementPlan != "" {
														retirementPlanStr := strings.Split(info.RetirementPlan, "(")[0]
														retirementPlan, e := time.Parse("01/02/2006", "01/01"+retirementPlanStr)
														ErrorHandler(e, "generateMaintenanceDataBrowser")
														data.TInfRetirementPlan = retirementPlan
													}

													installedMWH := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
														y := x.(GeneralInfoDetails)
														return y.GenID == info.Id && y.Type == "InstalledMWH" && y.Year == year
													}).Exec().Result.Data().([]GeneralInfoDetails)[0]

													data.TInfInstalledMWH = installedMWH.Value

													actualEnergyGeneration := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
														y := x.(GeneralInfoDetails)
														return y.GenID == info.Id && y.Type == "ActualEnergyGeneration" && y.Year == year
													}).Exec().Result.Data().([]GeneralInfoDetails)[0]

													data.TInfActualEnergyGeneration = actualEnergyGeneration.Value

													capacityFactor := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
														y := x.(GeneralInfoDetails)
														return y.GenID == info.Id && y.Type == "CapacityFactor" && y.Year == year
													}).Exec().Result.Data().([]GeneralInfoDetails)[0]

													data.TInfCapacityFactor = capacityFactor.Value

													actualFuelConsumption := crowd.From(&generalInfoActualFuelConsumption).Where(func(x interface{}) interface{} {
														y := x.(GeneralInfoActualFuelConsumption)
														return y.GenID == info.Id && y.Year == year
													}).Exec().Result.Data().([]GeneralInfoActualFuelConsumption)[0]

													data.TInfActualFuelConsumption_CrudeBarrel = actualFuelConsumption.CrudeBarrel
													data.TInfActualFuelConsumption_DieselBarrel = actualFuelConsumption.DieselBarrel
													data.TInfActualFuelConsumption_GASMMSCF = actualFuelConsumption.GASMMSCF
													data.TInfActualFuelConsumption_HFOBarrel = actualFuelConsumption.HFOBarrel

													fuelCostCrowd := crowd.From(&fuelCosts).Where(func(x interface{}) interface{} {
														y := x.(FuelCost)
														unitID := strings.Replace(
															strings.Replace(
																strings.Replace(
																	strings.Replace(y.UnitId, ".", "", -1), " 0", "", -1), " ", "", -1), "C.C ", "", -1)

														return y.Year == year && y.Plant == data.Plant.PlantName && unitID == data.TInfShortName
													})

													data.TInfUpdateEnergyGeneration = fuelCostCrowd.Sum(func(x interface{}) interface{} {
														y := x.(FuelCost)
														return y.EnergyNetProduction
													}).Exec().Result.Data().(float64)

													data.TInfUpdateFuelConsumption = fuelCostCrowd.Sum(func(x interface{}) interface{} {
														y := x.(FuelCost)
														return y.PrimaryFuelConsumed
													}).Exec().Result.Data().(float64)
												}
											}
											// Vibrations handled by sql query
										}

										// Maintenance handled by sql query
										// FailureNotifications handled by sql query
										// MROElements handled by sql query
										// Operationals handled by sql query

										// e = ctx.Insert(&data)
										saveDatas = append(saveDatas, &data)
										dataCount++
										if len(saveDatas) == 1000 {
											mugen.Lock()
											e = ctx.InsertBulk(saveDatas)
											mugen.Unlock()
											ErrorHandler(e, "generateMaintenanceDataBrowser")
											tk.Printf("%v : %v \n", plantCodeStr, dataCount)

											saveDatas = []orm.IModel{}
										}

										/*ErrorHandler(e, "generateMaintenanceDataBrowser")
										tk.Println("save 1")*/
									}
								}
							}

						} else {
							// with system assets
							if i != 2 {
								turbinesCodes = append(turbinesCodes, asset.FunctionalLocationCode)
							}

							for _, year := range years {
								data := DataBrowser{}
								data.PeriodYear = year
								data.FunctionalLocation = asset.FunctionalLocationCode
								data.FLDescription = asset.Description
								data.IsTurbine = false
								data.IsSystem = false

								// tk.Printf("%v | %v | ", asset.FunctionalLocationCode, i)

								if asset.FunctionalLocationCode == asset.FunctionalLocationCode && i != 2 {
									data.IsTurbine = true
									data.IsSystem = true
									// tk.Println(" isTurbine: TRUE")
								} else {
									// tk.Println(" isTurbine: FALSE")
									data.TurbineParent = asset.FunctionalLocationCode
									data.SystemParent = asset.FunctionalLocationCode
								}

								data.AssetType = "Other"

								if i == 0 {
									data.AssetType = "Steam"
								} else if i == 1 {
									data.AssetType = "Gas"
								}

								data.EquipmentType = "System"
								data.EquipmentTypeDescription = "System"
								data.Plant = plant
								data.PlantCode = plant.PlantCode

								if data.IsTurbine {
									info := GeneralInfo{}
									substr := ""
									substrValInt := 0
									if data.AssetType == "Steam" {
										substrValInt = 1
										substr = "ST"
									} else if data.AssetType == "Gas" {
										substrValInt = 2
									}

									if substrValInt != 0 {
										tmpInfo := crowd.From(&generalInfos).Where(func(x interface{}) interface{} {
											y := x.(GeneralInfo)
											substr = substr + data.FunctionalLocation[len(data.FunctionalLocation)-substrValInt:]
											return strings.Contains(strings.ToLower(strings.Trim(y.Plant, " ")), strings.ToLower(plant.PlantName)) && y.Unit == substr
										}).Exec().Result.Data().([]GeneralInfo)
										if len(tmpInfo) > 0 {
											info = tmpInfo[0]
										}

										if info.Id != "" {
											data.TInfShortName = info.Unit
											data.TInfManufacturer = info.Manufacturer
											data.TInfModel = info.Model
											data.TInfUnitType = info.UnitType
											data.TInfInstalledCapacity = info.InstalledCapacity
											data.TInfOperationalCapacity = info.OperationalCapacity
											data.TInfPrimaryFuel = info.PrimaryFuel1
											data.TInfPrimaryFuel2 = info.PrimaryFuel2Startup
											data.TInfBackupFuel = info.BackupFuel
											data.TInfHeatRate = info.HeatRate
											data.TInfEfficiency = info.Efficiency

											commDate, e := time.Parse("01/02/2006", "01/01"+tk.ToString(info.CommissioningDate))
											ErrorHandler(e, "generateMaintenanceDataBrowser")
											data.TInfCommisioningDate = commDate

											if info.RetirementPlan != "" {
												retirementPlanStr := strings.Split(info.RetirementPlan, "(")[0]
												retirementPlan, e := time.Parse("01/02/2006", "01/01"+retirementPlanStr)
												ErrorHandler(e, "generateMaintenanceDataBrowser")
												data.TInfRetirementPlan = retirementPlan
											}

											installedMWH := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
												y := x.(GeneralInfoDetails)
												return y.GenID == info.Id && y.Type == "InstalledMWH" && y.Year == year
											}).Exec().Result.Data().([]GeneralInfoDetails)[0]

											data.TInfInstalledMWH = installedMWH.Value

											actualEnergyGeneration := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
												y := x.(GeneralInfoDetails)
												return y.GenID == info.Id && y.Type == "ActualEnergyGeneration" && y.Year == year
											}).Exec().Result.Data().([]GeneralInfoDetails)[0]

											data.TInfActualEnergyGeneration = actualEnergyGeneration.Value

											capacityFactor := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
												y := x.(GeneralInfoDetails)
												return y.GenID == info.Id && y.Type == "CapacityFactor" && y.Year == year
											}).Exec().Result.Data().([]GeneralInfoDetails)[0]

											data.TInfCapacityFactor = capacityFactor.Value

											actualFuelConsumption := crowd.From(&generalInfoActualFuelConsumption).Where(func(x interface{}) interface{} {
												y := x.(GeneralInfoActualFuelConsumption)
												return y.GenID == info.Id && y.Year == year
											}).Exec().Result.Data().([]GeneralInfoActualFuelConsumption)[0]

											data.TInfActualFuelConsumption_CrudeBarrel = actualFuelConsumption.CrudeBarrel
											data.TInfActualFuelConsumption_DieselBarrel = actualFuelConsumption.DieselBarrel
											data.TInfActualFuelConsumption_GASMMSCF = actualFuelConsumption.GASMMSCF
											data.TInfActualFuelConsumption_HFOBarrel = actualFuelConsumption.HFOBarrel

											fuelCostCrowd := crowd.From(&fuelCosts).Where(func(x interface{}) interface{} {
												y := x.(FuelCost)
												unitID := strings.Replace(
													strings.Replace(
														strings.Replace(
															strings.Replace(y.UnitId, ".", "", -1), " 0", "", -1), " ", "", -1), "C.C ", "", -1)

												return y.Year == year && y.Plant == data.Plant.PlantName && unitID == data.TInfShortName
											})

											data.TInfUpdateEnergyGeneration = fuelCostCrowd.Sum(func(x interface{}) interface{} {
												y := x.(FuelCost)
												return y.EnergyNetProduction
											}).Exec().Result.Data().(float64)

											data.TInfUpdateFuelConsumption = fuelCostCrowd.Sum(func(x interface{}) interface{} {
												y := x.(FuelCost)
												return y.PrimaryFuelConsumed
											}).Exec().Result.Data().(float64)
										}
									}
									// Vibrations handled by sql query
								}

								// Maintenance handled by sql query
								// FailureNotifications handled by sql query
								// MROElements handled by sql query
								// Operationals handled by sql query

								// e = ctx.Insert(&data)
								// dataCount++
								saveDatas = append(saveDatas, &data)
								dataCount++
								if len(saveDatas) == 1000 {
									mugen.Lock()
									e = ctx.InsertBulk(saveDatas)
									mugen.Unlock()
									ErrorHandler(e, "generateMaintenanceDataBrowser")
									tk.Printf("%v : %v \n", plantCodeStr, dataCount)

									saveDatas = []orm.IModel{}
								}

								/*ErrorHandler(e, "generateMaintenanceDataBrowser")
								tk.Println("save 2")*/

							}

							for _, sysAsset := range systemAssets {
								query = []*dbox.Filter{}

								if i != 2 {
									query = append(query, dbox.Contains("FunctionalLocationCode", sysAsset.FunctionalLocationCode))
								} else {
									query = append(query, dbox.Eq("FunctionalLocationCode", sysAsset.FunctionalLocationCode))
								}

								csrDet, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Cursor(nil)
								ErrorHandler(e, "generateMaintenanceDataBrowser")
								e = csrDet.Fetch(&relatedAssets, 0, false)
								ErrorHandler(e, "generateMaintenanceDataBrowser")
								csrDet.Close()

								for _, relasset := range relatedAssets {
									isTurbineSystem := false
									if (relasset.FunctionalLocationCode == asset.FunctionalLocationCode || relasset.FunctionalLocationCode == sysAsset.FunctionalLocationCode) && i != 2 {
										isTurbineSystem = true
									}

									newEquipment := d.getNewEquipmentType(relasset.ObjectType, isTurbineSystem)

									if newEquipment != "" {

										if i != 2 {
											turbinesCodes = append(turbinesCodes, relasset.FunctionalLocationCode)
										}

										for _, year := range years {
											data := DataBrowser{}
											data.PeriodYear = year
											data.FunctionalLocation = relasset.FunctionalLocationCode
											data.FLDescription = relasset.Description
											data.IsTurbine = false
											data.IsSystem = false

											// tk.Printf("%v | %v | ", relasset.FunctionalLocationCode, i)

											if relasset.FunctionalLocationCode == sysAsset.FunctionalLocationCode && i != 2 {
												data.IsTurbine = true
												data.IsSystem = true
												// tk.Println(" isTurbine: TRUE")
											} else {
												// tk.Println(" isTurbine: FALSE")
												data.TurbineParent = asset.FunctionalLocationCode
												data.SystemParent = asset.FunctionalLocationCode
											}

											data.AssetType = "Other"

											if i == 0 {
												data.AssetType = "Steam"
											} else if i == 1 {
												data.AssetType = "Gas"
											}

											data.EquipmentType = newEquipment
											data.EquipmentTypeDescription = newEquipment
											data.Plant = plant
											data.PlantCode = plant.PlantCode

											if data.IsTurbine {
												info := GeneralInfo{}
												substr := ""
												substrValInt := 0
												if data.AssetType == "Steam" {
													substrValInt = 1
													substr = "ST"
												} else if data.AssetType == "Gas" {
													substrValInt = 2
												}

												if substrValInt != 0 {
													tmpInfo := crowd.From(&generalInfos).Where(func(x interface{}) interface{} {
														y := x.(GeneralInfo)
														substr = substr + data.FunctionalLocation[len(data.FunctionalLocation)-substrValInt:]
														return strings.Contains(strings.ToLower(strings.Trim(y.Plant, " ")), strings.ToLower(plant.PlantName)) && y.Unit == substr
													}).Exec().Result.Data().([]GeneralInfo)
													if len(tmpInfo) > 0 {
														info = tmpInfo[0]
													}

													if info.Id != "" {
														data.TInfShortName = info.Unit
														data.TInfManufacturer = info.Manufacturer
														data.TInfModel = info.Model
														data.TInfUnitType = info.UnitType
														data.TInfInstalledCapacity = info.InstalledCapacity
														data.TInfOperationalCapacity = info.OperationalCapacity
														data.TInfPrimaryFuel = info.PrimaryFuel1
														data.TInfPrimaryFuel2 = info.PrimaryFuel2Startup
														data.TInfBackupFuel = info.BackupFuel
														data.TInfHeatRate = info.HeatRate
														data.TInfEfficiency = info.Efficiency

														commDate, e := time.Parse("01/02/2006", "01/01"+tk.ToString(info.CommissioningDate))
														ErrorHandler(e, "generateMaintenanceDataBrowser")
														data.TInfCommisioningDate = commDate

														if info.RetirementPlan != "" {
															retirementPlanStr := strings.Split(info.RetirementPlan, "(")[0]
															retirementPlan, e := time.Parse("01/02/2006", "01/01"+retirementPlanStr)
															ErrorHandler(e, "generateMaintenanceDataBrowser")
															data.TInfRetirementPlan = retirementPlan
														}

														installedMWH := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
															y := x.(GeneralInfoDetails)
															return y.GenID == info.Id && y.Type == "InstalledMWH" && y.Year == year
														}).Exec().Result.Data().([]GeneralInfoDetails)[0]

														data.TInfInstalledMWH = installedMWH.Value

														actualEnergyGeneration := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
															y := x.(GeneralInfoDetails)
															return y.GenID == info.Id && y.Type == "ActualEnergyGeneration" && y.Year == year
														}).Exec().Result.Data().([]GeneralInfoDetails)[0]

														data.TInfActualEnergyGeneration = actualEnergyGeneration.Value

														capacityFactor := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
															y := x.(GeneralInfoDetails)
															return y.GenID == info.Id && y.Type == "CapacityFactor" && y.Year == year
														}).Exec().Result.Data().([]GeneralInfoDetails)[0]

														data.TInfCapacityFactor = capacityFactor.Value

														actualFuelConsumption := crowd.From(&generalInfoActualFuelConsumption).Where(func(x interface{}) interface{} {
															y := x.(GeneralInfoActualFuelConsumption)
															return y.GenID == info.Id && y.Year == year
														}).Exec().Result.Data().([]GeneralInfoActualFuelConsumption)[0]

														data.TInfActualFuelConsumption_CrudeBarrel = actualFuelConsumption.CrudeBarrel
														data.TInfActualFuelConsumption_DieselBarrel = actualFuelConsumption.DieselBarrel
														data.TInfActualFuelConsumption_GASMMSCF = actualFuelConsumption.GASMMSCF
														data.TInfActualFuelConsumption_HFOBarrel = actualFuelConsumption.HFOBarrel

														fuelCostCrowd := crowd.From(&fuelCosts).Where(func(x interface{}) interface{} {
															y := x.(FuelCost)
															unitID := strings.Replace(
																strings.Replace(
																	strings.Replace(
																		strings.Replace(y.UnitId, ".", "", -1), " 0", "", -1), " ", "", -1), "C.C ", "", -1)

															return y.Year == year && y.Plant == data.Plant.PlantName && unitID == data.TInfShortName
														})

														data.TInfUpdateEnergyGeneration = fuelCostCrowd.Sum(func(x interface{}) interface{} {
															y := x.(FuelCost)
															return y.EnergyNetProduction
														}).Exec().Result.Data().(float64)

														data.TInfUpdateFuelConsumption = fuelCostCrowd.Sum(func(x interface{}) interface{} {
															y := x.(FuelCost)
															return y.PrimaryFuelConsumed
														}).Exec().Result.Data().(float64)
													}
												}
												// Vibrations handled by sql query
											}

											// Maintenance handled by sql query
											// FailureNotifications handled by sql query
											// MROElements handled by sql query
											// Operationals handled by sql query

											// e = ctx.Insert(&data)
											saveDatas = append(saveDatas, &data)
											dataCount++
											if len(saveDatas) == 1000 {
												mugen.Lock()
												e = ctx.InsertBulk(saveDatas)
												mugen.Unlock()
												ErrorHandler(e, "generateMaintenanceDataBrowser")
												tk.Printf("%v : %v \n", plantCodeStr, dataCount)

												saveDatas = []orm.IModel{}
											}

											/*ErrorHandler(e, "generateMaintenanceDataBrowser")
											tk.Println("save 3")*/
										}
									}
								}
							}
						}
					} else {
						// another plant(s)

						query = []*dbox.Filter{}

						if i != detsLen {
							query = append(query, dbox.Contains("FunctionalLocationCode", asset.FunctionalLocationCode))
						} else {
							query = append(query, dbox.Eq("FunctionalLocationCode", asset.FunctionalLocationCode))
						}

						csrDet, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Cursor(nil)
						ErrorHandler(e, "generateMaintenanceDataBrowser")
						e = csrDet.Fetch(&relatedAssets, 0, false)
						ErrorHandler(e, "generateMaintenanceDataBrowser")
						csrDet.Close()
						tk.Printf("-- related assets: %v \n", len(relatedAssets))

						for _, relasset := range relatedAssets {
							isTurbineSystem := false
							if relasset.FunctionalLocationCode == asset.FunctionalLocationCode && i != detsLen {
								isTurbineSystem = true
							}

							newEquipment := ""
							newEquipment = d.getNewEquipmentType(relasset.ObjectType, isTurbineSystem)

							if newEquipment != "" {

								if i != detsLen {
									turbinesCodes = append(turbinesCodes, relasset.FunctionalLocationCode)
								}

								for _, year := range years {
									_ = year
									data := DataBrowser{}
									data.PeriodYear = year
									data.FunctionalLocation = relasset.FunctionalLocationCode
									data.FLDescription = relasset.Description
									data.IsTurbine = false

									if relasset.FunctionalLocationCode == asset.FunctionalLocationCode && i != detsLen {
										data.IsTurbine = true
									} else {
										data.TurbineParent = asset.FunctionalLocationCode
									}

									data.AssetType = "Other"

									if i == 0 {
										data.AssetType = "Steam"
									} else if i == 1 && detsLen > 1 {
										data.AssetType = "Gas"
									}

									data.EquipmentType = newEquipment
									data.EquipmentTypeDescription = newEquipment
									data.Plant = plant
									data.PlantCode = plant.PlantCode

									if data.IsTurbine {
										info := GeneralInfo{}
										substr := ""
										substrValInt := 0
										if data.AssetType == "Steam" {
											// substrValInt = 1
											substrValInt = 2
											substr = "ST" + substr
										} else if data.AssetType == "Gas" {
											substrValInt = 2
											substr = "GT" + substr
										}

										if substrValInt != 0 {
											tmpInfo := crowd.From(&generalInfos).Where(func(x interface{}) interface{} {
												y := x.(GeneralInfo)
												substr = data.FunctionalLocation[len(data.FunctionalLocation)-substrValInt:]
												return strings.Contains(strings.ToLower(strings.Trim(y.Plant, " ")), strings.ToLower(plant.PlantName)) && y.Unit == substr
											}).Exec().Result.Data().([]GeneralInfo)
											if len(tmpInfo) > 0 {
												info = tmpInfo[0]
											}

											if info.Id != "" {
												data.TInfShortName = info.Unit
												data.TInfManufacturer = info.Manufacturer
												data.TInfModel = info.Model
												data.TInfUnitType = info.UnitType
												data.TInfInstalledCapacity = info.InstalledCapacity
												data.TInfOperationalCapacity = info.OperationalCapacity
												data.TInfPrimaryFuel = info.PrimaryFuel1
												data.TInfPrimaryFuel2 = info.PrimaryFuel2Startup
												data.TInfBackupFuel = info.BackupFuel
												data.TInfHeatRate = info.HeatRate
												data.TInfEfficiency = info.Efficiency

												commDate, e := time.Parse("01/02/2006", "01/01"+tk.ToString(info.CommissioningDate))
												ErrorHandler(e, "generateMaintenanceDataBrowser")

												data.TInfCommisioningDate = commDate

												if info.RetirementPlan != "" {
													retirementPlanStr := strings.Split(info.RetirementPlan, "(")[0]
													retirementPlan, e := time.Parse("01/02/2006", "01/01"+retirementPlanStr)
													ErrorHandler(e, "generateMaintenanceDataBrowser")
													data.TInfRetirementPlan = retirementPlan
												}

												installedMWH := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
													y := x.(GeneralInfoDetails)
													return y.GenID == info.Id && y.Type == "InstalledMWH" && y.Year == year
												}).Exec().Result.Data().([]GeneralInfoDetails)[0]

												data.TInfInstalledMWH = installedMWH.Value

												actualEnergyGeneration := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
													y := x.(GeneralInfoDetails)
													return y.GenID == info.Id && y.Type == "ActualEnergyGeneration" && y.Year == year
												}).Exec().Result.Data().([]GeneralInfoDetails)[0]

												data.TInfActualEnergyGeneration = actualEnergyGeneration.Value

												capacityFactor := crowd.From(&generalInfoDetails).Where(func(x interface{}) interface{} {
													y := x.(GeneralInfoDetails)
													return y.GenID == info.Id && y.Type == "CapacityFactor" && y.Year == year
												}).Exec().Result.Data().([]GeneralInfoDetails)[0]

												data.TInfCapacityFactor = capacityFactor.Value

												actualFuelConsumption := crowd.From(&generalInfoActualFuelConsumption).Where(func(x interface{}) interface{} {
													y := x.(GeneralInfoActualFuelConsumption)
													return y.GenID == info.Id && y.Year == year
												}).Exec().Result.Data().([]GeneralInfoActualFuelConsumption)[0]

												data.TInfActualFuelConsumption_CrudeBarrel = actualFuelConsumption.CrudeBarrel
												data.TInfActualFuelConsumption_DieselBarrel = actualFuelConsumption.DieselBarrel
												data.TInfActualFuelConsumption_GASMMSCF = actualFuelConsumption.GASMMSCF
												data.TInfActualFuelConsumption_HFOBarrel = actualFuelConsumption.HFOBarrel

												fuelCostCrowd := crowd.From(&fuelCosts).Where(func(x interface{}) interface{} {
													y := x.(FuelCost)
													unitID := strings.Replace(
														strings.Replace(
															strings.Replace(
																strings.Replace(y.UnitId, ".", "", -1), " 0", "", -1), " ", "", -1), "C.C ", "", -1)

													return y.Year == year && y.Plant == data.Plant.PlantName && unitID == data.TInfShortName
												})

												data.TInfUpdateEnergyGeneration = fuelCostCrowd.Sum(func(x interface{}) interface{} {
													y := x.(FuelCost)
													return y.EnergyNetProduction
												}).Exec().Result.Data().(float64)

												data.TInfUpdateFuelConsumption = fuelCostCrowd.Sum(func(x interface{}) interface{} {
													y := x.(FuelCost)
													return y.PrimaryFuelConsumed
												}).Exec().Result.Data().(float64)
											}
										}
										// Vibrations handled by sql query
									}

									// Maintenance handled by sql query
									// FailureNotifications handled by sql query
									// MROElements handled by sql query
									// Operationals handled by sql query

									// e = ctx.Insert(&data)
									saveDatas = append(saveDatas, &data)
									dataCount++
									if len(saveDatas) == 1000 {
										mugen.Lock()
										e = ctx.InsertBulk(saveDatas)
										mugen.Unlock()
										ErrorHandler(e, "generateMaintenanceDataBrowser")
										tk.Printf("%v : %v \n", plantCodeStr, dataCount)

										saveDatas = []orm.IModel{}
									}

									/*ErrorHandler(e, "generateMaintenanceDataBrowser")
									tk.Println("save 4")*/
								}
							}
						}
					}

				}
			}
		}

		e = c.NewQuery().Delete().From(new(GenDataBrowserNotInTmp).TableName()).SetConfig("multiexec", true).Where(dbox.Eq("ID", genIDTempTable)).Exec(nil)

		if len(saveDatas) > 0 {
			e = ctx.InsertBulk(saveDatas)
			ErrorHandler(e, "generateMaintenanceDataBrowser")
			tk.Printf("%v : %v \n", plantCodeStr, dataCount)
		}

	}

	mugen.Lock()
	dataCounts.Set(plantCodeStr, dataCount)
	mugen.Unlock()

	wg.Done()
}
示例#22
0
func EmployeeGetByTitleEnable(pTitle string, pEnable bool, orders string) (*Employee, error) {
	return EmployeeGet(dbox.And(dbox.Eq("title", pTitle), dbox.Eq("enable", pEnable)), "", 0)
}
// generatePreventiveCorrectiveSummary
func (s *GenPreventiveCorrectiveSummary) generatePreventiveCorrectiveSummary() error {
	var e error
	ctx := s.BaseController.Ctx
	c := ctx.Connection

	years := [3]int{2013, 2014, 2015}
	sintax := "select Distinct(Element) from MORSummary"
	csr, e := c.NewQuery().Command("freequery", tk.M{}.Set("syntax", sintax)).Cursor(nil)
	defer csr.Close()

	if e != nil {
		return e
	}

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

	csr1, e := c.NewQuery().From(new(MasterEquipmentType).TableName()).Cursor(nil)
	defer csr1.Close()

	if e != nil {
		return e
	}

	query := []*dbox.Filter{}

	for _, year := range years {
		yearFirst := strconv.Itoa(year)
		yearFirst = yearFirst + "-01-01 00:00:00.000"

		yearLast := strconv.Itoa(year + 1)
		yearLast = yearLast + "-01-01 00:00:00.000"

		query = append(query, dbox.And(dbox.Gte("Period", yearFirst), dbox.Lte("Period", yearLast)))

		csr2, e := c.NewQuery().From(new(MaintenanceCost).TableName()).Where(query...).Cursor(nil)
		defer csr2.Close()

		if e != nil {
			return e
		}

		datas := []tk.M{}
		e = csr2.Fetch(&datas, 0, false)

		Plants := crowd.From(&datas).Group(func(x interface{}) interface{} {
			return x.(tk.M).GetString("plant")
		}, nil).Exec().Result.Data().([]crowd.KV)

		if len(Plants) > 0 {
			for _, p := range Plants {
				plant := p.Key.(string)
				EqType := crowd.From(&datas).Where(func(x interface{}) interface{} {
					period := x.(tk.M).GetString("period")
					return strings.Contains(period, strconv.Itoa(year)) && x.(tk.M).GetString("plant") == plant
				}).Exec().Result.Data().([]tk.M)

				if len(EqType) > 0 {
					EquipmentTypes := crowd.From(&EqType).Group(func(x interface{}) interface{} {
						return x.(tk.M).GetString("equipmenttype")
					}, nil).Exec().Result.Data().([]crowd.KV)

					for _, eq := range EquipmentTypes {
						EquipmentType := eq.Key.(string)
						ActType := crowd.From(&EqType).Where(func(x interface{}) interface{} {
							return x.(tk.M).GetString("equipmenttype") == EquipmentType
						}).Exec().Result.Data().([]tk.M)

						if len(ActType) > 0 {
							MaintActivityTypes := crowd.From(&ActType).Group(func(x interface{}) interface{} {
								return x.(tk.M).GetString("maintenanceactivitytype")
							}, nil).Exec().Result.Data().([]crowd.KV)

							for _, act := range MaintActivityTypes {
								MaintActivityType := act.Key.(string)

								OrderType := crowd.From(&ActType).Where(func(x interface{}) interface{} {
									return x.(tk.M).GetString("maintenanceactivitytype") == MaintActivityType
								}).Exec().Result.Data().([]tk.M)

								if len(OrderType) > 0 {
									OrderTypes := crowd.From(&OrderType).Group(func(x interface{}) interface{} {
										return x.(tk.M).GetString("ordertype")
									}, nil).Exec().Result.Data().([]crowd.KV)

									for _, order := range OrderTypes {
										OrderTypeString := order.Key.(string)
										OrderNo := crowd.From(&OrderType).Where(func(x interface{}) interface{} {
											return x.(tk.M).GetString("ordertype") == OrderTypeString
										}).Exec().Result.Data().([]tk.M)
										if len(OrderNo) > 0 {
											Equipment := crowd.From(&OrderNo).Group(func(x interface{}) interface{} {
												return x.(tk.M).GetString("equipment")
											}, nil).Exec().Result.Data().([]crowd.KV)

											for _, eqNo := range Equipment {
												eqNoString := eqNo.Key.(string)
												for _, element := range MROElements {
													_ = element
													pcs := new(PreventiveCorrectiveSummary)
													pcs.PeriodYear = year
													pcs.OrderType = OrderTypeString
													pcs.EquipmentNo = eqNoString

													equipmentDescription := crowd.From(&OrderNo).Where(func(x interface{}) interface{} {
														return x.(tk.M).GetString("equipment") == eqNoString
													}).Exec().Result.Data().([]tk.M)

													if len(equipmentDescription) > 0 {
														pcs.EquipmentDescription = equipmentDescription[0].GetString("equipmentdesc")
													}

													if len(EquipmentTypes) == 1 {
														pcs.EquipmentType = "Other"
														pcs.EquipmentTypeDescription = "Other"
													} else {
														pcs.EquipmentType = EquipmentType
														pcs.EquipmentTypeDescription = equipmentDescription[0].GetString("equipmenttypedesc")
													}

													pcs.ActivityType = MaintActivityType
													pcs.Plant = PlantNormalization(plant)
													pcs.Element = element.GetString("element")

													result := float64(len(equipmentDescription) / len(MROElements))
													pcs.MOCount = int(Round(result, .5, 2))

													switch element.GetString("element") {
													case "Internal Labor":
														pcs.Value = crowd.From(&equipmentDescription).Sum(func(x interface{}) interface{} {
															return x.(tk.M).GetFloat64("internallaboractual")
														}).Exec().Result.Sum
														break
													case "Internal Material":
														pcs.Value = crowd.From(&equipmentDescription).Sum(func(x interface{}) interface{} {
															return x.(tk.M).GetFloat64("internalmaterialactual")
														}).Exec().Result.Sum
														break
													case "Direct Material":
														pcs.Value = crowd.From(&equipmentDescription).Sum(func(x interface{}) interface{} {
															return x.(tk.M).GetFloat64("directmaterialactual")
														}).Exec().Result.Sum
														break
													case "External Service":
														pcs.Value = crowd.From(&equipmentDescription).Sum(func(x interface{}) interface{} {
															return x.(tk.M).GetFloat64("externalserviceactual")
														}).Exec().Result.Sum
														break
													}

													_, e := ctx.InsertOut(pcs)

													if e != nil {
														log.Println(e.Error())
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return e
}
func (m *HistoricalValueEquation) GetValueEquationDocument(ctx *orm.DataContext, VEDQList []*ValueEquationDataQuality) {
	for _, i := range VEDQList {
		query := []*dbox.Filter{}
		query = append(query, dbox.Eq("VEId", int(i.Id)))

		CapacityPaymentDocuments := []VEDQCapacityPaymentDocuments{}
		csr, _ := ctx.Find(new(VEDQCapacityPaymentDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&CapacityPaymentDocuments, 0, false)
			i.CapacityPaymentDocuments = CapacityPaymentDocuments
		}
		csr.Close()

		BackupFuelDocuments := []VEDQBackupFuelDocuments{}
		csr, _ = ctx.Find(new(VEDQBackupFuelDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&BackupFuelDocuments, 0, false)
			i.BackupFuelDocuments = BackupFuelDocuments
		}
		csr.Close()

		EnergyPaymentDocuments := []VEDQEnergyPaymentDocuments{}
		csr, _ = ctx.Find(new(VEDQEnergyPaymentDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&EnergyPaymentDocuments, 0, false)
			i.EnergyPaymentDocuments = EnergyPaymentDocuments
		}
		csr.Close()

		MaintenanceCostDocuments := []VEDQMaintenanceCostDocuments{}
		csr, _ = ctx.Find(new(VEDQMaintenanceCostDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&MaintenanceCostDocuments, 0, false)
			i.MaintenanceCostDocuments = MaintenanceCostDocuments
		}
		csr.Close()

		MaintenanceDurationDocuments := []VEDQMaintenanceDurationDocuments{}
		csr, _ = ctx.Find(new(VEDQMaintenanceDurationDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&MaintenanceDurationDocuments, 0, false)
			i.MaintenanceDurationDocuments = MaintenanceDurationDocuments
		}
		csr.Close()

		PenaltyDocuments := []VEDQPenaltyDocuments{}
		csr, _ = ctx.Find(new(VEDQPenaltyDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&PenaltyDocuments, 0, false)
			i.PenaltyDocuments = PenaltyDocuments
		}
		csr.Close()

		IncentiveDocuments := []VEDQIncentiveDocuments{}
		csr, _ = ctx.Find(new(VEDQIncentiveDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&IncentiveDocuments, 0, false)
			i.IncentiveDocuments = IncentiveDocuments
		}
		csr.Close()

		PrimaryFuel1stDocuments := []VEDQPrimaryFuel1stDocuments{}
		csr, _ = ctx.Find(new(VEDQPrimaryFuel1stDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&PrimaryFuel1stDocuments, 0, false)
			i.PrimaryFuel1stDocuments = PrimaryFuel1stDocuments
		}
		csr.Close()

		PrimaryFuel2ndDocuments := []VEDQPrimaryFuel2ndDocuments{}
		csr, _ = ctx.Find(new(VEDQPrimaryFuel2ndDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&PrimaryFuel2ndDocuments, 0, false)
			i.PrimaryFuel2ndDocuments = PrimaryFuel2ndDocuments
		}
		csr.Close()

		StartupPaymentDocuments := []VEDQStartupPaymentDocuments{}
		csr, _ = ctx.Find(new(VEDQStartupPaymentDocuments), tk.M{}.Set("where", dbox.And(query...)))
		if csr != nil {
			csr.Fetch(&StartupPaymentDocuments, 0, false)
			i.StartupPaymentDocuments = StartupPaymentDocuments
		}
		csr.Close()
	}
}
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
}
示例#26
0
// generateDurationIntervalSummary ...
func (d *GenWOListSummary) generateDurationIntervalSummary() error {
	years := [3]int{2013, 2014, 2015}

	c := d.Ctx.Connection

	csr, e := c.NewQuery().From(new(TempMstPlant).TableName()).Cursor(nil)

	if e != nil {
		return e
	} else {
		defer csr.Close()
	}

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

	for _, year := range years {
		query := []*dbox.Filter{}

		yearFirst := strconv.Itoa(year)
		yearFirst = yearFirst + "-01-01 00:00:00.000"

		yearLast := strconv.Itoa(year + 1)
		yearLast = yearLast + "-01-01 00:00:00.000"

		query = append(query, dbox.And(dbox.Gte("ActualStart", yearFirst), dbox.Lte("ActualFinish", yearLast)))

		csr1, e := c.NewQuery().From(new(WOList).TableName()).Where(query...).Order("ActualStart").Cursor(nil)

		if e != nil {
			return e
		} else {
			defer csr1.Close()
		}

		datas := []tk.M{}
		e = csr1.Fetch(&datas, 0, false)

		if len(datas) > 0 {
			for _, data := range datas {
				woles := new(WOListSummary)
				woles.PeriodYear = year
				woles.OrderType = data.GetString("type")
				woles.FunctionalLocation = data.GetString("functionallocation")

				query = nil
				query = append(query, dbox.Eq("FunctionalLocationCode", data.GetString("functionallocation")))

				csr2, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Select("CatProf").Cursor(nil)

				if e != nil {
					return e
				} else {
					defer csr2.Close()
				}

				tempResult := []tk.M{}
				e = csr2.Fetch(&tempResult, 0, false)

				if len(tempResult) > 0 {
					woles.EquipmentType = tempResult[0].GetString("catprof")
				}

				woles.MainenanceOrderCode = data.GetString("ordercode")
				woles.NotificationCode = data.GetString("notificationcode")

				query = nil
				query = append(query, dbox.Eq("FunctionalLocationCode", data.GetString("plant")))

				csr3, e := c.NewQuery().From(new(FunctionalLocation).TableName()).Where(query...).Select("Description").Cursor(nil)

				if e != nil {
					return e
				} else {
					defer csr3.Close()
				}

				tempResult = []tk.M{}
				e = csr3.Fetch(&tempResult, 0, false)

				if len(tempResult) > 0 {
					woles.Plant = tempResult[0].GetString("description")
				}

				woles.PlanStart, e = time.Parse(time.RFC3339, data["scheduledstart"].(string))

				if e != nil {
					return e
				}

				woles.PlanEnd, e = time.Parse(time.RFC3339, data["scheduledfinish"].(string))

				if e != nil {
					return e
				}

				subTime := woles.PlanEnd.Sub(woles.PlanStart)
				woles.PlanDuration = subTime.Hours()

				woles.ActualStart, e = time.Parse(time.RFC3339, data.GetString("actualstart"))
				if e != nil {
					return e
				}

				woles.ActualEnd, e = time.Parse(time.RFC3339, data.GetString("actualfinish"))
				if e != nil {
					return e
				}

				subTime = woles.ActualEnd.Sub(woles.ActualStart)
				woles.ActualDuration = subTime.Hours()

				actualstartPart := strings.Split(woles.ActualStart.String(), " ")
				actualstartPart = []string{actualstartPart[0], actualstartPart[1]}
				query = nil
				query = append(query, dbox.Lt("ActualFinish", strings.Join(actualstartPart, " ")))
				query = append(query, dbox.Eq("FunctionalLocation", woles.FunctionalLocation))
				csr4, e := c.NewQuery().Select("ActualFinish").From(new(WOList).TableName()).Order("-ActualFinish").Where(query...).Cursor(nil)

				if e != nil {
					return e
				} else {
					defer csr4.Close()
				}

				tempResult = []tk.M{}
				e = csr4.Fetch(&tempResult, 0, false)

				if len(tempResult) > 0 {
					woles.LastMaintenanceEnd, e = time.Parse(time.RFC3339, tempResult[0].GetString("actualfinish"))
				}

				if woles.ActualStart.String() != "" && woles.LastMaintenanceEnd.String() != "" {
					subTime = woles.ActualStart.Sub(woles.LastMaintenanceEnd)
					woles.LastMaintenanceInterval = subTime.Seconds() / 86400
				}

				woles.Cost = data.GetFloat64("actualcost")
				plantTypes := crowd.From(&MstPlantData).Where(func(x interface{}) interface{} {
					return x.(tk.M).GetString("plantcode") == data.GetString("plant")
				}).Exec().Result.Data().([]tk.M)

				if len(plantTypes) > 0 {
					woles.PlantType = plantTypes[0].GetString("plantcode")
				}

				for {
					e = d.Ctx.Insert(woles)
					if e == nil {
						break
					} else {
						d.Ctx.Connection.Connect()
					}
				}
			}
		}
	}
	return nil
}
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
}
示例#28
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
}
示例#29
0
func (w *WebGrabberController) DaemonToggle(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := struct {
		OP string `json:"op"`
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	if runtime.GOOS == "windows" {

		if payload.OP == "off" {
			// cek tasklist -> sedotand.exe
			sedotandExist := GetSedotandWindows()

			if sedotandExist == false {
				return helper.CreateResult(false, false, "")
			}

			err := exec.Command("taskkill", "/IM", "sedotand.exe", "/F").Start()

			if err != nil {
				return helper.CreateResult(false, false, err.Error())
			}
			return helper.CreateResult(true, false, "")
		} else {
			sedotanPath := f.Join(EC_APP_PATH, "cli", "sedotand.exe")
			sedotanConfigPath := f.Join(EC_APP_PATH, "config", "webgrabbers.json")
			sedotanConfigArg := fmt.Sprintf(`-config="%s"`, sedotanConfigPath)
			sedotanLogPath := f.Join(EC_APP_PATH, "daemon")
			sedotanLogArg := fmt.Sprintf(`-logpath="%s"`, sedotanLogPath)

			fmt.Println("===> ", sedotanPath, sedotanConfigArg, sedotanLogArg, "&")

			err := exec.Command(sedotanPath, sedotanConfigArg, sedotanLogArg, "&").Start()
			//syscal.exec
			/*
				binary, lookErr := exec.LookPath("cmd")
				if lookErr != nil {
					panic(lookErr)
				}
				err := syscall.Exec(binary, []string{"cmd", "-c", sedotanPath}, os.Environ())
			*/
			if err != nil {
				return helper.CreateResult(false, false, err.Error())
			}
			return helper.CreateResult(true, true, "")
		}
	} else {
		if err := w.SyncConfig(); err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}

		filters := dbox.And(dbox.Eq("serverType", "node"), dbox.Eq("os", "linux"))
		cursor, err := colonycore.Find(new(colonycore.Server), filters)
		if err != nil {
			return helper.CreateResult(false, false, err.Error())
		}

		all := []colonycore.Server{}
		err = cursor.Fetch(&all, 0, true)
		if err != nil {
			return helper.CreateResult(false, false, err.Error())
		}

		if len(all) == 0 {
			return helper.CreateResult(false, false, "No server registered")
		}

		serverC := &ServerController{}

		if payload.OP == "off" {
			var howManyErrors = 0
			for _, server := range all {
				_, err = serverC.ToggleSedotanService("stop", server.ID)
				if err != nil {
					howManyErrors = howManyErrors + 1
				}
			}

			if howManyErrors == 0 {
				return helper.CreateResult(true, nil, "")
			}

			return helper.CreateResult(false, nil, "Sedotan won't start on some servers")
		} else {
			var howManyErrors = 0
			for _, server := range all {
				_, err = serverC.ToggleSedotanService("start stop", server.ID)
				if err != nil {
					howManyErrors = howManyErrors + 1
				}
			}

			if howManyErrors == 0 {
				return helper.CreateResult(true, nil, "")
			}

			return helper.CreateResult(false, nil, "Sedotan won't start on some servers")
		}
	}

	return helper.CreateResult(false, false, "Internal server error")
}
示例#30
0
func (d *WebGrabberController) SyncConfig() error {
	all := []colonycore.Server{}
	configName := fmt.Sprintf("%s.json", new(colonycore.WebGrabber).TableName())
	srcOriginal := f.Join(EC_APP_PATH, "config", configName)

	bytes, err := ioutil.ReadFile(srcOriginal)
	if err != nil {
		return err
	}

	ifaces, _ := net.Interfaces()
	addr, _ := ifaces[len(ifaces)-1].Addrs()
	ip := addr[len(addr)-1].(*net.IPNet).IP.String()

	srcString := string(bytes)
	for _, keyword := range []string{`host":"localhost`, `host":"http://localhost`, `host":"https://localhost`} {
		if strings.Contains(srcString, keyword) {
			newKeyword := strings.Replace(keyword, "localhost", ip, -1)
			srcString = strings.Replace(srcString, keyword, newKeyword, -1)
		}
	}

	src := f.Join(EC_APP_PATH, "config", "tmp"+configName)
	os.Create(src)
	err = ioutil.WriteFile(src, []byte(srcString), 755)
	if err != nil {
		return err
	}

	filters := dbox.And(dbox.Eq("serverType", "node"), dbox.Eq("os", "linux"))
	cursor, err := colonycore.Find(new(colonycore.Server), filters)
	if err != nil {
		return err
	}

	err = cursor.Fetch(&all, 0, true)
	if err != nil {
		return err
	}

	errs := []error{}

	for _, each := range all {
		setting, _, err := (&each).Connect()
		if err != nil {
			errs = append(errs, err)
			continue
		}

		dst := f.Join(each.AppPath, "config", configName)
		err = setting.SshCopyByPath(src, dst)
		if err != nil {
			errs = append(errs, err)
			continue
		}
	}

	os.Remove(src)

	if len(errs) == len(all) && len(errs) > 0 {
		return errs[0]
	}

	fmt.Println(configName, "synced w/ errors", errs)

	return nil
}