github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/big_query/get_data.go (about) 1 package big_query 2 3 // https://godoc.org/code.google.com/p/google-api-go-client/bigquery/v2 4 // https://developers.google.com/bigquery/bigquery-api-quickstart 5 import ( 6 "bytes" 7 "log" 8 "math/rand" 9 "time" 10 11 aelog "google.golang.org/appengine/log" 12 13 "fmt" 14 "net/http" 15 16 // bq "code.google.com/p/google-api-go-client/bigquery/v2" 17 bq "google.golang.org/api/bigquery/v2" 18 19 "google.golang.org/appengine" 20 21 "github.com/pbberlin/tools/appengine/util_appengine" 22 "github.com/pbberlin/tools/dsu" 23 "github.com/pbberlin/tools/net/http/loghttp" 24 "github.com/pbberlin/tools/sort/sortmap" 25 "github.com/pbberlin/tools/stringspb" 26 "github.com/pbberlin/tools/util" 27 "golang.org/x/net/context" 28 "golang.org/x/oauth2/google" 29 ) 30 31 // print it to http writer 32 func printPlaintextTable(w http.ResponseWriter, r *http.Request, vVDest [][]byte) { 33 34 //c := appengine.NewContext(r) 35 b1 := new(bytes.Buffer) 36 defer func() { 37 w.Header().Set("Content-Type", "text/plain") 38 w.Write(b1.Bytes()) 39 }() 40 41 for i0 := 0; i0 < len(vVDest); i0++ { 42 b1.WriteString("--") 43 b1.Write(vVDest[i0]) 44 b1.WriteString("--") 45 b1.WriteString("\n") 46 } 47 48 } 49 50 func queryIntoDatastore(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 51 52 limitUpper := util.MonthsBack(1) 53 limitLower := util.MonthsBack(25) 54 55 var q bq.QueryRequest = bq.QueryRequest{} 56 q.Query = ` 57 SELECT 58 repository_language 59 , LEFT(repository_pushed_at,7) monthx 60 , CEIL( count(*)/1000) Tausend 61 FROM githubarchive:github.timeline 62 where 1=1 63 AND LEFT(repository_pushed_at,7) >= '` + limitLower + `' 64 AND LEFT(repository_pushed_at,7) <= '` + limitUpper + `' 65 AND repository_language in ('Go','go','Golang','golang','C','Java','PHP','JavaScript','C++','Python','Ruby') 66 AND type="PushEvent" 67 group by monthx, repository_language 68 order by repository_language , monthx 69 ; 70 ` 71 72 c := appengine.NewContext(r) 73 74 // The following client will be authorized by the App Engine 75 // app's service account for the provided scopes. 76 // "https://www.googleapis.com/auth/bigquery" 77 // "https://www.googleapis.com/auth/devstorage.full_control" 78 79 // 2015-06: instead of oauth2.NoContext we get a new type of context 80 var ctx context.Context = appengine.NewContext(r) 81 oauthHttpClient, err := google.DefaultClient( 82 ctx, "https://www.googleapis.com/auth/bigquery") 83 84 if err != nil { 85 log.Fatal(err) 86 } 87 88 bigqueryService, err := bq.New(oauthHttpClient) 89 90 loghttp.E(w, r, err, false) 91 92 fmt.Fprint(w, "s1<br>\n") 93 94 // Create a query statement and query request object 95 // query_data = {'query':'SELECT TOP(title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;'} 96 // query_request = bigquery_service.jobs() 97 // Make a call to the BigQuery API 98 // query_response = query_request.query(projectId=PROJECT_NUMBER, body=query_data).execute() 99 100 js := bq.NewJobsService(bigqueryService) 101 jqc := js.Query("347979071940", &q) 102 103 fmt.Fprint(w, "s2 "+util.TimeMarker()+" <br>\n") 104 resp, err := jqc.Do() 105 loghttp.E(w, r, err, false) 106 107 rows := resp.Rows 108 var vVDest [][]byte = make([][]byte, len(rows)) 109 110 aelog.Errorf(c, "%#v", rows) 111 112 for i0, v0 := range rows { 113 114 cells := v0.F 115 116 b_row := new(bytes.Buffer) 117 b_row.WriteString(fmt.Sprintf("r%0.2d -- ", i0)) 118 for i1, v1 := range cells { 119 val1 := v1.V 120 b_row.WriteString(fmt.Sprintf("c%0.2d: %v ", i1, val1)) 121 } 122 vVDest[i0] = []byte(b_row.Bytes()) 123 } 124 125 key_combi, _ := dsu.BufPut(c, dsu.WrapBlob{Name: "bq_res1", VVByte: vVDest}, "bq_res1") 126 dsObj, _ := dsu.BufGet(c, key_combi) 127 128 printPlaintextTable(w, r, dsObj.VVByte) 129 130 fmt.Fprint(w, "s3 "+util.TimeMarker()+" <br>\n") 131 132 } 133 134 func mockDateIntoDatastore(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 135 136 c := appengine.NewContext(r) 137 138 rand.Seed(time.Now().UnixNano()) 139 140 row_max := 100 141 col_max := 3 142 143 var languages []string = []string{"C", "C++", "Rambucto"} 144 145 var vVDest [][]byte = make([][]byte, row_max) 146 for i0 := 0; i0 < row_max; i0++ { 147 148 vVDest[i0] = make([]byte, col_max) 149 150 b_row := new(bytes.Buffer) 151 b_row.WriteString(fmt.Sprintf("r%0.2d -- ", i0)) 152 153 for i1 := 0; i1 < col_max; i1++ { 154 if i1 == 0 { 155 val := languages[i0/10%3] 156 b_row.WriteString(fmt.Sprintf(" c%0.2d: %-10.8v ", i1, val)) 157 } else if i1 == 2 { 158 val := rand.Intn(300) 159 b_row.WriteString(fmt.Sprintf(" c%0.2d: %10v ", i1, val)) 160 } else { 161 162 f2 := "2006-01-02 15:04:05" 163 f2 = "2006-01" 164 tn := time.Now() 165 //tn = tn.Add( - time.Hour * 85 *24 ) 166 tn = tn.Add(-time.Hour * time.Duration(i0) * 24) 167 val := tn.Format(f2) 168 b_row.WriteString(fmt.Sprintf(" c%0.2d: %v ", i1, val)) 169 } 170 } 171 vVDest[i0] = []byte(b_row.Bytes()) 172 173 } 174 175 key_combi, _ := dsu.BufPut(c, dsu.WrapBlob{Name: "bq_res_test", VVByte: vVDest}, "bq_res_test") 176 dsObj, _ := dsu.BufGet(c, key_combi) 177 178 printPlaintextTable(w, r, dsObj.VVByte) 179 180 } 181 182 func regroupFromDatastore01(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 183 184 c := appengine.NewContext(r) 185 186 b1 := new(bytes.Buffer) 187 defer func() { 188 w.Header().Set("Content-Type", "text/html") 189 w.Write(b1.Bytes()) 190 }() 191 192 var vVSrc [][]byte 193 194 if util_appengine.IsLocalEnviron() { 195 vVSrc = bq_statified_res1 196 } else { 197 dsObj1, _ := dsu.BufGet(c, "dsu.WrapBlob__bq_res1") 198 vVSrc = dsObj1.VVByte 199 } 200 201 if r.FormValue("mock") == "1" { 202 dsObj1, _ := dsu.BufGet(c, "dsu.WrapBlob__bq_res_test") 203 vVSrc = dsObj1.VVByte 204 } 205 206 var vVDest [][]byte = make([][]byte, len(vVSrc)) 207 208 for i0 := 0; i0 < len(vVSrc); i0++ { 209 210 s_row := string(vVSrc[i0]) 211 v_row := stringspb.SplitByWhitespace(s_row) 212 b_row := new(bytes.Buffer) 213 214 b_row.WriteString(fmt.Sprintf("%16.12s ", v_row[3])) // leading spaces 215 b_row.WriteString(fmt.Sprintf("%16.12s ", v_row[5])) 216 b_row.WriteString(fmt.Sprintf("%16.8s", v_row[7])) 217 218 vVDest[i0] = []byte(b_row.Bytes()) 219 220 } 221 222 key_combi, _ := dsu.BufPut(c, dsu.WrapBlob{Name: "res_processed_01", S: "[][]byte", VVByte: vVDest}, "res_processed_01") 223 dsObj2, _ := dsu.BufGet(c, key_combi) 224 225 printPlaintextTable(w, r, dsObj2.VVByte) 226 227 } 228 229 func regroupFromDatastore02(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 230 231 c := appengine.NewContext(r) 232 233 b1 := new(bytes.Buffer) 234 defer func() { 235 w.Header().Set("Content-Type", "text/html") 236 w.Write(b1.Bytes()) 237 }() 238 239 var vVSrc [][]byte 240 dsObj1, err := dsu.BufGet(c, "dsu.WrapBlob__res_processed_01") 241 loghttp.E(w, r, err, false) 242 vVSrc = dsObj1.VVByte 243 244 d := make(map[string]map[string]float64) 245 246 distinctLangs := make(map[string]interface{}) 247 distinctPeriods := make(map[string]interface{}) 248 f_max := 0.0 249 for i0 := 0; i0 < len(vVSrc); i0++ { 250 //vVDest[i0] = []byte( b_row.Bytes() ) 251 s_row := string(vVSrc[i0]) 252 v_row := stringspb.SplitByWhitespace(s_row) 253 254 lang := v_row[0] 255 period := v_row[1] 256 count := v_row[2] 257 fCount := util.Stof(count) 258 if fCount > f_max { 259 f_max = fCount 260 } 261 262 distinctLangs[lang] = 1 263 distinctPeriods[period] = 1 264 265 if _, ok := d[period]; !ok { 266 d[period] = map[string]float64{} 267 } 268 d[period][lang] = fCount 269 270 } 271 //fmt.Fprintf(w,"%#v\n",d2) 272 //fmt.Fprintf(w,"%#v\n",f_max) 273 274 sortedPeriods := sortmap.StringKeysToSortedArray(distinctPeriods) 275 sortedLangs := sortmap.StringKeysToSortedArray(distinctLangs) 276 277 cd := CData{} 278 _ = cd 279 280 cd.M = d 281 cd.VPeriods = sortedPeriods 282 cd.VLangs = sortedLangs 283 cd.F_max = f_max 284 285 SaveChartDataToDatastore(w, r, cd, "chart_data_01") 286 287 /* 288 if r.FormValue("f") == "table" { 289 showAsTable(w,r,cd) 290 } else { 291 showAsChart(w,r,cd) 292 } 293 */ 294 295 } 296 297 func init() { 298 http.HandleFunc("/big-query/query-into-datastore", loghttp.Adapter(queryIntoDatastore)) 299 http.HandleFunc("/big-query/mock-data-into-datastore", loghttp.Adapter(mockDateIntoDatastore)) 300 http.HandleFunc("/big-query/regroup-data-01", loghttp.Adapter(regroupFromDatastore01)) 301 http.HandleFunc("/big-query/regroup-data-02", loghttp.Adapter(regroupFromDatastore02)) 302 }