Skip to content

Commit

Permalink
kontroller mr-api topic consumer consumers fra maam
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrele committed Feb 8, 2024
1 parent a093fb8 commit 777032a
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 86 deletions.
17 changes: 11 additions & 6 deletions frontend/arena-adapter-manager/src/core/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export class ApiError extends Error {
}
}

export const getTopics = () =>
fetch("/mulighetsrommet-arena-adapter/topics", {
export enum ApiBase {
ARENA_ADAPTER = "/mulighetsrommet-arena-adapter",
MR_API = "/mulighetsrommet-api/api/internal/maam",
}

export const getTopics = (base: ApiBase) =>
fetch(`${base}/topics`, {
method: "GET",
headers: getDefaultHeaders(),
})
Expand All @@ -29,8 +34,8 @@ export const getArenaTables = () =>
.then(parseJson)
.catch((error) => toastError("Klarte ikke laste ArenaTables", error));

export const putTopicRunningState = (topics: Topic[]) =>
fetch("/mulighetsrommet-arena-adapter/topics", {
export const putTopicRunningState = (base: ApiBase, topics: Topic[]) =>
fetch(`${base}/topics`, {
method: "PUT",
headers: {
...getDefaultHeaders(),
Expand Down Expand Up @@ -107,8 +112,8 @@ export type MrApiTask =
| "initial-load-mulighetsrommet-tiltaksgjennomforinger"
| "sync-navansatte";

export const runTask = (task: MrApiTask) =>
fetch(`/mulighetsrommet-api/api/v1/internal/tasks/${task}`, {
export const runTask = (base: ApiBase, task: MrApiTask) =>
fetch(`${base}/tasks/${task}`, {
method: "POST",
headers: getDefaultHeaders(),
})
Expand Down
8 changes: 4 additions & 4 deletions frontend/arena-adapter-manager/src/core/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useEffect, useState } from "react";
import { Topic } from "../domain";
import { getArenaTables, getTopics } from "./api";
import { ApiBase, getArenaTables, getTopics } from "./api";

export function useTopics() {
export function useTopics(base: ApiBase) {
const [topics, setTopics] = useState<Topic[]>([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const fetchTopics = async () => {
const t = await getTopics();
const t = await getTopics(base);
setTopics(t ?? []);
setIsLoading(false);
};
fetchTopics();
}, []);
}, [base]);
return { topics, isTopicsLoading: isLoading, setTopics };
}

Expand Down
18 changes: 13 additions & 5 deletions frontend/arena-adapter-manager/src/pages/MrApiManagement.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Box, Heading, VStack, Link } from "@chakra-ui/react";
import { Box, Heading, Link, VStack } from "@chakra-ui/react";
import { UpdateVirksomhet } from "../sections/UpdateVirksomhet";
import { RunTask } from "../sections/RunTask";
import TopicOverview from "../sections/TopicOverview.tsx";
import { ApiBase } from "../core/api.tsx";

export function MrApiManagement() {
return (
<Box>
<Heading mb="10">mr-api</Heading>
<VStack spacing={8}>
<TopicOverview base={ApiBase.MR_API} />

<UpdateVirksomhet />
<RunTask task="generate-validation-report">

<RunTask base={ApiBase.MR_API} task="generate-validation-report">
<p>
Genererer en rapport med alle valideringsfeil på gjennomføringer og laster rapporten opp
til en
Expand All @@ -19,16 +24,19 @@ export function MrApiManagement() {
migrere.
</p>
</RunTask>
<RunTask task="initial-load-mulighetsrommet-tiltaksgjennomforinger">

<RunTask base={ApiBase.MR_API} task="initial-load-mulighetsrommet-tiltaksgjennomforinger">
Starter en initial load av alle gjennomføringer med opphav = MR_ADMIN_FLATE.
</RunTask>
<RunTask task="initial-load-tiltaksgjennomforinger">

<RunTask base={ApiBase.MR_API} task="initial-load-tiltaksgjennomforinger">
<p>Starter en initial load av alle gjennomføringer i API.</p>
<p>
Dette inkluderer både gjennomføringer med opphav = ARENA og opphav = MR_ADMIN_FLATE.
</p>
</RunTask>
<RunTask task={"sync-navansatte"}>

<RunTask base={ApiBase.MR_API} task={"sync-navansatte"}>
Synkoniserer NAV-ansatte fra relevante AD-grupper.
</RunTask>
</VStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import ReplayEvents from "../sections/ReplayEvents";
import TopicOverview from "../sections/TopicOverview";
import ReplayEvent from "../sections/ReplayEvent";
import DeleteEvents from "../sections/DeleteEvents";
import { ApiBase } from "../core/api.tsx";

export function MrArenaAdapterManagement() {
return (
<Box>
<Heading mb="10">mr-arena-adapter</Heading>
<VStack spacing={8}>
<TopicOverview />
<TopicOverview base={ApiBase.ARENA_ADAPTER} />
<ReplayEvents />
<ReplayEvent />
<DeleteEvents />
Expand Down
5 changes: 3 additions & 2 deletions frontend/arena-adapter-manager/src/sections/RunTask.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Button } from "@chakra-ui/react";
import { ReactNode, useState } from "react";
import { Section } from "../components/Section";
import { MrApiTask, runTask } from "../core/api";
import { ApiBase, MrApiTask, runTask } from "../core/api";

interface Props {
base: ApiBase;
task: MrApiTask;
children: ReactNode;
}
Expand All @@ -13,7 +14,7 @@ export function RunTask(props: Props) {

const executeTask = async () => {
setLoading(true);
await runTask(props.task);
await runTask(props.base, props.task);
setLoading(false);
};

Expand Down
23 changes: 11 additions & 12 deletions frontend/arena-adapter-manager/src/sections/TopicOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ import {
} from "@chakra-ui/react";
import { ChangeEvent, useState } from "react";
import { Section } from "../components/Section";
import { putTopicRunningState } from "../core/api";
import { ApiBase, putTopicRunningState } from "../core/api";
import { useTopics } from "../core/hooks";

function TopicOverview() {
const { topics, isTopicsLoading, setTopics } = useTopics();
interface Props {
base: ApiBase;
}

function TopicOverview({ base }: Props) {
const { topics, isTopicsLoading, setTopics } = useTopics(base);
const [isSaveLoading, setIsSaveLoading] = useState(false);

const setRunningState = (event: ChangeEvent<HTMLInputElement>) => {
const changedTopics = [...topics];
changedTopics[
changedTopics.map((t) => t.topic).indexOf(event.currentTarget.name)
].running = event.currentTarget.checked;
changedTopics[changedTopics.map((t) => t.topic).indexOf(event.currentTarget.name)].running =
event.currentTarget.checked;
setTopics(changedTopics);
};

const saveRunningState = async () => {
setIsSaveLoading(true);
await putTopicRunningState(topics);
await putTopicRunningState(base, topics);
setIsSaveLoading(false);
};

Expand Down Expand Up @@ -74,11 +77,7 @@ function TopicOverview() {
</TableContainer>
<Box mt="6">
<Flex justifyContent="end">
<Button
isLoading={isSaveLoading}
colorScheme="pink"
onClick={saveRunningState}
>
<Button isLoading={isSaveLoading} colorScheme="pink" onClick={saveRunningState}>
Save
</Button>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import no.nav.mulighetsrommet.api.plugins.*
import no.nav.mulighetsrommet.api.plugins.AuthProvider
import no.nav.mulighetsrommet.api.routes.featuretoggles.featureTogglesRoute
import no.nav.mulighetsrommet.api.routes.internal.frontendLoggerRoutes
import no.nav.mulighetsrommet.api.routes.internal.tasks
import no.nav.mulighetsrommet.api.routes.internal.maamRoutes
import no.nav.mulighetsrommet.api.routes.v1.*
import no.nav.mulighetsrommet.database.Database
import no.nav.mulighetsrommet.hoplite.loadConfiguration
Expand Down Expand Up @@ -42,7 +42,7 @@ fun Application.configure(config: AppConfig) {

routing {
authenticate(AuthProvider.AZURE_AD_TEAM_MULIGHETSROMMET.name) {
tasks()
maamRoutes()
}

authenticate(AuthProvider.AZURE_AD_NAV_IDENT.name, AuthProvider.AZURE_AD_TILTAKSADMINISTRASJON_GENERELL.name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package no.nav.mulighetsrommet.api.routes.internal

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import no.nav.mulighetsrommet.api.tasks.GenerateValidationReport
import no.nav.mulighetsrommet.api.tasks.InitialLoadTiltaksgjennomforinger
import no.nav.mulighetsrommet.api.tasks.InitialLoadTiltaksgjennomforingerInput
import no.nav.mulighetsrommet.api.tasks.SynchronizeNavAnsatte
import no.nav.mulighetsrommet.domain.constants.ArenaMigrering
import no.nav.mulighetsrommet.domain.serializers.UUIDSerializer
import no.nav.mulighetsrommet.kafka.KafkaConsumerOrchestrator
import no.nav.mulighetsrommet.kafka.Topic
import org.koin.ktor.ext.inject
import java.util.*

fun Route.maamRoutes() {
route("/api/internal/maam") {
route("/tasks") {
val generateValidationReport: GenerateValidationReport by inject()
val initialLoadTiltaksgjennomforinger: InitialLoadTiltaksgjennomforinger by inject()
val synchronizeNavAnsatte: SynchronizeNavAnsatte by inject()

post("generate-validation-report") {
val taskId = generateValidationReport.schedule()

call.respond(HttpStatusCode.Accepted, ScheduleTaskResponse(id = taskId))
}

post("initial-load-tiltaksgjennomforinger") {
val input = InitialLoadTiltaksgjennomforingerInput(opphav = null)
val taskId = initialLoadTiltaksgjennomforinger.schedule(input)

call.respond(HttpStatusCode.Accepted, ScheduleTaskResponse(id = taskId))
}

post("initial-load-mulighetsrommet-tiltaksgjennomforinger") {
val input = InitialLoadTiltaksgjennomforingerInput(opphav = ArenaMigrering.Opphav.MR_ADMIN_FLATE)
val taskId = initialLoadTiltaksgjennomforinger.schedule(input)

call.respond(HttpStatusCode.Accepted, ScheduleTaskResponse(id = taskId))
}

post("sync-navansatte") {
val taskId = synchronizeNavAnsatte.schedule()
call.respond(HttpStatusCode.Accepted, ScheduleTaskResponse(id = taskId))
}
}

route("/topics") {
val kafka: KafkaConsumerOrchestrator by inject()

get {
val topics = kafka.getTopics()
call.respond(topics)
}

put {
val topics = call.receive<List<Topic>>()
kafka.updateRunningTopics(topics)
call.respond(HttpStatusCode.OK)
}
}
}
}

@Serializable
data class ScheduleTaskResponse(
@Serializable(with = UUIDSerializer::class)
val id: UUID,
)

This file was deleted.

0 comments on commit 777032a

Please sign in to comment.