Skip to content

Commit

Permalink
Delete Systems cosmos db container
Browse files Browse the repository at this point in the history
  • Loading branch information
infhyroyage committed Aug 16, 2024
1 parent fa84c53 commit fc289a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 78 deletions.
15 changes: 2 additions & 13 deletions cosmosdb/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
* コマンドライン引数でコース名/テスト名指定した場合は、インポートデータから
Expand Down Expand Up @@ -69,17 +69,6 @@ export const createDatabasesAndContainers = async (
): Promise<void> => {
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",
Expand Down
67 changes: 8 additions & 59 deletions functions/src/en2ja.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Flag> = await container.items
.query<Flag>(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<Flag> = await container
.item(COSMOS_DB_ITEMS_ID)
.delete<Flag>();
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<Flag> =
await container.items.upsert<Flag>({
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) {
Expand Down
6 changes: 0 additions & 6 deletions types/cosmosDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,3 @@ export type Test = {
testName: string;
length: number;
};

export type Flag = {
id: string;
year: number;
month: number;
};

0 comments on commit fc289a5

Please sign in to comment.