Commit 7bd24f9c authored by Knut Behrends's avatar Knut Behrends
Browse files

added PUT request to update state of WR core

parent dfd29996
......@@ -12,17 +12,29 @@ async function upload(ax, url, post_data, i) {
}
}
async function update_item(ax, url, put_data, i) {
let response = {}
try {
response = await ax.put(url, put_data)
} catch (error) {
// console.error(error.config.data)
console.error(error.response.data.name)
console.error(error.response.data.message)
console.error(error.response.statusText)
}
return new Promise((resolve, reject) => {
resolve(response)
reject(response)
})
}
async function get_columns(ax, model_name) {
const base_url = "/cg/api/summary"
let model = {}
try {
const response = await ax.get(base_url)
if (
typeof response.data !== undefined &&
Array.isArray(response.data.models) &&
response.data.models.length > 0
) {
if (response?.data?.models?.length > 0) {
response.data.models
.find((m) => m.table.includes(model_name))
.columns.forEach((_) => (model[_] = undefined))
......@@ -163,4 +175,11 @@ async function post_splitform(
})
}
module.exports = { upload, get_columns, inspect_section, get_item, get_items }
module.exports = {
upload,
get_columns,
inspect_section,
get_item,
get_items,
update_item,
}
......@@ -116,7 +116,7 @@ module.exports = class CoreSectionSplit {
.slice(-2)
.join(", ")
return faker.fake(
`{{company.catchPhraseAdjective}}, ${cond} ({{name.firstName}} {{name.lastName}})`
`core ${this.core_id}: {{company.catchPhraseAdjective}}, ${cond} ({{name.firstName}} {{name.lastName}})`
)
}
find_curated_length() {
......
......@@ -43,7 +43,7 @@ const options = getopts(process.argv.slice(2), {
//let core_url = "/api/v1/form?name=core&per-page=1&sort=-id"
//const core_info = options.infile ? JSON.parse(options.infile) : {}
const ax = axios.create(config.endpoint)
const api_url_frag = `/api/v1/form?`
const api_url_frag = `/api/v1/form`
// al sections of
const qs_section_split = {
name: "split",
......@@ -55,7 +55,7 @@ const qs_section_split = {
let split_sections_by_core = {}
const split_section_get_url =
api_url_frag + querystring.stringify(qs_section_split)
api_url_frag + "?" + querystring.stringify(qs_section_split)
let split_sections_by_core_map = new Map()
console.log(querystring.unescape(split_section_get_url))
......@@ -108,12 +108,17 @@ axutil
splittable_sections_ids.push(k)
}
})
split_sections_by_core_map = new Map(
[...split_sections_by_core_map.entries()].sort(sort_by_combined_id_core)
)
if (!splittable_sections_ids.length) {
console.log(
`Stop early. Core ${split_sections_by_core[0].combined_id_core} (id ${options.core_id}) and its ${split_sections_by_core_map.size} sections
seems to be split already, at least partially.
Maybe create the missing split sections manually.`
)
// console.log(split_sections_by_core_map)
console.log(split_sections_by_core_map)
process.exit(0)
} else {
console.log(
......@@ -172,12 +177,46 @@ axutil
})
.then((fake_section_data) => {
return fake_section_data.flat().map((fake_split, i) => {
axutil.upload(ax, `${api_url_frag}name=split`, fake_split, i)
axutil.upload(ax, `${api_url_frag}?name=split`, fake_split, i)
})
})
.then((fake_split_promises) =>
Promise.all(fake_split_promises).then((s) => console.log("Done"))
Promise.all(fake_split_promises).then((s) =>
console.log(`
#############################################################
Core ${options.core_id}: Created A- and W- splits for ${fake_split_promises.length} sections
############################################################# `)
)
)
// TO DO issue a put request that split no longer exists
.then((_) => {
// set still_exists attribute from 1 to 0
for (let s of splittable_sections) {
let url = `${api_url_frag}/${s.id}?name=split`
let put_data = {
exists: 0,
remarks: `core ${options.core_id}`,
}
axutil.update_item(ax, url, put_data, s.id).then((updated) => {
console.log(
{
"'still_exists' is now:": updated?.data?.exists,
},
{ [`id_${updated?.data?.combined_id}`]: updated?.data?.id }
)
})
}
})
})
.catch((error) => console.dir(error, { depth: 0 }))
function sort_by_combined_id_core(a, b) {
var A = a[0].toUpperCase() // ignore upper and lowercase
var B = b[0].toUpperCase() // ignore upper and lowercase
if (A < B) {
return -1
}
if (A > B) {
return 1
}
return 0
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment