-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Giveth/add-fetching-project
Add fetching project
- Loading branch information
Showing
17 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = class Data1716207083370 { | ||
name = 'Data1716207083370' | ||
|
||
async up(db) { | ||
await db.query(`ALTER TABLE "project" ADD "slug" text`) | ||
await db.query(`ALTER TABLE "project" ADD "image" text`) | ||
} | ||
|
||
async down(db) { | ||
await db.query(`ALTER TABLE "project" DROP COLUMN "slug"`) | ||
await db.query(`ALTER TABLE "project" DROP COLUMN "image"`) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CRON_SCHEDULE = "0 0 * * *"; // UTC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const GIVETH_API_URL = | ||
process.env.GIVETH_API_URL || "https://mainnet.serve.giveth.io/graphql"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { DataSource } from "typeorm"; | ||
import { getDataSource } from "../../../helpers/db"; | ||
import { Project } from "../../../model"; | ||
import { fetchGivethProjectsBatch } from "./service"; | ||
import { GivethProjectInfo } from "./type"; | ||
|
||
const updateOrCreateProject = async ( | ||
dataSource: DataSource, | ||
project: GivethProjectInfo | ||
) => { | ||
const existingProject = await dataSource | ||
.getRepository(Project) | ||
.createQueryBuilder("project") | ||
.where("project.id = :id", { id: `giveth-${project.id}` }) | ||
.getOne(); | ||
|
||
if (existingProject) { | ||
const isUpdated = | ||
existingProject.title !== project.title || | ||
existingProject.description !== project.descriptionSummary || | ||
existingProject.slug !== project.slug || | ||
existingProject.image !== project.image; | ||
|
||
if (isUpdated) { | ||
const updatedProject = new Project({ | ||
...existingProject, | ||
title: project.title, | ||
description: project.descriptionSummary, | ||
image: project.image, | ||
slug: project.slug, | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.update(Project) | ||
.set(updatedProject) | ||
.where("id = :id", { id: `giveth-${project.id}` }) | ||
.execute(); | ||
} | ||
} else { | ||
const newProject = new Project({ | ||
id: `giveth-${project.id}`, | ||
title: project.title, | ||
description: project.descriptionSummary, | ||
image: project.image, | ||
slug: project.slug, | ||
projectId: project.id, | ||
source: "giveth", | ||
totalVouches: 0, | ||
totalFlags: 0, | ||
totalAttests: 0, | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(Project) | ||
.values([newProject]) | ||
.execute(); | ||
} | ||
}; | ||
|
||
const processProjectsBatch = async (projectsBatch: GivethProjectInfo[]) => { | ||
const dataSource = await getDataSource(); | ||
if (!dataSource) return; | ||
for (const project of projectsBatch) { | ||
console.log("Processing Project: Giveth", project.id); | ||
await updateOrCreateProject(dataSource, project); | ||
} | ||
}; | ||
|
||
export const fetchAndProcessGivethProjects = async () => { | ||
try { | ||
let hasMoreProjects = true; | ||
let skip = 0; | ||
const limit = 10; | ||
|
||
while (hasMoreProjects) { | ||
const projectsBatch = await fetchGivethProjectsBatch(limit, skip); | ||
if (projectsBatch.length > 0) { | ||
await processProjectsBatch(projectsBatch); | ||
skip += limit; | ||
} else { | ||
hasMoreProjects = false; | ||
} | ||
} | ||
} catch (error: any) { | ||
console.log("error on fetching giveth projects", error.message); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { graphQLRequest } from "../../../helpers/request"; | ||
import { GIVETH_API_URL } from "./constants"; | ||
import { GivethProjectInfo } from "./type"; | ||
|
||
export const fetchGivethProjectsBatch = async (limit: number, skip: number) => { | ||
const res = await graphQLRequest( | ||
GIVETH_API_URL, | ||
`query ($limit: Int, $skip: Int, $sortingBy: SortingField) { | ||
allProjects( | ||
limit: $limit | ||
skip: $skip | ||
sortingBy: $sortingBy | ||
) { | ||
projects { | ||
id | ||
title | ||
image | ||
slug | ||
descriptionSummary | ||
} | ||
} | ||
}`, | ||
{ | ||
limit, | ||
skip, | ||
sortingBy: "Newest", | ||
} | ||
); | ||
|
||
return res.data.allProjects.projects; | ||
}; | ||
|
||
export const fetchGivethProjects = async () => { | ||
let allProjects: GivethProjectInfo[] = []; | ||
let hasMoreProjects = true; | ||
let skip = 0; | ||
const limit = 10; | ||
|
||
while (hasMoreProjects) { | ||
const projectsBatch = await fetchGivethProjectsBatch(limit, skip); | ||
if (projectsBatch.length > 0) { | ||
allProjects = allProjects.concat(projectsBatch); | ||
skip += limit; | ||
} else { | ||
hasMoreProjects = false; | ||
} | ||
} | ||
|
||
return allProjects; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type GivethProjectInfo = { | ||
id: string; | ||
title: string; | ||
descriptionSummary: string; | ||
slug: string; | ||
image: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import cron from "node-cron"; | ||
import { fetchAndProcessGivethProjects } from "./giveth/helpers"; | ||
import { CRON_SCHEDULE } from "./config"; | ||
|
||
export const task = async () => { | ||
console.log("Importing Projects", new Date()); | ||
await fetchAndProcessGivethProjects(); | ||
}; | ||
|
||
export const importProjects = async () => { | ||
try { | ||
console.log("Importing Projects has been scheduled."); | ||
cron.schedule(CRON_SCHEDULE, task, { | ||
scheduled: true, | ||
timezone: "UTC", | ||
}); | ||
} catch (error) { | ||
console.log("Error", error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { task } from "."; | ||
|
||
task(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createOrmConfig } from "@subsquid/typeorm-config"; | ||
import { DataSource } from "typeorm"; | ||
|
||
let dataSource: DataSource | undefined = undefined; | ||
|
||
export const getDataSource = async () => { | ||
try { | ||
if (!dataSource || !dataSource.isInitialized) { | ||
let cfg = createOrmConfig({ projectDir: __dirname + "/../.." }); | ||
(cfg.entities as string[]).push(__dirname + "/../model/generated/*.ts"); | ||
|
||
dataSource = await new DataSource(cfg).initialize(); | ||
} | ||
return dataSource; | ||
} catch (error) { | ||
console.error("Failed to initialize DataSource", error); | ||
return null; | ||
} | ||
}; | ||
|
||
export const createEntityManager = async () => { | ||
const dataSource = await getDataSource(); | ||
if (!dataSource) return null; | ||
return dataSource.createEntityManager(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const graphQLRequest = async ( | ||
url: string, | ||
query: string, | ||
variables: any | ||
) => { | ||
const res = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ query, variables }), | ||
}); | ||
return await res.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters