Improve script to handle errors

This commit is contained in:
Pieter Vander Vennet 2024-08-24 01:50:55 +02:00
parent d28acfdb20
commit 3079bbb934
2 changed files with 101 additions and 69 deletions

View file

@ -1,5 +1,5 @@
import Script from "./Script"
import { readFileSync, writeFileSync } from "fs"
import { appendFileSync, readFileSync, writeFile, writeFileSync } from "fs"
import { ChangeDescription } from "../src/Logic/Osm/Actions/ChangeDescription"
import { Changes } from "../src/Logic/Osm/Changes"
import { OsmObject } from "../src/Logic/Osm/OsmObject"
@ -28,57 +28,15 @@ class HandleErrors extends Script {
constructor() {
super("Inspects the errors made on a given day. Argument: path to errors")
}
async main(args: string[]): Promise<void> {
const osmConnection = new OsmConnection()
const downloader = new OsmObjectDownloader(osmConnection.Backend(), undefined)
const path = args[0]
const lines = readFileSync(path, "utf8").split("\n")
private readonly ignoreUsers = new Set<string>([])
const createdChangesets = new Set<string>()
const refusedFiles: Set<string> = new Set<string>()
refusedFiles.add("[]")
const changesObj = new Changes(
{
dryRun: new ImmutableStore(true),
osmConnection,
},
false,
(err) => console.error(err)
)
const all: ErrorMessage[] = []
for (const line of lines) {
if (!line?.trim()) {
continue
}
try {
const parsed: ErrorMessage = JSON.parse(line)
const e = parsed.message
if (e.layout === "grb") {
console.log("Skipping GRB ")
continue
}
for (const pendingChange of e.pendingChanges) {
console.log(
"\t https://osm.org/" + pendingChange.type + "/" + pendingChange.id,
pendingChange.meta.changeType,
pendingChange.doDelete ? "DELETE" : ""
)
}
all.push(parsed)
} catch (e) {
console.log("Parsing line failed:", e)
}
}
for (const parsed of all) {
private async handleError(parsed: ErrorMessage, changesObj: Changes, downloader: OsmObjectDownloader, createdChangesets: Set<string>, refusedFiles: Set<string>) {
console.log(
parsed.message.username,
parsed.message.layout,
parsed.message.message,
parsed.date
parsed.date,
)
const e = parsed.message
@ -88,10 +46,22 @@ class HandleErrors extends Script {
id: string
osmObj: OsmObject | "deleted"
}>(
neededIds.map(async (id) => ({
neededIds.map(async (id) => {
try {
const osmObj = await downloader.DownloadObjectAsync(id)
return ({
id,
osmObj: await downloader.DownloadObjectAsync(id),
}))
osmObj,
})
} catch (e) {
console.error("COULD NOT DOWNLOAD OBJECT", id)
return {
id,
osmObj: "deleted",
}
}
}),
)
const objects = osmObjects
@ -104,7 +74,7 @@ class HandleErrors extends Script {
newObjects: OsmObject[]
modifiedObjects: OsmObject[]
deletedObjects: OsmObject[]
} = changesObj.CreateChangesetObjects(toUpload, objects)
} = changesObj.CreateChangesetObjects(toUpload, objects, true)
const changeset = Changes.createChangesetFor("", changes)
const path =
@ -142,6 +112,64 @@ ${changeset}`
console.log("Written refused", path)
}
}
async main(args: string[]): Promise<void> {
const osmConnection = new OsmConnection()
const downloader = new OsmObjectDownloader(osmConnection.Backend(), undefined)
const path = args[0]
const lines = readFileSync(path, "utf8").split("\n")
const createdChangesets = new Set<string>()
const refusedFiles: Set<string> = new Set<string>()
refusedFiles.add("[]")
const changesObj = new Changes(
{
dryRun: new ImmutableStore(true),
osmConnection,
},
false,
(err) => console.error(err),
)
const all: ErrorMessage[] = []
for (const line of lines) {
if (!line?.trim()) {
continue
}
try {
const parsed: ErrorMessage = JSON.parse(line)
const e = parsed.message
if (e.layout === "grb") {
console.log("Skipping GRB ")
continue
}
if (this.ignoreUsers.has(e.username)) {
continue
}
for (const pendingChange of e.pendingChanges) {
console.log(
"\t https://osm.org/" + pendingChange.type + "/" + pendingChange.id,
pendingChange.meta.changeType,
pendingChange.doDelete ? "DELETE" : "",
)
}
all.push(parsed)
} catch (e) {
console.log("Parsing line failed:", e)
}
}
for (const parsed of all) {
try {
await this.handleError(parsed, changesObj, downloader, createdChangesets, refusedFiles)
} catch (e) {
console.error("ERROR: could not handle ", parsed, " due to", e)
writeFileSync("ERRORS."+parsed.index, "ERROR: due to " + e + ": could not handle\n" + JSON.stringify(parsed), "utf8")
}
}
}
}

View file

@ -266,7 +266,8 @@ export class Changes {
public CreateChangesetObjects(
changes: ChangeDescription[],
downloadedOsmObjects: OsmObject[]
downloadedOsmObjects: OsmObject[],
ignoreNoCreate: boolean = false
): {
newObjects: OsmObject[]
modifiedObjects: OsmObject[]
@ -314,6 +315,9 @@ export class Changes {
}
if (change.changes === undefined) {
// This object is a change to a newly created object. However, we have not seen the creation changedescription yet!
if(ignoreNoCreate){
continue
}
throw "Not a creation of the object: " + JSON.stringify(change)
}
// This is a new object that should be created