remove old boltdb-related tools

This commit is contained in:
Herman Schaaf
2019-07-28 21:41:08 +01:00
parent 2b9fccd81b
commit 9c65b472da
3 changed files with 0 additions and 281 deletions

View File

@@ -1,125 +0,0 @@
package main
import (
"container/heap"
"encoding/json"
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/boltdb/bolt"
"github.com/gojp/goreportcard/handlers"
)
var (
repo = flag.String("remove", "", "repo to remove from high scores")
listDupes = flag.Bool("list-duplicates", false, "list duplicate repos in cache")
)
func deleteRepo(repo string) error {
db, err := bolt.Open("goreportcard.db", 0755, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return fmt.Errorf("could not open bolt db: %v", err)
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
mb := tx.Bucket([]byte("meta"))
if mb == nil {
return fmt.Errorf("high score bucket not found")
}
scoreBytes := mb.Get([]byte("scores"))
scores := &handlers.ScoreHeap{}
json.Unmarshal(scoreBytes, scores)
heap.Init(scores)
var found bool
for i := range *scores {
if strings.ToLower((*scores)[i].Repo) == strings.ToLower(repo) {
log.Printf("repo %q found in high scores. Removing...", repo)
heap.Remove(scores, i)
found = true
break
}
}
if !found {
log.Printf("repo %q not found in high scores. Exiting...", repo)
return nil
}
scoreBytes, err := json.Marshal(&scores)
if err != nil {
return err
}
return mb.Put([]byte("scores"), scoreBytes)
})
}
func listDuplicates() error {
db, err := bolt.Open("goreportcard.db", 0755, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return fmt.Errorf("could not open bolt db: %v", err)
}
defer db.Close()
return db.View(func(tx *bolt.Tx) error {
repos := map[string][]string{}
rb := tx.Bucket([]byte("repos"))
if rb == nil {
return fmt.Errorf("repos bucket not found")
}
err = rb.ForEach(func(k, v []byte) error {
lower := strings.ToLower(string(k))
if _, ok := repos[lower]; ok {
repos[lower] = append(repos[lower], string(k))
return nil
}
repos[lower] = []string{string(k)}
return nil
})
if err != nil {
return err
}
for _, v := range repos {
if len(v) > 1 {
for _, repo := range v {
fmt.Println(repo)
}
}
}
return nil
})
}
func main() {
flag.Parse()
if *repo == "" && !*listDupes {
log.Println("Usage: manage_db.go [-list-duplicates] [-remove repo]")
return
}
var err error
if *repo != "" {
err = deleteRepo(*repo)
}
if *listDupes {
err = listDuplicates()
}
if err != nil {
log.Fatal(err)
}
}

View File

@@ -1,110 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"log"
"regexp"
"time"
"github.com/boltdb/bolt"
"github.com/gojp/goreportcard/handlers"
)
const (
repoBucket string = "repos"
metaBucket string = "meta"
)
func main() {
oldFormat := regexp.MustCompile(`^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)$`)
db, err := bolt.Open(handlers.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
rb := tx.Bucket([]byte(repoBucket))
if rb == nil {
return fmt.Errorf("repo bucket not found")
}
toDelete := []string{}
rb.ForEach(func(k, v []byte) error {
sk := string(k)
m := oldFormat.FindStringSubmatch(sk)
if m != nil {
err = rb.Put([]byte("github.com/"+sk), v)
if err != nil {
return err
}
toDelete = append(toDelete, string(v))
}
return nil
})
for i := range toDelete {
err = rb.Delete([]byte(toDelete[i]))
if err != nil {
return err
}
}
// finally update the high scores
mb := tx.Bucket([]byte(metaBucket))
if mb == nil {
return fmt.Errorf("meta bucket not found")
}
scoreBytes := mb.Get([]byte("scores"))
if scoreBytes == nil {
scoreBytes, _ = json.Marshal([]scoreHeap{})
}
scores := &scoreHeap{}
json.Unmarshal(scoreBytes, scores)
for i := range *scores {
m := oldFormat.FindStringSubmatch((*scores)[i].Repo)
if m != nil {
(*scores)[i] = scoreItem{
Repo: "github.com/" + (*scores)[i].Repo,
Score: (*scores)[i].Score,
Files: (*scores)[i].Files,
}
}
}
scoreBytes, err = json.Marshal(scores)
if err != nil {
return err
}
return mb.Put([]byte("scores"), scoreBytes)
})
if err != nil {
log.Fatal(err)
}
}
type scoreItem struct {
Repo string `json:"repo"`
Score float64 `json:"score"`
Files int `json:"files"`
}
// An scoreHeap is a min-heap of ints.
type scoreHeap []scoreItem
func (h scoreHeap) Len() int { return len(h) }
func (h scoreHeap) Less(i, j int) bool { return h[i].Score < h[j].Score }
func (h scoreHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *scoreHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(scoreItem))
}
func (h *scoreHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}

View File

@@ -1,46 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/boltdb/bolt"
"github.com/gojp/goreportcard/handlers"
)
const repoBucket = "repos"
type checksResp struct {
Repo string `json:"repo"`
Average float64 `json:"average"`
}
func main() {
db, err := bolt.Open(handlers.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
rb := tx.Bucket([]byte(repoBucket))
if rb == nil {
return fmt.Errorf("repo bucket not found")
}
rb.ForEach(func(k, v []byte) error {
resp := checksResp{}
err := json.Unmarshal(v, &resp)
if err != nil {
return err
}
log.Printf("%s - %.2f", resp.Repo, resp.Average*100)
return nil
})
return nil
})
if err != nil {
log.Fatal(err)
}
}