问:
Unordered F# AsyncSeq.mapParallel with throttling
I'm using F# and have an AsyncSeq<Async<'t>>. Each item will take a varying amount of time to process and does I/O that's rate-limited.

I want to run all the operations in parallel and then pass them down the chain as an AsyncSeq<'t> so I can perform further manipulations on them and ultimately AsyncSeq.fold them into a final outcome.

The following AsyncSeq operations almost meet my needs:

mapAsyncParallel - does the parallelism, but it's unconstrained, (and I don't need the order preserved)

iterAsyncParallelThrottled - parallel and has a max degree of parallelism but doesn't let me return results (and I don't need the order preserved)

What I really need is like a mapAsyncParallelThrottled. But, to be more precise, really the operation would be entitled mapAsyncParallelThrottledUnordered.

Things I'm considering:

use mapAsyncParallel but use a Semaphore within the function to constrain the parallelism myself, which is probably not going to be optimal in terms of concurrency, and due to buffering the results to reorder them.

use iterAsyncParallelThrottled and do some ugly folding of the results into an accumulator as they arrive guarded by a lock kinda like this - but I don't need the ordering so it won't be optimal.

build what I need by enumerating the source and emitting results via AsyncSeqSrc like this. I'd probably have a set of Async.StartAsTask tasks in flight and start more after each Task.WaitAny gives me something to AsyncSeqSrc.put until I reach the maxDegreeOfParallelism

Surely I'm missing a simple answer and there's a better way?

Failing that, would love someone to sanity check my option 3 in either direction!

I'm open to using AsyncSeq.toAsyncEnum and then use an IAsyncEnumerable way of achieving the same outcome if that exists, though ideally without getting into TPL DataFlow or RX land if it can be avoided (I've done extensive SO searching for that without results...).

答:
Here's a testbed I used to validate @akara's excellent work:

```<code>#r "nuget:FSharp.Control.AsyncSeq"
open FSharp.Control
</code>```
```<code>module AsyncSeqEx =

    open System.Threading.Channels

    let mapAsyncParallelBoundedUnordered boundedAmount (mapper: 't -> Async<'u>) source = asyncSeq {
        let! ct = Async.CancellationToken
        let channel : Channel<'u> = Channel.CreateUnbounded()
        let handle req = async {
            let! res = mapper req
            do! let t = channel.Writer.WriteAsync(res, ct) in t.AsTask() |> Async.AwaitTask }
        let! _ = Async.StartChild <| async {
            do! source |> AsyncSeq.iterAsyncParallelThrottled boundedAmount handle
            channel.Writer.Complete() }
        yield! channel.Reader.ReadAllAsync(ct) |> AsyncSeq.ofAsyncEnum
    }
</code>```

I also ported the same code to use AsyncSeqSrc instead of channels, which seems to work too, with equivalent perf:

```<code>    // AsyncSeqSrc-based reimpl of the above
    let mapAsyncParallelBoundedUnordered2 boundedAmount (mapper: 't -> Async<'u>) source = asyncSeq {
        let output = AsyncSeqSrc.create ()
        let handle req = async { let! res = mapper req in AsyncSeqSrc.put res output }
        let! _ = Async.StartChild <| async {
            do! source |> AsyncSeq.iterAsyncParallelThrottled boundedAmount handle
            AsyncSeqSrc.close output }
        yield! AsyncSeqSrc.toAsyncSeq output
    }
</code>```

The following impl, leaning on AsyncSeq.mapAsyncParallel seems to achieve similar perf to both:

```<code>module Async =

    let parallelThrottled dop f = Async.Parallel(f, maxDegreeOfParallelism = dop)
    type Semaphore(max) =
        let inner = new System.Threading.SemaphoreSlim(max)
        member _.Await() = async {
            let! ct = Async.CancellationToken
            return! inner.WaitAsync ct |> Async.AwaitTask }
        member _.Release() =
            inner.Release() |> ignore
    let throttle degreeOfParallelism f =
        let s = Semaphore degreeOfParallelism
        fun x -> async {
            do! s.Await()
            try return! f x
            finally s.Release() }

module AsyncSeq =

    open FSharp.Control

    // see https://stackoverflow.com/a/71065152/11635
    let mapAsyncParallelThrottled degreeOfParallelism (f: 't -> Async<'u>) : AsyncSeq<'t> -> AsyncSeq<'u> =
        let throttle = Async.throttle degreeOfParallelism
        AsyncSeq.mapAsyncParallel (throttle f)
</code>```

Testbed:

```<code>let dop = 10
let r = System.Random()
let durations = Array.init 10000 (fun _ -> r.Next(10, 100))
let work =
    let sleep (x : int) = async {
        do! Async.Sleep x
        return x
    }
    AsyncSeq.ofSeq durations |> AsyncSeq.mapAsyncParallelThrottled dop sleep
let start = System.Diagnostics.Stopwatch.StartNew()
let results = work |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
let timeTaken = start.ElapsedMilliseconds
let totalTimeTaken = Array.sum results
let expectedWallTime = float totalTimeTaken / float dop
let overhead = timeTaken - int64 expectedWallTime
let inline stringf format (x : ^a) =
    (^a : (member ToString : string -> string) (x, format))
let inline sep x = stringf "N0" x
printfn $"Gross {sep totalTimeTaken}ms Threads {dop} Wall {sep timeTaken}ms overhead {sep overhead}ms ordered: {durations = results}"
</code>```

Result:

Gross 544,873ms Threads 10 Wall 55,659ms overhead 1,172ms ordered: True

For now, it seems that for my use case there's no major win to be had by admitting unordered results vs just having the function argument to mapAsyncParallel self-govern to achieve the desired throttling effect

Body:
Here's a testbed I used to validate @akara's excellent work:

#r "nuget:FSharp.Control.AsyncSeq"
open FSharp.Control

module AsyncSeqEx =

    open System.Threading.Channels

    let mapAsyncParallelBoundedUnordered boundedAmount (mapper: 't -> Async<'u>) source = asyncSeq {
        let! ct = Async.CancellationToken
        let channel : Channel<'u> = Channel.CreateUnbounded()
        let handle req = async {
            let! res = mapper req
            do! let t = channel.Writer.WriteAsync(res, ct) in t.AsTask() |> Async.AwaitTask }
        let! _ = Async.StartChild <| async {
            do! source |> AsyncSeq.iterAsyncParallelThrottled boundedAmount handle
            channel.Writer.Complete() }
        yield! channel.Reader.ReadAllAsync(ct) |> AsyncSeq.ofAsyncEnum
    }

I also ported the same code to use AsyncSeqSrc instead of channels, which seems to work too, with equivalent perf:

    // AsyncSeqSrc-based reimpl of the above
    let mapAsyncParallelBoundedUnordered2 boundedAmount (mapper: 't -> Async<'u>) source = asyncSeq {
        let output = AsyncSeqSrc.create ()
        let handle req = async { let! res = mapper req in AsyncSeqSrc.put res output }
        let! _ = Async.StartChild <| async {
            do! source |> AsyncSeq.iterAsyncParallelThrottled boundedAmount handle
            AsyncSeqSrc.close output }
        yield! AsyncSeqSrc.toAsyncSeq output
    }

The following impl, leaning on AsyncSeq.mapAsyncParallel seems to achieve similar perf to both:

module Async =

    let parallelThrottled dop f = Async.Parallel(f, maxDegreeOfParallelism = dop)
    type Semaphore(max) =
        let inner = new System.Threading.SemaphoreSlim(max)
        member _.Await() = async {
            let! ct = Async.CancellationToken
            return! inner.WaitAsync ct |> Async.AwaitTask }
        member _.Release() =
            inner.Release() |> ignore
    let throttle degreeOfParallelism f =
        let s = Semaphore degreeOfParallelism
        fun x -> async {
            do! s.Await()
            try return! f x
            finally s.Release() }

module AsyncSeq =

    open FSharp.Control

    // see https://stackoverflow.com/a/71065152/11635
    let mapAsyncParallelThrottled degreeOfParallelism (f: 't -> Async<'u>) : AsyncSeq<'t> -> AsyncSeq<'u> =
        let throttle = Async.throttle degreeOfParallelism
        AsyncSeq.mapAsyncParallel (throttle f)

Testbed:

let dop = 10
let r = System.Random()
let durations = Array.init 10000 (fun _ -> r.Next(10, 100))
let work =
    let sleep (x : int) = async {
        do! Async.Sleep x
        return x
    }
    AsyncSeq.ofSeq durations |> AsyncSeq.mapAsyncParallelThrottled dop sleep
let start = System.Diagnostics.Stopwatch.StartNew()
let results = work |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
let timeTaken = start.ElapsedMilliseconds
let totalTimeTaken = Array.sum results
let expectedWallTime = float totalTimeTaken / float dop
let overhead = timeTaken - int64 expectedWallTime
let inline stringf format (x : ^a) =
    (^a : (member ToString : string -> string) (x, format))
let inline sep x = stringf "N0" x
printfn $"Gross {sep totalTimeTaken}ms Threads {dop} Wall {sep timeTaken}ms overhead {sep overhead}ms ordered: {durations = results}"

Result:

Gross 544,873ms Threads 10 Wall 55,659ms overhead 1,172ms ordered: True

For now, it seems that for my use case there's no major win to be had by admitting unordered results vs just having the function argument to mapAsyncParallel self-govern to achieve the desired throttling effect


来源: stackexchange

问:
MySQL "AND ONLY HAS" type operator/function?
MySQL here. I have the following data model:

```<code>[applications]
===
id : PK
status : VARCHAR
...lots of other fields

[invoices]
===
id : PK
application_id : FK to applications.id
status : VARCHAR
... lot of other fields
</code>```

It is possible for the same application to have 0+ invoices associated with it, each with a different status. I am trying to write a query that looks for applications that:

have a status of "Pending"; and

have only invoices whose status is "Accepted"

My best attempt at such a query is:

```<code>SELECT a.id,
       i.id,
       a.status,
       i.status
FROM application a
INNER JOIN invoice i ON  a.id = i.application_id
WHERE a.status = "Pending"
AND i.status = "Accepted"
</code>```

The problem here is that this query does not exclude applications that are associated with non-Accepted invoices. Hence it might return a row of, say:

```<code>+--------+--------+-----------+-----------+
| id     | id     |  status   |  status   |
+--------+--------+-----------+-----------+
| 123    | 456    |  Pending  |  Accepted |
+--------+--------+-----------+-----------+
</code>```

