Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dashboard] Weekly/monthly funds trend #3080

Open
wants to merge 8 commits into
base: feat/enhanced-dashboard
Choose a base branch
from

Conversation

mmioana
Copy link
Contributor

@mmioana mmioana commented Sep 12, 2024

Feat: Add cron lambda to cache all needed previous domain balances

Description

  • This PR adds a cron lambda to cache all previous domain-level and colony-level balances for both the previous last 30 days and the previous total amount from the start of last week. Given the time at which the lambda is triggered, we have no idea what the selected currency is and we want to provide a uniform solution, the cached value will be in USDC

  • These values will be used for the increase/decrease trends in these cards, however this PR fetches the value only for the Total in and out card

Property 1=Completed
Streaming widget

Testing

TODO: Given you can't really test the cron lambda runs properly locally, as there is no event to trigger it, we need to make use of a manual query

  • Step 1. First things first, run your dev environment
  • Step 2. Run the create-data script
  • Step 3. For ease of testing (kind of) please save the following code in a new file (you can name it .diff)
diff --git a/amplify/backend/api/colonycdapp/schema/schema.graphql b/amplify/backend/api/colonycdapp/schema/schema.graphql
index f13bc5b98..16ffb08c4 100644
--- a/amplify/backend/api/colonycdapp/schema/schema.graphql
+++ b/amplify/backend/api/colonycdapp/schema/schema.graphql
@@ -3149,6 +3149,7 @@ type ColonyAction @model @searchable {
   Approved tokens impacted by the action (used for manage tokens)
   """
   approvedTokenChanges: ApprovedTokenChanges
+  updatedAt: AWSDateTime!
 }
 
 """
diff --git a/src/graphql/generated.ts b/src/graphql/generated.ts
index 797a88204..bfb2f4889 100644
--- a/src/graphql/generated.ts
+++ b/src/graphql/generated.ts
@@ -1338,6 +1338,7 @@ export type CreateColonyActionInput = {
   toPotId?: InputMaybe<Scalars['Int']>;
   tokenAddress?: InputMaybe<Scalars['ID']>;
   type: ColonyActionType;
+  updatedAt?: InputMaybe<Scalars['AWSDateTime']>;
 };
 
 export type CreateColonyActionMetadataInput = {
@@ -2685,6 +2686,7 @@ export type ModelColonyActionConditionInput = {
   toPotId?: InputMaybe<ModelIntInput>;
   tokenAddress?: InputMaybe<ModelIdInput>;
   type?: InputMaybe<ModelColonyActionTypeInput>;
+  updatedAt?: InputMaybe<ModelStringInput>;
 };
 
 export type ModelColonyActionConnection = {
@@ -2727,6 +2729,7 @@ export type ModelColonyActionFilterInput = {
   toPotId?: InputMaybe<ModelIntInput>;
   tokenAddress?: InputMaybe<ModelIdInput>;
   type?: InputMaybe<ModelColonyActionTypeInput>;
+  updatedAt?: InputMaybe<ModelStringInput>;
 };
 
 export type ModelColonyActionMetadataConditionInput = {
@@ -3898,6 +3901,7 @@ export type ModelSubscriptionColonyActionFilterInput = {
   toPotId?: InputMaybe<ModelSubscriptionIntInput>;
   tokenAddress?: InputMaybe<ModelSubscriptionIdInput>;
   type?: InputMaybe<ModelSubscriptionStringInput>;
+  updatedAt?: InputMaybe<ModelSubscriptionStringInput>;
 };
 
 export type ModelSubscriptionColonyActionMetadataFilterInput = {
@@ -8813,6 +8817,7 @@ export type UpdateColonyActionInput = {
   toPotId?: InputMaybe<Scalars['Int']>;
   tokenAddress?: InputMaybe<Scalars['ID']>;
   type?: InputMaybe<ColonyActionType>;
+  updatedAt?: InputMaybe<Scalars['AWSDateTime']>;
 };
 
 export type UpdateColonyActionMetadataInput = {

The reason for this patch is to be able to manipulate the updatedAt field for a colony action

  • Step 4. Run git apply .diff
  • Step 5. Now the interesting part - let's create some historical testing data 👨‍🦳
    Go to http://localhost:20002 and run the following mutation
mutation MyMutation {
  createColonyAction(
    input: {
      amount: "230000000000000000000" # any amount would be fine, though for this action to have a visible result use a value higher than 10^18
      showInActionsList: true
      initiatorAddress: "<LEELA_WALLET_ADDRESS_HERE>"
      blockNumber: 977
      colonyId: "<COLONY_ADDRESS_HERE>"
      fromDomainId: "<DOMAIN_ID_HERE>" # or just use <COLONY_ADDRESS_HERE_1>
      rootHash: "0x0000000000000000000000000000000000000000000000000000000000000000"
      toDomainId: "<ANOTHER_DOMAIN_ID_HERE>" # or just use <COLONY_ADDRESS_HERE_2>
      tokenAddress: "<TOKEN_ADDRESS_HERE>"
      type: MOVE_FUNDS
      updatedAt: "2024-08-16T14:55:07.825Z" # update this to any date previous to 30 days ago
    }
  ) {
    amount
    blockNumber
    colonyId
    createdAt
    fromDomainId
    id
    recipientAddress
    rootHash
    toDomainId
    tokenAddress
    type
    updatedAt
  }
}
  • Step 6. Run a few mutations in a couple of domains to get some testing data
  • Step 7. Now it's time to cache the values from the past so run the following query
query MyQuery {
  cacheAllDomainBalance {
    statusCode
  }
}
  • Step 8. You can test the cache has been created by running this query
query MyQuery {
  listCacheTotalBalances(
    filter: {domainId: {eq: "0xD4c521eE2C1EF1325FF354C4DEd6cB9c3d62a6ec_1"}}
  ) {
    items {
      colonyAddress
      date
      domainId
      id
      timeframePeriod
      timeframeType
      totalIn
      totalOut
    }
  }
}

Property 1=Completed

To be sure, the values are properly computed check the getValuesTrend method
The formula is
If current === 0
-100
if current !== 0 and previous === 0
100
if previous > 0 and current > 0
(previous - current) * 100 / previous - special thanks to @Nortsova 🥇

Further testing: please be as creative as possible

Diffs

New stuff

  • The wonderful cacheDomainBalance lambda
  • useCurrencyHistoricalConversionRate hook to compute the USDC value to the selected currency based on the date at which the historical balance was computed
  • getValuesTrend utils
  • GetCachedDomainBalance query

Changes 🏗

  • Add previousValue property to TitledSection component

TODO

  • Still need to test the cron lambda gets triggered based on provided schedule - but probably will tackle it in another issue

Resolves #2858

@mmioana mmioana self-assigned this Sep 12, 2024
@mmioana mmioana force-pushed the feat/2844-enhanced-dashboard-total-in-out-lambda branch from 517627b to 087fcd2 Compare September 12, 2024 13:05
@mmioana mmioana force-pushed the feat/2858-funds-trend branch 2 times, most recently from d9a379c to 912e713 Compare September 12, 2024 16:36
@mmioana mmioana changed the base branch from feat/2844-enhanced-dashboard-total-in-out-lambda to feat/dashboard-total-in-out-card-enhancement September 12, 2024 16:36
lastUpdatedAt?: Date;
}

export const useCurrencyConversionRate = ({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update this to take historical data into account is date is provided

@mmioana mmioana force-pushed the feat/dashboard-total-in-out-card-enhancement branch from b20c0e6 to e5b85e8 Compare September 13, 2024 13:44
@mmioana mmioana force-pushed the feat/dashboard-total-in-out-card-enhancement branch from e5b85e8 to d125286 Compare September 16, 2024 08:47
@mmioana mmioana marked this pull request as ready for review September 16, 2024 17:03
@mmioana mmioana requested review from a team as code owners September 16, 2024 17:03
@mmioana mmioana force-pushed the feat/dashboard-total-in-out-card-enhancement branch from 9fdf423 to b8cf268 Compare September 17, 2024 14:46
Base automatically changed from feat/dashboard-total-in-out-card-enhancement to feat/enhanced-dashboard September 18, 2024 14:18
@mmioana mmioana force-pushed the feat/2858-funds-trend branch 3 times, most recently from f0e9fb9 to 6b6ca2a Compare September 19, 2024 09:03
Copy link
Contributor

@bassgeta bassgeta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D A M N
took me a while to get through everything, but this stuff is actually quite straight forward. Nice job with the caching per day, basically saving us a ton of compute time for subsequent requests 👍
Left just some minor "cosmetic" comments which we can tackle, but overall a really great job with this!
So I can confirm that batch processing happens when triggering this lambda:
image
image
image
Filtering over teams works like a charm too:
image
image
It also doesn't break on mobile:
image

So I think we're good with this implementation, we probably need to run it against large sets of data to see how many resources it's gonna hog :D

TOTAL: "TOTAL"
}

const getYearsFromNow = (numberOfYears) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the naming of these helpers could be something like getPastYears since "from now" can go either into the future or into the past? or just something like subtractYearsFromNow

} = event.arguments?.input || {};
const periodFromNow = getPeriodFromNow(timeframePeriod, timeframeType);
const periodFromNow = getPeriodFromNow(timeframePeriod, timeframeType, timeframePeriodEndDate);
console.log('fetchDomainBalance', periodFromNow, timeframeType, timeframePeriodEndDate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀


return {
value: `${trend.toString()}%`,
isIncrease: trend.gte(0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's probably no realistic chance for this to happen, but should we just return percentages for value and if it's 0 don't display the trend at all?

Feat: Add cron lambda to cache all needed previous domain balances
Feat: Add payments and income trends
Update cacheDomainBalance cron lambda
Process balance requests in batches
Rename requests.js into operations.js
…data

Refactor: Update previousBalance to be converted to token historical data
Refactor: Changes after testing historical data
Refactor: Align after rebase
Refactor: Add missing condition for 0 percent trend
Fix: Align after rebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants