Skip to content

Commit

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

This file was deleted.

56 changes: 0 additions & 56 deletions src/lib/models/fixtures/probabilities/types.ts

This file was deleted.

192 changes: 60 additions & 132 deletions src/routes/api/hasura/fixtures/probabilities/+server.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
//#region ➤ Package Imports

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

import {
REDIS_CACHE_FIXTURE_PROBABILITIES_0,
REDIS_CACHE_FIXTURE_PROBABILITIES_1
} from '$lib/graphql/fixtures/probabilities/query';
import { initGrapQLClient } from '$lib/graphql/init_graphQL';

import type {
BETARENA_HASURA_probabilities_query,
BETARENA_HASURA_SURGICAL_JSONB_historic_fixtures,
Fixture_Probabilities,
REDIS_CACHE_SINGLE_probabilities_translation
} from '$lib/models/fixtures/probabilities/types';
import { PR_F_get_target_fixture, PR_F_get_widget_translations, PR_F_translations_main } from '@betarena/scores-lib/dist/functions/func.probabilities.js';
import type { B_PR_T, PR_Fixture } from '@betarena/scores-lib/types/probabilities';

//#endregion ➤ Package Imports

//#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 lang: string =
req.url['searchParams'].get('lang');
const fixture_id: string =
req.url['searchParams'].get('fixture_id');
export async function GET(
req
): Promise <unknown> {
const lang: string = req.url['searchParams'].get('lang');
const fixture_id: string = req.url['searchParams'].get('fixture_id');

// [ℹ] target widget [data]
if (fixture_id) {
const response_hasura = await main(
const response_hasura = await main_data(
fixture_id
);
if (response_hasura) {
Expand All @@ -46,149 +48,75 @@ export async function GET(req): Promise<unknown> {
}
}

// IMPORTANT - fallback to NULL
return json(null);
}

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

async function main(
/**
* @description [MAIN] method - obtains target
* data on a specific fixture-id of its probability
* information;
* @version 1.0
* @param {string} _fixture_id
* @returns Promise < unknown | null >
*/
async function main_data(
_fixture_id: string
): Promise<unknown | 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 fixture = await PR_F_get_target_fixture(
graphQlInstance,
FIXTURE_ID
);
// [ℹ] exit
)
// [ℹ] (validation) exit;
if (fixture == undefined) {
return null;
}

const fixture_data = fixture[0];

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

const fixture_id = fixture_data?.id;
const probabilites_data =
fixture_data?.probabilities;
const probabilites_data = fixture_data?.probabilities;

// [ℹ] generate [final] fixture object
const fixture_object: Fixture_Probabilities = {
const FINAL_OBJECT: PR_Fixture = {
id: fixture_id || null,
probabilites: probabilites_data || null
};

// [ℹ] return fixture
return fixture_object;
return FINAL_OBJECT;
}

async function main_trans_and_seo(LANG: string) {
const response = await get_widget_translations(
LANG
);

/**
* [ℹ] MAIN
*/

const object: REDIS_CACHE_SINGLE_probabilities_translation =
{};
object.lang = LANG;

const objectFixOdds =
response.scores_fixture_probabilities_translations.find(
({ lang }) => lang === LANG
);

const objectFixGeneralTranslation =
response.scores_general_translations.find(
({ lang }) => lang === LANG
);

const mergedObj = {
...object,
...objectFixOdds?.data,
...objectFixGeneralTranslation?.widgets_no_data_available
};

// [🐞]
/*
if (dev) {
const data = JSON.stringify(fix_odds_translation_map.values(), null, 4)
fs.writeFile('./datalog/main_trans_and_seo.json', data, err => {
if (err) {
console.error(err);
}
});
}
*/

return mergedObj;
/**
* @description [MAIN] method - obtains target
* data on a target langauge for Probabilities Fixture (widget);
* @version 1.0 - past versions: []
* @param {string} LANG
* @returns Promise < B_PR_T >
*/
async function main_trans_and_seo(
LANG: string
): Promise < B_PR_T > {

const res = await PR_F_get_widget_translations(
graphQlInstance,
[LANG]
)

const fix_odds_translation_map = await PR_F_translations_main(
res,
[LANG]
)

return fix_odds_translation_map.get(LANG);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~
// [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_PROBABILITIES_0';
const t0 = performance.now();
const VARIABLES = {
fixture_id
};
const response: BETARENA_HASURA_probabilities_query =
await initGrapQLClient().request(
REDIS_CACHE_FIXTURE_PROBABILITIES_0,
VARIABLES
);
const t1 = performance.now();
logs.push(
`${queryName} completed in: ${
(t1 - t0) / 1000
} sec`
);

return response.historic_fixtures;
}

async function get_widget_translations(
lang: string
): Promise<BETARENA_HASURA_probabilities_query> {
const queryName =
'REDIS_CACHE_FIXTURE_PROBABILITIES_1';
const t0 = performance.now();
const VARIABLES = {
lang
};
const response: BETARENA_HASURA_probabilities_query =
await initGrapQLClient().request(
REDIS_CACHE_FIXTURE_PROBABILITIES_1,
VARIABLES
);
const t1 = performance.now();
logs.push(
`${queryName} completed in: ${
(t1 - t0) / 1000
} sec`
);

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

0 comments on commit 065ef28

Please sign in to comment.