However, when I query the invoice table for any invoices tied to application id = 123, there are many non-Accepted invoices that come back in the results. Its almost as if I wished SQL support some type of "AND ONLY HAS" so I could make my clause: "AND ONLY HAS i.status = 'Accepted'"

So I'm missing the clause that excludes results for applications with 1+ non-Accepted invoices. Any ideas where I'm going awry?

答:
You can use the following logic:</p>
```<code>SELECT *
FROM application
WHERE status = 'pending'
AND EXISTS (
    SELECT 1
    FROM invoice
    WHERE invoice.application_id = application.id
    HAVING SUM(CASE WHEN invoice.status = 'accepted' THEN 1 ELSE 0 END) > 0 -- count of accepted invoices > 0
    AND    SUM(CASE WHEN invoice.status = 'accepted' THEN 0 ELSE 1 END) = 0 -- count of anyother invoices = 0
)
</code>```

ContentLicense: CC BY-SA 4.0

Body:
MySQL here. I have the following data model:

[applications]
===
id : PK
status : VARCHAR
...lots of other fields

[invoices]
===
id : PK
application_id : FK to applications.id
status : VARCHAR
... lot of other fields

It is possible for the same application to have 0+ invoices associated with it, each with a different status. I am trying to write a query that looks for applications that:

have a status of "Pending"; and

have only invoices whose status is "Accepted"

My best attempt at such a query is:

SELECT a.id,
       i.id,
       a.status,
       i.status
FROM application a
INNER JOIN invoice i ON  a.id = i.application_id
WHERE a.status = "Pending"
AND i.status = "Accepted"

The problem here is that this query does not exclude applications that are associated with non-Accepted invoices. Hence it might return a row of, say:

+--------+--------+-----------+-----------+
| id     | id     |  status   |  status   |
+--------+--------+-----------+-----------+
| 123    | 456    |  Pending  |  Accepted |
+--------+--------+-----------+-----------+

However, when I query the invoice table for any invoices tied to application id = 123, there are many non-Accepted invoices that come back in the results. Its almost as if I wished SQL support some type of "AND ONLY HAS" so I could make my clause: "AND ONLY HAS i.status = 'Accepted'"

So I'm missing the clause that excludes results for applications with 1+ non-Accepted invoices. Any ideas where I'm going awry?

Body:
You can use the following logic:

SELECT *
FROM application
WHERE status = 'pending'
AND EXISTS (
    SELECT 1
    FROM invoice
    WHERE invoice.application_id = application.id
    HAVING SUM(CASE WHEN invoice.status = 'accepted' THEN 1 ELSE 0 END) > 0 -- count of accepted invoices > 0
    AND    SUM(CASE WHEN invoice.status = 'accepted' THEN 0 ELSE 1 END) = 0 -- count of anyother invoices = 0
)


问:
Problem with the calculating distance between locations using Haversine formula
I am calculating the distance between two points recorded in the history of Yandex.Maps in the Android 11 app. Everything works well in the getPoints method. We write all the coordinates they were in our database to a list of arrays. I even implemented overflow, exit, and array checks. Again, up to this point, everything worked well and as expected.

```<code>public ArrayList<Double> getPoints () {
    ArrayList<Double> location = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select latitude,longitude from "+Table_Name_Location,null);
    if(cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            Double latitude = cursor.getDouble(cursor.getColumnIndex("Lat"));
            Double longitude = cursor.getDouble(cursor.getColumnIndex("Longi"));
            location.add(latitude);
            location.add(longitude);
        }
    }
    cursor.close();
    return location;
}
</code>```

However, when I try to calculate the length in the distance method, several latitudes and longitudes stored in the SQLite database incorrectly calculate the total distance, for example, 450 kilometres, and according to our data, we should get 230 km. A calculation error occurs.

```<code>private double distance(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
        * Math.sin(deg2rad(lat2))
        + Math.cos(deg2rad(lat1))
        * Math.cos(deg2rad(lat2))
        * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}
</code>```

I tried to calculate the distance using the haversine formula. I also wrote functions to convert radians to degrees and vice versa. In the distance method, I calculate the distance using the haversine formula. I suspect that the error is in calculating the distance, namely in the implementation of the haversine formula.

答:
```<code>     /**
     * Calculate the distance between two points in meters using the Haversine formula
     *
     * @param lat latitude of the first point
     * @param lon longitude of the first point
     * @param lat2 latitude of the second point
     * @param lon2 longitude of the second point
     * @return the distance between the two points in meters
     **/
    public static double distance(double lat, double lon, double lat2, double lon2) {
        final double R = 6371e3;
        final double la = lat * java.lang.Math.PI / 180;
        final double laa = lat2 * java.lang.Math.PI / 180;
        final double lo = (lat2 - lat) * java.lang.Math.PI / 180;
        final double loo = (lon2 - lon) * java.lang.Math.PI / 180;
        final double a = java.lang.Math.sin(lo / 2) * java.lang.Math.sin(lo / 2) + java.lang.Math.cos(la) * java.lang.Math.cos(laa) * java.lang.Math.sin(loo / 2) * java.lang.Math.sin(loo / 2);
        final double c = 2 * java.lang.Math.atan2(java.lang.Math.sqrt(a), java.lang.Math.sqrt(1 - a));
        return R * c;
    }
</code>```

Body:
I am calculating the distance between two points recorded in the history of Yandex.Maps in the Android 11 app. Everything works well in the getPoints method. We write all the coordinates they were in our database to a list of arrays. I even implemented overflow, exit, and array checks. Again, up to this point, everything worked well and as expected.

public ArrayList<Double> getPoints () {
    ArrayList<Double> location = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select latitude,longitude from "+Table_Name_Location,null);
    if(cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            Double latitude = cursor.getDouble(cursor.getColumnIndex("Lat"));
            Double longitude = cursor.getDouble(cursor.getColumnIndex("Longi"));
            location.add(latitude);
            location.add(longitude);
        }
    }
    cursor.close();
    return location;
}

However, when I try to calculate the length in the distance method, several latitudes and longitudes stored in the SQLite database incorrectly calculate the total distance, for example, 450 kilometres, and according to our data, we should get 230 km. A calculation error occurs.

private double distance(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
        * Math.sin(deg2rad(lat2))
        + Math.cos(deg2rad(lat1))
        * Math.cos(deg2rad(lat2))
        * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

I tried to calculate the distance using the haversine formula. I also wrote functions to convert radians to degrees and vice versa. In the distance method, I calculate the distance using the haversine formula. I suspect that the error is in calculating the distance, namely in the implementation of the haversine formula.


问:
Status expected:<200> but was:<400> error with MockMvc
I have an issue with testing my Spring Boot Controller I'm trying to solve for the past few hours. I'm quite new to coding, but I got that assignment in my company nevertheless.

My Controller:

```<code>@GetMapping(value = "/data", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getData(@RequestParam("core") List<String> core, @RequestParam("date") List<String> date,
        @RequestParam("state") String state, @RequestParam("area") List<String> area,
        @RequestParam("type") List<String> type, @RequestParam("analyt") String analyt,
        @RequestParam("wp") String wp, @RequestParam("subDesc") String subDesc,
        @RequestParam("anaMonitor") List<String> anaMonitor, @RequestParam("ecoMonitor") List<String> ecoMonitor,
        @RequestParam("subType") List<String> subType, @RequestParam("needed") List<String> needed,
        @RequestParam("notify") List<String> notify, @RequestParam("available") List<String> available,
        @RequestParam("phase") List<String> phase, @RequestParam("subAvailable") List<String> subAvailable,
        @RequestParam("subShipped") List<String> subShipped, @RequestParam("external") List<String> external,
        @RequestParam("valid") List<String> valid, @RequestParam("contract") List<String> contract) {
    FilterInputModel model = new FilterInputModel(core, date, state, type, area, analyt, wp, subDesc, anaMonitor, ecoMonitor, subType, needed, notify, available, phase, subAvailable, subShipped, external, valid, contract);
        return new ResponseEntity<>(vaultService.getFilteredFields(model), HttpStatus.OK);
    }
</code>```

Here's a test:

```<code>@DisplayName("Controller Test")
@AutoConfigureMockMvc
@WebMvcTest(controllers = Controller.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private FilterInputModel model;

    @MockBean
    private TestService testService;

    @Autowired
    ControllerTest() {
    }

    @BeforeEach
    void setup() {
        MockitoAnnotations.openMocks(this);

    @Test
    void getDataTest() throws Exception {
        List<Object> response = new ArrayList<>();
        when(vaultService.getFilteredFields(model))
            .thenReturn(response)
            .thenReturn(HttpStatus.OK);

        this.mockMvc.perform(
                MockMvcRequestBuilders.get("/api/data")
                    .contentType(MediaType.APPLICATION_JSON_VALUE)
                    .accept("application/json"))
                .andExpect(MockMvcResultMatchers.status().isOk());
                .andExpect(result -> assertEquals(response, result.getAsyncResult()));
   }
</code>```

And here's an answer I got. It says in an error message that 'Required request parameter 'core' for method parameter type List is not present' but that answer didn't help me much. I'm still stuck looking for an answer.

```<code>MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /api/data
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
             Body = null
    Session Attrs = {}

Handler:
             Type = hr.ogcs.ltt.api.lttapi.controller.LttController
           Method = hr.ogcs.ltt.api.lttapi.controller.LttController#getData(List, List, String, List, List, String, String, String, List, List, List, List, List, List, List, List, List, List, List, List)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.bind.MissingServletRequestParameterException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 400
    Error message = Required request parameter 'core' for method parameter type List is not present
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
     Content type = null
             Body =
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Status expected:<200> but was:<400>
Expected :200
Actual   :400
<Click to see difference>

java.lang.AssertionError: Status expected:<200> but was:<400>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
    at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:212)
   at hr.ogcs.ltt.api.apicontroller.ControllerTest.getDataTest(ControllerTest.java:126) <31 internal lines>
   at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
   at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>
</code>```

答:
You are not sending the required parameters (the parameters annotated with @RequestParam) to the method. By default they are all required. If not present Spring blocks the request with missing arguments.

Example of sending parameters:

```<code>mvc.perform(
    MockMvcRequestBuilders.get("/public/xpto/{code}", "21212").param("core", "ubababacaio")
        .contentType(MediaType.APPLICATION_JSON)
        .contentType(MediaType.APPLICATION_JSON_VALUE).characterEncoding("UTF-8")).andExpect(status().isOk());
</code>```

Body:
I have an issue with testing my Spring Boot Controller I'm trying to solve for the past few hours. I'm quite new to coding, but I got that assignment in my company nevertheless.

My Controller:

@GetMapping(value = "/data", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getData(@RequestParam("core") List<String> core, @RequestParam("date") List<String> date,
        @RequestParam("state") String state, @RequestParam("area") List<String> area,
        @RequestParam("type") List<String> type, @RequestParam("analyt") String analyt,
        @RequestParam("wp") String wp, @RequestParam("subDesc") String subDesc,
        @RequestParam("anaMonitor") List<String> anaMonitor, @RequestParam("ecoMonitor") List<String> ecoMonitor,
        @RequestParam("subType") List<String> subType, @RequestParam("needed") List<String> needed,
        @RequestParam("notify") List<String> notify, @RequestParam("available") List<String> available,
        @RequestParam("phase") List<String> phase, @RequestParam("subAvailable") List<String> subAvailable,
        @RequestParam("subShipped") List<String> subShipped, @RequestParam("external") List<String> external,
        @RequestParam("valid") List<String> valid, @RequestParam("contract") List<String> contract) {
    FilterInputModel model = new FilterInputModel(core, date, state, type, area, analyt, wp, subDesc, anaMonitor, ecoMonitor, subType, needed, notify, available, phase, subAvailable, subShipped, external, valid, contract);
        return new ResponseEntity<>(vaultService.getFilteredFields(model), HttpStatus.OK);
    }

Here's a test:

@DisplayName("Controller Test")
@AutoConfigureMockMvc
@WebMvcTest(controllers = Controller.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private FilterInputModel model;

    @MockBean
    private TestService testService;

    @Autowired
    ControllerTest() {
    }

    @BeforeEach
    void setup() {
        MockitoAnnotations.openMocks(this);

    @Test
    void getDataTest() throws Exception {
        List<Object> response = new ArrayList<>();
        when(vaultService.getFilteredFields(model))
            .thenReturn(response)
            .thenReturn(HttpStatus.OK);

        this.mockMvc.perform(
                MockMvcRequestBuilders.get("/api/data")
                    .contentType(MediaType.APPLICATION_JSON_VALUE)
                    .accept("application/json"))
                .andExpect(MockMvcResultMatchers.status().isOk());
                .andExpect(result -> assertEquals(response, result.getAsyncResult()));
   }

And here's an answer I got. It says in an error message that 'Required request parameter 'core' for method parameter type List is not present' but that answer didn't help me much. I'm still stuck looking for an answer.

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /api/data
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
             Body = null
    Session Attrs = {}

Handler:
             Type = hr.ogcs.ltt.api.lttapi.controller.LttController
           Method = hr.ogcs.ltt.api.lttapi.controller.LttController#getData(List, List, String, List, List, String, String, String, List, List, List, List, List, List, List, List, List, List, List, List)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.bind.MissingServletRequestParameterException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 400
    Error message = Required request parameter 'core' for method parameter type List is not present
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
     Content type = null
             Body =
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Status expected:<200> but was:<400>
Expected :200
Actual   :400
<Click to see difference>

java.lang.AssertionError: Status expected:<200> but was:<400>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
    at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:212)
   at hr.ogcs.ltt.api.apicontroller.ControllerTest.getDataTest(ControllerTest.java:126) <31 internal lines>
   at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
   at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>

Body:
You are not sending the required parameters (the parameters annotated with @RequestParam) to the method. By default they are all required. If not present Spring blocks the request with missing arguments.

Example of sending parameters:

mvc.perform(
    MockMvcRequestBuilders.get("/public/xpto/{code}", "21212").param("core", "ubababacaio")
        .contentType(MediaType.APPLICATION_JSON)
        .contentType(MediaType.APPLICATION_JSON_VALUE).characterEncoding("UTF-8")).andExpect(status().isOk());


问:
how to get special character from string in nodejs and replace
how to find specific special character from string and replace it to unicode \u0026  + = \u002B

replace only these special character : [$, &, +, #]

example : "HELLO JAMES (WITH ME YOUR) \n+++ SEE & FIELD 4-B +++

MY code

```<code> var char = '+';
          var saa =char.charCodeAt(0);
          console.log(saa)
          var codeHex = saa.toString(16).toUpperCase();
          while (codeHex.length < 4) {
            codeHex = "0" + codeHex;
        }
        var afteruni = name.replaceAll('\+','\\u'+codeHex)

String.prototype.replaceAll = function(search, replacement) {
  var target = this;
  return target.replace(new RegExp(search, 'g'), replacement);
};
</code>```

i want like this :

"HELLO JAMES (WITH ME YOUR) \n\u002B\u002B\u002B SEE \u0026 FIELD 4-B
\u002B\u002B\u002B"

ERROR : Invalid regular expression: /+/: Nothing to repeat\

答:
you don't need to redefined replaceAll - why do that? replaceAll will already give you a correct response :

just replace line: var afteruni = name.replaceAll('\+','\\u'+codeHex)

with var afteruni = name.replaceAll(/\+/g,'\\u'+codeHex)

iv'e run that in my PC and it is working.

Body:
how to find specific special character from string and replace it to unicode \u0026  + = \u002B

replace only these special character : [$, &, +, #]

example : "HELLO JAMES (WITH ME YOUR) \n+++ SEE & FIELD 4-B +++

MY code

 var char = '+';
          var saa =char.charCodeAt(0);
          console.log(saa)
          var codeHex = saa.toString(16).toUpperCase();
          while (codeHex.length < 4) {
            codeHex = "0" + codeHex;
        }
        var afteruni = name.replaceAll('\+','\\u'+codeHex)

String.prototype.replaceAll = function(search, replacement) {
  var target = this;
  return target.replace(new RegExp(search, 'g'), replacement);
};

i want like this :

"HELLO JAMES (WITH ME YOUR) \n\u002B\u002B\u002B SEE \u0026 FIELD 4-B
\u002B\u002B\u002B"

ERROR : Invalid regular expression: /+/: Nothing to repeat\


问:
Perform a repeatable operation on StringBuilder when button pressed
I have a StringBulder strB = "123456"; and when a numerical button is pressed, i want the method to:

Delete first char from strB (should be "23456")

Append value from the button on the right side (e.g. "1", so "234561")

Have the resulting value ("234561") be the strB value

Have the ability to press one button after the other and it changing the strB (e.g. pressing 2,4,9 now, should result in steB = "561249") And with every one of the button presses the method would do its thing

How can I do that? I've been sitting over this for the past 3 days.
Tried String, StringBuilder, StringBuffer, for loops, if, none of them i could make working beyond a first button click, please help :/

答:
Okay, i figured it out. If anyone runs into this problem, this worked for me. My input comes from onClick method, which sends a number as string "4", the loop triggers once, and it's ready to run again on the next onClick event.

countdown is declared in the main method as
private StringBuilder countdown = new StringBuilder(6).append("000000");.

```<code>public void countDownTimer(String input){
        int inputLgt = input.length();
        if (clicks<7) {
            for (int i = 0; i < 1; i++) {
                if (inputLgt == 1) {
                    countdown.delete(0, 1);
                    countdown.append(input);
                } else if (inputLgt == 2) {
                    countdown.delete(0, 2);
                    countdown.append(input);
                } else if (inputLgt == 3) {
                    countdown.delete(5, 6);
                    countdown.insert(0,"0");
                }
            }
        }

        String strNew = countdown.toString();
        String h = strNew.substring(0, 2);
        String m = strNew.substring(2, 4);
        String s = strNew.substring(4, 6);
        String hms = h + "h " + m + "m " + s + "s";

        editValue.setText(hms);
        String click = String.valueOf(clicks);
        Log.println(Log.INFO, "Clicks:", " "+click+" "+strNew);

    }
</code>```

Body:
Okay, i figured it out. If anyone runs into this problem, this worked for me. My input comes from onClick method, which sends a number as string "4", the loop triggers once, and it's ready to run again on the next onClick event.

countdown is declared in the main method as
private StringBuilder countdown = new StringBuilder(6).append("000000");.

public void countDownTimer(String input){
        int inputLgt = input.length();
        if (clicks<7) {
            for (int i = 0; i < 1; i++) {
                if (inputLgt == 1) {
                    countdown.delete(0, 1);
                    countdown.append(input);
                } else if (inputLgt == 2) {
                    countdown.delete(0, 2);
                    countdown.append(input);
                } else if (inputLgt == 3) {
                    countdown.delete(5, 6);
                    countdown.insert(0,"0");
                }
            }
        }

        String strNew = countdown.toString();
        String h = strNew.substring(0, 2);
        String m = strNew.substring(2, 4);
        String s = strNew.substring(4, 6);
        String hms = h + "h " + m + "m " + s + "s";

        editValue.setText(hms);
        String click = String.valueOf(clicks);
        Log.println(Log.INFO, "Clicks:", " "+click+" "+strNew);

    }


问:
"There was an error parsing the body" error on requesting endpoint in FastAPI
I have implemented a endpoint in FastAPI and I am testing it from Postman. But whenever I send request I get this error "There was an error parsing the body", while searching for the error I found a solution somewhere that I need to have python-multipart installed, this package is already installed but I am still facing this error.
Following is my code:

```<code>@router.put('/user')
def update_user(user_data: dict):
    from crain.uma import update_user
    user_id = user_data['id']
    update_user(user_id, user_data)
    return {"message": "DONE"}

</code>```

The endpoint except a dict like this:

```<code>user_data =    {
      "username":"admin",
      "id":"2d06aa3b-c25a-4499-948a-86341ac4adc5",
      "email":null,
      "firstName":"admin",
      "lastName":"admin",
      "createdTimestamp":1638268009973
   },
</code>```

答:
In short, your endpoint expects JSON data, but your client sends form-data instead. Thus, when sending the request through Postman, you should go to Body, then choose raw, and finally, select JSON from the dropdown list, as described in this answer.

Additioanlly, I would highly sugget to use Pydantic models for sending JSON data, as described in the documentation. Thus, you can make use of the data validation that Pydantic offers. You can even use EmailStr type for validating email inputs (requires email-validator to be installed, as described in the documentation). Example below:

```<code>from pydantic import BaseModel, EmailStr
from datetime import datetime
class User(BaseModel):
    username: str
    id: str
    email: EmailStr = None
    firstName: str
    lastName: str
    createdTimestamp: datetime

@app.put('/user')
def update_user(user_data: User):
    from crain.uma import update_user
    user_data = user_data.dict()
    user_id = user_data['id']
    update_user(user_id, user_data)
    return {"message": "DONE"}
</code>```

JSON payload should look like:

```<code>{
    "username":"admin",
    "id":"2d06aa3b-c25a-4499-948a-86341ac4adc5",
    "email":null,
    "firstName":"admin",
    "lastName":"admin",
    "createdTimestamp":1638268009973
}
</code>```

Body:
I have implemented a endpoint in FastAPI and I am testing it from Postman. But whenever I send request I get this error "There was an error parsing the body", while searching for the error I found a solution somewhere that I need to have python-multipart installed, this package is already installed but I am still facing this error.
Following is my code:

@router.put('/user')
def update_user(user_data: dict):
    from crain.uma import update_user
    user_id = user_data['id']
    update_user(user_id, user_data)
    return {"message": "DONE"}

The endpoint except a dict like this:

user_data =    {
      "username":"admin",
      "id":"2d06aa3b-c25a-4499-948a-86341ac4adc5",
      "email":null,
      "firstName":"admin",
      "lastName":"admin",
      "createdTimestamp":1638268009973
   },

Body:
In short, your endpoint expects JSON data, but your client sends form-data instead. Thus, when sending the request through Postman, you should go to Body, then choose raw, and finally, select JSON from the dropdown list, as described in this answer.

Additioanlly, I would highly sugget to use Pydantic models for sending JSON data, as described in the documentation. Thus, you can make use of the data validation that Pydantic offers. You can even use EmailStr type for validating email inputs (requires email-validator to be installed, as described in the documentation). Example below:

from pydantic import BaseModel, EmailStr
from datetime import datetime
class User(BaseModel):
    username: str
    id: str
    email: EmailStr = None
    firstName: str
    lastName: str
    createdTimestamp: datetime

@app.put('/user')
def update_user(user_data: User):
    from crain.uma import update_user
    user_data = user_data.dict()
    user_id = user_data['id']
    update_user(user_id, user_data)
    return {"message": "DONE"}

JSON payload should look like:

{
    "username":"admin",
    "id":"2d06aa3b-c25a-4499-948a-86341ac4adc5",
    "email":null,
    "firstName":"admin",
    "lastName":"admin",
    "createdTimestamp":1638268009973
}


问:
How to plot number of events occurring at each hour of the day in a pandas dataframe?
Say I have the following data:

```<code>import pandas as pd
data = {'time':[7, 1, 2, 7, 2, 2, 1, 2, 7, 3, 5], 'event':['a', 'b', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a']}
df = pd.DataFrame(data)
</code>```

I want to display how many events of each type occurred at each hour of the day. However, there are only 5 unique times present in the "time" column of the dataset.

Plotting a histogram with bins=24 works when all the 24 unique hours of the day (1 to 24) are present in the dataset. But if only a few hours of the day are present, histogram doesn't do this task.

For example, with the above data, the code df.hist() produces this chart:

It is unclear where the x-axis ticks are located exactly - what I want is, that the 5 spikes in this chart should be located at x = 1, 2, 3, 5 and 7, and there should be no spikes present at x = 4, 6 and 8 through 24.

With df.time.hist(bins=24), the following chart is produced:

Here, it is a bit better as we can see that at least the first 4 spikes are located at x = 1, 2, 3, and 5, with x = 4 and x = 6 being left blank. However, at x=7, the spike is drawn to the left of the grid lines, while the other 4 spikes are drawn to the right of the grid lines. Also, this doesn't display the empty spikes at x = 8 through 24.

So, how do I do it?

答:
Try this:</p>
```<code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = {'time':[7, 1, 2, 7, 2, 2, 1, 2, 7, 3, 5], 'event':['a', 'b', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a']}
df = pd.DataFrame(data)
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(16, 10))

df.hist(ax=axes, bins=range(24))

# offset the xticks
axes.set_xticks(np.arange(24) + .5)

# name the label accordingly
axes.set_xticklabels(range(24))
</code>```

Body:
Say I have the following data:

import pandas as pd
data = {'time':[7, 1, 2, 7, 2, 2, 1, 2, 7, 3, 5], 'event':['a', 'b', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a']}
df = pd.DataFrame(data)

I want to display how many events of each type occurred at each hour of the day. However, there are only 5 unique times present in the "time" column of the dataset.

Plotting a histogram with bins=24 works when all the 24 unique hours of the day (1 to 24) are present in the dataset. But if only a few hours of the day are present, histogram doesn't do this task.

For example, with the above data, the code df.hist() produces this chart:

It is unclear where the x-axis ticks are located exactly - what I want is, that the 5 spikes in this chart should be located at x = 1, 2, 3, 5 and 7, and there should be no spikes present at x = 4, 6 and 8 through 24.

With df.time.hist(bins=24), the following chart is produced:

Here, it is a bit better as we can see that at least the first 4 spikes are located at x = 1, 2, 3, and 5, with x = 4 and x = 6 being left blank. However, at x=7, the spike is drawn to the left of the grid lines, while the other 4 spikes are drawn to the right of the grid lines. Also, this doesn't display the empty spikes at x = 8 through 24.

So, how do I do it?

Body:
Try this:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = {'time':[7, 1, 2, 7, 2, 2, 1, 2, 7, 3, 5], 'event':['a', 'b', 'a', 'a', 'b', 'a', 'a', 'b', 'b', 'b', 'a']}
df = pd.DataFrame(data)
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(16, 10))

df.hist(ax=axes, bins=range(24))

# offset the xticks
axes.set_xticks(np.arange(24) + .5)

# name the label accordingly
axes.set_xticklabels(range(24))


问:
Conversion vb6 to c#
I am trying to convert some vb6 code to c# and I am struggling a bit.
I have looked at this page below and others similar, but am still stumped.

Why use hex?

vb6 code below:

```<code>    Dim Cal As String
    Cal = vbNull

    For i = 1 To 8
        Cal = Cal + Hex(Xor1 Xor Xor2)
    Next i
</code>```

This is my c# code - it still has some errors.

```<code>        string Cal = null;
        int Xor1 = 0;
        int Xor2 = 0;

        for (i = 1; i <= 8; i++)
            {
            Cal = Cal + Convert.Hex(Xor1 ^ Xor2);
            }
</code>```

The errors are:

```<code>        Cal = Cal + Convert.Hex(Xor1 ^ Xor2 ^ 6);
</code>```

Any advice as to why I cant get the hex to convert would be appreciated.
I suspect its my lack of understanding the .Hex on line 3 above and the "&H" on line 1/2 above.

答:
Note: This answer was written at a point where the lines Xor1 = CDec("&H" + Mid(SN1, i, 1))
 and Xor1 = Convert.ToDecimal("&H" + SN1.Substring(i, 1)); were still present in the question.

What's the &H?

In Visual Basic (old VB6 and also VB.NET), hexadecimal constants can be used by prefixing them with &H. E.g., myValue = &H20 would assign the value 32 to the variable myValue. Due to this convention, the conversion functions of VB6 also accepted this notation. For example, CInt("20") returned the integer 20, and CInt("&H20") returned the integer 32.

Your code example uses CDec to convert the value to the data type Decimal (actually, to the Decimal subtype of Variant) and then assigns the result to an integer, causing an implicit conversion. This is actually not necessary, using CInt would be correct. Apparently, the VB6 code was written by someone who did not understand that (a) the Decimal data type and (b) representing a number in decimal notation are two completely different things.

So, how do I convert between strings in hexadecimal notation and number data types in C#?

To convert a hexadecimal string into a number use

```<code>int number = Convert.ToInt32(hex, 16);   // use this instead of Convert.ToDecimal
</code>```

In C#, there's no need to pad the value with "&H" in the beginning. The second parameter,16, tells the conversion function that the value is in base 16 (i.e., hexadecimal).

On the other hand, to convert a number into its hex representation, use

```<code>string hex = number.ToString("X");       // use this instead of Convert.ToHex
</code>```

What you are using, Convert.ToDecimal, does something completely different: It converts a value into the decimal data type, which is a special data type used for floating-point numbers with decimal precision. That's not what you need. Your other method, Convert.Hex simply does not exist.

ContentLicense: CC BY-SA 3.0

Title: Conversion vb6 to c#

Body:
I am trying to convert some vb6 code to c# and I am struggling a bit.
I have looked at this page below and others similar, but am still stumped.

Why use hex?

vb6 code below:

    Dim Cal As String
    Cal = vbNull

    For i = 1 To 8
        Cal = Cal + Hex(Xor1 Xor Xor2)
    Next i

This is my c# code - it still has some errors.

        string Cal = null;
        int Xor1 = 0;
        int Xor2 = 0;

        for (i = 1; i <= 8; i++)
            {
            Cal = Cal + Convert.Hex(Xor1 ^ Xor2);
            }

The errors are:

        Cal = Cal + Convert.Hex(Xor1 ^ Xor2 ^ 6);

Any advice as to why I cant get the hex to convert would be appreciated.
I suspect its my lack of understanding the .Hex on line 3 above and the "&H" on line 1/2 above.

Body:
Note: This answer was written at a point where the lines Xor1 = CDec("&H" + Mid(SN1, i, 1))
 and Xor1 = Convert.ToDecimal("&H" + SN1.Substring(i, 1)); were still present in the question.

What's the &H?

In Visual Basic (old VB6 and also VB.NET), hexadecimal constants can be used by prefixing them with &H. E.g., myValue = &H20 would assign the value 32 to the variable myValue. Due to this convention, the conversion functions of VB6 also accepted this notation. For example, CInt("20") returned the integer 20, and CInt("&H20") returned the integer 32.

Your code example uses CDec to convert the value to the data type Decimal (actually, to the Decimal subtype of Variant) and then assigns the result to an integer, causing an implicit conversion. This is actually not necessary, using CInt would be correct. Apparently, the VB6 code was written by someone who did not understand that (a) the Decimal data type and (b) representing a number in decimal notation are two completely different things.

So, how do I convert between strings in hexadecimal notation and number data types in C#?

To convert a hexadecimal string into a number use

int number = Convert.ToInt32(hex, 16);   // use this instead of Convert.ToDecimal

In C#, there's no need to pad the value with "&H" in the beginning. The second parameter,16, tells the conversion function that the value is in base 16 (i.e., hexadecimal).

On the other hand, to convert a number into its hex representation, use

string hex = number.ToString("X");       // use this instead of Convert.ToHex

What you are using, Convert.ToDecimal, does something completely different: It converts a value into the decimal data type, which is a special data type used for floating-point numbers with decimal precision. That's not what you need. Your other method, Convert.Hex simply does not exist.


问:
Save PlayerPref on Button Press Unity 3D C#
I'm currently developing a tycoon game where you can develop your own phones and then sell them.
It works like this:

Enter Phone Name in Input Field
(Name gets saved in PlayerPrefs for later use)
...
Press a button to start customizing
...
Customize the phone and phone cost variable gets saved in PlayerPrefs
...
Press button to sell the phone
...

Now the Problem I have with this system:
-Everytime I create a new phone it would overwrite the old phones Name and cost. I would like it if everytime you create a new phone it would automatically create two new PlayerPrefs, one for the cost and one for the Name and I could always recall them.

Something like This:

```<code>PlayerPrefs.GetInt("Phone1Cost)
PlayerPrefs.GetInt("Phone2Cost")
PlayerPrefs.GetString("Phone1Name")
PlayerPrefs.GetString("Phone2Name")
</code>```

I hope this was clear but basically I want to create two new PlayerPrefs on button click and they should depend on how many times you already pressed the button. So If you pressed it 2 times already(created two phones) then it should make a Phone3Cost PlayerPref, but if you already made 10 phones then it should make a Phone11Cost PlayerPref.

I Know this might seem complicated but I appreciate you help;)

答:
See This will Work?</p>
```<code>
    private int PhoneCostValue = 40;
    private void OnBtnClick()
    {
        int howManyClick = PlayerPrefs.GetInt("howManyClick"); // for Get value
        PlayerPrefs.SetInt("howManyClick", howManyClick + 1); // for save
        Debug.Log("howManyClick" + howManyClick);

        PlayerPrefs.SetInt("PhoneCost" + howManyClick, PhoneCostValue); // for save "PhoneCost" + howManyClick = this key will be "PhoneCost1"
        int Getcost = PlayerPrefs.GetInt("PhoneCost" + howManyClick); // GetValue
    }

</code>```

Body:
I'm currently developing a tycoon game where you can develop your own phones and then sell them.
It works like this:

Enter Phone Name in Input Field
(Name gets saved in PlayerPrefs for later use)
...
Press a button to start customizing
...
Customize the phone and phone cost variable gets saved in PlayerPrefs
...
Press button to sell the phone
...

Now the Problem I have with this system:
-Everytime I create a new phone it would overwrite the old phones Name and cost. I would like it if everytime you create a new phone it would automatically create two new PlayerPrefs, one for the cost and one for the Name and I could always recall them.

Something like This:

PlayerPrefs.GetInt("Phone1Cost)
PlayerPrefs.GetInt("Phone2Cost")
PlayerPrefs.GetString("Phone1Name")
PlayerPrefs.GetString("Phone2Name")

I hope this was clear but basically I want to create two new PlayerPrefs on button click and they should depend on how many times you already pressed the button. So If you pressed it 2 times already(created two phones) then it should make a Phone3Cost PlayerPref, but if you already made 10 phones then it should make a Phone11Cost PlayerPref.

I Know this might seem complicated but I appreciate you help;)

Body:
See This will Work?

    private int PhoneCostValue = 40;
    private void OnBtnClick()
    {
        int howManyClick = PlayerPrefs.GetInt("howManyClick"); // for Get value
        PlayerPrefs.SetInt("howManyClick", howManyClick + 1); // for save
        Debug.Log("howManyClick" + howManyClick);

        PlayerPrefs.SetInt("PhoneCost" + howManyClick, PhoneCostValue); // for save "PhoneCost" + howManyClick = this key will be "PhoneCost1"
        int Getcost = PlayerPrefs.GetInt("PhoneCost" + howManyClick); // GetValue
    }


问:
How to use malloc to assign large two-dimensional arrays in C
I know a very large two-dimensional arrays in C needs a long identifier for the variable type when assigning memory to the array to avoid stack overflow errors and memory issues. Suppose I intend to assign memory to my matrix A which should hold a very large number of numbers using malloc, how am I supposed to go about it?</p>
```<code>int main() {

    int rows = 20000;
    int cols = 20000;

    // How do I define the two-dimensional
    // array using 'long int'?
    long long int** A = new long long int(rows);

    // I got that from this site, but it's
    // wrong. How do I go about it?
}
</code>```

答:
Use pointers to array:

```<code>int main()
{
    size_t rows=20000;
    size_t cols=20000;

    long long int (*A)[cols] = malloc(rows * sizeof(*A));

    //You can use it as normal array.
    A[6][323] = rand();
}
</code>```

or (but access has different syntax)

```<code>int main()
{
    size_t rows=20000;
    size_t cols=20000;

    long long int (*A)[rows][cols] = malloc(sizeof(*A));

    //You can use it almost as normal array.
    (*A)[6][323] = rand();
}
</code>```

Body:
I know a very large two-dimensional arrays in C needs a long identifier for the variable type when assigning memory to the array to avoid stack overflow errors and memory issues. Suppose I intend to assign memory to my matrix A which should hold a very large number of numbers using malloc, how am I supposed to go about it?

int main() {

    int rows = 20000;
    int cols = 20000;

    // How do I define the two-dimensional
    // array using 'long int'?
    long long int** A = new long long int(rows);

    // I got that from this site, but it's
    // wrong. How do I go about it?
}

Body:
Use pointers to array:

int main()
{
    size_t rows=20000;
    size_t cols=20000;

    long long int (*A)[cols] = malloc(rows * sizeof(*A));

    //You can use it as normal array.
    A[6][323] = rand();
}

or (but access has different syntax)

int main()
{
    size_t rows=20000;
    size_t cols=20000;

    long long int (*A)[rows][cols] = malloc(sizeof(*A));

    //You can use it almost as normal array.
    (*A)[6][323] = rand();
}


问:
Retrieve Year from week number
Using Embarcadero Delphi, I need to populate a table with week number and the week year.
The problem is at the end of December and starting January.

First case:
Date "29/12/2024": is in the year 2024 and is referred to the 52° week of 2024.
So I'll insert a record like:
WEEK NUMBER = 52
WEEK YEAR = 2024

Second case:
Date "30/12/2024": is in the year 2024 but is referred to the 1° week of 2025.
So I need to insert a record like:
WEEK NUMBER = 1
WEEK YEAR = 2025

How can I calculate this dynamically? Monday is the first date of the week

答:
You can use this procedure:

```<code>USES System.DateUtils;

PROCEDURE GetWeek(D : TDate ; OUT Week,Year : Cardinal);
  BEGIN
    Week:=WeekOf(D); Year:=YearOf(D);
    CASE MonthOf(D) OF
       1 : IF Week>50 THEN DEC(Year);
      12 : IF Week<50 THEN INC(Year)
    END
  END;
</code>```

It will return the Week and the corresponding Year for any given date in the Julian Calendar.

Title: Retrieve Year from week number

Body:
You can use this procedure:

USES System.DateUtils;

PROCEDURE GetWeek(D : TDate ; OUT Week,Year : Cardinal);
  BEGIN
    Week:=WeekOf(D); Year:=YearOf(D);
    CASE MonthOf(D) OF
       1 : IF Week>50 THEN DEC(Year);
      12 : IF Week<50 THEN INC(Year)
    END
  END;

It will return the Week and the corresponding Year for any given date in the Julian Calendar.


问:
How to use v-model and v-model.trim at the same time in vue
Firs of all here is my code:

```<code><input type="text" id="first_name" class="form-control" v-model="user.attributes.first_name"
                       @keyup.enter="updateProfile" v-model.trim="$v.first_name.$model"
                       :class="{'is-invalid':$v.first_name.$error, 'is-valid':!$v.first_name.$invalid}">
</code>```

I am using vuelidate in my form to validate my input field. But at the same time, I am getting the user object as a prop and I want to show the first name of the object as a value of the input field. Since I trim the value of the input field, I think I am not able to use v-model to show my value anymore. Do you have any solution for this?

答:
First, you can't have multiple v-model on the same element.

Then, if you want to use a prop as a v-model, you have to set the @input event yourself.

cf vuelidate documentation

<template>
 <input v-model.trim="$v.first_name.$model" @input="updateFirstname">
</template>

```

Body:
Firs of all here is my code:

<input type="text" id="first_name" class="form-control" v-model="user.attributes.first_name"
                       @keyup.enter="updateProfile" v-model.trim="$v.first_name.$model"
                       :class="{'is-invalid':$v.first_name.$error, 'is-valid':!$v.first_name.$invalid}">

I am using vuelidate in my form to validate my input field. But at the same time, I am getting the user object as a prop and I want to show the first name of the object as a value of the input field. Since I trim the value of the input field, I think I am not able to use v-model to show my value anymore. Do you have any solution for this?

Body:
First, you can't have multiple v-model on the same element.

Then, if you want to use a prop as a v-model, you have to set the @input event yourself.

cf vuelidate documentation

<template>
 <input v-model.trim="$v.first_name.$model" @input="updateFirstname">
</template>

<script>
export default {
  props: {
     user: Object
  },
  validations: {
     first_name: { ... }
  },
  methods: {
    updateFirstName(newFirstName) {
      this.user.attributes.first_name = newFirstName
      this.$v.first_name.$touch()
    }
  }
}
</script>


问:
How can I view the return value from Future<bool>
How can I view the return value from Future

 Future<bool> res;
 res = cancelDialog();
 print("res..........: ${res}");
 ```
</div>
</div>
</p>
<p>my cancel dialog:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>Future<bool> cancelDialog(_formKey, context, cancelFn) async {
  var res = await showModalActionSheet(
      onWillPop: () async => false,
      context: context,
      actions: <SheetAction>[
        SheetAction(label: "Evet", key: "evet"),
        SheetAction(label: "Hayır", key: "hayir")
      ],
      title: "Tüm değişilikler geri alınacaktır.",
      message: "Devam etmek istediğinize emin misiniz?");
  if (res == "evet") {
    _formKey.currentState?.reset();
     cancelFn.call();
     //return true;
     return Future<bool>.value(true);

  } else {
   //retun false;
    return Future<bool>.value(false);
  }
}</code>```

output : res..........: Instance of 'Future'

The value I want to see in the res is true or false, how can I provide it?

Thank YOU!

答:
you have to await to get the aswer of any value  for example</p>
```<code>bool dialogvalue=await res;
 print(dailogvalue)
</code>```

Body:
How can I view the return value from Future

 Future<bool> res;
 res = cancelDialog();
 print("res..........: ${res}");

my cancel dialog:

Future<bool> cancelDialog(_formKey, context, cancelFn) async {
  var res = await showModalActionSheet(
      onWillPop: () async => false,
      context: context,
      actions: <SheetAction>[
        SheetAction(label: "Evet", key: "evet"),
        SheetAction(label: "Hayır", key: "hayir")
      ],
      title: "Tüm değişilikler geri alınacaktır.",
      message: "Devam etmek istediğinize emin misiniz?");
  if (res == "evet") {
    _formKey.currentState?.reset();
     cancelFn.call();
     //return true;
     return Future<bool>.value(true);

  } else {
   //retun false;
    return Future<bool>.value(false);
  }
}

output : res..........: Instance of 'Future'

The value I want to see in the res is true or false, how can I provide it?

Thank YOU!

Body:
you have to await to get the aswer of any value  for example

bool dialogvalue=await res;
 print(dailogvalue)


问:
Android build suddenly taking 10-15 minutes
I upgraded my gradle build tool to com.android.tools.build:gradle:7.0.4.

Since then my builds are taking 10-15 instead of seconds.

Also, I have updated my gradle itself.
This is my gradle-wrapper.properties file:

```<code>distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
</code>```

I am totally stuck.
I have performed a clean and an invalidate cache to no avail.

One of the places it seems to get stuck is on

```<code>kaptGenerateStubsDebugKotlin
</code>```

The relevant sections of my project level build.gradle:

```<code>buildscript {
    ext.kotlin_version = "1.7.0"
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "io.realm:realm-gradle-plugin:6.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
    }
}
</code>```

Build analyser (the two biggest)

答:
If you clean and invalidate caches, then gradle needs to rebuild those to complete the build.
Once you have completed the build once, without making any further code changes, rebuild again.

When you rebuild this time, look at the list of tasks that were carried out. They should mostly report UP-TO-DATE. Some may say NO-SOURCE. Any that don't are tasks that needed to run.

This build should inevitably be quicker than the last.

You can then use either a gradle build scan or Android Studio build analyzer and see further details of what task needed to re-run.

Using build analyzer you can see some reasoning as to why the task needed to run.

I've seen lots of different reasons for tasks needing to run. Obviously if some source code changes, then it needs to recompile, but even things like changing the target device in Android Studio can be enough to trigger rebuilding.

Hope this helps you to understand what gradle is doing.
Based on the details you have given in the question, there is not enough to determine the actual reason though.

Body:
I upgraded my gradle build tool to com.android.tools.build:gradle:7.0.4.

Since then my builds are taking 10-15 instead of seconds.

Also, I have updated my gradle itself.
This is my gradle-wrapper.properties file:

distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

I am totally stuck.
I have performed a clean and an invalidate cache to no avail.

One of the places it seems to get stuck is on

kaptGenerateStubsDebugKotlin

The relevant sections of my project level build.gradle:

buildscript {
    ext.kotlin_version = "1.7.0"
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "io.realm:realm-gradle-plugin:6.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
    }
}

Build analyser (the two biggest)


问:
How to capture databricks notebook failure to cluster timeout into DataFactory pipeline, passing the error through usp to azure sql db
DataFactory connects to data bricks and calls a notebook. When the notebook completes it either

Upon success > sends to pipeline log

Upon failure > sends to the pipeline error log

This works fine and picks up errors if there is a problem with the data source.

However, when the notebook fails due to it losing connection to data bricks, by which I mean the database cluster loses connection.

Consequently, the data factory pipeline fails and does not pass this into the activity Stored Procedure: SP_Pipeline_Error_Log

which is just a stored procedure that runs to update an error table with some parameters being passed through

Error from data bricks notebook:
“the spark driver has stopped unexpectedly and is restarting. your notebook will be automatically reattached”

I can see that the data factory pipeline failure happens around the same time the cluster connection goes via the data bricks cluster log.

How can I capture the databricks connection failure into my SP_Pipeline_Error_Log?

How can I prevent this error from happening? I've read online it can be down to a memory issue caused by cache.

I can provide more detail if needed.

答:
"Pipeline result is success if and only if all nodes evaluated succeed" Here are some know errors and their troubleshooting docs

You can checkout this external document of a similar scenario and for alternative methods of capturing errors, read  Get Any Azure Data Factory Pipeline Activity Error details with Azure Functions. and Share your feedback at official ADF forum here


问:
winston logs to EFK Stack on GKE
I'm writing my nodejs logs to a file using winston js and storing them on /tmp/logs volume on my host node on GKE. Now, I'm trying to add the EFK Stack to store them and visualize them but my problem is that I'm new to the whole logging process and I'm facing troubles understanding the configuration of fluentd. I've tried to configure the stack on my local cluster with the help of this tutorial and did visualize the stdout and stderr logs of my containers but those of my winston files are not shown.

To do so:

How should I tell fluentd to read *.log files that I'm storing under /tmp/logs on the cluster?

What is the best way to install EFK stack on GKE cluster (ECK....) since I already have Elasticsearch and Kibana installed for production purposes. I'm thinking of isolating the new ones under a logging namespace but I also found that I can install the monitoring stack on a GCE machine directly.

In terms of resource management, does the EFK stack require a lot of RAM, CPU...?

NB: GCP used to have Elastic gke logging on the marketplace that I wanted to try but it's not there anymore.

答:
fluentd config can be entered into ConfigMap, for example:

```<code>apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
data:
  fluentd.conf: |
    <source>
      type tail
      format none
      path /tmp/logs/1.log
      pos_file /tmp/logs/1.log.pos
      tag count.format1
    </source>

    <source>
      type tail
      format none
      path /tmp/logs/2.log
      pos_file /tmp/logs/2.log.pos
      tag count.format2
    </source>

    <match **>
      type google_cloud
    </match>
</code>```

There are also complete tutorials on configureing EFK on GKE here and here.

Title: winston logs to EFK Stack on GKE

Body:
fluentd config can be entered into ConfigMap, for example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
data:
  fluentd.conf: |
    <source>
      type tail
      format none
      path /tmp/logs/1.log
      pos_file /tmp/logs/1.log.pos
      tag count.format1
    </source>

    <source>
      type tail
      format none
      path /tmp/logs/2.log
      pos_file /tmp/logs/2.log.pos
      tag count.format2
    </source>

    <match **>
      type google_cloud
    </match>

There are also complete tutorials on configureing EFK on GKE here and here.


问:
Duckling, int() argument must be a string, a bytes-like object or a number, not 'java.lang.String',
I want to use duckling for time entity extraction from any string.

I am trying to use basic code for using duckling.

```<code>from duckling import DucklingWrapper
d = DucklingWrapper()
print(d.parse_time(u'Let\'s meet at 11:45am'))
</code>```

For this I am getting following error.
int() argument must be a string, a bytes-like object or a number, not 'java.lang.String'

could anyone please help me with this?

答:
This is an incompatibility issue between duckling and Jpype.

This worked for me:

pip install --force-reinstall JPype1==0.6.3

Body:
I want to use duckling for time entity extraction from any string.

I am trying to use basic code for using duckling.

from duckling import DucklingWrapper
d = DucklingWrapper()
print(d.parse_time(u'Let\'s meet at 11:45am'))

For this I am getting following error.
int() argument must be a string, a bytes-like object or a number, not 'java.lang.String'

could anyone please help me with this?


问:
How can I disable the input panel for a form?
I've created a Windows CE 5.0 application which runs on a handheld scanner. The scanner has its own (hardware) keypad and almost all input comes from the scanning unit.

Unfortunately whenever the text box receiving the scanned characters is focused the input panel appears at the bottom of the screen, blocking almost a third of the screen space.

Is it possible to deactivate it in my form or in the whole application?

答:
If you're not doing it manually via the InputPanel control, then I assume you have aygshell in the image and they are being rendered with a WC_SIPPREF control.  I'm not certain if you can remove that control manually - I've never tried.  There may be an agshell function that will allow you to disable/remove it, or maybe some work with the InputPanel for your app can remove it.

You may also want to see this blog entry for a bit more detail.

OwnerDisplayName: DR

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: ctacke


问:
Segmentation fault in C program using pointers as parameters
Im trying to understand pointers as function parameters, and in one of the programs there is a segmentation error I can't fix. Firstly, why to use pointers in function arguments? and Why is this error showing?

```<code>#include <stdio.h>

void square_it(int* a)
{
  printf("The final value is: %d\n", *a * *a);
}

int main()
{
  int* input;

  puts("This program squares the input integer number");
  puts("Please put the number:");

  scanf("%d", &input);

  square_it(input);
  return 0;
}
</code>```

答:
This is the working code:

#include <stdio.h>

void square_it(int* a)
{
  printf("The final value is: %d\n", *a * *a);
}

int main()
{
  int i = 0;
  int* input = &i;

  puts("This program squares the input integer number");
  puts("Please put the number:");

  scanf("%d", input);

  square_it(input);
  return 0;
}
```

There are some errors in the original code:

According to the man-pages to scanf, it takes a format string and then the address of where to store the input.

You gave it the address of a pointer (eg. an int**), which is not what scanf expects.

Also you need to provide memory to store the input in. The scanf string tells that you want an integer as input. In the above code snippet that is i.

input points to i, so i can give the int*, that is input to scanf. scanf will then write into i. We can then go ahead and put the address of i into the sqare_it function.

Since we did not use the heap, we don't need to worry about memory management.

Body:
Im trying to understand pointers as function parameters, and in one of the programs there is a segmentation error I can't fix. Firstly, why to use pointers in function arguments? and Why is this error showing?

#include <stdio.h>

void square_it(int* a)
{
  printf("The final value is: %d\n", *a * *a);
}

int main()
{
  int* input;

  puts("This program squares the input integer number");
  puts("Please put the number:");

  scanf("%d", &input);

  square_it(input);
  return 0;
}

Body:
This is the working code:

#include <stdio.h>

void square_it(int* a)
{
  printf("The final value is: %d\n", *a * *a);
}

int main()
{
  int i = 0;
  int* input = &i;

  puts("This program squares the input integer number");
  puts("Please put the number:");

  scanf("%d", input);

  square_it(input);
  return 0;
}

There are some errors in the original code:

According to the man-pages to scanf, it takes a format string and then the address of where to store the input.

You gave it the address of a pointer (eg. an int**), which is not what scanf expects.

Also you need to provide memory to store the input in. The scanf string tells that you want an integer as input. In the above code snippet that is i.

input points to i, so i can give the int*, that is input to scanf. scanf will then write into i. We can then go ahead and put the address of i into the sqare_it function.

Since we did not use the heap, we don't need to worry about memory management.


答:
int* input; does not allocate memory for an int. It mearly makes it possible to make input point at an int (allocated elsewhere). Currently, by dereferencing it (like you do with *a), you make your program have undefined behavior. If you really want an intermediate pointer variable for this, this example shows how it could be done:

#include <stdio.h>

void square_it(int *a) {
    *a *= *a;     // same as  *a = *a * *a;
}

int main() {
    int data;
    int* input = &data; // now `input` points at an  `int`

    puts("This program squares the input integer number");
    puts("Please put the number:");

    // check that `scanf` succeeds:
    if(scanf("%d", input) == 1) { // don't take its address, it's a pointer already
        square_it(input);
        // since `input` is pointing at `data`, it's actually the value of `data`
        // that is affected by `scanf` and `square_it`, which makes the below work:
        printf("The final value is: %d\n", data);
    }
}
```
<p>Without an intermediate pointer variable:</p>
<pre class="lang-c prettyprint-override"><code>#include <stdio.h>

void square_it(int *a) {
    *a *= *a;
}

int main() {
    int input; // note that it's not a pointer here

    puts("This program squares the input integer number");
    puts("Please put the number:");

    if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
        square_it(&input);         // and here too
        printf("The final value is: %d\n", input);
    }
}
</code>```

Without any pointers at all, it could look like this:

#include <stdio.h>

int square_it(int a) {
    return a * a;
}

int main() {
    int input;

    puts("This program squares the input integer number");
    puts("Please put the number:");

    if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
        int result = square_it(input);
        printf("The final value is: %d\n", result);
    }
}
```

Title: Segmentation fault in C program using pointers as parameters

Body:
int* input; does not allocate memory for an int. It mearly makes it possible to make input point at an int (allocated elsewhere). Currently, by dereferencing it (like you do with *a), you make your program have undefined behavior. If you really want an intermediate pointer variable for this, this example shows how it could be done:

#include <stdio.h>

void square_it(int *a) {
    *a *= *a;     // same as  *a = *a * *a;
}

int main() {
    int data;
    int* input = &data; // now `input` points at an  `int`

    puts("This program squares the input integer number");
    puts("Please put the number:");

    // check that `scanf` succeeds:
    if(scanf("%d", input) == 1) { // don't take its address, it's a pointer already
        square_it(input);
        // since `input` is pointing at `data`, it's actually the value of `data`
        // that is affected by `scanf` and `square_it`, which makes the below work:
        printf("The final value is: %d\n", data);
    }
}

Without an intermediate pointer variable:

#include <stdio.h>

void square_it(int *a) {
    *a *= *a;
}

int main() {
    int input; // note that it's not a pointer here

    puts("This program squares the input integer number");
    puts("Please put the number:");

    if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
        square_it(&input);         // and here too
        printf("The final value is: %d\n", input);
    }
}

Without any pointers at all, it could look like this:

#include <stdio.h>

int square_it(int a) {
    return a * a;
}

int main() {
    int input;

    puts("This program squares the input integer number");
    puts("Please put the number:");

    if(scanf("%d", &input) == 1) { // here, taking the address of `input` makes sense
        int result = square_it(input);
        printf("The final value is: %d\n", result);
    }
}


问:
In a Join, How to Ensure Each Group In Table B Joins to Every Row in Table A
My SQL dialect is MySQL. I am creating a view on two tables.

I have TableA which looks like this:

```<code>| date | value |
|------|-------|
| 1    | 100   |
| 2    | 150   |
</code>```

I have TableB which looks like this:

```<code>| date | group | name  |
|------|-------|-------|
| 1    | d     | alice |
| 1    | e     | bob   |
| 2    | d     | clark |
| 2    | e     | mick  |
</code>```

I want to do a join and combine them on the date column, in such a way the each group in B gets a join on A. To be more clear, here's what the output data should look like:

```<code>| date | group | value | name  |
|------|-------|-------|-------|
| 1    | d     | 100   | alice |
| 1    | e     | 100   | bob   |
| 2    | d     | 150   | clark |
| 2    | e     | 150   | mick  |
</code>```

So this is a pretty contrived example, just to make it easy to read in markdown. But some details about my actual problem:

I have a large of number of groups, so many nested derived tables is not feasible, and would slow the view query time too much.

I need to generate columns that are some function of both tables (e.g. some output like value + name, which is obviously nonsensical with this example's types)

I have tried using:

```<code>SELECT value, name, group
FROM TableA
RIGHT OUTER JOIN TableB
ON TableA.date = TableB.date

</code>```

The output in this case just takes one of the groups, and ditches the rest.

What is the syntax for this?

答:
Based on the data sample you've shared, it looks like you're looking for an INNER JOIN instead of a RIGHT JOIN.

```<code>SELECT value, name, group
FROM TableA
JOIN TableB
 ON TableA.date = TableB.date
</code>```

Body:
My SQL dialect is MySQL. I am creating a view on two tables.

I have TableA which looks like this:

| date | value |
|------|-------|
| 1    | 100   |
| 2    | 150   |

I have TableB which looks like this:

| date | group | name  |
|------|-------|-------|
| 1    | d     | alice |
| 1    | e     | bob   |
| 2    | d     | clark |
| 2    | e     | mick  |

I want to do a join and combine them on the date column, in such a way the each group in B gets a join on A. To be more clear, here's what the output data should look like:

| date | group | value | name  |
|------|-------|-------|-------|
| 1    | d     | 100   | alice |
| 1    | e     | 100   | bob   |
| 2    | d     | 150   | clark |
| 2    | e     | 150   | mick  |

So this is a pretty contrived example, just to make it easy to read in markdown. But some details about my actual problem:

I have a large of number of groups, so many nested derived tables is not feasible, and would slow the view query time too much.

I need to generate columns that are some function of both tables (e.g. some output like value + name, which is obviously nonsensical with this example's types)

I have tried using:

SELECT value, name, group
FROM TableA
RIGHT OUTER JOIN TableB
ON TableA.date = TableB.date

The output in this case just takes one of the groups, and ditches the rest.

What is the syntax for this?

Body:
Based on the data sample you've shared, it looks like you're looking for an INNER JOIN instead of a RIGHT JOIN.

SELECT value, name, group
FROM TableA
JOIN TableB
 ON TableA.date = TableB.date


问:
Microsoft Bot Framework with LUIS not detecting all intents
I'm creating a chatbot using the MS Bot Framework with LUIS in node js. I have created a couple of intents called "Book" and "Pending" and an entity called "Annual Leave" in LUIS. I have added the LUIS URL to my chatbot code and when I test it in my console, I see that certain utterances get a response while some don't.

For example when I say "How do I check my remaining annual leave?", I get a response saying "You can find your remaining A/L here". But when I say "How can I check my pending annual leave?", I get no reply.

My Git Bash response for the working utterance

ChatConnector: message received.

session.beginDialog(/)

/ - session.sendBatch() sending 0 messages

/ - IntentDialog.matches(Pending)

/ - waterfall() step 1 of 1

/ - session.beginDialog(BotBuilder:Prompts)

.Prompts.text - session.send()

.Prompts.text - session.sendBatch() sending 1 messages

My Git Bash response for the non-working utterance

ChatConnector: message received.

.Prompts.text - session.endDialogWithResult()

/ - session.endDialogWithResult()

session.sendBatch() sending 0 messages

But when I test the exact same utterances on LUIS, it detects the intent and entity with a high confidence score.

This is happening with a lot of utterances. I can't figure out what's causing this issue.

My Chatbot code

```<code>var restify = require('restify');
var builder = require('botbuilder');

//=========================================================
// Bot Setup
//=========================================================

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url);
});

// Create chat bot
var connector = new builder.ChatConnector({
    appId: 'MY_APP_ID',
    appPassword: 'MY_APP_PASSWORD'
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());

server.get('/', restify.serveStatic({
 directory: __dirname,
 default: '/index.html'
}));

//=========================================================
// Bots Dialogs
//=========================================================

var recognizer = new builder.LuisRecognizer('MY_LUIS_URL');
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);

intents.matches('Book', [
    function (session, args, next) {
       var task = builder.EntityRecognizer.findEntity(args.entities, 'Annual Leave');
                  builder.Prompts.text(session, "You can book your A/L here.");
    }
]),

intents.matches('Pending', [
    function (session, args, next) {
        var task = builder.EntityRecognizer.findEntity(args.entities, 'Annual Leave');
                  builder.Prompts.text(session, "You can find your remaining A/L here.");

    }
]);
</code>```

答: In case there is anybody else having this issue. First check that your appsettings.json LuisSlot is pointing at Staging and then after publishing the luis use (bf luis:generate:cs) commmand to convert your json to cs file again and update your LuisModel in botframework.

Body:
I'm creating a chatbot using the MS Bot Framework with LUIS in node js. I have created a couple of intents called "Book" and "Pending" and an entity called "Annual Leave" in LUIS. I have added the LUIS URL to my chatbot code and when I test it in my console, I see that certain utterances get a response while some don't.

For example when I say "How do I check my remaining annual leave?", I get a response saying "You can find your remaining A/L here". But when I say "How can I check my pending annual leave?", I get no reply.

My Git Bash response for the working utterance

ChatConnector: message received.

session.beginDialog(/)

/ - session.sendBatch() sending 0 messages

/ - IntentDialog.matches(Pending)

/ - waterfall() step 1 of 1

/ - session.beginDialog(BotBuilder:Prompts)

.Prompts.text - session.send()

.Prompts.text - session.sendBatch() sending 1 messages

My Git Bash response for the non-working utterance

ChatConnector: message received.

.Prompts.text - session.endDialogWithResult()

/ - session.endDialogWithResult()

session.sendBatch() sending 0 messages

But when I test the exact same utterances on LUIS, it detects the intent and entity with a high confidence score.

This is happening with a lot of utterances. I can't figure out what's causing this issue.

My Chatbot code

var restify = require('restify');
var builder = require('botbuilder');

//=========================================================
// Bot Setup
//=========================================================

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url);
});

// Create chat bot
var connector = new builder.ChatConnector({
    appId: 'MY_APP_ID',
    appPassword: 'MY_APP_PASSWORD'
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());

server.get('/', restify.serveStatic({
 directory: __dirname,
 default: '/index.html'
}));

//=========================================================
// Bots Dialogs
//=========================================================

var recognizer = new builder.LuisRecognizer('MY_LUIS_URL');
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);

intents.matches('Book', [
    function (session, args, next) {
       var task = builder.EntityRecognizer.findEntity(args.entities, 'Annual Leave');
                  builder.Prompts.text(session, "You can book your A/L here.");
    }
]),

intents.matches('Pending', [
    function (session, args, next) {
        var task = builder.EntityRecognizer.findEntity(args.entities, 'Annual Leave');
                  builder.Prompts.text(session, "You can find your remaining A/L here.");

    }
]);


答:
You are working with a Future<T> object from an async API, which is actually an object that will resolve (in a future) to your T.

Because of that, you need to either:

```<code>bool result = await res;
print(result); // true or false
</code>```

or

```<code>res.then((result) => print(result)); // true or false
</code>```

Title: How can I view the return value from Future<bool>

Body:
You are working with a Future<T> object from an async API, which is actually an object that will resolve (in a future) to your T.

Because of that, you need to either:

bool result = await res;
print(result); // true or false

or

res.then((result) => print(result)); // true or false


问:
Merge duplicates from Json file
My JSON file looks like this

```<code> [
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": {
      "Type": "Renal",
      "Drug": "Sunitinib",
      "Effects": "Response"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Ovarian",
      "Drug": "Cetuximab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "Regorafenib",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Leukemia",
      "Drug": "Akt",
      "Effects": "Resistance"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "gefitimab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845=",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "APC",
    "HGVSp_Short": "p.Q1338*",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "MET",
    "HGVSp_Short": "p. M1S",
    "Other": {
      "Type": "Eye",
      "Drug": "",
      "Effects": "Response"
    }
  }
]
</code>```

I want to merge the duplicates in my JSON file

Expected output

```<code>[
    {
      "Hugo_Symbol": "FLT3",
      "HGVSp_Short": "p.T227M",
      "Other": [
      {"Type": "Renal", "Drug": "Sunitinib","Effect": "Response" }
      ]
    },
    {
      "Hugo_Symbol": "KRAS",
      "HGVSp_Short": "p.G12D",
      "Other": [
      {"Type": "Ovarian","Drug": "Cetuximab","Effect": "Sensitivity"},
      {"Type": "Lung","Drug": "Regorafenib","Effect": "Sensitivity"},
      {"Type": "Leukemia","Drug": "Akt","Effect": "Resistance"},
      {"Type": "Lung", "Drug": "gefitimab", "Effect": "Sensitivity"}
      ]
    },
    {
      "Hugo_Symbol": "ALK",
      "HGVSp_Short": "p.G845=",
      "Other": [
      {"Type": "", "Drug": "","Effect": "" }
      ]
    },
    {
      "Hugo_Symbol": "APC",
      "HGVSp_Short": "p.Q1338*",
      "Other":[
       {"Type": "","Drug": "","Effect": ""}
       ]
     },
     {
      "Hugo_Symbol": "MET",
      "HGVSp_Short": "p.M1S",
      "OtherTumorInfo": [
      {"Type": "Eye","Drug": "","Effect": "Response" }
      ]
     }
  ]
</code>```

My initial file is a CSV file I have converted my CSV file into JSON using R

```<code>func <- function(x) {
     grps <- split(names(x), gsub("[.].*", "", names(x)))
     for (nm in names(grps)) {
         if (length(grps[[nm]]) > 1 || !nm %in% grps[[nm]]) {
             x[[nm]] <- setNames(subset(x, select = grps[[nm]]),
                                 gsub("^[^.]+[.]", "", grps[[nm]]))
             x[,setdiff(grps[[nm]], nm)] <- NULL
         }
     }
     for (nm in names(x)) {
         if (is.data.frame(x[[nm]])) {
             x[[nm]] <- func(x[[nm]])
         }
     }
     if (any(grepl("[.]", names(x)))) func(x) else x
 }
</code>```

I used this function to convert my CSV file to a nested JSON file

```<code>newdf= func(x)
</code>```

Converting my data frame into JSON

```<code>library(jsonlite)
json_f=toJSON(newdf,dataframe = 'rows',pretty = T)
</code>```

My CSV file looks like this

```<code>| Hugo_Symbol | HGVSp_Short | Other.Type | Other.Drug | Other.Effects |
| ----------- | ----------- | ---------- | ---------- | ------------- |
|FLT3         | p.T227M     | Renal      | Sunitinib  | Response      |
|KRAS         | p.G12D      | Ovarian    | Cetuximab  | Sensitivity   |
|KRAS         | p.G12D      | Lung       | Regorafenib| Sensitivity   |
|KRAS         | p.G12D      | Leukemia   | Akt        | Resistance    |
|KRAS         | p.G12D      | Lung       | gefitimab  | Sensitivity   |
|ALK          | p.G845=     |            |            |               |
|APC          | p.Q1338*    |            |            |               |
|MET          | p. M1S      | Eye        |            | Response      |
</code>```

Can anyone suggest me a simple way to merge my duplicates array in the JSON file in the R?

答:
Its much easier to do the re-nesting on tables, so lets convert the json back to a tibble:

```<code>library(jsonlite)
library(tidyverse)

