Skip to content

Commit

Permalink
[migrate] replace old Chart Data API with Restored one
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Feb 26, 2024
1 parent 2cb1e5c commit a4356e9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 36 deletions.
30 changes: 20 additions & 10 deletions source/page/Map/adapter/isaaclin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import {
Series
} from './patientStatInterface';
import { long2short } from './long2short'; // some city names are NOT short names so we also convert them here
import { City, Province, StatisticData } from '../../../service/Epidemic';

const convertStat = (source: StatisticData): PatientStatData => ({
confirmed: source.confirmedCount,
import { Base, City, Province, StatisticData } from '../../../service/Epidemic';

export const convertStat = ({
id,
updateTime,
...source
}: Base & StatisticData): Base & PatientStatData => ({
id,
updateTime,
suspected: source.suspectedCount,
confirmed: source.confirmedCount,
serious: source.seriousCount,
cured: source.curedCount,
dead: source.deadCount
});
Expand All @@ -22,8 +29,9 @@ const convertStat = (source: StatisticData): PatientStatData => ({
*/
export const convertCountry = (source: Province[]): CountryData => ({
name: '中国',
confirmed: 0,
suspected: 0,
confirmed: 0,
serious: 0,
cured: 0,
dead: 0,
provinces: Object.fromEntries(
Expand All @@ -36,13 +44,13 @@ export function convertProvince(province: Province): ProvinceData {

return {
name,
timestamp: updateTime,
timestamp: +updateTime,
cities:
cities &&
Object.fromEntries(
cities.map(item => [
long2short(item.cityName),
convertCity(item, updateTime)
convertCity(item, +updateTime)
])
),
...convertStat(province)
Expand Down Expand Up @@ -89,10 +97,12 @@ export function convertProvincesSeries(
) {
const res: Series<ProvinceData> = {};

source.sort(item => item.updateTime);
source = [...source].sort(
({ updateTime: a }, { updateTime: b }) => +b - +a
);

for (const item of source) {
const t = roundTime(item.updateTime, resolution);
const t = roundTime(+item.updateTime, resolution);

if (res[t] === undefined) res[t] = {};

Expand Down Expand Up @@ -136,5 +146,5 @@ export const convertCountrySeries = (
resolution: number
): Series<CountryOverviewData> =>
Object.fromEntries(
source.map(item => [roundTime(item.updateTime, resolution), item])
source.map(item => [roundTime(+item.updateTime, resolution), item])
);
7 changes: 4 additions & 3 deletions source/page/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
CountryData,
convertCountry,
convertProvincesSeries,
convertCountrySeries
convertCountrySeries,
convertStat
} from './adapter';
import { getHistory, getCurrent, getOverall } from '../../service';
import * as style from './index.module.css';
Expand All @@ -35,7 +36,7 @@ export default class MapsPage extends HTMLElement implements CustomElement {
mountedCallback() {
this.classList.add(style.box);

// this.loadMapData();
this.loadMapData();
}

async loadMapData() {
Expand All @@ -47,7 +48,7 @@ export default class MapsPage extends HTMLElement implements CustomElement {

this.virusData = {
provincesSeries: convertProvincesSeries(rawData, resolution, true),
countrySeries: convertCountrySeries(overviewData, resolution),
countrySeries: convertCountrySeries(overviewData.map(convertStat), resolution),
countryData: convertCountry(rawCurrentData)
};
this.loading = false;
Expand Down
85 changes: 62 additions & 23 deletions source/service/Epidemic.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,93 @@
// 疫情数据调用封装
// NOTE: 访问接口地址并不是和系统对应的接口是同一服务
import { DataObject } from 'dom-renderer';
import { HTTPClient } from 'koajax';

// @credit: https://lab.ahusmart.com/nCoV/api/ 提供了丁香园的疫情数据
// @credit: https://lab.isaaclin.cn/nCoV/ 同样提供了丁香园的疫情数据
// @credit: https://github.com/BlankerL/DXY-COVID-19-Data 提供了丁香园的疫情数据

const apikey =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlkeWNwY3l4Z2pqcHV1dmV5aWVnIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDg4NDkzMDEsImV4cCI6MjAyNDQyNTMwMX0.EwwLK6PX2l5BgahNnPwzRINS-_ZI2AJCY2jR-SpzJdw';

export const epidemic = new HTTPClient({
baseURI: 'https://lab.ahusmart.com/nCoV/api/',
baseURI: 'https://idycpcyxgjjpuuveyieg.supabase.co/rest/v1/',
responseType: 'json'
}).use(({ request }, next) => {
request.headers = {
...request.headers,
apikey,
Authorization: `Bearer ${apikey}`
};
return next();
});

interface MapData<T = DataObject> {
results: T[];
export interface Base {
id: number;
updateTime: Date;
}

export type StatisticType = 'confirmed' | 'suspected' | 'cured' | 'dead';
export type StatisticType =
| 'suspected'
| 'confirmed'
| 'serious'
| 'cured'
| 'dead';
export type StatisticData = Record<
`${StatisticType | 'currentConfirmed'}Count`,
number
>;
export type IncreasingData = Record<
`${StatisticType | 'currentConfirmed' | `yesterday${'Confirmed' | 'Suspected'}Count`}Incr`,
number
>;

export type AreaType = 'hbFeiHb' | 'quanguo' | 'foreign';

export type StatisticData = Record<`${StatisticType}Count`, number>;
export interface Overall
extends Base,
StatisticData,
Partial<IncreasingData>,
Partial<Record<`${'mid' | 'high'}DangerCount`, number>>,
Partial<
Record<
| 'dailyPics'
| 'summary'
| `${'count' | 'general' | 'abroad'}Remark`
| `remark${1 | 2 | 3 | 4 | 5}`
| `note${1 | 2 | 3}`,
string
>
>,
Record<`${'foreign' | 'global'}Statistics`, null>,
Record<`${AreaType}TrendChart`, null>,
Record<`importantForeignTrendChart${'' | 'Global'}`, null> {
dailyPic: string;
marquee?: any[];
foreignTrendChartGlobal?: any;
globalOtherTrendChartData?: string;
}

export interface City extends StatisticData {
export interface City extends Base, StatisticData {
cityName: string;
}

export interface Province extends StatisticData {
export interface Province extends Base, StatisticData {
provinceShortName: string;
cities?: City[];
updateTime: number;
}

export async function getOverall() {
const {
body: { results }
} = await epidemic.get<MapData>('overall?latest=0');
const { body } = await epidemic.get<Overall[]>('Overall', { Range: '0-9' });

return results;
return body;
}

export async function getHistory() {
const {
body: { results }
} = await epidemic.get<MapData<Province>>('area?latest=0');
const { body } = await epidemic.get<Province[]>('Area', { Range: '0-9' });

return results;
return body;
}

export async function getCurrent() {
const {
body: { results }
} = await epidemic.get<MapData<Province>>('area');
const { body } = await epidemic.get<Province[]>('Area', { Range: '0-9' });

return results;
return body;
}

1 comment on commit a4356e9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for wuhan2020 ready!

✅ Preview
https://wuhan2020-4w5q42jqm-techquery.vercel.app

Built with commit a4356e9.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.