-
Notifications
You must be signed in to change notification settings - Fork 6
/
pip_cache.ado
429 lines (339 loc) · 11.1 KB
/
pip_cache.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*==================================================
project: Cache previous results of pip command
Author: R.Andres Castaneda
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 3 May 2023 - 11:06:35
Modification Date:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_cache, rclass
syntax [anything(name=subcmd)], ///
[ ///
query(string) ///
PREfix(string) ///
cachedir(string) ///
piphash(string) ///
clear ///
cacheforce ///
* ///
]
version 16.1
/*++++++++++++++++++++++++++++++++++++++++++++++++++
SET UP
++++++++++++++++++++++++++++++++++++++++++++++++++*/
if ("`subcmd'" == "info") {
noi pip_cache_info, `options'
return add
exit
}
if ("${pip_cachedir}" == "0") {
return local piphash = "0"
return local pc_exists = 0
exit
}
if ("`subcmd'" == "gethash") {
pip_cache_gethash, query(`"`query'"')
return add
exit
}
if ("`subcmd'" == "inventory") {
pip_cache_inventory
noi disp "{res:Cache inventory has been loaded}"
exit
}
if (ustrregexm("`subcmd'","^iscache")) {
noi pip_cache_iscache
return add
exit
}
/*==================================================
1:load
==================================================*/
if ("`subcmd'" == "load") {
pip_cache gethash, query(`query')
local piphash = "`r(piphash)'"
return local piphash = "`piphash'"
if ("`cacheforce'" != "") {
return local pc_exists = 0
exit
}
// use default of dir selected by user
if (`"`cachedir'"' == "") local cachedir `"${pip_cachedir}"'
if (`"`cachedir'"' == "0") {
return local pc_exists = 0
exit
}
// check if cache already pc_exists
mata: st_local("pc_file", pathjoin("`cachedir'", "`piphash'.dta"))
cap confirm file "`pc_file'"
if _rc {
return local pc_exists = 0
exit
}
else {
return local pc_exists = 1
use "`pc_file'", `clear'
char _dta[piphash] `piphash'
char _dta[pipquery] `query'
exit
}
}
/*==================================================
3: save
==================================================*/
if ("`subcmd'" == "save") {
if (`"`cachedir'"' == "") local cachedir `"${pip_cachedir}"'
if (`"`cachedir'"' == "0") exit
mata: st_local("pc_file", pathjoin("`cachedir'", "`piphash'.dta"))
cap confirm new file "`pc_file'"
if (_rc == 0 | "`cacheforce'" != "") {
qui save "`pc_file'", replace
//------------ write cache infor file
mata: st_local("fuse", pathjoin("${pip_cachedir}", "pip_cache_info.txt"))
tempname hd
file open `hd' using "`fuse'", write append
file write `hd' "`piphash' , `query'" _n
file close `hd'
}
exit
}
//========================================================
// Delete cache
//========================================================
if ("`subcmd'" == "delete") {
noi pip_cache_delete, piphash(`piphash') cachedir(`cachedir')
}
end
//------------ Get Hash based on string
program define pip_cache_gethash, rclass
syntax [anything(name=subcmd)], [ ///
query(string) ///
PREfix(string) ///
]
version 16.1
qui {
if ("`prefix'" == "") local prefix = "pip"
tempname spiphash
mata: st_numscalar("`spiphash'", hash1(`"`prefix'`query'"', ., 2))
local piphash = "_pip" + strofreal(`spiphash', "%12.0g")
return local piphash = "`piphash'"
}
end
//------------ Check if it is cached
program define pip_cache_iscache, rclass
local iscache: char _dta[piphash]
if ("`iscache'" != "") {
disp "{res}Yes! {txt}this is cached data :)"
local query: char _dta[pipquery]
return local query = "`query'"
}
else {
disp "{err}No. {txt} this is not cached data :("
}
return local hash = "`iscache'"
end
//------------ Delete
program define pip_cache_delete, rclass
syntax [, piphash(string) cachedir(string)]
if ("`cachedir'" == "") local cachedir "${pip_cachedir}"
if ("`piphash'" == "") {
local pc_files: dir "`cachedir'" files "_pip*"
local nfiles: word count `pc_files'
noi disp "{err:Warning:} you will delete `nfiles' cache files." _n ///
"Do you want to continue? (Y/n)", _n(2) _request(_confirm)
if (lower("`confirm'") == "y") {
foreach f of local pc_files {
erase "`cachedir'/`f'"
}
erase "`cachedir'/pip_cache_info.txt"
}
}
else {
local piphash = ustrtrim("`piphash'")
local f2delete "`cachedir'/`piphash'.dta"
cap confirm file "`f2delete'"
if (_rc) {
noi disp "{err:Cache file }{cmd:`piphash'.dta }{err:not found}"
}
else {
erase "`f2delete'"
}
// locals tempf and origf are created in MATA routine
local cfile "`cachedir'/pip_cache_info.txt"
mata: pip_replace_in_pattern("`cfile'", `"`piphash'"', `""')
copy `tempf' "`origf'" , replace
noi disp "cache {res:`piphash'} deleted"
}
end
//========================================================
// Cache info
//========================================================
program define pip_cache_info, rclass
syntax [, condition(string) frame(string)]
local rname = floor(runiform(1e4, 99999))
// Make sure name is not repeated (very rare)
if ("`frame'" != "") {
if ustrregexm("`frame'", "(.*)([0-9]{5}$)") local fname = ustrregexs(2)
while ("`rname'" == "`fname'") {
local rname = floor(runiform(1e4, 99999))
}
}
if (`"`condition'"' == `""') {
mata: pip_drop_cache_info_frames()
local frame "cache_info_`rname'"
frame create `frame'
qui frame `frame': pip_cache_inventory
} // end of condition == ""
//========================================================
// Treat frames for new stage
//========================================================
local frame2 "cache_info_`rname'"
if ("`frame'" != "`frame2'") frame copy `frame' `frame2'
if (`"`condition'"' != `""') {
qui frame `frame2': keep if `condition'
}
//========================================================
// Display initial conditions
//========================================================
// sleep number 18007179503
frame `frame2' {
local nobs = _N
if (`nobs' > 1) {
noi disp "{break}{title:{res:Cache data available}} " /*
*/ "{txt:{it:(Filter your data by parameter)}}" _n
local novars "hash query n"
qui ds, has(type string)
local vars = "`r(varlist)'"
local vars: list vars - novars
foreach v of local vars {
//------------ Unique values
tempvar uniq
qui bysort `v': gen byte `uniq' = (_n==_N) if `v' != ""
qui summ `uniq', meanonly
if (r(sum) > 1) local filterable = "{txt:{it:(filterable)}}"
else local filterable = ""
//------------ build call of pip_utils click
local condition = `"condition(`"`v' == "obsi""')"'
noi pip_utils click, variable(`v') title("{title:`v'} `filterable'") /*
*/ statacode(`"pip_cache info, `condition' frame(`frame2')"')
} // end for parameters loop
disp _n
} // end if more than one data available
else {
*##s
local incsv = ustrtrim(query[1])
local injson: subinstr local incsv "&format=csv" ""
if ustrregexm("`incsv'","(.+)/(.+)\?(.+)") {
local host = ustrregexs(1)
local endpoint = ustrregexs(2)
local parameters = ustrregexs(3)
}
local hash = ustrtrim(hash[1])
noi disp _n "{title:Cache information for selected query}" _n
noi disp "{p2col 10 36 36 30:attribute}{dup 8: }value{p_end}" /*
*/ "{p2colset 10 30 32 30}" /*
*/ "{p2line}" /*
*/ "{p2col :{res:hash}}{txt:`hash'}{p_end}" /*
*/ "{p2col :{res:host}} {txt:`host'}{p_end}" /*
*/ "{p2col :{res:endpoint}} {txt:`endpoint'}{p_end}"
tokenize "`parameters'", parse("&")
local i = 1
while ("`1'" != "") {
if ("`1'" == "&") {
macro shift
continue
}
if (`i' == 1) {
local aname "parameters"
local i = `i' + 1
}
else local aname "."
noi disp "{p2col :{res:`aname'}} `1'{p_end}"
macro shift
}
noi disp "{p2line}"
//------------ Build Stata calls
mata: st_local("fuse", pathjoin("${pip_cachedir}", "`hash'.dta"))
local duse `"use "`fuse'", clear "'
local ddelete `"pip_cache delete, piphash(`hash')"'
noi disp "{break}{pstd}{ul:{res:ACTION}}{p_end}" _n /*
*/ `"{pmore}{stata `"`duse'"':use}{p_end}"' _n /*
*/ `"{pmore}{browse "`injson'":see in browser}{p_end}"' _n /*
*/ `"{pmore}{browse "`incsv'":download .csv}{p_end}"' _n /*
*/ `"{pmore}{stata `"`ddelete'"':delete}{err: {it: (use with caution)}}{p_end}"'
*##e
} // end of else
} // end of frame2
return local cache_file = "`fuse'"
return local piphash = "`hash'"
return local f_json = "`injson'"
return local f_csv = "`incsv'"
return local host = "`host'"
return local endpoint = "`endpoint'"
end
program define pip_cache_inventory, rclass
tempname cache_txt
frame create `cache_txt'
//========================================================
// Call original Date
//========================================================
qui frame `cache_txt' {
import delimited hash query using "${pip_cachedir}/pip_cache_info.txt", /*
*/ clear delimiters(",")
* pause on
* pause cache_txt
split query, gen(ep) parse(?)
gen endpoint = ustrregexs(2) if ustrregexm(ep1, "(.*)/([^/]+)$")
split ep2, gen(par) parse(&)
qui ds par*
local level1 = "`r(varlist)'"
qui foreach v of local level1 {
split `v', gen(`v'_) parse(=)
}
//------------Key for linking frames
gen n = _n
local cN = _N
}
//========================================================
// Organize data into readable format
//========================================================
// Create a frame whose vars are each available parameter
// associated with a particular hash.
qui {
drop _all
set obs `cN'
gen n = _n
frlink 1:1 n, frame(`cache_txt')
forvalues i = 1/`cN' { // for each observation
foreach v of loca level1 { // forech parameter
local vname = _frval(`cache_txt', `v'_1, `i')
local vvalue = _frval(`cache_txt', `v'_2, `i')
// if par does not exists or is format
if inlist("`vname'", "", "format") continue
cap confirm var `vname'
if (_rc) gen `vname' = "`vvalue'" in `i'
else replace `vname' = "`vvalue'" in `i'
}
* replace hash = _frval(`cache_txt', hash, `i') in `i'
* replace query = _frval(`cache_txt', query, `i') in `i'
}
frget hash query endpoint, from(`cache_txt')
gen server = ""
replace server = "qa" if regexm(query,"${pip_svr_qa}")
replace server = "dev" if regexm(query,"${pip_svr_dev}")
replace server = "prod" if regexm(query,"https://api.worldbank.org/pip")
} // end of qui
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control: