Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 8ebe0aa

Browse files
author
Sung Won Cho
committed
Generate mutation for votes
1 parent a163275 commit 8ebe0aa

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

scripts/votes/main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Original schema:
2+
// https://ia800500.us.archive.org/22/items/stackexchange/readme.txt
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"encoding/xml"
9+
"flag"
10+
"fmt"
11+
"io/ioutil"
12+
"log"
13+
"math/rand"
14+
"net/http"
15+
"sync"
16+
"time"
17+
)
18+
19+
var (
20+
dir = flag.String("dir", "", "Directory which holds Votes.xml file")
21+
dryRun = flag.Bool("dryrun", true, "Only show mutations.")
22+
)
23+
24+
// random generates a random integer given a range
25+
func random(min, max int) int {
26+
return rand.Intn(max-min) + min
27+
}
28+
29+
func init() {
30+
rand.Seed(time.Now().Unix())
31+
}
32+
33+
type Vote struct {
34+
Id string `xml:",attr"`
35+
PostId int `xml:",attr"`
36+
VoteTypeId int `xml:",attr"`
37+
CreationDate string `xml:",attr"`
38+
}
39+
40+
type Votes struct {
41+
Rows []Vote `xml:"row"`
42+
}
43+
44+
func check(err error) {
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
}
49+
50+
func main() {
51+
flag.Parse()
52+
53+
data, err := ioutil.ReadFile(*dir + "/Votes.xml")
54+
check(err)
55+
var votes Votes
56+
check(xml.Unmarshal(data, &votes))
57+
58+
var wg sync.WaitGroup
59+
limiter := make(chan struct{}, 100)
60+
if *dryRun {
61+
limiter = make(chan struct{}, 1)
62+
}
63+
64+
for _, v := range votes.Rows[:10] {
65+
fmt.Println(v.Id)
66+
var b bytes.Buffer
67+
68+
node := "v" + v.Id
69+
b.WriteString("mutation { set { ")
70+
71+
// We generate userId for the user that casted the vote, because dataset is anonymized
72+
// and does not always contain userId
73+
author_id := random(1, 1000)
74+
b.WriteString(fmt.Sprintf("<%v> <Author> <u%v> .\n", node, author_id))
75+
b.WriteString(fmt.Sprintf("<%v> <Post> <p%v> .\n", node, v.PostId))
76+
b.WriteString(fmt.Sprintf("<%v> <Timestamp> %q .\n", node, v.CreationDate))
77+
78+
if v.VoteTypeId == 2 {
79+
b.WriteString(fmt.Sprintf("<%v> <Type> \"up\" .\n", node))
80+
} else if v.VoteTypeId == 3 {
81+
b.WriteString(fmt.Sprintf("<%v> <Type> \"down\" .\n", node))
82+
}
83+
84+
b.WriteString("}}")
85+
wg.Add(1)
86+
go func(b *bytes.Buffer) {
87+
limiter <- struct{}{}
88+
fmt.Println(b.String())
89+
if !*dryRun {
90+
resp, err := http.Post("http://localhost:8080/query", "", b)
91+
check(err)
92+
body, err := ioutil.ReadAll(resp.Body)
93+
check(err)
94+
fmt.Printf("%q\n\n", body)
95+
check(resp.Body.Close())
96+
}
97+
wg.Done()
98+
<-limiter
99+
}(&b)
100+
}
101+
102+
wg.Wait()
103+
fmt.Println(len(votes.Rows), "processed")
104+
}

0 commit comments

Comments
 (0)