|
|
@ -1,24 +1,55 @@ |
|
|
import { AutomationResults } from "../../definitions/automation" |
|
|
import { |
|
|
|
|
|
AutomationLog, |
|
|
|
|
|
AutomationResults, |
|
|
|
|
|
AutomationStatus, |
|
|
|
|
|
} from "../../definitions/automation" |
|
|
import { getAppDB } from "@budibase/backend-core/context" |
|
|
import { getAppDB } from "@budibase/backend-core/context" |
|
|
import { |
|
|
import { |
|
|
generateAutomationLogID, |
|
|
generateAutomationLogID, |
|
|
getAutomationLogParams, |
|
|
getAutomationLogParams, |
|
|
// getQueryIndex,
|
|
|
getQueryIndex, |
|
|
// ViewNames,
|
|
|
ViewNames, |
|
|
} from "../../db/utils" |
|
|
} from "../../db/utils" |
|
|
// import { createLogByAutomationView } from "../../db/views/staticViews"
|
|
|
import { createLogByAutomationView } from "../../db/views/staticViews" |
|
|
|
|
|
import { Automation } from "../../definitions/common" |
|
|
|
|
|
|
|
|
|
|
|
const EARLIEST_DATE = new Date(0).toISOString() |
|
|
const FREE_EXPIRY_SEC = 86400 |
|
|
const FREE_EXPIRY_SEC = 86400 |
|
|
// const PRO_EXPIRY_SEC = FREE_EXPIRY_SEC * 30
|
|
|
const PRO_EXPIRY_SEC = FREE_EXPIRY_SEC * 30 |
|
|
|
|
|
|
|
|
|
|
|
function getStatus(results: AutomationResults) { |
|
|
|
|
|
let status = AutomationStatus.SUCCESS |
|
|
|
|
|
let first = true |
|
|
|
|
|
for (let step of results.steps) { |
|
|
|
|
|
// skip the trigger, its always successful if automation ran
|
|
|
|
|
|
if (first) { |
|
|
|
|
|
first = false |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
if (!step.outputs?.success) { |
|
|
|
|
|
status = AutomationStatus.ERROR |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return status |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// export function oneMonthAgo() {
|
|
|
|
|
|
// return new Date(
|
|
|
|
|
|
// new Date().getTime() - PRO_EXPIRY_SEC * 1000
|
|
|
|
|
|
// ).toISOString()
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
export function oneDayAgo() { |
|
|
|
|
|
return new Date(new Date().getTime() - FREE_EXPIRY_SEC * 1000).toISOString() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
async function clearOldHistory() { |
|
|
async function clearOldHistory() { |
|
|
const db = getAppDB() |
|
|
const db = getAppDB() |
|
|
// TODO: handle license lookup for deletion
|
|
|
// TODO: handle license lookup for deletion
|
|
|
const time = new Date(new Date().getTime() - FREE_EXPIRY_SEC * 1000) |
|
|
const expiredEnd = oneDayAgo() |
|
|
const queryParams: any = { |
|
|
const results = await getAllLogs(EARLIEST_DATE, expiredEnd, { |
|
|
endIso: time, |
|
|
include_docs: false, |
|
|
} |
|
|
}) |
|
|
const results = await db.allDocs(getAutomationLogParams(queryParams)) |
|
|
|
|
|
const toDelete = results.map((doc: any) => ({ |
|
|
const toDelete = results.map((doc: any) => ({ |
|
|
_id: doc.id, |
|
|
_id: doc.id, |
|
|
_rev: doc.rev, |
|
|
_rev: doc.rev, |
|
|
@ -27,36 +58,66 @@ async function clearOldHistory() { |
|
|
await db.bulkDocs(toDelete) |
|
|
await db.bulkDocs(toDelete) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// async function getByAutomationID(automationId: string) {
|
|
|
async function getAllLogs( |
|
|
// const db = getAppDB()
|
|
|
startDate: string, |
|
|
// try {
|
|
|
endDate: string, |
|
|
// const queryParams: any = {
|
|
|
opts: any = { include_docs: true } |
|
|
// automationId,
|
|
|
) { |
|
|
// }
|
|
|
const db = getAppDB() |
|
|
// return (
|
|
|
const queryParams: any = { |
|
|
// await db.query(
|
|
|
endDate, |
|
|
// getQueryIndex(ViewNames.LOGS_BY_AUTOMATION),
|
|
|
startDate, |
|
|
// getAutomationLogParams(queryParams, { include_docs: true })
|
|
|
} |
|
|
// )
|
|
|
let response = (await db.allDocs(getAutomationLogParams(queryParams, opts))) |
|
|
// ).rows.map((row: any) => row.doc)
|
|
|
.rows |
|
|
// } catch (err: any) {
|
|
|
if (opts?.include_docs) { |
|
|
// if (err != null && err.name === "not_found") {
|
|
|
response = response.map((row: any) => row.doc) |
|
|
// await createLogByAutomationView()
|
|
|
} |
|
|
// return getByAutomationID(automationId)
|
|
|
return response |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
export async function storeHistory( |
|
|
async function getLogsByAutomationID( |
|
|
automationId: string, |
|
|
automationId: string, |
|
|
|
|
|
opts: { startDate?: string; endDate?: string } = {} |
|
|
|
|
|
): Promise<AutomationLog[]> { |
|
|
|
|
|
const db = getAppDB() |
|
|
|
|
|
try { |
|
|
|
|
|
const queryParams = { |
|
|
|
|
|
startDate: opts?.startDate, |
|
|
|
|
|
endDate: opts?.startDate, |
|
|
|
|
|
automationId, |
|
|
|
|
|
} |
|
|
|
|
|
return ( |
|
|
|
|
|
await db.query( |
|
|
|
|
|
getQueryIndex(ViewNames.LOGS_BY_AUTOMATION), |
|
|
|
|
|
getAutomationLogParams(queryParams, { include_docs: true }) |
|
|
|
|
|
) |
|
|
|
|
|
).rows.map((row: any) => row.doc) |
|
|
|
|
|
} catch (err: any) { |
|
|
|
|
|
if (err != null && err.name === "not_found") { |
|
|
|
|
|
await createLogByAutomationView() |
|
|
|
|
|
return getLogsByAutomationID(automationId, opts) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return [] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function storeLog( |
|
|
|
|
|
automation: Automation, |
|
|
results: AutomationResults |
|
|
results: AutomationResults |
|
|
) { |
|
|
) { |
|
|
|
|
|
const automationId = automation._id |
|
|
|
|
|
const name = automation.name |
|
|
const db = getAppDB() |
|
|
const db = getAppDB() |
|
|
const isoDate = new Date().toISOString() |
|
|
const isoDate = new Date().toISOString() |
|
|
const id = generateAutomationLogID(isoDate, automationId) |
|
|
const id = generateAutomationLogID(automationId, isoDate) |
|
|
|
|
|
|
|
|
await db.put({ |
|
|
await db.put({ |
|
|
// results contain automationId and status for view
|
|
|
// results contain automationId and status for view
|
|
|
...results, |
|
|
...results, |
|
|
|
|
|
automationId, |
|
|
|
|
|
automationName: name, |
|
|
|
|
|
status: getStatus(results), |
|
|
createdAt: isoDate, |
|
|
createdAt: isoDate, |
|
|
_id: id, |
|
|
_id: id, |
|
|
}) |
|
|
}) |
|
|
@ -64,4 +125,16 @@ export async function storeHistory( |
|
|
await clearOldHistory() |
|
|
await clearOldHistory() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export async function retrieve() {} |
|
|
export async function getLogs(startDate: string, automationId?: string) { |
|
|
|
|
|
let logs: AutomationLog[] |
|
|
|
|
|
let endDate = new Date().toISOString() |
|
|
|
|
|
if (automationId) { |
|
|
|
|
|
logs = await getLogsByAutomationID(automationId, { |
|
|
|
|
|
startDate, |
|
|
|
|
|
endDate, |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
logs = await getAllLogs(startDate, endDate) |
|
|
|
|
|
} |
|
|
|
|
|
return logs |
|
|
|
|
|
} |
|
|
|