read_json("data.json") %>%
  map(as.data.frame) %>%
  bind_rows() %>%
  as_tibble() %>%
  nest(-Hugo_Symbol, -HGVSp_Short) %>%
  mutate(data = data %>% map(~ {
    colnames(.x) <- colnames(.x) %>% str_remove("^Other.")
    .x
  })) %>%
  rename(Other = data) %>%
  toJSON(pretty = T)
</code>```

resulting in

```<code>[
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": [
      {
        "Type": "Renal",
        "Drug": "Sunitinib",
        "Effects": "Response"
      }
    ]
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": [
      {
        "Type": "Ovarian",
        "Drug": "Cetuximab",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Lung",
        "Drug": "Regorafenib",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Leukemia",
        "Drug": "Akt",
        "Effects": "Resistance"
      },
      {
        "Type": "Lung",
        "Drug": "gefitimab",
        "Effects": "Sensitivity"
      }
    ]
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845="
...
</code>```

Title: Merge duplicates from Json file

Body:
My JSON file looks like this

 [
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": {
      "Type": "Renal",
      "Drug": "Sunitinib",
      "Effects": "Response"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Ovarian",
      "Drug": "Cetuximab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "Regorafenib",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Leukemia",
      "Drug": "Akt",
      "Effects": "Resistance"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "gefitimab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845=",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "APC",
    "HGVSp_Short": "p.Q1338*",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "MET",
    "HGVSp_Short": "p. M1S",
    "Other": {
      "Type": "Eye",
      "Drug": "",
      "Effects": "Response"
    }
  }
]

I want to merge the duplicates in my JSON file

Expected output

[
    {
      "Hugo_Symbol": "FLT3",
      "HGVSp_Short": "p.T227M",
      "Other": [
      {"Type": "Renal", "Drug": "Sunitinib","Effect": "Response" }
      ]
    },
    {
      "Hugo_Symbol": "KRAS",
      "HGVSp_Short": "p.G12D",
      "Other": [
      {"Type": "Ovarian","Drug": "Cetuximab","Effect": "Sensitivity"},
      {"Type": "Lung","Drug": "Regorafenib","Effect": "Sensitivity"},
      {"Type": "Leukemia","Drug": "Akt","Effect": "Resistance"},
      {"Type": "Lung", "Drug": "gefitimab", "Effect": "Sensitivity"}
      ]
    },
    {
      "Hugo_Symbol": "ALK",
      "HGVSp_Short": "p.G845=",
      "Other": [
      {"Type": "", "Drug": "","Effect": "" }
      ]
    },
    {
      "Hugo_Symbol": "APC",
      "HGVSp_Short": "p.Q1338*",
      "Other":[
       {"Type": "","Drug": "","Effect": ""}
       ]
     },
     {
      "Hugo_Symbol": "MET",
      "HGVSp_Short": "p.M1S",
      "OtherTumorInfo": [
      {"Type": "Eye","Drug": "","Effect": "Response" }
      ]
     }
  ]

My initial file is a CSV file I have converted my CSV file into JSON using R

func <- function(x) {
     grps <- split(names(x), gsub("[.].*", "", names(x)))
     for (nm in names(grps)) {
         if (length(grps[[nm]]) > 1 || !nm %in% grps[[nm]]) {
             x[[nm]] <- setNames(subset(x, select = grps[[nm]]),
                                 gsub("^[^.]+[.]", "", grps[[nm]]))
             x[,setdiff(grps[[nm]], nm)] <- NULL
         }
     }
     for (nm in names(x)) {
         if (is.data.frame(x[[nm]])) {
             x[[nm]] <- func(x[[nm]])
         }
     }
     if (any(grepl("[.]", names(x)))) func(x) else x
 }

I used this function to convert my CSV file to a nested JSON file

newdf= func(x)

Converting my data frame into JSON

library(jsonlite)
json_f=toJSON(newdf,dataframe = 'rows',pretty = T)

My CSV file looks like this

| Hugo_Symbol | HGVSp_Short | Other.Type | Other.Drug | Other.Effects |
| ----------- | ----------- | ---------- | ---------- | ------------- |
|FLT3         | p.T227M     | Renal      | Sunitinib  | Response      |
|KRAS         | p.G12D      | Ovarian    | Cetuximab  | Sensitivity   |
|KRAS         | p.G12D      | Lung       | Regorafenib| Sensitivity   |
|KRAS         | p.G12D      | Leukemia   | Akt        | Resistance    |
|KRAS         | p.G12D      | Lung       | gefitimab  | Sensitivity   |
|ALK          | p.G845=     |            |            |               |
|APC          | p.Q1338*    |            |            |               |
|MET          | p. M1S      | Eye        |            | Response      |

Can anyone suggest me a simple way to merge my duplicates array in the JSON file in the R?

Body:
Its much easier to do the re-nesting on tables, so lets convert the json back to a tibble:

library(jsonlite)
library(tidyverse)

read_json("data.json") %>%
  map(as.data.frame) %>%
  bind_rows() %>%
  as_tibble() %>%
  nest(-Hugo_Symbol, -HGVSp_Short) %>%
  mutate(data = data %>% map(~ {
    colnames(.x) <- colnames(.x) %>% str_remove("^Other.")
    .x
  })) %>%
  rename(Other = data) %>%
  toJSON(pretty = T)

resulting in

[
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": [
      {
        "Type": "Renal",
        "Drug": "Sunitinib",
        "Effects": "Response"
      }
    ]
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": [
      {
        "Type": "Ovarian",
        "Drug": "Cetuximab",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Lung",
        "Drug": "Regorafenib",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Leukemia",
        "Drug": "Akt",
        "Effects": "Resistance"
      },
      {
        "Type": "Lung",
        "Drug": "gefitimab",
        "Effects": "Sensitivity"
      }
    ]
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845="
...