diff --git a/cosmosdb/src/common.ts b/cosmosdb/src/common.ts index 8d0e01f..a31d51c 100644 --- a/cosmosdb/src/common.ts +++ b/cosmosdb/src/common.ts @@ -7,11 +7,11 @@ import { SqlQuerySpec, UpsertOperationInput, } from "@azure/cosmos"; +import { Dirent, existsSync, readFileSync, readdirSync } from "fs"; +import { join } from "path"; import { v4 as uuidv4 } from "uuid"; import { Question, Test } from "../../types/cosmosDB"; import { ImportData, ImportDatabaseData, ImportItem } from "../../types/import"; -import { Dirent, existsSync, readFileSync, readdirSync } from "fs"; -import { join } from "path"; /** * コマンドライン引数でコース名/テスト名指定した場合は、インポートデータから @@ -69,17 +69,6 @@ export const createDatabasesAndContainers = async ( ): Promise => { let databaseRes: DatabaseResponse; - // Systemsデータベース - databaseRes = await cosmosClient.databases.createIfNotExists({ - id: "Systems", - }); - - // SystemsデータベースのFlagコンテナー - await databaseRes.database.containers.createIfNotExists({ - id: "Flag", - partitionKey: "/id", - }); - // Usersデータベース databaseRes = await cosmosClient.databases.createIfNotExists({ id: "Users", diff --git a/functions/src/en2ja.ts b/functions/src/en2ja.ts index 524caca..56e0d30 100644 --- a/functions/src/en2ja.ts +++ b/functions/src/en2ja.ts @@ -1,17 +1,10 @@ -import { FeedResponse, ItemResponse, SqlQuerySpec } from "@azure/cosmos"; import { HttpRequest, HttpResponseInit, InvocationContext, } from "@azure/functions"; -import { getReadWriteContainer } from "./cosmosDBWrapper"; -import { Flag } from "../cosmosDB"; -import { translateByAzureTranslator, translateByDeepL } from "./axiosWrapper"; import { PutEn2JaReq, PutEn2JaRes } from "../functions"; - -const COSMOS_DB_DATABASE_NAME = "Systems"; -const COSMOS_DB_CONTAINER_NAME = "Flag"; -const COSMOS_DB_ITEMS_ID = "isUsedUpTransLator"; +import { translateByAzureTranslator, translateByDeepL } from "./axiosWrapper"; export default async function ( request: HttpRequest, @@ -22,61 +15,17 @@ export default async function ( const texts: PutEn2JaReq = testsStr !== "" && JSON.parse(testsStr); context.info({ texts }); - // Cosmos DBのSystemsデータベースのFlagsコンテナーから、 - // Azure Translatorの無料枠を使い切ったかのフラグを取得 - let isUsedUpTransLator: boolean = false; - const query: SqlQuerySpec = { - query: "SELECT * FROM c WHERE c.id = @id", - parameters: [{ name: "@id", value: COSMOS_DB_ITEMS_ID }], - }; - const container = getReadWriteContainer( - COSMOS_DB_DATABASE_NAME, - COSMOS_DB_CONTAINER_NAME + // Azure Translatorで翻訳 + // Azure Translatorの無料枠を使い切った場合はWarningログを出力し、代わりにDeepLで翻訳 + let jsonBody: PutEn2JaRes | undefined = await translateByAzureTranslator( + texts ); - const fetchAllResponse: FeedResponse = await container.items - .query(query) - .fetchAll(); - context.info({ fetchAllResponse }); - - // 当月中にAzure Translatorの無料枠を使い切ったことがある場合、Azure Translatorにリクエストする必要がないため - // 最初からDeepLにリクエストを送るように制御する - if (fetchAllResponse.resources.length > 0) { - const now: Date = new Date(); - if ( - fetchAllResponse.resources[0].year === now.getFullYear() && - fetchAllResponse.resources[0].month === now.getMonth() + 1 - ) { - isUsedUpTransLator = true; - } else { - const deleteResponse: ItemResponse = await container - .item(COSMOS_DB_ITEMS_ID) - .delete(); - context.info({ deleteResponse }); - } - } - - let jsonBody: PutEn2JaRes | undefined = undefined; - if (isUsedUpTransLator) { + if (!jsonBody) { + context.warn("Azure Translator Free Tier is used up."); jsonBody = await translateByDeepL(texts); - } else { - jsonBody = await translateByAzureTranslator(texts); - if (!jsonBody) { - // Azure Translatorの無料枠を使い切ったため、Warningログを出力してフラグを設定 - context.warn("Azure Translator Free Tier is used up."); - const now: Date = new Date(); - const upsertResponse: ItemResponse = - await container.items.upsert({ - id: COSMOS_DB_ITEMS_ID, - year: now.getFullYear(), - month: now.getMonth() + 1, - }); - context.info({ upsertResponse }); - - jsonBody = await translateByDeepL(texts); - } } context.info({ body: jsonBody }); - if (!jsonBody) throw new Error("Translated texts are empty."); + if (!jsonBody) throw new Error("Cannot translate texts."); return { status: 200, jsonBody }; } catch (e) { diff --git a/types/cosmosDB.ts b/types/cosmosDB.ts index e64f596..3ff09d3 100644 --- a/types/cosmosDB.ts +++ b/types/cosmosDB.ts @@ -28,9 +28,3 @@ export type Test = { testName: string; length: number; }; - -export type Flag = { - id: string; - year: number; - month: number; -};