Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sync"
  6. "strconv"
  7. _ "github.com/lib/pq"
  8. "database/sql"
  9. "time"
  10. "math/rand"
  11. "sort"
  12. )
  13.  
  14. //Create a struct that will be written to and then passed into a channel of that struct type
  15. //query results with result.Value and place them into db.QueryRow function to update database
  16. //then implement a simple method for creating purposeful errors with the database
  17. //test simple KNN impute algorithm against the database, as a seperate application
  18. //create the presentation around this
  19.  
  20. func printLn(query string, count int, results chan<- string) {
  21. result := query + "_" + strconv.Itoa(count)
  22. // fmt.Println(result)
  23. results <- result
  24. }
  25.  
  26. func main () {
  27. results := make(chan string)
  28. var waitGroup sync.WaitGroup
  29. query := "friend_ID_"
  30. for i := 1; i < 101; i++ {
  31. waitGroup.Add(1)
  32. go func (query string, i int) {
  33. printLn(query, i, results)
  34. waitGroup.Done()
  35. }(query, i)
  36. }
  37.  
  38. go func() {
  39. // Wait for everything to be processed.
  40. waitGroup.Wait()
  41.  
  42. // Close the channel to signal to the Display
  43. // function that we can exit the program.
  44. close(results)
  45. }()
  46. // Display our final results
  47. writeDB(results)
  48. }
  49.  
  50. func writeDB (results chan string) {
  51. count := 0
  52. const (
  53. DB_USER = "postgres"
  54. DB_PASSWORD = "postgres"
  55. DB_NAME = "orangemvp"
  56. )
  57. dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
  58. DB_USER, DB_PASSWORD, DB_NAME)
  59. db, err := sql.Open("postgres", dbinfo)
  60. if err != nil {
  61. fmt.Println(fmt.Sprint(err))
  62. }
  63. defer db.Close()
  64. for result := range results {
  65. count++
  66. var friendSlice []float64
  67. if (count % 1000 == 0) {
  68. fmt.Println(result + ": " + strconv.Itoa(count))
  69. }
  70. for i := 0; i < 7; i++ {
  71. friendSlice = append(friendSlice, randNum())
  72. }
  73. sort.Float64s(friendSlice)
  74. //This can be used to simulate randomly missing values within our database
  75. if (count % 25000 == 0) {
  76. friendSlice[random(0, 6)] = 0.0
  77. }
  78. // fmt.Println(friendSlice)
  79. // fmt.Println(result)
  80. db.QueryRow("INSERT INTO friendlistrelation(friend_id, friend_1, friend_2, friend_3, friend_4, friend_5, friend_6, friend_7, created) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)", result, friendSlice[6], friendSlice[5], friendSlice[4], friendSlice[3], friendSlice[2], friendSlice[1], friendSlice[0], time.Now().Local()).Scan()
  81. }
  82.  
  83. return
  84. }
  85.  
  86. func randNum() (randFloat float64) {
  87. return rand.Float64()
  88. }
  89.  
  90. func random(min, max int) int {
  91. rand.Seed(time.Now().Unix())
  92. return rand.Intn(max - min) + min
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement