Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Concordium/concordium-scan into lma…
Browse files Browse the repository at this point in the history
…/release/github_actions
  • Loading branch information
lassemand committed Nov 8, 2024
2 parents 9f910ff + 60fd084 commit 3cbda4e
Show file tree
Hide file tree
Showing 122 changed files with 7,457 additions and 6,929 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: Backend (Rust) Check formatting and build
name: Check backend (Rust)

on:
push:
branches: main
paths:
- backend-rust/**
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [ main ]
paths:
- backend-rust/**

env:
RUST_FMT: "nightly-2023-04-01"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
name: Build
name: Check backend (.NET)

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [ main ]
paths:
- backend/**

env:
RUST_VERSION: "1.65"
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/frontend-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Check frontend

on:
push:
branches: main
paths:
- frontend/**
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [ main ]
paths:
- frontend/**

env:
NODE_VERSION: "18.18.2"

jobs:
checks:
name: Formatting and types
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'yarn'
cache-dependency-path: frontend/yarn.lock
- name: Install deps
working-directory: frontend
run: yarn install --frozen-lockfile --immutable
- name: Check formatting
working-directory: frontend
run: yarn run formatcheck
- name: Check types
working-directory: frontend
run: yarn run typecheck
# - name: Linting
# working-directory: frontend
# run: yarn run lintcheck
48 changes: 48 additions & 0 deletions .github/workflows/frontend-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Publish frontend docker image on DockerHub

on:
push:
tags:
- frontend/*

jobs:
publish-docker-image:
runs-on: ubuntu-latest
environment: release
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
flavor: latest=false
images: concordium/ccdscan-frontend
tags: type=match,pattern=frontend/(.*),group=1
- name: Ensure tag matches version in package.json
run: |
EXPECTED=$(jq -r .version frontend/package.json)
EXTRACTED="${{ steps.meta.outputs.version }}"
if [ "$EXPECTED" = "$EXTRACTED" ]; then
printf "Extracted version matches the version in package.json ($EXTRACTED).\n"
exit 0
else
printf "ERROR: Extracted version does not match the version in package.json. \nExtracted: '$EXTRACTED'\nExpected: '$EXPECTED'\n"
exit 1
fi
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: concordium
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ./frontend
file: ./frontend/Dockerfile
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend-rust/migrations/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ CREATE TABLE accounts(
-- credential_registration_id
);

-- Important for performance when joining accounts with its associated creation block.
CREATE INDEX accounts_created_block_idx ON accounts (created_block);

-- Add foreign key constraint now that the account table is created.
ALTER TABLE transactions
ADD CONSTRAINT fk_transaction_sender
Expand Down
5 changes: 3 additions & 2 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#![allow(unused_variables)]

mod account_metrics;
mod transaction_metrics;

// TODO remove this macro, when done with first iteration
Expand All @@ -15,6 +16,7 @@ macro_rules! todo_api {
};
}

use account_metrics::AccountMetricsQuery;
use anyhow::Context as _;
use async_graphql::{
http::GraphiQLSource,
Expand Down Expand Up @@ -63,7 +65,7 @@ pub struct ApiServiceConfig {
}

#[derive(MergedObject, Default)]
pub struct Query(BaseQuery, TransactionMetricsQuery);
pub struct Query(BaseQuery, AccountMetricsQuery, TransactionMetricsQuery);

pub struct Service {
pub schema: Schema<Query, EmptyMutation, Subscription>,
Expand Down Expand Up @@ -754,7 +756,6 @@ LIMIT 30", // WHERE slot_time > (LOCALTIMESTAMP - $1::interval)
})
}

// accountsMetrics(period: MetricsPeriod!): AccountsMetrics
// bakerMetrics(period: MetricsPeriod!): BakerMetrics!
// rewardMetrics(period: MetricsPeriod!): RewardMetrics!
// rewardMetricsForAccount(accountId: ID! period: MetricsPeriod!):
Expand Down
113 changes: 113 additions & 0 deletions backend-rust/src/graphql_api/account_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::sync::Arc;

use async_graphql::{Context, Object, SimpleObject};
use sqlx::postgres::types::PgInterval;

use super::{get_pool, ApiError, ApiResult, DateTime, MetricsPeriod, TimeSpan};

#[derive(SimpleObject)]
struct AccountMetrics {
/// Total number of accounts created (all time).
last_cumulative_accounts_created: i64,

/// Total number of accounts created in requested period.
accounts_created: i64,

buckets: AccountMetricsBuckets,
}

#[derive(SimpleObject)]
struct AccountMetricsBuckets {
/// The width (time interval) of each bucket.
bucket_width: TimeSpan,

/// Start of the bucket time period. Intended x-axis value.
#[graphql(name = "x_Time")]
x_time: Vec<DateTime>,

/// Total number of accounts created (all time) at the end of the bucket
/// period. Intended y-axis value.
#[graphql(name = "y_LastCumulativeAccountsCreated")]
y_last_cumulative_accounts_created: Vec<i64>,

/// Number of accounts created within bucket time period. Intended y-axis
/// value.
#[graphql(name = "y_AccountsCreated")]
y_accounts_created: Vec<i64>,
}

#[derive(Default)]
pub(crate) struct AccountMetricsQuery;

#[Object]
impl AccountMetricsQuery {
async fn account_metrics(
&self,
ctx: &Context<'_>,
period: MetricsPeriod,
) -> ApiResult<AccountMetrics> {
let pool = get_pool(ctx)?;

let last_cumulative_accounts_created =
sqlx::query_scalar!("SELECT COALESCE(MAX(index), 0) FROM accounts")
.fetch_one(pool)
.await?
.expect("coalesced");

// The full period interval, e.g. 7 days.
let period_interval: PgInterval = period
.as_duration()
.try_into()
.map_err(|e| ApiError::DurationOutOfRange(Arc::new(e)))?;

let cumulative_accounts_created_before_period = sqlx::query_scalar!(
"SELECT COALESCE(MAX(index), 0)
FROM accounts
LEFT JOIN blocks ON created_block = height
WHERE slot_time < (now() - $1::interval)",
period_interval,
)
.fetch_one(pool)
.await?
.expect("coalesced");

let accounts_created =
last_cumulative_accounts_created - cumulative_accounts_created_before_period;

let bucket_width = period.bucket_width();

// The bucket interval, e.g. 6 hours.
let bucket_interval: PgInterval =
bucket_width.try_into().map_err(|err| ApiError::DurationOutOfRange(Arc::new(err)))?;

let rows = sqlx::query_file!(
"src/graphql_api/account_metrics.sql",
period_interval,
bucket_interval,
)
.fetch_all(pool)
.await?;

let x_time = rows
.iter()
.map(|r| r.bucket_time.expect("generated by generate_series so never null"))
.collect();
let y_last_cumulative_accounts_created =
rows.iter().map(|r| r.end_index.expect("coalesced")).collect();
let y_accounts_created = rows
.iter()
.map(|r| r.end_index.expect("coalesced") - r.start_index.expect("coalesced"))
.collect();

Ok(AccountMetrics {
last_cumulative_accounts_created,
accounts_created,
buckets: AccountMetricsBuckets {
bucket_width: TimeSpan(bucket_width),
x_time,
y_last_cumulative_accounts_created,
y_accounts_created,
},
})
}
}
Loading

0 comments on commit 3cbda4e

Please sign in to comment.