| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 	"math/rand" | 
					
						
							| 
									
										
										
										
											2017-03-31 02:23:40 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	bus.AddHandler("sql", InsertSqlTestData) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 16:56:23 +08:00
										 |  |  | func sqlRandomWalk(m1 string, m2 string, intWalker int64, floatWalker float64, sess *DBSession) error { | 
					
						
							| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 16:09:52 +08:00
										 |  |  | 	timeWalker := time.Now().UTC().Add(time.Hour * -200) | 
					
						
							| 
									
										
										
										
											2017-03-31 18:49:05 +08:00
										 |  |  | 	now := time.Now().UTC() | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 	step := time.Minute | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	row := &m.SqlTestData{ | 
					
						
							|  |  |  | 		Metric1:      m1, | 
					
						
							|  |  |  | 		Metric2:      m2, | 
					
						
							|  |  |  | 		TimeEpoch:    timeWalker.Unix(), | 
					
						
							|  |  |  | 		TimeDateTime: timeWalker, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 	for timeWalker.Unix() < now.Unix() { | 
					
						
							|  |  |  | 		timeWalker = timeWalker.Add(step) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		row.Id = 0 | 
					
						
							| 
									
										
										
										
											2017-04-19 16:09:52 +08:00
										 |  |  | 		row.ValueBigInt += rand.Int63n(200) - 100 | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 		row.ValueDouble += rand.Float64() - 0.5 | 
					
						
							|  |  |  | 		row.ValueFloat += rand.Float32() - 0.5 | 
					
						
							|  |  |  | 		row.TimeEpoch = timeWalker.Unix() | 
					
						
							|  |  |  | 		row.TimeDateTime = timeWalker | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sqlog.Info("Writing SQL test data row") | 
					
						
							| 
									
										
										
										
											2017-03-31 02:23:40 +08:00
										 |  |  | 		if _, err := sess.Table("test_data").Insert(row); err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func InsertSqlTestData(cmd *m.InsertSqlTestDataCommand) error { | 
					
						
							| 
									
										
										
										
											2017-05-23 16:56:23 +08:00
										 |  |  | 	return inTransaction(func(sess *DBSession) error { | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 		var err error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sqlog.Info("SQL TestData: Clearing previous test data") | 
					
						
							|  |  |  | 		res, err := sess.Exec("TRUNCATE test_data") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rows, _ := res.RowsAffected() | 
					
						
							|  |  |  | 		sqlog.Info("SQL TestData: Truncate done", "rows", rows) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sqlRandomWalk("server1", "frontend", 100, 1.123, sess) | 
					
						
							| 
									
										
										
										
											2017-03-31 19:57:10 +08:00
										 |  |  | 		sqlRandomWalk("server2", "frontend", 100, 1.123, sess) | 
					
						
							|  |  |  | 		sqlRandomWalk("server3", "frontend", 100, 1.123, sess) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2017-03-31 02:01:04 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |