Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Murm Services maintenance #55

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 38 additions & 52 deletions app/routes/index-explorer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { loadCountries } from '~/utils/countries'
import { timestampToDatetime } from '~/utils/datetime'
import HandleError from '~/components/HandleError'
import Pagination from '~/components/Pagination'
import { getNodes } from '~/utils/index-api'

function getSearchUrl(params, removePage) {
let searchParams = ''
Expand Down Expand Up @@ -86,68 +87,53 @@ export async function action({ request }) {
}

export async function loader({ request }) {
try {
const schemas = await loadSchema()
const countries = await loadCountries()
const schemas = await loadSchema()
const countries = await loadCountries()

const url = new URL(request.url)
let params = {}
for (let param of url.searchParams.entries()) {
params[param[0]] = param[1]
}

if (Object.keys(params).length === 0) {
return json({
schemas: schemas,
countries: countries
})
}

if (!params?.schema) {
return json({
schemas: schemas,
countries: countries,
message: 'The schema is required',
success: false
})
}

let searchParams = getSearchUrl(params, false)
if (params.schema === 'all') {
searchParams = searchParams.replace('schema=all', '')
}
let response = await fetchGet(
`${process.env.PUBLIC_INDEX_URL}/v2/nodes?${searchParams}`
)
const url = new URL(request.url)
let params = {}
for (let param of url.searchParams.entries()) {
params[param[0]] = param[1]
}

const nodes = await response.json()
if (Object.keys(params).length === 0) {
return json({
schemas: schemas,
countries: countries
})
}

if (!response.ok) {
if (response.status === 400) {
return json({
schemas: schemas,
countries: countries,
params: params,
message: nodes.errors?.[0].detail,
success: false
})
}
if (!params?.schema) {
return json({
schemas: schemas,
countries: countries,
message: 'The schema is required',
success: false
})
}

return new Response('Schema list loading error', {
status: response.status
})
}
let searchParams = getSearchUrl(params, false)
if (params.schema === 'all') {
searchParams = searchParams.replace('schema=all', '')
}
const nodes = await getNodes(searchParams)

if (nodes.status === 400) {
return json({
schemas: schemas,
countries: countries,
nodes: nodes,
params: params
params: params,
message: nodes?.data?.errors?.[0].detail,
success: false
})
} catch (error) {
console.error(error)
return null
}

return json({
schemas: schemas,
countries: countries,
nodes: nodes?.data,
params: params
})
}

export default function GetNodes() {
Expand Down
30 changes: 21 additions & 9 deletions app/utils/countries.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { fetchGet } from '~/utils/fetcher'
import { handleNotOK } from '~/utils/handleResponse'

export async function loadCountries() {
let response = await fetchGet(
`${process.env.PUBLIC_LIBRARY_URL}/v2/countries`
)
if (!response.ok) {
throw new Response('Country list loading error', {
status: response.status
})
try {
let response = await fetchGet(
`${process.env.PUBLIC_LIBRARY_URL}/v2/countries`
)
if (!response.ok) {
const data = await handleNotOK(response)
throw new Response(JSON.stringify(data), {
status: response.status
})
}
let countries = await response.json()
return Object.keys(countries).map(country => ({ name: country }))
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`loadSchemas error: ${e.message || e}`, {
status: 500
})
}
}
let countries = await response.json()
return Object.keys(countries).map(country => ({ name: country }))
}
10 changes: 10 additions & 0 deletions app/utils/handleResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export async function handleNotOK(res) {
let data
const contentType = res.headers.get('content-type')
if (contentType && contentType.includes('application/json')) {
data = await res.json()
} else {
data = await res.text()
}
return data
}
116 changes: 99 additions & 17 deletions app/utils/index-api.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,107 @@
import { fetchDelete, fetchGet, fetchJsonPost } from '~/utils/fetcher'
import { handleNotOK } from '~/utils/handleResponse'

export async function getNodes(searchParams) {
try {
let response = await fetchGet(
`${process.env.PUBLIC_INDEX_URL}/v2/nodes?${searchParams}`
)

console.log(`${process.env.PUBLIC_INDEX_URL}/v2/nodes?${searchParams}`)

if (!response.ok && response.status !== 400) {
const data = await handleNotOK(response)
throw new Response(JSON.stringify(data), {
status: response.status
})
}

const nodes = await response.json()

return {
data: nodes,
status: response.status
}
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`Failed to load nodes: ${e.message || e}`, {
status: 500
})
}
}
}

export async function postNode(url) {
return fetch(process.env.PUBLIC_INDEX_URL + '/v2/nodes-sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: `{"profile_url": "${url}"}`
})
.then(data => data.json())
.catch(err => err)
try {
const response = await fetchJsonPost(
process.env.PUBLIC_INDEX_URL + '/v2/nodes-sync',
{
profile_url: url
}
)
if (!response.ok) {
const data = await handleNotOK(response)
throw new Response(JSON.stringify(data), {
status: response.status
})
}
return response.json()
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`Failed to post node: ${e.message || e}`, {
status: 500
})
}
}
}

export async function getNodeStatus(node_id) {
return fetch(`${process.env.PUBLIC_INDEX_URL}/v2/nodes/${node_id}`)
.then(res => res.json())
.then(body => body)
.catch(err => err)
try {
let response = await fetchGet(
`${process.env.PUBLIC_INDEX_URL}/v2/nodes/${node_id}`
)
if (!response.ok) {
const data = await handleNotOK(response)
throw new Response(JSON.stringify(data), {
status: response.status
})
}
return response
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`Failed to get node status: ${e.message || e}`, {
status: 500
})
}
}
}

export async function deleteNode(node_id) {
return fetch(`${process.env.PUBLIC_INDEX_URL}/v2/nodes/${node_id}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }
})
.then(data => data.json())
.catch(err => err)
try {
let response = await fetchDelete(
`${process.env.PUBLIC_INDEX_URL}/v2/nodes/${node_id}`
)
if (!response.ok) {
const data = await handleNotOK(response)
throw new Response(JSON.stringify(data), {
status: response.status
})
}

return response.json()
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`Failed to delete node: ${e.message || e}`, {
status: 500
})
}
}
}
38 changes: 25 additions & 13 deletions app/utils/schema.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { fetchGet } from '~/utils/fetcher'
import { handleNotOK } from '~/utils/handleResponse'

export async function loadSchema() {
let response = await fetchGet(process.env.PUBLIC_LIBRARY_URL + '/v2/schemas')
if (!response.ok) {
throw new Response('Schema list loading error', {
status: response.status
})
try {
let res = await fetchGet(process.env.PUBLIC_LIBRARY_URL + '/v2/schemas')
if (!res.ok) {
const data = await handleNotOK(res)
throw new Response(JSON.stringify(data), {
status: res.status
})
}
let schema = await res.json()
return schema.data
.filter(s => {
return !s.name.startsWith('default-v')
})
.filter(s => {
return !s.name.startsWith('test_schema-v')
})
} catch (e) {
if (e instanceof Response) {
throw e
} else {
throw new Response(`loadSchemas error: ${e.message || e}`, {
status: 500
})
}
}
let schema = await response.json()
return schema.data
.filter(s => {
return !s.name.startsWith('default-v')
})
.filter(s => {
return !s.name.startsWith('test_schema-v')
})
}
Loading