Esempio n. 1
0
func main() {
	d1, err := epochdate.Parse(epochdate.RFC3339, "2013-04-11") // thursday
	if err != nil {
		panic(err)
	}
	d2, err := epochdate.Parse(epochdate.RFC3339, "2013-07-17") // sunday
	if err != nil {
		panic(err)
	}
	du, err := epochdate.Parse(epochdate.RFC3339, "2013-06-04") // tuesday
	if err != nil {
		panic(err)
	}

	p, err := dapegen.NewGenerator(d1, d2, dapegen.DAY)
	//p.FirstDayOfWeek = time.Wednesday
	if err != nil {
		panic(err)
	}

	// print first days until du
	// loop 4 times to test if nextuntil is really stopping
	for i := 0; i < 4; i++ {
		for p.NextUntil(du) {
			fmt.Printf("%s\n", p.CurrentDate.String())
		}
		fmt.Printf("@@ %s\n", p.CurrentDate.String())
	}

	// print remaining days
	for p.Next() {
		fmt.Printf("!! %s\n", p.CurrentDate.String())
	}
}
Esempio n. 2
0
func TestGeneratorDay(t *testing.T) {
	d1, err := epochdate.Parse(epochdate.RFC3339, "2013-08-02")
	if err != nil {
		t.Fatal(err)
	}
	d2, err := epochdate.Parse(epochdate.RFC3339, "2013-08-10")
	if err != nil {
		t.Fatal(err)
	}
	d3, err := epochdate.Parse(epochdate.RFC3339, "2013-08-05")
	if err != nil {
		t.Fatal(err)
	}

	// creates a new generator by day
	p, err := NewGenerator(d1, d2, DAY)
	if err != nil {
		t.Fatal(err)
	}

	ct := 0
	for p.Next() {
		if ct == 3 && p.CurrentDate != d3 {
			t.Fatalf("Invalid date generated (%s - should be %s)", p.CurrentDate.String(), d3.String())
		}
		ct++
	}

	if ct != 9 {
		t.Fatalf("Should have generated 9 items, but generated %d", ct)
	}
}
Esempio n. 3
0
func TestGeneratorMonth(t *testing.T) {
	d1, err := epochdate.Parse(epochdate.RFC3339, "2013-05-12")
	if err != nil {
		t.Fatal(err)
	}
	d2, err := epochdate.Parse(epochdate.RFC3339, "2013-08-25")
	if err != nil {
		t.Fatal(err)
	}

	d3, err := epochdate.Parse(epochdate.RFC3339, "2013-07-01")
	if err != nil {
		t.Fatal(err)
	}

	// creates a new generator by month
	p, err := NewGenerator(d1, d2, MONTH)
	if err != nil {
		t.Fatal(err)
	}

	ct := 0
	for p.Next() {
		if ct == 2 && p.CurrentDate != d3 {
			t.Fatalf("Invalid date generated (%s - should be %s)", p.CurrentDate.String(), d3.String())
		}
		ct++
	}

	if ct != 4 {
		t.Fatalf("Should have generated 4 items, but generated %d", ct)
	}
}
Esempio n. 4
0
func TestGeneratorWeek(t *testing.T) {
	d1, err := epochdate.Parse(epochdate.RFC3339, "2013-08-01") // thursday
	if err != nil {
		t.Fatal(err)
	}
	d2, err := epochdate.Parse(epochdate.RFC3339, "2013-08-25") // sunday
	if err != nil {
		t.Fatal(err)
	}

	d3, err := epochdate.Parse(epochdate.RFC3339, "2013-08-14")
	if err != nil {
		t.Fatal(err)
	}

	// creates a new generator by week
	p, err := NewGenerator(d1, d2, WEEK)
	p.FirstDayOfWeek = time.Wednesday
	if err != nil {
		t.Fatal(err)
	}

	ct := 0
	for p.Next() {
		if ct == 2 && p.CurrentDate != d3 {
			t.Fatalf("Invalid date generated (%s - should be %s)", p.CurrentDate.String(), d3.String())
		}
		ct++
	}

	if ct != 4 {
		t.Fatalf("Should have generated 4 items, but generated %d", ct)
	}
}
Esempio n. 5
0
func TestGeneratorUntil(t *testing.T) {
	d1, err := epochdate.Parse(epochdate.RFC3339, "2013-04-11") // thursday
	if err != nil {
		t.Fatal(err)
	}
	d2, err := epochdate.Parse(epochdate.RFC3339, "2013-07-17") // sunday
	if err != nil {
		t.Fatal(err)
	}
	du, err := epochdate.Parse(epochdate.RFC3339, "2013-06-04") // tuesday
	if err != nil {
		t.Fatal(err)
	}

	// creates a new generator by day
	p, err := NewGenerator(d1, d2, DAY)
	if err != nil {
		t.Fatal(err)
	}

	ct := 0

	// loop 4 times to test if nextuntil is really stopping
	for i := 0; i < 4; i++ {
		for p.NextUntil(du) {
			ct++
		}
	}

	if ct != 54 {
		t.Fatalf("Should have generated 54 items, but generated %d", ct)
	}

	if p.CurrentDate != du {
		t.Fatalf("Invalid current date (%s - should be %s)", p.CurrentDate.String(), du.String())
	}

	ct++ // count stopped date

	for p.Next() {
		ct++
	}

	if ct != 98 {
		t.Fatalf("Should have generated 98 items, but generated %d", ct)
	}
}
Esempio n. 6
0
func QueryStats(db *mgo.Database, statsquery *StatsQuery) (*StatsQueryResult, error) {
	// check parameters
	if statsquery.Process == "" || statsquery.Data == nil || len(statsquery.Data) == 0 {
		return nil, fmt.Errorf("Required parameter not sent")
	}
	if statsquery.Groups == nil {
		statsquery.Groups = make([]string, 0)
	}

	// sanitize
	if !data.ValidateValueName(statsquery.Process) {
		return nil, fmt.Errorf("Invalid process name - name not validated: %s", statsquery.Process)
	}
	if statsquery.App != "" && statsquery.App != "@" && !data.ValidateName(statsquery.App) {
		return nil, fmt.Errorf("Invalid app name - name not validated: %s", statsquery.App)
	}
	for _, gval := range statsquery.Groups {
		if gval != "_app" && !data.ValidateName(gval) {
			return nil, fmt.Errorf("Invalid group name - name not validated: %s", gval)
		}
	}

	if statsquery.Amount < 1 {
		statsquery.Amount = 1
	}

	// find collection
	cname := fmt.Sprintf("stat_%s", statsquery.Process)
	if statsquery.App != "" {
		cname += "-app"
	}

	if !infoCollectionExists(db, cname) {
		return nil, fmt.Errorf("Process not found: %s", statsquery.Process)
	}

	c := db.C(cname)
	if statsquery.App != "" {
		c.EnsureIndex(mgo.Index{
			Key:        []string{"_dt", "_app"},
			Background: true,
			Sparse:     true,
		})
	} else {
		c.EnsureIndex(mgo.Index{
			Key:        []string{"_dt"},
			Background: true,
			Sparse:     true,
		})
	}

	// start time
	startdate := epochdate.TodayUTC() - epochdate.Date(statsquery.Amount) + 1
	enddate := epochdate.TodayUTC()

	//log.Printf("StartDate: %s - EndDate: %s", startdate.String(), enddate.String())

	// build mongodb filter
	filter := bson.M{"_dt": bson.M{"$gte": startdate.String()}}
	if statsquery.App != "" && statsquery.App != "@" {
		filter["_app"] = statsquery.App
	}

	if statsquery.Filters != nil {
		for pn, pv := range statsquery.Filters {
			// sanitize
			if !data.ValidateValueName(pn) {
				return nil, fmt.Errorf("Invalid filter name - name not validated: %s", pn)
			}
			//log.Printf("Filter: %s = %s", pn, pv)
			filter[pn] = pv
		}
	}

	querysort := []string{"_dt"}
	if len(statsquery.Groups) > 0 {
		for _, g := range statsquery.Groups {
			querysort = append(querysort, g)
		}
	}

	query := c.Find(filter).Sort(querysort...).Iter()

	groupcollect := make(map[string]*InfoGroupInfo, 0)

	fdata := make(map[string]interface{})
	for query.Next(&fdata) {
		datadate, _ := epochdate.Parse(epochdate.RFC3339, fdata["_dt"].(string))
		//log.Printf("DateDate: %s", datadate.String())

		// build group string
		curgroup := ""
		if len(statsquery.Groups) > 0 {
			for _, g := range statsquery.Groups {
				if gv, ok := fdata[g]; ok {
					curgroup = curgroup + "::" + fmt.Sprintf("%v", gv)
				} else {
					return nil, fmt.Errorf("No such field %s", g)
				}
			}
		}

		ginfo, sdok := groupcollect[curgroup]
		if !sdok {
			//log.Printf("New data for group %s", curgroup)

			// stats collector, fills empty periods with 0
			scollect := NewSDayCollect(statsquery.Period)
			for _, ditem := range statsquery.Data {
				// add data - output name is equals data name
				scollect.AddImport(ditem, ditem)
			}
			scollect.Init(startdate, enddate)
			ginfo = &InfoGroupInfo{GroupId: curgroup, Groups: make(map[string]interface{}), Collect: scollect}
			groupcollect[curgroup] = ginfo

			for _, g := range statsquery.Groups {
				if gv, ok := fdata[g]; ok {
					ginfo.Groups[g] = gv
				} else {
					return nil, fmt.Errorf("No such field %s", g)
				}
			}
		}

		// fill day from data
		ginfo.Collect.ValueDay(datadate, fdata)
	}

	if err := query.Close(); err != nil {
		return nil, fmt.Errorf("Error reading data: %s", err)
	}

	var res interface{}
	if len(statsquery.Groups) > 0 {
		resgroup := &InfoResultGroup{Group: make([]*InfoResultGroupItem, 0)}
		for _, gv := range groupcollect {
			resgroup.Group = append(resgroup.Group, &InfoResultGroupItem{GroupId: gv.GroupId, Groups: gv.Groups, InfoResult: &InfoResult{List: gv.Collect.BuildResult()}})
		}
		res = resgroup
	} else {
		resinfo := &InfoResult{List: nil}
		if ri, ok := groupcollect[""]; ok {
			resinfo.List = ri.Collect.BuildResult()
		} else {
			resinfo.List = nil
		}
		res = resinfo
	}

	return &StatsQueryResult{
		StartDate: startdate,
		//EndDate:   curdate,
		EndDate: enddate,
		Result:  res,
	}, nil
}