Skip to content

Instantly share code, notes, and snippets.

@kdabir
Last active August 29, 2015 13:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdabir/9332408 to your computer and use it in GitHub Desktop.
Save kdabir/9332408 to your computer and use it in GitHub Desktop.
This script pulls your starred repos on github and shows some interesting stats at the end. It was created as an example demonstrating how Gstorm is useful in scripts.

This script is created as an example to show how easy it is to use gstorm to crunch and consume data from rest apis.

Usage

Either download the script and run:

groovy ghstarred.groovy <your_github_id>

Or hotload it:

groovy https://gist.githubusercontent.com/kdabir/9332408/raw/9211348681a3fa02b842b06306f360cd48e46561/ghstarred.groovy <your_github_id>

About

SQL make perfect sense in scripts that have to do some reporting or data crunching. In-memory hsqldb starts with no fuss without any setup required. Gstorm takes away the pain of setting up hsqldb, creating table and writing insert queries. All that is left for you to do is write queries and take the control of your data.

Contribute

I am totally bad at SQL and you could help me by writing more interesting queries on the data. Either fork the gist and let me know or add comment. Contributions welcome on Gstorm as well in form of pull requests, issues, documentation and sharing a word :)

@GrabResolver(name='gstorm', root='http://dl.bintray.com/kdabir/maven')
@GrabConfig(systemClassLoader = true) @Grab('gstorm:gstorm:0.6')
import gstorm.*
import groovy.json.*
def g = new Gstorm() // could give a file path here if we want the storage to be persistent
// define what you want to collect / store
class Repo {
String name, full_name, html_url, language, default_branch
int stargazers_count, watchers_count, forks_count, open_issues_count
}
g.stormify(Repo) // stormifying creates the table and adds crud methods to class
// get data using github api
def user = ((args.length)? args.first() : "kdabir"), page = 1, json = []
def interesting_fields = ["name" , "full_name" , "html_url" , "language" , "stargazers_count" , "watchers_count" , "forks_count" , "open_issues_count" , "default_branch"]
println "fetching data from github account of ${user}"
while (json = new JsonSlurper().parse(new URL("https://api.github.com/users/${user}/starred?page=${page++}&per_page=80"))){
json.collect { print "."; new Repo(it.subMap(interesting_fields)).save() } // its just as easy to persist this in db
println ""
}
// and you are done, use sql queries to do fancy reporting
println "Your Starred Repo's Summary"
println "="*80
// some methods are available on the class
println "Total starred Repos: " +
Repo.count
println "Number of Repos with more than 100 stars:" +
Repo.count("stargazers_count > 100")
println "Number of Repos with more than 100 forks:" +
Repo.count("forks_count > 100")
// little more advanced stuff, still do'able with gstorm
println "5 top starred groovy repos :" +
Repo.where("language like 'Groovy' order by stargazers_count desc limit 5").collectEntries { [it.name, it.stargazers_count]}
println "5 top forked groovy repos : " +
Repo.where("language like 'Groovy' order by forks_count desc limit 5").collectEntries { [it.name, it.forks_count]}
// for some operations, you will have to resort to using sql, and gstorm doesn't come in your way
println "Your starred repos by Languages : " +
g.sql.rows("select language, count(language) as repos_count from repo group by language order by repos_count desc ").collectEntries { [it.language, it.repos_count]}
println "various default branch names that peopl use:" +
g.sql.rows("select distinct(default_branch) from repo").collect {it.default_branch}. join(", ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment