11#!/usr/bin/env node
22
33import { createClient } from "@libsql/client" ;
4- import ejs from ' ejs' ;
5- import fs from 'fs' ;
4+ import ejs from " ejs" ;
5+ import fs from "fs" ;
66
77const client = createClient ( {
88 url : process . env . TURSO_DB_URL ?? "file:./zig-milestone.db" ,
99 authToken : process . env . TURSO_TOKEN ,
1010} ) ;
1111const GITHUB_HEADERS = {
1212 Authorization : `Bearer ${ process . env . GITHUB_TOKEN } ` ,
13- Accept : ' application/json' ,
13+ Accept : " application/json" ,
1414} ;
1515
1616async function initDatabase ( ) {
17- let sqls = fs . readFileSync ( ' schema.sql' , ' utf8' ) . split ( ';' ) ;
17+ let sqls = fs . readFileSync ( " schema.sql" , " utf8" ) . split ( ";" ) ;
1818 sqls = sqls . filter ( ( sql ) => sql . trim ( ) . length > 0 ) ;
1919 console . log ( sqls , sqls . length ) ;
20- const ret = await client . batch ( sqls , ' write' ) ;
20+ const ret = await client . batch ( sqls , " write" ) ;
2121 console . log ( ret ) ;
2222}
2323
@@ -47,16 +47,16 @@ query {
4747 }
4848}
4949` ;
50- const r = await fetch ( ' https://api.github.com/graphql' , {
51- method : ' POST' ,
50+ const r = await fetch ( " https://api.github.com/graphql" , {
51+ method : " POST" ,
5252 headers : GITHUB_HEADERS ,
53- body : JSON . stringify ( { query : graphql } )
53+ body : JSON . stringify ( { query : graphql } ) ,
5454 } ) ;
55- if ( ! r . ok ) {
55+ if ( ! r . ok ) {
5656 throw new Error ( await r . text ( ) ) ;
5757 }
5858 const body = JSON . parse ( await r . text ( ) ) ;
59- const repoInfo = await body [ ' data' ] [ ' repository' ] ;
59+ const repoInfo = await body [ " data" ] [ " repository" ] ;
6060 const sqlRet = await client . execute ( {
6161 sql : `
6262INSERT INTO repo_histories (
@@ -74,30 +74,33 @@ INSERT INTO repo_histories (
7474` ,
7575 args : [
7676 Date . now ( ) ,
77- repoInfo [ ' forkCount' ] ,
78- repoInfo [ ' stargazerCount' ] ,
79- repoInfo [ ' watchers' ] [ ' totalCount' ] ,
80- repoInfo [ ' openPulls' ] [ ' totalCount' ] ,
81- repoInfo [ ' closedPulls' ] [ ' totalCount' ] ,
82- repoInfo [ ' mergedPulls' ] [ ' totalCount' ] ,
83- repoInfo [ ' openIssues' ] [ ' totalCount' ] ,
84- repoInfo [ ' closedIssues' ] [ ' totalCount' ] ,
77+ repoInfo [ " forkCount" ] ,
78+ repoInfo [ " stargazerCount" ] ,
79+ repoInfo [ " watchers" ] [ " totalCount" ] ,
80+ repoInfo [ " openPulls" ] [ " totalCount" ] ,
81+ repoInfo [ " closedPulls" ] [ " totalCount" ] ,
82+ repoInfo [ " mergedPulls" ] [ " totalCount" ] ,
83+ repoInfo [ " openIssues" ] [ " totalCount" ] ,
84+ repoInfo [ " closedIssues" ] [ " totalCount" ] ,
8585 ] ,
8686 } ) ;
8787 console . log ( `insert repo history result` , sqlRet ) ;
8888}
8989
9090async function fetchMilestoneHistories ( ) {
9191 const result = await client . execute (
92- "select id from milestones where state = 'open'"
92+ "select id from milestones where state = 'open'" ,
9393 ) ;
9494 const now = Date . now ( ) ;
9595 let firstErr = null ;
9696 for ( const row of result . rows ) {
97- const mid = row [ 'id' ] ;
98- const resp = await fetch ( `https://api.github.com/repos/ziglang/zig/milestones/${ mid } ` , {
99- headers : GITHUB_HEADERS ,
100- } ) ;
97+ const mid = row [ "id" ] ;
98+ const resp = await fetch (
99+ `https://api.github.com/repos/ziglang/zig/milestones/${ mid } ` ,
100+ {
101+ headers : GITHUB_HEADERS ,
102+ } ,
103+ ) ;
101104 const milestone = await resp . json ( ) ;
102105 try {
103106 const r = await client . execute ( {
@@ -108,8 +111,8 @@ INSERT INTO milestone_histories (created_at, mid, open_issues, closed_issues)
108111 args : {
109112 created_at : now ,
110113 mid : mid ,
111- open_issues : milestone [ ' open_issues' ] ,
112- closed_issues : milestone [ ' closed_issues' ] ,
114+ open_issues : milestone [ " open_issues" ] ,
115+ closed_issues : milestone [ " closed_issues" ] ,
113116 } ,
114117 } ) ;
115118 console . log ( `insert milestone history` , r ) ;
@@ -128,15 +131,16 @@ INSERT INTO milestone_histories (created_at, mid, open_issues, closed_issues)
128131
129132async function fetchMilestones ( ) {
130133 // https://docs.github.com/en/rest/issues/milestones?apiVersion=2022-11-28
131- const url = 'https://api.github.com/repos/ziglang/zig/milestones?state=all&per_page=100' ;
134+ const url =
135+ "https://api.github.com/repos/ziglang/zig/milestones?state=all&per_page=100" ;
132136 const resp = await fetch ( url , {
133137 headers : GITHUB_HEADERS ,
134138 } ) ;
135139 if ( ! resp . ok ) {
136140 throw new Error ( await resp . text ( ) ) ;
137141 }
138142 const milestones = await resp . json ( ) ;
139- for ( const m of milestones ) {
143+ for ( const m of milestones ) {
140144 const r = await client . execute ( {
141145 sql : `
142146INSERT INTO milestones (id, created_at, updated_at, state, title, description)
@@ -146,26 +150,20 @@ ON CONFLICT (id)
146150 updated_at = excluded.updated_at, state = excluded.state, title = excluded.title, description = excluded.description
147151` ,
148152 args : {
149- id : m [ ' number' ] ,
150- created_at : m [ ' created_at' ] ,
151- updated_at : m [ ' updated_at' ] ,
152- state : m [ ' state' ] ,
153- title : m [ ' title' ] ,
154- description : m [ ' description' ] ,
153+ id : m [ " number" ] ,
154+ created_at : m [ " created_at" ] ,
155+ updated_at : m [ " updated_at" ] ,
156+ state : m [ " state" ] ,
157+ title : m [ " title" ] ,
158+ description : m [ " description" ] ,
155159 } ,
156160 } ) ;
157161 console . log ( `insert milestone result` , r ) ;
158162 }
159163}
160164
161165async function GenerateHtml ( ) {
162- const fileOpts = { 'encoding' : 'utf8' , 'flags' : 'w' } ;
163- const idToTitle = {
164- '19' : '0.15.0' ,
165- '20' : '0.14.0' ,
166- '23' : '0.12.0' ,
167- '25' : '0.13.0' ,
168- } ;
166+ const fileOpts = { encoding : "utf8" , flags : "w" } ;
169167 let repoHistories = [ ] ;
170168 {
171169 const rs = await client . execute (
@@ -185,78 +183,47 @@ FROM
185183ORDER BY
186184 created_at desc
187185limit 1000
188- `
186+ ` ,
189187 ) ;
190188 for ( const row of rs . rows ) {
191189 repoHistories . push ( [
192- row [ ' created_at' ] ,
193- row [ ' forks' ] ,
194- row [ ' stars' ] ,
195- row [ ' watchers' ] ,
196- row [ ' open_pulls' ] ,
197- row [ ' closed_pulls' ] ,
198- row [ ' merged_pulls' ] ,
199- row [ ' open_issues' ] ,
200- row [ ' closed_issues' ] ,
190+ row [ " created_at" ] ,
191+ row [ " forks" ] ,
192+ row [ " stars" ] ,
193+ row [ " watchers" ] ,
194+ row [ " open_pulls" ] ,
195+ row [ " closed_pulls" ] ,
196+ row [ " merged_pulls" ] ,
197+ row [ " open_issues" ] ,
198+ row [ " closed_issues" ] ,
201199 ] ) ;
202200 }
203201 }
204202
205- const idsToShow = [
206- // '25', // 0.13.0
207- // '23', // 0.12.0
208- // '20', // '0.14.0',
209- '19' , // '0.15.0',
210- ] ;
211-
212- const sqls = idsToShow . map ( ( id ) => {
213- return { sql : `
214- SELECT
215- created_at,
216- open_issues,
217- closed_issues
218- FROM
219- milestone_histories
220- where mid = ?
221- ORDER BY
222- created_at desc
223- limit 1000
224- ` , args : [ id ] } ;
225- } ) ;
226- const histories = await client . batch ( sqls , 'read' ) ;
227- let historiesById = { } ; // id -> [[timestamp, open, closed], ...]
228- idsToShow . forEach ( ( id , idx ) => {
229- historiesById [ id ] = histories [ idx ] . rows . map ( ( row ) =>
230- [ row [ 'created_at' ] , row [ 'open_issues' ] , row [ 'closed_issues' ] ] ) ;
231- } ) ;
232203 let tmpl = fs . readFileSync ( `template.ejs` , fileOpts ) ;
233204 let body = ejs . render ( tmpl , {
234- now : new Date ( ) . toLocaleString ( 'en-GB' ) ,
235- historiesById : historiesById ,
236- historiesByIdStr : JSON . stringify ( historiesById ) ,
205+ now : new Date ( ) . toLocaleString ( "en-GB" ) ,
237206 repoHistoriesStr : JSON . stringify ( repoHistories ) ,
238- idToTitle : idToTitle ,
239- idsToShow : idsToShow ,
240207 } ) ;
241- fs . writeFileSync ( ' web/raw.html' , body , fileOpts ) ;
208+ fs . writeFileSync ( " web/raw.html" , body , fileOpts ) ;
242209}
243210
244211const args = process . argv . slice ( 2 ) ;
245212const cmd = args [ 0 ] ;
246- switch ( cmd ) {
247- case ' fetch-history' :
248- await fetchRepoHistories ( ) ;
249- await fetchMilestoneHistories ( ) ;
250- break ;
251- case ' fetch-milestone' :
252- await fetchMilestones ( ) ;
253- break ;
254- case ' gen-html' :
255- await GenerateHtml ( ) ;
256- break ;
257- case ' init-db' :
258- await initDatabase ( ) ;
259- break ;
260- default :
261- console . error ( "unknown cmd" , cmd ) ;
213+ switch ( cmd ) {
214+ case " fetch-history" :
215+ await fetchRepoHistories ( ) ;
216+ // await fetchMilestoneHistories();
217+ break ;
218+ case " fetch-milestone" :
219+ await fetchMilestones ( ) ;
220+ break ;
221+ case " gen-html" :
222+ await GenerateHtml ( ) ;
223+ break ;
224+ case " init-db" :
225+ await initDatabase ( ) ;
226+ break ;
227+ default :
228+ console . error ( "unknown cmd" , cmd ) ;
262229}
0 commit comments