Spaces:
Sleeping
Sleeping
File size: 6,276 Bytes
951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 99c0467 951bd56 | 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 | import asyncio
from contextlib import asynccontextmanager
from pathlib import Path
from cashews import cache
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from api import (
author_countries,
get_openalex_countries,
get_ror_suggestions,
get_tops_table_country,
ojs_all_languages,
ojs_all_metadata,
ojs_all_topics,
ojs_article_for_institution,
ojs_articles_by_year,
ojs_articles_for_journal,
publication_per_discipline_country,
publication_to_journal_location_country,
publications_per_institutions_locations,
publications_per_year_country,
share_of_ojs_articles,
share_of_ojs_articles_country,
)
from data_processing import (
articles_to_author_countries,
articles_to_disciplines_count,
articles_to_institution_count,
articles_to_languages,
articles_to_ojs_locations,
articles_to_publication_year_stats,
get_journal_options,
get_tops_table,
journal_countries,
ojs_journals_stats,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
cache.setup('mem://')
yield
await cache.close()
app = FastAPI(
title='OJS Dashboard API',
description='JSON API for the OJS publication analytics dashboard.',
version='1.0.0',
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_methods=['GET'],
allow_headers=['*'],
)
@app.get('/journals', summary='List all available journals')
@cache(ttl='24h')
async def list_journals():
return get_journal_options()
@app.get('/index', summary='Get general OJS article information')
@cache(ttl='24h')
async def get_index():
yearly_articles, stats, topics, languages, countries = await asyncio.gather(
asyncio.to_thread(ojs_articles_by_year),
asyncio.to_thread(ojs_journals_stats),
asyncio.to_thread(ojs_all_topics),
asyncio.to_thread(ojs_all_languages),
asyncio.to_thread(journal_countries),
)
return {
'yearly_articles': yearly_articles.to_dicts(),
'stats': stats,
'topics': topics.to_dicts(),
'languages': languages.to_dicts(),
'countries': countries.to_dicts(),
}
@app.get('/metadata_quality', summary='Get metadata quality in OpenAlex on OJS')
@cache(ttl='24h')
async def get_metadata_quality():
return ojs_all_metadata()
@app.get('/list-countries', summary='List all countries in OpenAelx')
@cache(ttl='24h')
async def list_countries():
return get_openalex_countries()
@app.get(
'/country-analysis/{country_code}',
summary='Get full analysis results for one country',
)
@cache(ttl='24h')
async def country_analsis(country_code: str):
table_authors, table_institutions = get_tops_table_country(country_code)
return {
'ojs_share': share_of_ojs_articles_country(country_code).to_dicts(),
'yearly_articles': publications_per_year_country(country_code).to_dicts(),
'disciplines': publication_per_discipline_country(country_code).to_dicts(),
'institutions': publications_per_institutions_locations(
country_code
).to_dicts(),
'journal_locations': publication_to_journal_location_country(
country_code
).to_dicts(),
'top_authors': table_authors.to_dicts(),
'top_institutions': table_institutions.to_dicts(),
'countries': author_countries(country_code).to_dicts(),
}
@app.get('/institution-analysis/', summary='Get full analysis results for one country')
@cache(ttl='24h')
async def institution_analsis(
fractional: bool = Query(..., description='Whether fractional or full counting'),
institution: str = Query(..., description='Institution to analyse'),
):
articles = ojs_article_for_institution(institution)
ojs_share = None
if not fractional:
ojs_share = share_of_ojs_articles(institution).to_dicts()
return {
'yearly_stats': articles_to_publication_year_stats(
articles, fractional, institution
).to_dicts(),
'disciplines': articles_to_disciplines_count(
articles, institution, fractional
).to_dicts(),
'journal_locations': articles_to_ojs_locations(
articles, fractional, institution
).to_dicts(),
'countries': articles_to_author_countries(articles, False).to_dicts(),
'languages': articles_to_languages(articles).to_dicts(),
'institutions': articles_to_institution_count(articles, fractional=fractional)[
:1000
].to_dicts(),
'ojs_share': ojs_share,
}
@app.get('/institutions/suggest', summary='ROR auto-complete suggestions')
@cache(ttl='200h')
async def suggest_institutions(
q: str = Query(..., min_length=3, description='Partial institution name'),
):
"""Returns ROR suggestions for a partial institution name string."""
return get_ror_suggestions(q)
@app.get('/journal-analysis/', summary='Get full analysis results for one journal')
@cache(ttl='24h')
async def journal_analsis(
fractional: bool = Query(..., description='Whether fractional or full counting'),
journal: str = Query(..., description='ISSNs of journals to analyse'),
):
articles = ojs_articles_for_journal(journal.replace('\\n', '\n').split('\n'))
table_authors, table_institutions = get_tops_table(articles, fractional=fractional)
return {
'yearly_stats': articles_to_publication_year_stats(
articles, False, None
).to_dicts(),
'top_authors': table_authors.to_dicts(),
'top_institutions': table_institutions.to_dicts(),
'author_countries': articles_to_author_countries(
articles, fractional
).to_dicts(),
'institutions': articles_to_institution_count(articles, fractional).to_dicts(),
}
@app.get('/{full_path:path}', include_in_schema=False)
async def serve_frontend(full_path: str):
dist = Path(__file__).parent / 'frontend' / 'dist'
candidate = (dist / full_path).resolve()
if full_path and candidate.is_file() and candidate.is_relative_to(dist.resolve()):
return FileResponse(candidate)
return FileResponse(dist / 'index.html') # SPA fallback
|