Skip to content

Commit

Permalink
feat: issue #1063 (statistics);
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed Mar 5, 2023
1 parent 065ef28 commit d8504cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 306 deletions.
124 changes: 0 additions & 124 deletions src/lib/graphql/fixtures/statistics/query.ts

This file was deleted.

82 changes: 0 additions & 82 deletions src/lib/models/fixtures/statistics/types.ts

This file was deleted.

138 changes: 38 additions & 100 deletions src/routes/api/hasura/fixtures/statistics/+server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
//#region ➤ Package Imports

import { json } from '@sveltejs/kit';
import { performance } from 'perf_hooks';

import { REDIS_CACHE_FIXTURE_STATISTICS_DATA_4 } from '$lib/graphql/fixtures/statistics/query';
import { initGrapQLClient } from '$lib/graphql/init_graphQL';
import { ST_F_data_main, ST_F_generate_historic_fixtures_map, ST_F_get_target_fixture } from '@betarena/scores-lib/dist/functions/func.statistics.js';

//#endregion ➤ Package Imports

import type {
BETARENA_HASURA_statistics_query,
BETARENA_HASURA_SURGICAL_JSONB_historic_fixtures,
Fixture_Statistics,
Incident_Team,
REDIS_CACHE_SINGLE_statistics_data
} from '$lib/models/fixtures/statistics/types';
//#region ➤ [VARIABLES] Imports

const graphQlInstance = initGrapQLClient()
// [ℹ] debug info
const logs = [];

//#endregion ➤ [VARIABLES] Imports

//#region ➤ [METHODS]

// ~~~~~~~~~~~~~~~~~~~~~~~~
// [MAIN] ENDPOINT METHOD
// ~~~~~~~~~~~~~~~~~~~~~~~~

export async function GET(req): Promise<unknown> {
const fixture_id: string =
req.url['searchParams'].get('fixture_id');
export async function GET(
req
): Promise<unknown> {
const fixture_id: string = req.url['searchParams'].get('fixture_id');
const target_season_fixtures = await main(
fixture_id
);
Expand All @@ -32,107 +35,42 @@ export async function GET(req): Promise<unknown> {
// [MAIN] METHOD
// ~~~~~~~~~~~~~~~~~~~~~~~~

/**
* @description [MAIN] method - obtains target
* data on a specific fixture-id of its statistics
* information;
* @version 1.0
* @param {string} _fixture_id
* @returns Promise < unknown | null >
*/
async function main(
_fixture_id: string
): Promise<REDIS_CACHE_SINGLE_statistics_data | null> {
// [ℹ] relying on Fixture Id
// [ℹ] to get Target Fixture
// [ℹ] and return
): Promise< unknown | null > {

const FIXTURE_ID = parseInt(_fixture_id);

/**
* [ℹ] obtain target historic_fixtures [fixture_id]
*/

const fixture = await get_target_fixture(
const h_fixtures_arr = await ST_F_get_target_fixture(
graphQlInstance,
FIXTURE_ID
);
// [ℹ] exit
if (fixture == undefined) {
// [ℹ] (validation) exit;
if (h_fixtures_arr == undefined) {
return null;
}

const fixture_data = fixture[0];

/**
* [ℹ] generate FIXTURE data
*/

const fixture_id = fixture_data?.id;
const status = fixture_data?.status_j;
const stats =
fixture_data?.stats_j == undefined
? []
: fixture_data?.stats_j;
// [ℹ] home-team
const home_team_id =
fixture_data?.localteam_id_j;
const home_team_name =
fixture_data?.home_team_name || null;
const home_team_logo =
fixture_data?.home_team_logo || null;

// [ℹ] away-team
const away_team_id =
fixture_data?.visitorteam_id_j;
const away_team_name =
fixture_data?.away_team_name;
const away_team_logo =
fixture_data?.away_team_logo;

const home_team_obj: Incident_Team = {
team_name: home_team_name || null,
team_logo: home_team_logo || null,
team_id: home_team_id
};

const away_team_obj: Incident_Team = {
team_name: away_team_name || null,
team_logo: away_team_logo || null,
team_id: away_team_id
};

// [ℹ] generate [final] fixture object
const fixture_object: Fixture_Statistics = {
id: fixture_id || null,
status,
home: home_team_obj,
away: away_team_obj,
stats
};

// [ℹ] return fixture
return fixture_object;
const historic_fixtures_map = await ST_F_generate_historic_fixtures_map(
h_fixtures_arr
)

const cache_data_arr = await ST_F_data_main(
historic_fixtures_map
)

return cache_data_arr[0];
}

// ~~~~~~~~~~~~~~~~~~~~~~~~
// [HELPER] OTHER METHODS
// ~~~~~~~~~~~~~~~~~~~~~~~~

async function get_target_fixture(
fixture_id: number
): Promise<
BETARENA_HASURA_SURGICAL_JSONB_historic_fixtures[]
> {
// [ℹ] obtain target historic_fixtures [fixture_id]
const queryName =
'REDIS_CACHE_FIXTURE_STATISTICS_DATA_4';
const t0 = performance.now();
const VARIABLES = {
fixture_id
};
const response: BETARENA_HASURA_statistics_query =
await initGrapQLClient().request(
REDIS_CACHE_FIXTURE_STATISTICS_DATA_4,
VARIABLES
);
const t1 = performance.now();
logs.push(
`${queryName} completed in: ${
(t1 - t0) / 1000
} sec`
);

return response.historic_fixtures;
}
//#endregion ➤ [METHODS]

0 comments on commit d8504cf

Please sign in to comment.