Skip to content

Commit

Permalink
feat(deploy-log): extract link to logs and display them
Browse files Browse the repository at this point in the history
  • Loading branch information
dr460nf1r3 committed Jun 26, 2024
1 parent ac0a619 commit 53ac294
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 87 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/publish-dockerfile.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Create and publish a Docker image for the backend
on:
push:
branches: [ "main" ]
branches: ["main"]

env:
REGISTRY: ghcr.io
Expand All @@ -16,29 +16,29 @@ jobs:
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
2 changes: 1 addition & 1 deletion backend/src/telegram/telegram.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class TelegramController {
return this.telegramService.getNews()
}

@Get("deployments/:amount")
@Get("deployments/all/:amount")
getTelegram(@Param() params: any): any {
return this.telegramService.getDeployments(params.amount)
}
Expand Down
42 changes: 32 additions & 10 deletions backend/src/telegram/telegram.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CACHE_TTL, CAUR_DEPLOY_LOG_ID, CAUR_NEWS_ID, TgMessage, TgMessageList } from "@./shared-lib"
import { Cache, CACHE_MANAGER } from "@nestjs/cache-manager"
import { CACHE_MANAGER, Cache } from "@nestjs/cache-manager"
import { Inject, Injectable, Logger } from "@nestjs/common"
import { getTdjson } from "prebuilt-tdlib"
import * as tdl from "tdl"
Expand All @@ -21,7 +21,7 @@ export class TelegramService {
apiHash: TELEGRAM_API_HASH,
databaseEncryptionKey: TELEGRAM_DB_ENCRYPTION_KEY,
databaseDirectory: "./tdlib/db",
filesDirectory: "./tdlib/files"
filesDirectory: "./tdlib/files",
})
Logger.log("Telegram client started!", "TelegramService")
} else {
Expand Down Expand Up @@ -121,7 +121,7 @@ export class TelegramService {
id: string,
desiredCount: number,
// eslint-disable-next-line @typescript-eslint/ban-types
process?: Function
process?: Function,
): Promise<TgMessage[]> {
await this.getAllChats()
let extractedMessages: TgMessage[] = []
Expand All @@ -138,15 +138,15 @@ export class TelegramService {
author: lastMessage.author_signature,
view_count: lastMessage.interaction_info.view_count,
link: await this.getMessageLink(lastMessage.chat_id, lastMessage.id),
id: lastMessage.id
id: lastMessage.id,
})
let from = lastMessage.id

while (extractedMessages.length < desiredCount) {
const newMessages = await this.getChatHistory({ chat: id, from: from })
for (const message of newMessages.messages) {
if (
// Some messages seemingly don't have content, lets filter those out
// Some messages seemingly don't have content, let's filter those out
Object.hasOwn(message, "content") &&
Object.hasOwn(message.content, "text")
) {
Expand All @@ -156,7 +156,8 @@ export class TelegramService {
author: message.author_signature,
view_count: message.interaction_info.view_count,
link: await this.getMessageLink(message.chat_id, message.id),
id: message.id
id: message.id,
log: this.getLogLink(message.content.text),
})
}
}
Expand All @@ -182,7 +183,7 @@ export class TelegramService {

// Ensure we don't return more messages than desired, which can happen if
// we receive messages up to the default limit of 50 (tdlib dynamically
// seems to optimize the actually returned amount of messages)
// seems to optimize the actually returned number of messages)
return extractedMessages.slice(0, desiredCount)
}

Expand All @@ -194,7 +195,7 @@ export class TelegramService {
private async getAllChats(): Promise<any> {
return await this.tgClient.invoke({
_: "getChats",
limit: 50
limit: 50,
})
}

Expand All @@ -209,11 +210,32 @@ export class TelegramService {
const linkObject = await this.tgClient.invoke({
_: "getMessageLink",
chat_id: chat,
message_id: message
message_id: message,
})
return linkObject.link
}

/**
* Extract links to logfiles from a specific chat
* @param messageText The Telegram message object to parse
* @returns The log link as string or undefined
* @private
*/
private getLogLink(messageText: any): string | undefined {
// The first one is usually bold, the second one is the link we need
// Likely very infancy code used to prevent an undefined crash.
if (
messageText.entities[1] !== undefined &&
messageText.entities[1].type !== undefined &&
messageText.entities[1].type.url !== undefined &&
messageText.entities[1].type.url.includes("https")
) {
return messageText.entities[1].type.url
} else {
return undefined
}
}

/**
* Get the chat history of a specific chat
* @param params The chat ID, with from message ID and offset as optional parameters
Expand All @@ -226,7 +248,7 @@ export class TelegramService {
chat_id: params.chat,
from_message_id: params.from ? params.from : undefined,
limit: params.limit ? params.limit : 50,
offset: params.offset ? params.offset : undefined
offset: params.offset ? params.offset : undefined,
})
}

Expand Down
79 changes: 52 additions & 27 deletions frontend/src/app/deploy-log-full/deploy-log-full.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,77 @@ <h1 class="text-center text-4xl font-extrabold text-maroon">Deployment log</h1>
class="text-input mx-auto block w-full rounded-lg border border-red bg-surface0 p-4 ps-10 text-sm text-maroon focus:border-red focus:bg-surface1 max-w-md"
id="input-log-amount"
name="logamount"
placeholder="Amount of logs to show..."
placeholder="Amount of logs to show... (default 50)"
required
type="search"
/>

<div class=" py-2.5 mt-2.5">
<ul class="items-center mx-auto max-w-2xl text-sm font-medium text-text border border-mauve bg-surface0 rounded-lg sm:flex">
<div class="py-2.5 mt-2.5">
<ul
class="items-center mx-auto max-w-2xl text-sm font-medium text-text border border-mauve bg-surface0 rounded-lg sm:flex"
>
<li class="w-full border-b border-mauve sm:border-b-0 sm:border-r">
<div class="flex items-center ps-3">
<input
(change)="changeDeploymentType($event)"
class="w-4 h-4 border-mauve focus:ring-mauve text-text" id="horizontal-list-radio-all"
name="list-radio" type="radio"
value="0">
<label class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-all">All 🌍</label>
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-all"
name="list-radio"
type="radio"
value="0"
/>
<label class="w-full py-3 ms-2 text-sm font-medium text-text" for="horizontal-list-radio-all"
>All 🌍</label
>
</div>
</li>
<li class="w-full border-b border-mauve sm:border-b-0 sm:border-r">
<div class="flex items-center ps-3">
<input
(change)="changeDeploymentType($event)"
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-success" name="list-radio" type="radio"
value="1">
<label class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-success">Success 📣</label>
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-success"
name="list-radio"
type="radio"
value="1"
/>
<label
class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-success"
>Success 📣</label
>
</div>
</li>
<li class="w-full border-b border-mauve sm:border-b-0 sm:border-r">
<div class="flex items-center ps-3">
<input
(change)="changeDeploymentType($event)"
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-failed" name="list-radio" type="radio"
value="2">
<label class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-failed">Failed 🚫</label>
id="horizontal-list-radio-failed"
name="list-radio"
type="radio"
value="2"
/>
<label class="w-full py-3 ms-2 text-sm font-medium text-text" for="horizontal-list-radio-failed"
>Failed 🚫</label
>
</div>
</li>
<li class="w-full">
<div class="flex items-center ps-3">
<input
(change)="changeDeploymentType($event)"
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-timeout" name="list-radio" type="radio"
value="3">
<label class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-timeout">Timed out ⏳</label>
class="w-4 h-4 border-mauve focus:ring-mauve text-text"
id="horizontal-list-radio-timeout"
name="list-radio"
type="radio"
value="3"
/>
<label
class="w-full py-3 ms-2 text-sm font-medium text-text"
for="horizontal-list-radio-timeout"
>Timed out ⏳</label
>
</div>
</li>
</ul>
Expand All @@ -71,7 +93,7 @@ <h1 class="text-center text-4xl font-extrabold text-maroon">Deployment log</h1>
</button>
@if (requestedTooMany) {
<p class="pt-5 text-center text-red"
>You requested more logs than we currently have. Showing the max amount of
>You requested more logs than we currently have. Showing the max amount of
{{ latestDeployments.length }}.</p
>
}
Expand All @@ -84,12 +106,15 @@ <h1 class="text-center text-4xl font-extrabold text-maroon">Deployment log</h1>
class="items-center justify-between rounded-lg border border-mauve bg-surface0 p-4 shadow-sm sm:flex"
>
<time class="mb-1 text-xs font-normal text-text sm:order-last sm:mb-0"
>{{ deployment.date }}
>{{ deployment.date }}
</time>
<div class="lex text-sm font-normal text-text"
>{{ deployment.string }} <a class="text-maroon">{{ deployment.name }}</a> to
<span class="font-semibold text-red">{{ deployment.repo }}</span></div
>
>{{ deployment.string }} <a class="text-maroon">{{ deployment.name }}</a> to
<span class="font-semibold text-red">{{ deployment.repo }}</span>
@if (deployment.log) {
- <a class="text-yellow" href="{{ deployment.log }}">view log</a>
}
</div>
</div>
</li>
}
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/app/deploy-log-full/deploy-log-full.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FormsModule } from "@angular/forms"
standalone: true,
imports: [FormsModule],
templateUrl: "./deploy-log-full.component.html",
styleUrl: "./deploy-log-full.component.css"
styleUrl: "./deploy-log-full.component.css",
})
export class DeployLogFullComponent implements AfterViewInit {
latestDeployments: DeploymentList = []
Expand All @@ -20,10 +20,7 @@ export class DeployLogFullComponent implements AfterViewInit {
}

async updateLogAmount(amount: number) {
this.latestDeployments = parseDeployments(
await getDeployments(amount, this.currentType),
this.currentType
)
this.latestDeployments = parseDeployments(await getDeployments(amount, this.currentType), this.currentType)

// Show if we requested too many deployments
this.requestedTooMany = this.latestDeployments.length < amount
Expand All @@ -42,7 +39,7 @@ export class DeployLogFullComponent implements AfterViewInit {
alert("Please enter a valid number")
}
} else {
alert("Please enter a number")
await this.updateLogAmount(50)
}
}

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/app/deploy-log/deploy-log.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export class DeployLogComponent implements AfterViewInit {
* Check for new deployments and update the list.
*/
async checkNewDeployments(): Promise<void> {
const newList = parseDeployments(await getDeployments(30, DeploymentType.SUCCESS), DeploymentType.SUCCESS)
const newList: DeploymentList = parseDeployments(
await getDeployments(30, DeploymentType.SUCCESS),
DeploymentType.SUCCESS,
)
if (newList !== this.latestDeployments) {
this.latestDeployments = newList
}
Expand Down
8 changes: 2 additions & 6 deletions shared-lib/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/shared-lib",
"main": "shared-lib/src/index.ts",
"tsConfig": "shared-lib/tsconfig.lib.json",
"assets": [
"shared-lib/*.md"
],
"assets": ["shared-lib/*.md"],
"generatePackageJson": true
}
}
Expand Down
Loading

0 comments on commit 53ac294

Please sign in to comment.